Merged silc_1_1_branch to trunk.
[silc.git] / apps / irssi / src / silc / core / silc-core.c
index 9510a9756f7b077e6e67fe0c7856cd27aafde75e..8b6f9f74ed50fc08326068ad805b09a3c27d86d0 100644 (file)
@@ -50,7 +50,7 @@ static int opt_bits = 0;
 static int init_failed = 0;
 #endif
 
-static int idletag = -1;
+static int running = 0;
 
 /* SILC Client */
 SilcClient silc_client = NULL;
@@ -76,10 +76,62 @@ void silc_lag_deinit(void);
 void silc_core_deinit(void);
 #endif
 
-static int my_silc_scheduler(void)
+static gboolean my_silc_scheduler(gpointer data)
 {
+  SILC_LOG_DEBUG(("Timeout"));
   silc_client_run_one(silc_client);
-  return 1;
+  return FALSE;
+}
+
+static gboolean my_silc_scheduler_fd(GIOChannel *source,
+                                     GIOCondition condition,
+                                     gpointer data)
+{
+  SILC_LOG_DEBUG(("I/O event, %d", SILC_PTR_TO_32(data)));
+  silc_client_run_one(silc_client);
+  return TRUE;
+}
+
+static void scheduler_notify_cb(SilcSchedule schedule,
+                               SilcBool added, SilcTask task,
+                               SilcBool fd_task, SilcUInt32 fd,
+                               SilcTaskEvent event,
+                               long seconds, long useconds,
+                               void *context)
+{
+  if (added) {
+    if (fd_task) {
+      /* Add fd */
+      GIOChannel *ch;
+      guint e = 0;
+
+      SILC_LOG_DEBUG(("Add fd %d, events %d", fd, event));
+      g_source_remove_by_user_data(SILC_32_TO_PTR(fd));
+
+      if (event & SILC_TASK_READ)
+        e |= (G_IO_IN | G_IO_PRI | G_IO_HUP | G_IO_ERR);
+      if (event & SILC_TASK_WRITE)
+        e |= (G_IO_OUT | G_IO_HUP | G_IO_ERR | G_IO_NVAL);
+
+      if (e) {
+       ch = g_io_channel_unix_new(fd);
+       g_io_add_watch(ch, e, my_silc_scheduler_fd, SILC_32_TO_PTR(fd));
+       g_io_channel_unref(ch);
+      }
+    } else {
+      /* Add timeout */
+      guint t;
+
+      t = (seconds * 1000) + (useconds / 1000);
+      SILC_LOG_DEBUG(("interval %d msec", t));
+      g_timeout_add(t, my_silc_scheduler, NULL);
+    }
+  } else {
+    if (fd_task) {
+      /* Remove fd */
+      g_source_remove_by_user_data(SILC_32_TO_PTR(fd));
+    }
+  }
 }
 
 static CHATNET_REC *create_chatnet(void)
@@ -244,7 +296,7 @@ static void silc_register_cipher(SilcClient client, const char *cipher)
       SILC_LOG_ERROR(("Unknown cipher `%s'", cipher));
 #ifdef SILC_PLUGIN
       init_failed = -1;
-      returnn;
+      return;
 #else
       exit(1);
 #endif
@@ -270,7 +322,7 @@ static void silc_register_hash(SilcClient client, const char *hash)
       SILC_LOG_ERROR(("Unknown hash function `%s'", hash));
 #ifdef SILC_PLUGIN
       init_failed = -1;
-      returnn;
+      return;
 #else
       exit(1);
 #endif
@@ -296,7 +348,7 @@ static void silc_register_hmac(SilcClient client, const char *hmac)
       SILC_LOG_ERROR(("Unknown HMAC `%s'", hmac));
 #ifdef SILC_PLUGIN
       init_failed = -1;
-      returnn;
+      return;
 #else
       exit(1);
 #endif
@@ -326,7 +378,7 @@ void silc_opt_callback(poptContext con,
   SilcUInt32 *argv_lens=NULL, *argv_types=NULL, argc=0;
   int i;
   unsigned char privkey[128], pubkey[128];
-   
+
   memset(privkey, 0, sizeof(privkey));
   memset(pubkey, 0, sizeof(pubkey));
   snprintf(pubkey, sizeof(pubkey) - 1, "%s/%s", get_irssi_dir(),
@@ -431,7 +483,7 @@ void silc_opt_callback(poptContext con,
     sig_setup_changed();
     printformat_module("fe-common/silc", NULL, NULL,
                       MSGLEVEL_CRAP, SILCTXT_CONFIG_DEBUG,
-                      (settings_get_bool("debug") == TRUE ? 
+                      (settings_get_bool("debug") == TRUE ?
                        "enabled" : "disabled"));
     goto out;
 #endif
@@ -473,7 +525,7 @@ void silc_opt_callback(poptContext con,
     rec = g_new0(CREATE_KEY_REC, 1);
     rec->pkcs = (pkcs == NULL ? NULL : g_strdup(pkcs));
     rec->bits = bits;
-             
+
     keyboard_entry_redirect((SIGNAL_FUNC) create_key_passphrase,
                            format_get_text("fe-common/silc", NULL, NULL,
                                            NULL, SILCTXT_CONFIG_PASS_ASK2),
@@ -500,7 +552,7 @@ void silc_opt_callback(poptContext con,
     /* Change the passphrase of the private key file */
 #ifdef SILC_PLUGIN
     CREATE_KEY_REC *rec;
-    
+
     rec = g_new0(CREATE_KEY_REC, 1);
     rec->file = g_strdup((argc == 3 ? argv[2] : privkey));
 
@@ -549,20 +601,11 @@ out:
 #undef FUNCTION_EXIT
 
 /* Called to indicate the client library has stopped. */
-
 static void
 silc_stopped(SilcClient client, void *context)
 {
   SILC_LOG_DEBUG(("Client library has stopped"));
-  if (idletag != -1)
-    g_source_remove(idletag);
-  signal_emit("chat protocol deinit", 1,
-             chat_protocol_find("SILC"));
-}
-
-static void sig_gui_quit(SILC_SERVER_REC *server, const char *msg)
-{
-  silc_client_stop(silc_client, silc_stopped, NULL);
+  *(int*)context = -1;
 }
 
 /* Called to indicate the client library is running. */
@@ -570,6 +613,7 @@ static void sig_gui_quit(SILC_SERVER_REC *server, const char *msg)
 static void
 silc_running(SilcClient client, void *context)
 {
+  running = 1;
   SILC_LOG_DEBUG(("Client library is running"));
 }
 
@@ -611,6 +655,8 @@ static void sig_init_finished(void)
     return;
   }
 
+  silc_schedule_set_notify(silc_client->schedule, scheduler_notify_cb, NULL);
+
   silc_log_set_callback(SILC_LOG_INFO, silc_log_misc, NULL);
   silc_log_set_callback(SILC_LOG_WARNING, silc_log_misc, NULL);
   silc_log_set_callback(SILC_LOG_ERROR, silc_log_misc, NULL);
@@ -618,8 +664,8 @@ static void sig_init_finished(void)
 
   silc_hash_alloc("sha1", &sha1hash);
 
-  /* register SILC scheduler */
-  idletag = g_timeout_add(5, (GSourceFunc) my_silc_scheduler, NULL);
+  /* Run SILC scheduler */
+  my_silc_scheduler(NULL);
 }
 
 /* Init SILC. Called from src/fe-text/silc.c */
@@ -683,6 +729,7 @@ void silc_core_init(void)
   settings_add_str("server", "session_filename", "session.$chatnet");
   settings_add_bool("server", "sign_channel_messages", FALSE);
   settings_add_bool("server", "sign_private_messages", FALSE);
+  settings_add_str("silc", "nickname_format", "%n#%a");
 
   /* Requested Attributes settings */
   settings_add_bool("silc", "attr_allow", TRUE);
@@ -707,7 +754,6 @@ void silc_core_init(void)
 #ifndef SILC_PLUGIN
   signal_add("irssi init finished", (SIGNAL_FUNC) sig_init_finished);
 #endif
-  signal_add("gui exit", (SIGNAL_FUNC) sig_gui_quit);
 
 #if defined (SILC_PLUGIN) && defined (SILC_DEBUG)
   if (settings_get_bool("debug") == TRUE)
@@ -723,7 +769,8 @@ void silc_core_init(void)
 
   /* Initialize client parameters */
   memset(&params, 0, sizeof(params));
-  strcat(params.nickname_format, "%n@%h%a");
+  strcat(params.nickname_format, settings_get_str("nickname_format"));
+  params.full_channel_names = TRUE;
 
   /* Allocate SILC client */
   silc_client = silc_client_alloc(&ops, &params, NULL, silc_version_string);
@@ -797,8 +844,12 @@ void silc_core_init(void)
 
 void silc_core_deinit(void)
 {
-  if (idletag != -1)
-    g_source_remove(idletag);
+  if (running) {
+    volatile int stopped = 0;
+    silc_client_stop(silc_client, silc_stopped, (void *)&stopped);
+    while (!stopped)
+      silc_client_run_one(silc_client);
+  }
 
   if (opt_hostname)
     silc_free(opt_hostname);
@@ -806,13 +857,14 @@ void silc_core_deinit(void)
     g_free(opt_nickname);
 
   signal_remove("setup changed", (SIGNAL_FUNC) sig_setup_changed);
-  signal_remove("gui exit", (SIGNAL_FUNC) sig_gui_quit);
 #ifdef SILC_PLUGIN
   command_unbind("silc", (SIGNAL_FUNC) silc_opt_callback);
 #else
   signal_remove("irssi init finished", (SIGNAL_FUNC) sig_init_finished);
 #endif
 
+  signal_emit("chat protocol deinit", 1, chat_protocol_find("SILC"));
+
   silc_hash_free(sha1hash);
 
   silc_queue_deinit();
@@ -825,7 +877,9 @@ void silc_core_deinit(void)
 
   chat_protocol_unregister("SILC");
 
-  silc_pkcs_public_key_free(irssi_pubkey);
-  silc_pkcs_private_key_free(irssi_privkey);
+  if (irssi_pubkey)
+    silc_pkcs_public_key_free(irssi_pubkey);
+  if (irssi_privkey)
+    silc_pkcs_private_key_free(irssi_privkey);
   silc_client_free(silc_client);
 }