Class: PurpleRuby

Inherits:
Object
  • Object
show all
Defined in:
ext/purple_ruby.c

Defined Under Namespace

Classes: Account, Protocol

Constant Summary collapse

NOTIFY_MSG_ERROR =
INT2NUM(PURPLE_NOTIFY_MSG_ERROR)
NOTIFY_MSG_WARNING =
INT2NUM(PURPLE_NOTIFY_MSG_WARNING)
NOTIFY_MSG_INFO =
INT2NUM(PURPLE_NOTIFY_MSG_INFO)

Class Method Summary collapse

Class Method Details

.init(*args) ⇒ Object



370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'ext/purple_ruby.c', line 370

static VALUE init(int argc, VALUE *argv, VALUE self)
{
  VALUE debug, path, settings;

  rb_scan_args(argc, argv, "01", &settings);

  if (argc == 0) {
    debug = Qnil;
    path = Qnil;
  }
  else {
    settings = rb_convert_type(settings, T_HASH, "Hash", "to_hash");

    debug = rb_hash_aref(settings, ID2SYM(DEBUG));
    path = rb_hash_aref(settings, ID2SYM(USER_DIR));
  }

  signal(SIGCHLD, SIG_IGN);
  signal(SIGPIPE, SIG_IGN);
  signal(SIGINT, sighandler);
  
  data_hash_table = g_hash_table_new(NULL, NULL);
  fd_hash_table = g_hash_table_new(NULL, NULL);

  purple_debug_set_enabled(RTEST(debug) ? TRUE : FALSE);

  if (path != Qnil) {
    purple_util_set_user_dir(StringValueCStr(path));
  }

  purple_core_set_ui_ops(&core_uiops);
  purple_eventloop_set_ui_ops(&glib_eventloops);
  
  if (!purple_core_init(UI_ID)) {
    rb_raise(rb_eRuntimeError, "libpurple initialization failed");
  }

  /* Create and load the buddylist. */
  purple_set_blist(purple_blist_new());
  purple_blist_load();

  /* Load the preferences. */
  purple_prefs_load();

  /* Load the pounces. */
  purple_pounces_load();

  return Qnil;
}

.list_protocolsObject



731
732
733
734
735
736
737
738
739
740
741
742
743
# File 'ext/purple_ruby.c', line 731

static VALUE list_protocols(VALUE self)
{
  VALUE array = rb_ary_new();
  
  GList *iter = purple_plugins_get_protocols();
  int i;
  for (i = 0; iter; iter = iter->next) {
    VALUE protocol = Data_Wrap_Struct(cProtocol, NULL, NULL, iter->data);
    rb_ary_push(array, protocol);
  }
  
  return array;
}

.login(protocol, username, password) ⇒ Object



605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
# File 'ext/purple_ruby.c', line 605

static VALUE login(VALUE self, VALUE protocol, VALUE username, VALUE password)
{
  PurpleAccount* account = purple_account_new(StringValueCStr(username), StringValueCStr(protocol));
  if (NULL == account || NULL == account->presence) {
    rb_raise(rb_eRuntimeError, "No able to create account: %s", StringValueCStr(protocol));
  }
  
  purple_account_set_password(account, StringValueCStr(password));
  purple_account_set_remember_password(account, TRUE);
  purple_account_set_enabled(account, UI_ID, TRUE);
  PurpleSavedStatus *status = purple_savedstatus_new(NULL, PURPLE_STATUS_AVAILABLE);
  purple_savedstatus_activate(status);
	
  return Data_Wrap_Struct(cAccount, NULL, NULL, account);
}

.main_loop_runObject



621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
# File 'ext/purple_ruby.c', line 621

static VALUE main_loop_run(VALUE self)
{
  main_loop = g_main_loop_new(NULL, FALSE);
  g_main_loop_run(main_loop);
  
#ifdef DEBUG_MEM_LEAK
  printf("QUIT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
  purple_core_quit();
  if (im_handler == Qnil) rb_gc_unregister_address(&im_handler);
  if (signed_on_handler == Qnil) rb_gc_unregister_address(&signed_on_handler);
  if (connection_error_handler == Qnil) rb_gc_unregister_address(&connection_error_handler);
  if (notify_message_handler == Qnil) rb_gc_unregister_address(&notify_message_handler);
  if (request_handler == Qnil) rb_gc_unregister_address(&request_handler);
  if (ipc_handler == Qnil) rb_gc_unregister_address(&ipc_handler);
  if (timer_timeout != 0) g_source_remove(timer_timeout);
  if (timer_handler == Qnil) rb_gc_unregister_address(&timer_handler);
  if (new_buddy_handler == Qnil) rb_gc_unregister_address(&new_buddy_handler);
  rb_gc_start();
#endif
  
  return Qnil;
}

.main_loop_stopObject



644
645
646
647
648
# File 'ext/purple_ruby.c', line 644

static VALUE main_loop_stop(VALUE self)
{
  g_main_loop_quit(main_loop);
  return Qnil;
}

.watch_connection_errorObject



465
466
467
468
469
470
471
472
473
474
475
476
# File 'ext/purple_ruby.c', line 465

static VALUE watch_connection_error(VALUE self)
{
  finch_connections_init();
  purple_connections_set_ui_ops(&connection_ops);
  
  set_callback(&connection_error_handler, "connection_error_handler");
  
  /*int handle;
  purple_signal_connect(purple_connections_get_handle(), "connection-error", &handle,
        PURPLE_CALLBACK(connection_error), NULL);*/
  return connection_error_handler;
}

.watch_incoming_imObject



420
421
422
423
424
425
# File 'ext/purple_ruby.c', line 420

static VALUE watch_incoming_im(VALUE self)
{
  purple_conversations_set_ui_ops(&conv_uiops);
  set_callback(&im_handler, "im_handler");
  return im_handler;
}

.watch_incoming_ipc(serverip, port) ⇒ Object



543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
# File 'ext/purple_ruby.c', line 543

static VALUE watch_incoming_ipc(VALUE self, VALUE serverip, VALUE port)
{
	struct sockaddr_in my_addr;
	int soc;
	int on = 1;

	/* Open a listening socket for incoming conversations */
	if ((soc = socket(PF_INET, SOCK_STREAM, 0)) < 0)
	{
		rb_raise(rb_eRuntimeError, "Cannot open socket: %s\n", g_strerror(errno));
		return Qnil;
	}

	if (setsockopt(soc, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
	{
		rb_raise(rb_eRuntimeError, "SO_REUSEADDR failed: %s\n", g_strerror(errno));
		return Qnil;
	}

	memset(&my_addr, 0, sizeof(struct sockaddr_in));
	my_addr.sin_family = AF_INET;
	my_addr.sin_addr.s_addr = inet_addr(StringValueCStr(serverip));
	my_addr.sin_port = htons(FIX2INT(port));
	if (bind(soc, (struct sockaddr*)&my_addr, sizeof(struct sockaddr)) != 0)
	{
		rb_raise(rb_eRuntimeError, "Unable to bind to port %d: %s\n", (int)FIX2INT(port), g_strerror(errno));
		return Qnil;
	}

	/* Attempt to listen on the bound socket */
	if (listen(soc, 10) != 0)
	{
		rb_raise(rb_eRuntimeError, "Cannot listen on socket: %s\n", g_strerror(errno));
		return Qnil;
	}

  set_callback(&ipc_handler, "ipc_handler");
  
  /* Open a watcher in the socket we have just opened */
  purple_input_add(soc, PURPLE_INPUT_READ, _accept_socket_handler, NULL);
  
  return port;
}

.watch_new_buddyObject



441
442
443
444
445
446
# File 'ext/purple_ruby.c', line 441

static VALUE watch_new_buddy(VALUE self)
{
  purple_accounts_set_ui_ops(&account_ops);
  set_callback(&new_buddy_handler, "new_buddy_handler");
  return new_buddy_handler;
}

.watch_notify_messageObject



427
428
429
430
431
432
# File 'ext/purple_ruby.c', line 427

static VALUE watch_notify_message(VALUE self)
{
  purple_notify_set_ui_ops(&notify_ops);
  set_callback(&notify_message_handler, "notify_message_handler");
  return notify_message_handler;
}

.watch_requestObject



434
435
436
437
438
439
# File 'ext/purple_ruby.c', line 434

static VALUE watch_request(VALUE self)
{
  purple_request_set_ui_ops(&request_ops);
  set_callback(&request_handler, "request_handler");
  return request_handler;
}

.watch_signed_on_eventObject



456
457
458
459
460
461
462
463
# File 'ext/purple_ruby.c', line 456

static VALUE watch_signed_on_event(VALUE self)
{
  set_callback(&signed_on_handler, "signed_on_handler");
  int handle;
  purple_signal_connect(purple_connections_get_handle(), "signed-on", &handle,
        PURPLE_CALLBACK(signed_on), NULL);
  return signed_on_handler;
}

.watch_timer(delay) ⇒ Object



596
597
598
599
600
601
602
603
# File 'ext/purple_ruby.c', line 596

static VALUE watch_timer(VALUE self, VALUE delay)
{
  set_callback(&timer_handler, "timer_handler");
  if (timer_timeout != 0)
    g_source_remove(timer_timeout);
  timer_timeout = g_timeout_add(delay, do_timeout, timer_handler);
  return delay;
}