ruburple 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -18,15 +18,21 @@ Then, to build, I did:
18
18
 
19
19
  This is mainly because I dont need any of the disabled features to get ruburple running, and the configure script had big trouble finding the gnutls headers and libs on its own.
20
20
 
21
- == Usage example (using (almost?) all methods defined in the ruburple gem):
21
+ == Usage example (using almost all methods defined in the ruburple gem):
22
22
 
23
23
  require 'ruburple'
24
24
  Ruburple::init
25
25
  Ruburple::subscribe(:received_im_msg, (Proc.new do |a,b,c,d,e| puts "rcv im: #{a}, #{b}, #{c}, #{d}, #{e}" end))
26
26
  Ruburple::subscribe(:signed_on, (Proc.new do |a| puts "signed on: #{a}" end))
27
- p = Ruburple::protocols[5]
27
+ p = Ruburple::get_protocol("MSN")
28
28
  puts "gonna connect to #{p.id}, #{p.name}, #{p.version}, #{p.summary}, #{p.description}, #{p.author}, #{p.homepage}"
29
29
  a = p.get_account("some@msn.account", "some_msn_password")
30
30
  a.connect
31
31
  a.connection.send_im("some_other@msn_account", "some message")
32
32
  a.connection.close
33
+
34
+ == It doesnt work!
35
+
36
+ Do
37
+ Ruburple::debug = true
38
+ and mail me a description of the problem, and Ill see what I can do.
@@ -76,6 +76,10 @@ static GMainLoop *main_loop;
76
76
  static VALUE call_method_id;
77
77
  static VALUE idle_handler_method_id;
78
78
  static int purple_event_queue[2];
79
+ static gboolean glib_sleeping;
80
+ static VALUE gc_save_set;
81
+ static VALUE add_method_id;
82
+ static VALUE delete_method_id;
79
83
 
80
84
  /*
81
85
  * The UI name for libpurple.
@@ -246,25 +250,35 @@ add_single_shot(RuburpleCallFunc function, gpointer data)
246
250
  return call;
247
251
  }
248
252
 
249
- /* Ask glib to run something for us once and wait for the result to be ready */
253
+ /*
254
+ * Ask glib to run something for us once and wait for the result to be ready.
255
+ *
256
+ * If the glib main loop is sleeping due to us we will do it ourselves,
257
+ * if it is NOT sleeping due to us we will register a single shot call.
258
+ */
250
259
  static gpointer
251
260
  call_and_get_result(RuburpleCallFunc function, gpointer data)
252
261
  {
253
- RuburpleSingleShotCall *call = add_single_shot(function, data);
262
+ RuburpleSingleShotCall *call;
254
263
  gpointer return_value;
255
264
 
256
- g_mutex_lock(call->lock);
257
-
258
- while (!call->run) {
259
- g_cond_wait(call->done, call->lock);
265
+ if (glib_sleeping) {
266
+ return_value = function(data);
267
+ } else {
268
+ call = add_single_shot(function, data);
269
+
270
+ g_mutex_lock(call->lock);
271
+
272
+ while (!call->run) {
273
+ g_cond_wait(call->done, call->lock);
274
+ }
275
+
276
+ return_value = call->result;
277
+
278
+ g_mutex_unlock(call->lock);
279
+
280
+ ruburple_single_shot_call_free(call);
260
281
  }
261
-
262
- return_value = call->result;
263
-
264
- g_mutex_unlock(call->lock);
265
-
266
- ruburple_single_shot_call_free(call);
267
-
268
282
  return return_value;
269
283
  }
270
284
 
@@ -489,15 +503,27 @@ rb_ruburple_get_protocols(VALUE self)
489
503
  GList *iter;
490
504
  VALUE return_value = rb_ary_new();
491
505
 
492
- iter = (GList *) call_and_get_result(ruburple_get_protocols, NULL);
493
-
494
- for (iter = purple_plugins_get_protocols(); iter; iter = iter->next) {
506
+ for (iter = (GList *) call_and_get_result(ruburple_get_protocols, NULL); iter; iter = iter->next) {
495
507
  rb_ary_push(return_value, RB_RUBURPLE_PROTOCOL(iter->data));
496
508
  }
497
509
 
498
510
  return return_value;
499
511
  }
500
512
 
513
+ static VALUE
514
+ rb_ruburple_get_protocol(VALUE self, VALUE name)
515
+ {
516
+ GList *iter;
517
+ VALUE return_value;
518
+
519
+ for (iter = (GList *) call_and_get_result(ruburple_get_protocols, NULL); iter; iter = iter->next) {
520
+ if (strcmp(purple_plugin_get_name((PurplePlugin *) iter->data), RSTRING(name)->ptr) == 0)
521
+ return RB_RUBURPLE_PROTOCOL(iter->data);
522
+ }
523
+
524
+ return Qnil;
525
+ }
526
+
501
527
  static gpointer
502
528
  ruburple_protocol_account_free(gpointer data)
503
529
  {
@@ -535,7 +561,7 @@ static VALUE
535
561
  rb_ruburple_protocol_get_account(int argc, VALUE *argv, VALUE self)
536
562
  {
537
563
  PurplePlugin *plugin;
538
- gpointer *args;
564
+ gpointer args[3];
539
565
  VALUE return_value;
540
566
 
541
567
  if (argc > 2)
@@ -548,11 +574,6 @@ rb_ruburple_protocol_get_account(int argc, VALUE *argv, VALUE self)
548
574
 
549
575
  PURPLE_PLUGIN(self, plugin);
550
576
 
551
- if (argc == 1)
552
- args = g_new(gpointer, 2);
553
- else if (argc == 2)
554
- args = g_new(gpointer, 3);
555
-
556
577
  args[0] = (gpointer) RSTRING(argv[0])->ptr;
557
578
  args[1] = (gpointer) plugin->info->id;
558
579
 
@@ -563,8 +584,6 @@ rb_ruburple_protocol_get_account(int argc, VALUE *argv, VALUE self)
563
584
  return_value = RB_RUBURPLE_PROTOCOL_ACCOUNT_gc((PurpleAccount *) call_and_get_result(ruburple_protocol_get_account_and_set_password, args));
564
585
  }
565
586
 
566
- g_free(args);
567
-
568
587
  return return_value;
569
588
  }
570
589
 
@@ -580,7 +599,7 @@ static VALUE
580
599
  rb_ruburple_protocol_account_set_password(VALUE self, VALUE password)
581
600
  {
582
601
  PurpleAccount *account;
583
- gpointer *args = g_new(gpointer, 2);
602
+ gpointer args[2];
584
603
 
585
604
  Check_Type(password, T_STRING);
586
605
  PURPLE_ACCOUNT(self, account);
@@ -589,8 +608,6 @@ rb_ruburple_protocol_account_set_password(VALUE self, VALUE password)
589
608
 
590
609
  call_and_get_result(ruburple_protocol_account_set_password, args);
591
610
 
592
- g_free(args);
593
-
594
611
  return Qnil;
595
612
  }
596
613
 
@@ -613,6 +630,80 @@ rb_ruburple_protocol_account_connect(VALUE self)
613
630
  return Qnil;
614
631
  }
615
632
 
633
+ static gpointer
634
+ ruburple_protocol_account_get_username(gpointer data)
635
+ {
636
+ PurpleAccount *account = (PurpleAccount *) data;
637
+ return (gpointer) purple_account_get_username(account);
638
+ }
639
+
640
+ static VALUE
641
+ rb_ruburple_protocol_account_get_username(VALUE self)
642
+ {
643
+ PurpleAccount *account;
644
+ char *rval;
645
+ PURPLE_ACCOUNT(self, account);
646
+ rval = (char *) call_and_get_result(ruburple_protocol_account_get_username, (gpointer) account);
647
+ RETURN_QNIL_IF_NULL(rval);
648
+ return rb_str_new2(rval);
649
+ }
650
+
651
+ static gpointer
652
+ ruburple_protocol_account_get_password(gpointer data)
653
+ {
654
+ PurpleAccount *account = (PurpleAccount *) data;
655
+ return (gpointer) purple_account_get_password(account);
656
+ }
657
+
658
+ static VALUE
659
+ rb_ruburple_protocol_account_get_password(VALUE self)
660
+ {
661
+ PurpleAccount *account;
662
+ char *rval;
663
+ PURPLE_ACCOUNT(self, account);
664
+ rval = (char *) call_and_get_result(ruburple_protocol_account_get_password, (gpointer) account);
665
+ RETURN_QNIL_IF_NULL(rval);
666
+ return rb_str_new2(rval);
667
+ }
668
+
669
+ static gpointer
670
+ ruburple_protocol_account_get_alias(gpointer data)
671
+ {
672
+ PurpleAccount *account = (PurpleAccount *) data;
673
+ return (gpointer) purple_account_get_alias(account);
674
+ }
675
+
676
+ static VALUE
677
+ rb_ruburple_protocol_account_get_alias(VALUE self)
678
+ {
679
+ PurpleAccount *account;
680
+ char *rval;
681
+ PURPLE_ACCOUNT(self, account);
682
+ rval = (char *) call_and_get_result(ruburple_protocol_account_get_alias, (gpointer) account);
683
+ RETURN_QNIL_IF_NULL(rval);
684
+ return rb_str_new2(rval);
685
+ }
686
+
687
+ static gpointer
688
+ ruburple_protocol_account_set_alias(gpointer data)
689
+ {
690
+ gpointer *args = (gpointer *) data;
691
+ purple_account_set_alias((PurpleAccount *) args[0], (char *) args[1]);
692
+ return NULL;
693
+ }
694
+
695
+ static VALUE
696
+ rb_ruburple_protocol_account_set_alias(VALUE self, VALUE alias)
697
+ {
698
+ gpointer args[2];
699
+ PurpleAccount *account;
700
+ PURPLE_ACCOUNT(self, account);
701
+ args[0] = (gpointer) account;
702
+ args[1] = (gpointer) RSTRING(alias)->ptr;
703
+ call_and_get_result(ruburple_protocol_account_set_alias, args);
704
+ return Qnil;
705
+ }
706
+
616
707
  static gpointer
617
708
  ruburple_protocol_account_set_savedstatus(gpointer data)
618
709
  {
@@ -626,7 +717,7 @@ rb_ruburple_protocol_account_set_savedstatus(VALUE self, VALUE savedstatus_value
626
717
  {
627
718
  PurpleAccount *account;
628
719
  PurpleSavedStatus *savedstatus;
629
- gpointer *args = g_new(gpointer, 2);
720
+ gpointer args[2];
630
721
 
631
722
  PURPLE_ACCOUNT(self, account);
632
723
  PURPLE_SAVEDSTATUS(savedstatus_value, savedstatus);
@@ -636,8 +727,6 @@ rb_ruburple_protocol_account_set_savedstatus(VALUE self, VALUE savedstatus_value
636
727
 
637
728
  call_and_get_result(ruburple_protocol_account_set_savedstatus, args);
638
729
 
639
- g_free(args);
640
-
641
730
  return Qnil;
642
731
  }
643
732
 
@@ -665,7 +754,7 @@ static VALUE
665
754
  rb_ruburple_protocol_account_savedstatus_initialize(VALUE self, VALUE status_constant)
666
755
  {
667
756
  PurpleSavedStatus *savedstatus;
668
- gpointer *args = g_new(gpointer, 2);
757
+ gpointer args[2];
669
758
 
670
759
  Check_Type(status_constant, T_FIXNUM);
671
760
  PURPLE_SAVEDSTATUS(self, savedstatus);
@@ -675,8 +764,6 @@ rb_ruburple_protocol_account_savedstatus_initialize(VALUE self, VALUE status_con
675
764
 
676
765
  call_and_get_result(ruburple_protocol_account_savedstatus_initialize, args);
677
766
 
678
- g_free(args);
679
-
680
767
  return Qnil;
681
768
  }
682
769
 
@@ -957,9 +1044,11 @@ ruburple_callback(va_list args, void *data)
957
1044
  g_mutex_lock(event.lock);
958
1045
 
959
1046
  write(purple_event_queue[1], (char *) &event_pointer, sizeof(IncomingRuburpleEvent *));
960
-
1047
+
1048
+ glib_sleeping = TRUE;
961
1049
  g_cond_wait(event.flag, event.lock);
962
-
1050
+ glib_sleeping = FALSE;
1051
+
963
1052
  g_mutex_unlock(event.lock);
964
1053
 
965
1054
  g_mutex_free(event.lock);
@@ -1007,7 +1096,7 @@ rb_ruburple_subscribe(VALUE self, VALUE handle_value, VALUE signal_name, VALUE c
1007
1096
 
1008
1097
  return_value = RB_RUBURPLE_SUBSCRIPTION_gc(handler);
1009
1098
 
1010
- rb_gc_register_address(&return_value);
1099
+ rb_funcall(gc_save_set, add_method_id, 1, return_value);
1011
1100
 
1012
1101
  return return_value;
1013
1102
  }
@@ -1028,7 +1117,7 @@ rb_ruburple_subscription_unsubscribe(VALUE self)
1028
1117
 
1029
1118
  call_and_get_result(ruburple_signal_disconnect, (gpointer) handler);
1030
1119
 
1031
- rb_gc_unregister_address(&self);
1120
+ rb_funcall(gc_save_set, delete_method_id, 1, self);
1032
1121
 
1033
1122
  return Qnil;
1034
1123
  }
@@ -1048,9 +1137,8 @@ static gpointer
1048
1137
  ruburple_serv_send_im(gpointer data)
1049
1138
  {
1050
1139
  gpointer *args = (gpointer *) data;
1051
- int *rval = g_new(int, 1);
1052
- *rval = serv_send_im((PurpleConnection *) args[0], (char *) args[1], (char *) args[2], * (int *) args[3]);
1053
- return (gpointer) rval;
1140
+ * (int *) args[4] = serv_send_im((PurpleConnection *) args[0], (char *) args[1], (char *) args[2], * (int *) args[3]);
1141
+ return NULL;
1054
1142
  }
1055
1143
 
1056
1144
  static VALUE
@@ -1058,8 +1146,7 @@ rb_ruburple_protocol_connection_send_im(int argc, VALUE *argv, VALUE self)
1058
1146
  {
1059
1147
  PurpleConnection *connection;
1060
1148
  int flags = 0;
1061
- gpointer tmp;
1062
- gpointer args[4];
1149
+ gpointer args[5];
1063
1150
  int rval;
1064
1151
 
1065
1152
  if (argc > 3)
@@ -1081,10 +1168,9 @@ rb_ruburple_protocol_connection_send_im(int argc, VALUE *argv, VALUE self)
1081
1168
  args[1] = (gpointer) RSTRING(argv[0])->ptr;
1082
1169
  args[2] = (gpointer) RSTRING(argv[1])->ptr;
1083
1170
  args[3] = (gpointer) &flags;
1171
+ args[4] = (gpointer) &rval;
1084
1172
 
1085
- tmp = call_and_get_result(ruburple_serv_send_im, args);
1086
- rval = * (int *) tmp;
1087
- g_free(tmp);
1173
+ call_and_get_result(ruburple_serv_send_im, args);
1088
1174
 
1089
1175
  return INT2NUM(rval);
1090
1176
  }
@@ -1111,31 +1197,60 @@ rb_ruburple_protocol_connection_close(VALUE self)
1111
1197
  static gpointer
1112
1198
  ruburple_protocol_account_is_connected(gpointer data)
1113
1199
  {
1114
- PurpleAccount *account = (PurpleAccount *) data;
1115
- gboolean *rval = g_new(gboolean, 1);
1116
- *rval = purple_account_is_connected(account);
1117
- return rval;
1200
+ gpointer *args = (gpointer *) data;
1201
+ * (gboolean *) args[1] = purple_account_is_connected((PurpleAccount *) args[0]);
1202
+ return NULL;
1118
1203
  }
1119
1204
 
1120
1205
  static VALUE
1121
1206
  rb_ruburple_protocol_account_is_connected(VALUE self)
1122
1207
  {
1208
+ gpointer args[2];
1123
1209
  PurpleAccount *account;
1124
- gboolean *tmp;
1210
+ gboolean tmp;
1125
1211
  VALUE rval;
1126
1212
  PURPLE_ACCOUNT(self, account);
1127
- tmp = (gboolean *) call_and_get_result(ruburple_protocol_account_is_connected, account);
1128
- rval = BOOL(*tmp);
1129
- g_free(tmp);
1213
+ args[0] = (gpointer) account;
1214
+ args[1] = (gpointer) &tmp;
1215
+ call_and_get_result(ruburple_protocol_account_is_connected, args);
1216
+ rval = BOOL(tmp);
1130
1217
  return rval;
1131
1218
  }
1132
1219
 
1220
+ static gpointer
1221
+ ruburple_protocol_connection_get_account(gpointer data)
1222
+ {
1223
+ return (gpointer) purple_connection_get_account((PurpleConnection *) data);
1224
+ }
1225
+
1226
+ static VALUE
1227
+ rb_ruburple_protocol_connection_get_account(VALUE self)
1228
+ {
1229
+ PurpleConnection *plugin;
1230
+ PURPLE_CONNECTION(self, plugin);
1231
+ return RB_RUBURPLE_PROTOCOL_ACCOUNT((PurpleAccount *) call_and_get_result(ruburple_protocol_connection_get_account, (gpointer) plugin));
1232
+ }
1233
+
1133
1234
  #ifdef __cplusplus
1134
1235
  extern "C" {
1135
1236
  #endif
1136
1237
  void Init_ruburple_ext() {
1137
1238
  VALUE io_class = rb_define_class("IO", rb_cObject);
1138
1239
  VALUE new_method = rb_intern("new");
1240
+ VALUE set_class;
1241
+
1242
+ rb_f_require(0, rb_str_new2("set"));
1243
+
1244
+ set_class = rb_define_class("Set", rb_cObject);
1245
+
1246
+ gc_save_set = rb_funcall(set_class, new_method, 0);
1247
+
1248
+ g_thread_init(NULL);
1249
+
1250
+ glib_sleeping = FALSE;
1251
+
1252
+ add_method_id = rb_intern("add");
1253
+ delete_method_id = rb_intern("delete");
1139
1254
 
1140
1255
  if (pipe(purple_event_queue) == -1)
1141
1256
  rb_raise(rb_eRuntimeError, "error creating event pipe from libpurple to ruby: %s", strerror(errno));
@@ -1147,6 +1262,7 @@ extern "C" {
1147
1262
  rb_define_singleton_method(rb_ruburple, "quit", rb_ruburple_quit, 0);
1148
1263
  rb_define_singleton_method(rb_ruburple, "debug=", rb_ruburple_set_debug, 1);
1149
1264
  rb_define_singleton_method(rb_ruburple, "protocols", rb_ruburple_get_protocols, 0);
1265
+ rb_define_singleton_method(rb_ruburple, "get_protocol", rb_ruburple_get_protocol, 1);
1150
1266
  rb_define_singleton_method(rb_ruburple, "init", rb_ruburple_init, 0);
1151
1267
  rb_define_singleton_method(rb_ruburple, "_subscribe", rb_ruburple_subscribe, 3);
1152
1268
  rb_define_singleton_method(rb_ruburple, "_read_event", rb_ruburple_read_event, 0);
@@ -1171,6 +1287,8 @@ extern "C" {
1171
1287
  rb_define_const(rb_ruburple, "MESSAGE_IMAGES", INT2NUM(PURPLE_MESSAGE_IMAGES)); /**< Message contains images */
1172
1288
  rb_define_const(rb_ruburple, "MESSAGE_NOTIFY", INT2NUM(PURPLE_MESSAGE_NOTIFY)); /**< Message is a notification */
1173
1289
 
1290
+ rb_ruburple_pointer = rb_define_class_under(rb_ruburple, "Pointer", rb_cObject);
1291
+
1174
1292
  rb_define_const(rb_ruburple, "HANDLE_ACCOUNTS", RB_RUBURPLE_POINTER(purple_accounts_get_handle()));
1175
1293
  rb_define_const(rb_ruburple, "HANDLE_CONNECTIONS", RB_RUBURPLE_POINTER(purple_connections_get_handle()));
1176
1294
  rb_define_const(rb_ruburple, "HANDLE_CONVERSATIONS", RB_RUBURPLE_POINTER(purple_conversations_get_handle()));
@@ -1182,7 +1300,8 @@ extern "C" {
1182
1300
  rb_define_const(rb_ruburple, "HANDLE_SAVEDSTATUSES", RB_RUBURPLE_POINTER(purple_savedstatuses_get_handle()));
1183
1301
  rb_define_const(rb_ruburple, "HANDLE_XFERS", RB_RUBURPLE_POINTER(purple_xfers_get_handle()));
1184
1302
  rb_define_const(rb_ruburple, "EVENT_INPUT", rb_funcall(io_class, new_method, 1, INT2NUM(purple_event_queue[0])));
1185
-
1303
+ rb_define_const(rb_ruburple, "SUBSCRIPTIONS", gc_save_set);
1304
+
1186
1305
  rb_ruburple_plugin = rb_define_class_under(rb_ruburple, "Plugin", rb_cObject);
1187
1306
 
1188
1307
  rb_ruburple_protocol = rb_define_class_under(rb_ruburple, "Protocol", rb_ruburple_plugin);
@@ -1201,6 +1320,10 @@ extern "C" {
1201
1320
  rb_define_method(rb_ruburple_protocol_account, "savedstatus=", rb_ruburple_protocol_account_set_savedstatus, 1);
1202
1321
  rb_define_method(rb_ruburple_protocol_account, "connection", rb_ruburple_protocol_account_get_connection, 0);
1203
1322
  rb_define_method(rb_ruburple_protocol_account, "connected?", rb_ruburple_protocol_account_is_connected, 0);
1323
+ rb_define_method(rb_ruburple_protocol_account, "username", rb_ruburple_protocol_account_get_username, 0);
1324
+ rb_define_method(rb_ruburple_protocol_account, "password", rb_ruburple_protocol_account_get_password, 0);
1325
+ rb_define_method(rb_ruburple_protocol_account, "alias", rb_ruburple_protocol_account_get_alias, 0);
1326
+ rb_define_method(rb_ruburple_protocol_account, "alias=", rb_ruburple_protocol_account_set_alias, 1);
1204
1327
 
1205
1328
  rb_ruburple_protocol_account_conversation = rb_define_class_under(rb_ruburple_protocol_account, "Conversation", rb_cObject);
1206
1329
 
@@ -1223,6 +1346,7 @@ extern "C" {
1223
1346
  rb_ruburple_protocol_connection = rb_define_class_under(rb_ruburple_protocol, "Connection", rb_cObject);
1224
1347
  rb_define_method(rb_ruburple_protocol_connection, "send_im", rb_ruburple_protocol_connection_send_im, -1);
1225
1348
  rb_define_method(rb_ruburple_protocol_connection, "close", rb_ruburple_protocol_connection_close, 0);
1349
+ rb_define_method(rb_ruburple_protocol_connection, "account", rb_ruburple_protocol_connection_get_account, 0);
1226
1350
 
1227
1351
  rb_ruburple_protocol_account_transfer = rb_define_class_under(rb_ruburple_protocol_account, "Transfer", rb_cObject);
1228
1352
 
@@ -1249,10 +1373,6 @@ extern "C" {
1249
1373
  rb_ruburple_subscription = rb_define_class_under(rb_ruburple, "Subscription", rb_cObject);
1250
1374
  rb_define_method(rb_ruburple_subscription, "unsubscribe", rb_ruburple_subscription_unsubscribe, 0);
1251
1375
 
1252
- rb_ruburple_pointer = rb_define_class_under(rb_ruburple, "Pointer", rb_cObject);
1253
-
1254
- g_thread_init(NULL);
1255
-
1256
1376
  }
1257
1377
  #ifdef __cplusplus
1258
1378
  }
@@ -16,6 +16,7 @@
16
16
  # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
 
18
18
  require 'monitor'
19
+ require 'pp'
19
20
 
20
21
  module Ruburple
21
22
 
@@ -97,8 +98,13 @@ module Ruburple
97
98
  @@event_fetcher.kill if defined?(@@event_fetcher) && @@event_fetcher
98
99
  @@event_fetcher = Thread.new do
99
100
  loop do
100
- select([EVENT_INPUT], nil, nil)
101
- _read_event
101
+ begin
102
+ select([EVENT_INPUT], nil, nil)
103
+ _read_event
104
+ rescue Exception => e
105
+ puts "#{e.class}: #{e}"
106
+ pp e.backtrace
107
+ end
102
108
  end
103
109
  end
104
110
 
@@ -0,0 +1,68 @@
1
+
2
+ require File.join(File.dirname(__FILE__), 'test_helper')
3
+ require 'ruburple'
4
+
5
+ class RuburpleTest < Test::Unit::TestCase
6
+
7
+ accounts_file = Pathname.new(__FILE__).parent.join("test_accounts.rb")
8
+ die("You have to create a #{accounts_file} looking like #{accounts_file}.template to run #{__FILE__}") unless accounts_file.exist?
9
+ require accounts_file
10
+
11
+ @@events = {}
12
+
13
+ Ruburple::init
14
+ Ruburple::subscribe(:signed_on, (Proc.new do |c|
15
+ puts "got signed_on from #{c.account.username}"
16
+ @@events[:signed_on] << c
17
+ end))
18
+ Ruburple::subscribe(:received_im_msg, (Proc.new do |account, sender, message, conversation, flags|
19
+ puts "got received_im_msg from #{sender} to #{account.username}"
20
+ @@events[:received_im_msg] << [account.username, sender, message]
21
+ end))
22
+
23
+ def setup
24
+ @@events = {
25
+ :signed_on => [],
26
+ :received_im_msg => []
27
+ }
28
+ end
29
+
30
+ def test_msn
31
+ test_operations("MSN")
32
+ end
33
+
34
+ private
35
+
36
+ def test_operations(protocol)
37
+ test_accounts = $TEST_ACCOUNTS[protocol]
38
+ p = Ruburple::get_protocol(protocol)
39
+ a1 = p.get_account(test_accounts.first.username, test_accounts.first.password)
40
+ a1.connect
41
+ a2 = p.get_account(test_accounts.last.username, test_accounts.last.password)
42
+ a2.connect
43
+ puts "testing sign on..."
44
+ assert_within(30) do
45
+ a1_signed_on = @@events[:signed_on].find do |e|
46
+ e.account.username == test_accounts.first.username
47
+ end
48
+ a2_signed_on = @@events[:signed_on].find do |e|
49
+ e.account.username == test_accounts.last.username
50
+ end
51
+ a1_signed_on && a2_signed_on
52
+ end
53
+ puts "testing send im..."
54
+ a1.connection.send_im(a2.username, "hello a2")
55
+ a2.connection.send_im(a1.username, "hello a1")
56
+ assert_within(30) do
57
+ a1_got_message = @@events[:received_im_msg].find do |recipient, sender, message|
58
+ recipient == a1.username && sender = a2.username && message =~ /hello a1/
59
+ end
60
+ a2_got_message = @@events[:received_im_msg].find do |recipient, sender, message|
61
+ recipient == a2.username && sender = a1.username && message =~ /hello a2/
62
+ end
63
+ a1_got_message && a2_got_message
64
+ end
65
+ end
66
+
67
+
68
+ end
@@ -0,0 +1,7 @@
1
+
2
+ $TEST_ACCOUNTS = {
3
+ "MSN" => [
4
+ TestAccount.new("MSN", "blabahtest1@blabah.com", "blabah"),
5
+ TestAccount.new("MSN", "blabahtest2@blabah.com", "blabah")
6
+ ]
7
+ }
@@ -0,0 +1,7 @@
1
+
2
+ $TEST_ACCOUNTS = {
3
+ "MSN" => [
4
+ TestAccount.new("MSN", "some@msn.account", "some_msn_password"),
5
+ TestAccount.new("MSN", "some@msn.account", "some_msn_password")
6
+ ]
7
+ }
@@ -0,0 +1,50 @@
1
+
2
+ home = File.expand_path(File.dirname(__FILE__))
3
+ $: << File.join(home, "..", "lib")
4
+
5
+ require 'pp'
6
+ require 'test/unit'
7
+ require 'benchmark'
8
+ require 'ruburple'
9
+ require 'pathname'
10
+
11
+ def bm(label = "", options = {})
12
+ n = options[:n] || 1000
13
+ width = options[:width] || 50
14
+ Benchmark.benchmark(" " * width + Benchmark::Tms::CAPTION, width, Benchmark::Tms::FMTSTR, "ms/call") do |b|
15
+ times = b.report("#{n}x#{label}") do
16
+ n.times do
17
+ yield
18
+ end
19
+ end
20
+ [times * 1000 / n.to_f]
21
+ end
22
+ end
23
+
24
+ class TestAccount
25
+ attr_accessor :protocol, :username, :password
26
+ def initialize(protocol, username, password)
27
+ @protocol = protocol
28
+ @username = username
29
+ @password = password
30
+ end
31
+ end
32
+
33
+ def die(s)
34
+ puts "*** #{s} ***"
35
+ exit 1
36
+ end
37
+
38
+ class Test::Unit::TestCase
39
+
40
+ def assert_within(timeout, &block)
41
+ t = Time.new
42
+ rval = yield
43
+ while !rval && t > Time.new - timeout
44
+ rval = yield
45
+ sleep(0.05)
46
+ end
47
+ assert(rval)
48
+ end
49
+
50
+ end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.2
3
3
  specification_version: 1
4
4
  name: ruburple
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.0.3
7
- date: 2007-05-05 00:00:00 +02:00
6
+ version: 0.0.4
7
+ date: 2007-05-07 00:00:00 +02:00
8
8
  summary: An ruby extension to libpurple.
9
9
  require_paths:
10
10
  - lib
@@ -31,12 +31,17 @@ authors:
31
31
  files:
32
32
  - lib/ruburple.rb
33
33
  - lib/ruburple/ruburple.rb
34
+ - tests/ruburple_test.rb
35
+ - tests/test_accounts.rb
36
+ - tests/test_accounts.rb.template
37
+ - tests/test_helper.rb
34
38
  - ext/extconf.rb
35
39
  - ext/ruburple_ext.c
36
40
  - GPL-2
37
41
  - README
38
- test_files: []
39
-
42
+ test_files:
43
+ - tests/ruburple_test.rb
44
+ - tests/test_helper.rb
40
45
  rdoc_options:
41
46
  - --line-numbers
42
47
  - --inline-source