Rewrote SILC Scheduler handling. Use the notify callback instead
[silc.git] / apps / irssi / src / silc / core / silc-core.c
1 /*
2
3   silc-core.c
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2001 - 2007 Pekka Riikonen
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; either version 2 of the License, or
12   (at your option) any later version.
13
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License for more details.
18
19 */
20
21 #include "module.h"
22 #include "chat-protocols.h"
23 #include "args.h"
24
25 #include "chatnets.h"
26 #include "servers-setup.h"
27 #include "channels-setup.h"
28 #include "silc-servers.h"
29 #include "silc-channels.h"
30 #include "silc-queries.h"
31 #include "silc-nicklist.h"
32 #include "silc-chatnets.h"
33 #include "silc-cmdqueue.h"
34 #include "silc-commands.h"
35
36 #include "signals.h"
37 #include "levels.h"
38 #include "settings.h"
39 #include "commands.h"
40 #include "fe-common/core/printtext.h"
41 #include "fe-common/core/fe-channels.h"
42 #include "fe-common/core/keyboard.h"
43 #include "fe-common/silc/module-formats.h"
44
45 #ifndef SILC_PLUGIN
46 /* Command line option variables */
47 static char *opt_pkcs = NULL;
48 static int opt_bits = 0;
49 #else
50 static int init_failed = 0;
51 #endif
52
53 static int running = 0;
54
55 /* SILC Client */
56 SilcClient silc_client = NULL;
57 extern SilcClientOperations ops;
58
59 /* Our keypair */
60 SilcPublicKey irssi_pubkey = NULL;
61 SilcPrivateKey irssi_privkey = NULL;
62
63 char *opt_nickname = NULL;
64 char *opt_hostname = NULL;
65
66 /* Default hash function */
67 SilcHash sha1hash = NULL;
68
69 void silc_expandos_init(void);
70 void silc_expandos_deinit(void);
71
72 void silc_lag_init(void);
73 void silc_lag_deinit(void);
74
75 #ifdef SILC_PLUGIN
76 void silc_core_deinit(void);
77 #endif
78
79 static gboolean my_silc_scheduler(gpointer data)
80 {
81   SILC_LOG_DEBUG(("Timeout"));
82   silc_client_run_one(silc_client);
83   return FALSE;
84 }
85
86 static gboolean my_silc_scheduler_fd(GIOChannel *source,
87                                      GIOCondition condition,
88                                      gpointer data)
89 {
90   SILC_LOG_DEBUG(("I/O event, %d", SILC_PTR_TO_32(data)));
91   silc_client_run_one(silc_client);
92   return TRUE;
93 }
94
95 static void scheduler_notify_cb(SilcSchedule schedule,
96                                 SilcBool added, SilcTask task,
97                                 SilcBool fd_task, SilcUInt32 fd,
98                                 SilcTaskEvent event,
99                                 long seconds, long useconds,
100                                 void *context)
101 {
102   if (added) {
103     if (fd_task) {
104       /* Add fd */
105       GIOChannel *ch;
106       guint e = 0;
107
108       SILC_LOG_DEBUG(("Add fd %d, events %d", fd, event));
109       g_source_remove_by_user_data(SILC_32_TO_PTR(fd));
110
111       if (event & SILC_TASK_READ)
112         e |= G_IO_IN;
113       if (event & SILC_TASK_WRITE)
114         e |= G_IO_OUT;
115
116       if (e) {
117         ch = g_io_channel_unix_new(fd);
118         g_io_add_watch(ch, e, my_silc_scheduler_fd, SILC_32_TO_PTR(fd));
119       }
120     } else {
121       /* Add timeout */
122       guint t;
123
124       /* Zero timeouts are delievered always immediately, as per
125          SilcSchedule API documentation, no need to add them to glib. */
126       if (!seconds && !useconds)
127         return;
128
129       t = (seconds * 1000) + (useconds / 1000),
130       SILC_LOG_DEBUG(("interval %d msec", t));
131       g_timeout_add(t, my_silc_scheduler, NULL);
132     }
133   } else {
134     if (fd_task) {
135       /* Remove fd */
136       g_source_remove_by_user_data(SILC_32_TO_PTR(fd));
137     }
138   }
139 }
140
141 static CHATNET_REC *create_chatnet(void)
142 {
143   return g_malloc0(sizeof(CHATNET_REC));
144 }
145
146 static SERVER_SETUP_REC *create_server_setup(void)
147 {
148   return g_malloc0(sizeof(SERVER_SETUP_REC));
149 }
150
151 static CHANNEL_SETUP_REC *create_channel_setup(void)
152 {
153   return g_malloc0(sizeof(CHANNEL_SETUP_REC));
154 }
155
156 static SERVER_CONNECT_REC *create_server_connect(void)
157 {
158   return g_malloc0(sizeof(SILC_SERVER_CONNECT_REC));
159 }
160
161 static void destroy_server_connect(SERVER_CONNECT_REC *conn)
162 {
163
164 }
165
166 /* Checks user information and saves them to the config file it they
167    do not exist there already. */
168
169 static void silc_init_userinfo(void)
170 {
171   const char *set, *nick, *user_name, *str;
172   char *tmp;
173
174   /* check if nick/username/realname wasn't read from setup.. */
175   set = settings_get_str("real_name");
176   if (set == NULL || *set == '\0') {
177     str = g_getenv("SILCNAME");
178     if (!str)
179       str = g_getenv("IRCNAME");
180     settings_set_str("real_name",
181                      str != NULL ? str : silc_get_real_name());
182   }
183
184   /* Check that real name is UTF-8 encoded */
185   set = settings_get_str("real_name");
186   if (!silc_utf8_valid(set, strlen(set))) {
187     int len = silc_utf8_encoded_len(set, strlen(set), SILC_STRING_LOCALE);
188     tmp = silc_calloc(len, sizeof(*tmp));
189     if (tmp) {
190       silc_utf8_encode(set, strlen(set), SILC_STRING_LOCALE, tmp, len);
191       settings_set_str("real_name", tmp);
192       silc_free(tmp);
193     }
194   }
195
196   /* username */
197   user_name = settings_get_str("user_name");
198   if (user_name == NULL || *user_name == '\0') {
199     str = g_getenv("SILCUSER");
200     if (!str)
201       str = g_getenv("IRCUSER");
202     settings_set_str("user_name",
203                      str != NULL ? str : silc_get_username());
204
205     user_name = settings_get_str("user_name");
206   }
207
208   /* nick */
209   nick = settings_get_str("nick");
210   if (nick == NULL || *nick == '\0') {
211     str = g_getenv("SILCNICK");
212     if (!str)
213       str = g_getenv("IRCNICK");
214     settings_set_str("nick", str != NULL ? str : user_name);
215
216     nick = settings_get_str("nick");
217   }
218
219   /* alternate nick */
220   set = settings_get_str("alternate_nick");
221   if (set == NULL || *set == '\0') {
222     tmp = g_strconcat(nick, "_", NULL);
223     settings_set_str("alternate_nick", tmp);
224     g_free(tmp);
225   }
226
227   /* host name */
228   set = settings_get_str("hostname");
229   if (set == NULL || *set == '\0') {
230     str = g_getenv("SILCHOST");
231     if (!str)
232       str = g_getenv("IRCHOST");
233     if (str != NULL)
234       settings_set_str("hostname", str);
235   }
236 }
237
238 #if defined(SILC_DEBUG) || defined(SILC_PLUGIN)
239 static bool i_debug;
240 #endif
241
242 #ifdef SILC_DEBUG
243 static bool silc_irssi_debug_print(char *file, char *function, int line,
244                                    char *message, void *context)
245 {
246   printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
247             "DEBUG: %s:%d: %s", function, line, message);
248   return TRUE;
249 }
250 #endif
251
252 static void sig_setup_changed(void)
253 {
254 #ifdef SILC_DEBUG
255   bool debug = settings_get_bool("debug");
256   if (debug) {
257     const char *debug_string = settings_get_str("debug_string");
258     i_debug = TRUE;
259     silc_log_debug(TRUE);
260     if (strlen(debug_string))
261       silc_log_set_debug_string(debug_string);
262     silc_log_set_debug_callbacks(silc_irssi_debug_print, NULL, NULL, NULL);
263     return;
264   }
265   if (i_debug)
266     silc_log_debug(FALSE);
267 #endif
268 }
269
270 /* Log callbacks */
271
272 static bool silc_log_misc(SilcLogType type, char *message, void *context)
273 {
274   printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "%s: %s",
275             (type == SILC_LOG_INFO ? "[Info]" :
276              type == SILC_LOG_WARNING ? "[Warning]" : "[Error]"), message);
277   return TRUE;
278 }
279
280 static bool silc_log_stderr(SilcLogType type, char *message, void *context)
281 {
282   fprintf(stderr, "%s: %s\n",
283           (type == SILC_LOG_INFO ? "[Info]" :
284            type == SILC_LOG_WARNING ? "[Warning]" : "[Error]"), message);
285   return TRUE;
286 }
287
288 static void silc_register_cipher(SilcClient client, const char *cipher)
289 {
290   int i;
291
292   if (cipher) {
293     for (i = 0; silc_default_ciphers[i].name; i++)
294       if (!strcmp(silc_default_ciphers[i].name, cipher)) {
295         silc_cipher_register(&(silc_default_ciphers[i]));
296         break;
297       }
298
299     if (!silc_cipher_is_supported(cipher)) {
300       SILC_LOG_ERROR(("Unknown cipher `%s'", cipher));
301 #ifdef SILC_PLUGIN
302       init_failed = -1;
303       return;
304 #else
305       exit(1);
306 #endif
307     }
308   }
309
310   /* Register other defaults */
311   silc_cipher_register_default();
312 }
313
314 static void silc_register_hash(SilcClient client, const char *hash)
315 {
316   int i;
317
318   if (hash) {
319     for (i = 0; silc_default_hash[i].name; i++)
320       if (!strcmp(silc_default_hash[i].name, hash)) {
321         silc_hash_register(&(silc_default_hash[i]));
322         break;
323       }
324
325     if (!silc_hash_is_supported(hash)) {
326       SILC_LOG_ERROR(("Unknown hash function `%s'", hash));
327 #ifdef SILC_PLUGIN
328       init_failed = -1;
329       return;
330 #else
331       exit(1);
332 #endif
333     }
334   }
335
336   /* Register other defaults */
337   silc_hash_register_default();
338 }
339
340 static void silc_register_hmac(SilcClient client, const char *hmac)
341 {
342   int i;
343
344   if (hmac) {
345     for (i = 0; silc_default_hmacs[i].name; i++)
346       if (!strcmp(silc_default_hmacs[i].name, hmac)) {
347         silc_hmac_register(&(silc_default_hmacs[i]));
348         break;
349       }
350
351     if (!silc_hmac_is_supported(hmac)) {
352       SILC_LOG_ERROR(("Unknown HMAC `%s'", hmac));
353 #ifdef SILC_PLUGIN
354       init_failed = -1;
355       return;
356 #else
357       exit(1);
358 #endif
359     }
360   }
361
362   /* Register other defaults */
363   silc_hmac_register_default();
364 }
365
366 /* Finalize init. Init finish signal calls this. */
367
368 #ifdef SILC_PLUGIN
369 #define FUNCTION_EXIT goto out
370 void silc_opt_callback(const char *data, SERVER_REC *server,
371                         WI_ITEM_REC *item)
372 #else
373 #define FUNCTION_EXIT exit(0)
374 void silc_opt_callback(poptContext con,
375                        enum poptCallbackReason reason,
376                        const struct poptOption *opt,
377                        const char *arg, void *data)
378 #endif
379 {
380 #ifdef SILC_PLUGIN
381   unsigned char **argv=NULL, *tmp;
382   SilcUInt32 *argv_lens=NULL, *argv_types=NULL, argc=0;
383   int i;
384   unsigned char privkey[128], pubkey[128];
385
386   memset(privkey, 0, sizeof(privkey));
387   memset(pubkey, 0, sizeof(pubkey));
388   snprintf(pubkey, sizeof(pubkey) - 1, "%s/%s", get_irssi_dir(),
389            SILC_CLIENT_PUBLIC_KEY_NAME);
390   snprintf(privkey, sizeof(privkey) - 1, "%s/%s", get_irssi_dir(),
391            SILC_CLIENT_PRIVATE_KEY_NAME);
392
393   tmp = g_strconcat("SILC", " ", data, NULL);
394   silc_parse_command_line(tmp, &argv, &argv_lens, &argv_types, &argc, 6);
395   g_free(tmp);
396
397   if (argc < 2)
398     goto err;
399 #else
400   if (strcmp(opt->longName, "nick") == 0) {
401     g_free(opt_nickname);
402     opt_nickname = g_strdup(arg);
403   }
404
405   if (strcmp(opt->longName, "hostname") == 0) {
406     silc_free(opt_hostname);
407     opt_hostname = strdup(arg);
408   }
409 #endif
410
411 #ifdef SILC_PLUGIN
412   if ((argc == 2) && (strcasecmp(argv[1], "list-ciphers") == 0)) {
413 #else
414   if (strcmp(opt->longName, "list-ciphers") == 0) {
415     silc_cipher_register_default();
416 #endif
417     silc_client_list_ciphers();
418     FUNCTION_EXIT;
419   }
420
421 #ifdef SILC_PLUGIN
422   if ((argc == 2) && (strcasecmp(argv[1], "list-hash-funcs") == 0)) {
423 #else
424   if (strcmp(opt->longName, "list-hash-funcs") == 0) {
425     silc_hash_register_default();
426 #endif
427     silc_client_list_hash_funcs();
428     FUNCTION_EXIT;
429   }
430
431 #ifdef SILC_PLUGIN
432   if ((argc == 2) && (strcasecmp(argv[1], "list-hmacs") == 0)) {
433 #else
434   if (strcmp(opt->longName, "list-hmacs") == 0) {
435     silc_hmac_register_default();
436 #endif
437     silc_client_list_hmacs();
438     FUNCTION_EXIT;
439   }
440
441 #ifdef SILC_PLUGIN
442   if ((argc == 2) && (strcasecmp(argv[1], "list-pkcs") == 0)) {
443 #else
444   if (strcmp(opt->longName, "list-pkcs") == 0) {
445     silc_pkcs_register_default();
446 #endif
447     silc_client_list_pkcs();
448     FUNCTION_EXIT;
449   }
450
451 #ifdef SILC_PLUGIN
452   if ((argc < 5) && (strcasecmp(argv[1], "debug") == 0)) {
453 #else
454   if (strcmp(opt->longName, "debug") == 0) {
455     silc_log_debug(TRUE);
456     silc_log_debug_hexdump(TRUE);
457     silc_log_set_debug_string(arg);
458 #endif
459 #ifdef SILC_PLUGIN
460     if (argc == 2) {
461       printformat_module("fe-common/silc", NULL, NULL,
462                          MSGLEVEL_CRAP, SILCTXT_CONFIG_DEBUG,
463                          (i_debug == TRUE ? "enabled" : "disabled"));
464       goto out;
465     }
466 #endif
467 #ifndef SILC_DEBUG
468 #ifdef SILC_PLUGIN
469     printformat_module("fe-common/silc", NULL, NULL,
470                        MSGLEVEL_CRAP, SILCTXT_CONFIG_NODEBUG);
471 #else
472     fprintf(stdout,
473             "Run-time debugging is not enabled. To enable it recompile\n"
474             "the client with --enable-debug configuration option.\n");
475     sleep(1);
476 #endif
477 #else
478 #ifdef SILC_PLUGIN
479     if (strcasecmp(argv[2], "on") == 0) {
480       settings_set_bool("debug", TRUE);
481       if (argc == 4)
482          settings_set_str("debug_string", argv[3]);
483     } else if ((argc == 3) && (strcasecmp(argv[2], "off") == 0)) {
484       settings_set_bool("debug", FALSE);
485     } else
486       goto err;
487     sig_setup_changed();
488     printformat_module("fe-common/silc", NULL, NULL,
489                        MSGLEVEL_CRAP, SILCTXT_CONFIG_DEBUG,
490                        (settings_get_bool("debug") == TRUE ?
491                         "enabled" : "disabled"));
492     goto out;
493 #endif
494 #endif
495   }
496
497 #ifdef SILC_PLUGIN
498   if (strcasecmp(argv[1], "create-key-pair") == 0) {
499 #else
500   if (strcmp(opt->longName, "create-key-pair") == 0) {
501 #endif
502     /* Create new key pair and exit */
503 #ifdef SILC_PLUGIN
504     char *endptr, *pkcs=NULL;
505     long int val;
506     int bits=0;
507     CREATE_KEY_REC *rec;
508
509     if ((argc == 3) || (argc == 5))
510       goto err;
511
512     for (i=2; i<argc-1; i+=2)
513       if (strcasecmp(argv[i], "-pkcs") == 0) {
514         if (pkcs == NULL)
515           pkcs = argv[i+1];
516         else
517           goto err;
518       } else if (strcasecmp(argv[i], "-bits") == 0) {
519         if (bits == 0) {
520           val = strtol(argv[i+1], &endptr, 10);
521           if ((*endptr != '\0') || (val <= 0) || (val >= INT_MAX))
522             goto err;
523           bits = val;
524         } else
525           goto err;
526       } else
527         goto err;
528
529     rec = g_new0(CREATE_KEY_REC, 1);
530     rec->pkcs = (pkcs == NULL ? NULL : g_strdup(pkcs));
531     rec->bits = bits;
532
533     keyboard_entry_redirect((SIGNAL_FUNC) create_key_passphrase,
534                             format_get_text("fe-common/silc", NULL, NULL,
535                                             NULL, SILCTXT_CONFIG_PASS_ASK2),
536                             ENTRY_REDIRECT_FLAG_HIDDEN, rec);
537     printformat_module("fe-common/silc", NULL, NULL,
538                        MSGLEVEL_CRAP, SILCTXT_CONFIG_NEXTTIME);
539     goto out;
540 #else
541     silc_cipher_register_default();
542     silc_pkcs_register_default();
543     silc_hash_register_default();
544     silc_hmac_register_default();
545     silc_create_key_pair(opt_pkcs, opt_bits, NULL, NULL,
546                          NULL, NULL, NULL, NULL, TRUE);
547     exit(0);
548 #endif
549   }
550
551 #ifdef SILC_PLUGIN
552   if ((argc < 4) && (strcasecmp(argv[1], "passphrase-change") == 0)) {
553 #else
554   if (strcmp(opt->longName, "passphrase-change") == 0) {
555 #endif
556     /* Change the passphrase of the private key file */
557 #ifdef SILC_PLUGIN
558     CREATE_KEY_REC *rec;
559
560     rec = g_new0(CREATE_KEY_REC, 1);
561     rec->file = g_strdup((argc == 3 ? argv[2] : privkey));
562
563     keyboard_entry_redirect((SIGNAL_FUNC) change_private_key_passphrase,
564                             format_get_text("fe-common/silc", NULL, NULL,
565                                             NULL, SILCTXT_CONFIG_PASS_ASK1),
566                             ENTRY_REDIRECT_FLAG_HIDDEN, rec);
567     goto out;
568 #else
569     silc_cipher_register_default();
570     silc_pkcs_register_default();
571     silc_hash_register_default();
572     silc_hmac_register_default();
573     silc_change_private_key_passphrase(arg, NULL, NULL);
574     exit(0);
575 #endif
576   }
577
578 #ifndef SILC_PLUGIN
579   if (strcmp(opt->longName, "show-key") == 0) {
580     /* Dump the key */
581     silc_cipher_register_default();
582     silc_pkcs_register_default();
583     silc_hash_register_default();
584     silc_hmac_register_default();
585     silc_show_public_key_file((char *)arg);
586     exit(0);
587   }
588 #endif
589
590 #ifdef SILC_PLUGIN
591 err:
592   printformat_module("fe-common/silc", NULL, NULL,
593                      MSGLEVEL_CRAP, SILCTXT_CONFIG_UNKNOWN,
594                      data);
595
596 out:
597   for (i=0; i<argc; i++)
598     silc_free(argv[i]);
599
600   silc_free(argv);
601   silc_free(argv_lens);
602   silc_free(argv_types);
603 #endif
604 }
605 #undef FUNCTION_EXIT
606
607 /* Called to indicate the client library has stopped. */
608 static void
609 silc_stopped(SilcClient client, void *context)
610 {
611   SILC_LOG_DEBUG(("Client library has stopped"));
612   *(int*)context = -1;
613 }
614
615 /* Called to indicate the client library is running. */
616
617 static void
618 silc_running(SilcClient client, void *context)
619 {
620   running = 1;
621   SILC_LOG_DEBUG(("Client library is running"));
622 }
623
624 static void sig_init_finished(void)
625 {
626   /* Check ~/.silc directory and public and private keys */
627   if (!silc_client_check_silc_dir()) {
628 #ifdef SILC_PLUGIN
629     init_failed = -1;
630 #else
631     sleep(1);
632     exit(1);
633 #endif
634     return;
635   }
636
637   /* Load public and private key */
638   if (!silc_client_load_keys(silc_client)) {
639 #ifdef SILC_PLUGIN
640     init_failed = -1;
641 #else
642     sleep(1);
643     exit(1);
644 #endif
645     return;
646   }
647
648   /* Initialize the SILC client */
649   opt_hostname = (opt_hostname ? opt_hostname : silc_net_localhost());
650   if (!silc_client_init(silc_client, settings_get_str("user_name"),
651                         opt_hostname, settings_get_str("real_name"),
652                         silc_running, NULL)) {
653 #ifdef SILC_PLUGIN
654     init_failed = -1;
655 #else
656     sleep(1);
657     exit(1);
658 #endif
659     return;
660   }
661
662   silc_schedule_set_notify(silc_client->schedule, scheduler_notify_cb, NULL);
663
664   silc_log_set_callback(SILC_LOG_INFO, silc_log_misc, NULL);
665   silc_log_set_callback(SILC_LOG_WARNING, silc_log_misc, NULL);
666   silc_log_set_callback(SILC_LOG_ERROR, silc_log_misc, NULL);
667   silc_log_set_callback(SILC_LOG_FATAL, silc_log_misc, NULL);
668
669   silc_hash_alloc("sha1", &sha1hash);
670
671   /* Run SILC scheduler */
672   my_silc_scheduler(NULL);
673 }
674
675 /* Init SILC. Called from src/fe-text/silc.c */
676
677 void silc_core_init(void)
678 {
679 #ifndef SILC_PLUGIN
680   static struct poptOption silc_options[] = {
681     { NULL, '\0', POPT_ARG_CALLBACK, (void *)&silc_opt_callback, '\0', NULL },
682     { "list-ciphers", 0, POPT_ARG_NONE, NULL, 0,
683       "List supported ciphers", NULL },
684     { "list-hash-funcs", 0, POPT_ARG_NONE, NULL, 0,
685       "List supported hash functions", NULL },
686     { "list-hmacs", 0, POPT_ARG_NONE, NULL, 0,
687       "List supported HMACs", NULL },
688     { "list-pkcs", 0, POPT_ARG_NONE, NULL, 0,
689       "List supported PKCSs", NULL },
690 #ifdef SILC_DEBUG
691     { "debug", 'd', POPT_ARG_STRING, NULL, 0,
692       "Enable debugging", "STRING" },
693 #endif /* SILC_DEBUG */
694     { "create-key-pair", 'C', POPT_ARG_NONE, NULL, 0,
695       "Create new public key pair", NULL },
696     { "pkcs", 0, POPT_ARG_STRING, &opt_pkcs, 0,
697       "Set the PKCS of the public key pair (-C)", "PKCS" },
698     { "bits", 0, POPT_ARG_INT, &opt_bits, 0,
699       "Set the length of the public key pair (-C)", "VALUE" },
700     { "passphrase-change", 'P', POPT_ARG_STRING, NULL, 0,
701       "Change the passphrase of private key file", "FILE" },
702     { "show-key", 'S', POPT_ARG_STRING, NULL, 0,
703       "Show the contents of the public key", "FILE" },
704     { NULL, '\0', 0, NULL }
705   };
706 #endif
707
708   CHAT_PROTOCOL_REC *rec;
709   SilcClientParams params;
710   const char *def_cipher, *def_hash, *def_hmac;
711
712 #ifndef SILC_PLUGIN
713   args_register(silc_options);
714 #endif
715
716   /* Settings */
717 #ifndef SILC_PLUGIN
718   settings_add_bool("server", "skip_motd", FALSE);
719   settings_add_str("server", "alternate_nick", NULL);
720 #endif
721   settings_add_bool("server", "use_auto_addr", FALSE);
722   settings_add_str("server", "auto_bind_ip", "");
723   settings_add_str("server", "auto_public_ip", "");
724   settings_add_int("server", "auto_bind_port", 0);
725   settings_add_str("server", "crypto_default_cipher", SILC_DEFAULT_CIPHER);
726   settings_add_str("server", "crypto_default_hash", SILC_DEFAULT_HASH);
727   settings_add_str("server", "crypto_default_hmac", SILC_DEFAULT_HMAC);
728   settings_add_int("server", "key_exchange_timeout_secs", 120);
729   settings_add_int("server", "key_exchange_rekey_secs", 3600);
730   settings_add_bool("server", "key_exchange_rekey_pfs", FALSE);
731   settings_add_int("server", "heartbeat", 300);
732   settings_add_bool("server", "ignore_message_signatures", FALSE);
733   settings_add_str("server", "session_filename", "session.$chatnet");
734   settings_add_bool("server", "sign_channel_messages", FALSE);
735   settings_add_bool("server", "sign_private_messages", FALSE);
736   settings_add_str("silc", "nickname_format", "%n#%a");
737
738   /* Requested Attributes settings */
739   settings_add_bool("silc", "attr_allow", TRUE);
740   settings_add_str("silc", "attr_vcard", "");
741   settings_add_str("silc", "attr_services", "");
742   settings_add_str("silc", "attr_status_mood", "NORMAL");
743   settings_add_str("silc", "attr_status_text", "");
744   settings_add_str("silc", "attr_status_message", NULL);
745   settings_add_str("silc", "attr_preferred_language", "");
746   settings_add_str("silc", "attr_preferred_contact", "CHAT");
747   settings_add_bool("silc", "attr_timezone", TRUE);
748   settings_add_str("silc", "attr_geolocation", "");
749   settings_add_str("silc", "attr_device_info", NULL);
750   settings_add_str("silc", "attr_public_keys", "");
751
752 #ifdef SILC_DEBUG
753   settings_add_bool("debug", "debug", FALSE);
754   settings_add_str("debug", "debug_string", "");
755 #endif
756
757   signal_add("setup changed", (SIGNAL_FUNC) sig_setup_changed);
758 #ifndef SILC_PLUGIN
759   signal_add("irssi init finished", (SIGNAL_FUNC) sig_init_finished);
760 #endif
761
762 #if defined (SILC_PLUGIN) && defined (SILC_DEBUG)
763   if (settings_get_bool("debug") == TRUE)
764     sig_setup_changed();
765 #endif
766
767   silc_init_userinfo();
768
769   silc_log_set_callback(SILC_LOG_INFO, silc_log_stderr, NULL);
770   silc_log_set_callback(SILC_LOG_WARNING, silc_log_stderr, NULL);
771   silc_log_set_callback(SILC_LOG_ERROR, silc_log_stderr, NULL);
772   silc_log_set_callback(SILC_LOG_FATAL, silc_log_stderr, NULL);
773
774   /* Initialize client parameters */
775   memset(&params, 0, sizeof(params));
776   strcat(params.nickname_format, settings_get_str("nickname_format"));
777
778   /* Allocate SILC client */
779   silc_client = silc_client_alloc(&ops, &params, NULL, silc_version_string);
780
781   /* Get the ciphers and stuff from config file */
782   def_cipher = settings_get_str("crypto_default_cipher");
783   def_hash = settings_get_str("crypto_default_hash");
784   def_hmac = settings_get_str("crypto_default_hmac");
785   silc_register_cipher(silc_client, def_cipher);
786 #ifdef SILC_PLUGIN
787   if (init_failed)
788     return;
789 #endif
790   silc_register_hash(silc_client, def_hash);
791 #ifdef SILC_PLUGIN
792   if (init_failed)
793     return;
794 #endif
795   silc_register_hmac(silc_client, def_hmac);
796 #ifdef SILC_PLUGIN
797   if (init_failed)
798     return;
799 #endif
800   silc_pkcs_register_default();
801
802 #ifdef SILC_PLUGIN
803   command_bind("silc", MODULE_NAME, (SIGNAL_FUNC) silc_opt_callback);
804 #endif
805
806   /* Register SILC to the irssi */
807   rec = g_new0(CHAT_PROTOCOL_REC, 1);
808   rec->name = "SILC";
809   rec->fullname = "Secure Internet Live Conferencing";
810   rec->chatnet = "silcnet";
811   rec->create_chatnet = create_chatnet;
812   rec->create_server_setup = create_server_setup;
813   rec->create_channel_setup = create_channel_setup;
814   rec->create_server_connect = create_server_connect;
815   rec->destroy_server_connect = destroy_server_connect;
816   rec->server_init_connect = silc_server_init_connect;
817   rec->server_connect = silc_server_connect;
818   rec->channel_create = (CHANNEL_REC *(*) (SERVER_REC *, const char *,
819                                            const char *, int))
820     silc_channel_create;
821   rec->query_create = (QUERY_REC *(*) (const char *, const char *, int))
822     silc_query_create;
823
824   chat_protocol_register(rec);
825   g_free(rec);
826
827   silc_queue_init();
828   silc_server_init();
829   silc_channels_init();
830   silc_queries_init();
831   silc_expandos_init();
832   silc_lag_init();
833   silc_chatnets_init();
834
835 #ifdef SILC_PLUGIN
836   sig_init_finished();
837   if (init_failed) {
838     silc_core_deinit();
839     return;
840   }
841 #endif
842
843   module_register("silc", "core");
844 }
845
846 /* Deinit SILC. Called from src/fe-text/silc.c */
847
848 void silc_core_deinit(void)
849 {
850   if (running) {
851     volatile int stopped = 0;
852     silc_client_stop(silc_client, silc_stopped, (void *)&stopped);
853     while (!stopped)
854       silc_client_run_one(silc_client);
855   }
856
857   if (opt_hostname)
858     silc_free(opt_hostname);
859   if (opt_nickname)
860     g_free(opt_nickname);
861
862   signal_remove("setup changed", (SIGNAL_FUNC) sig_setup_changed);
863 #ifdef SILC_PLUGIN
864   command_unbind("silc", (SIGNAL_FUNC) silc_opt_callback);
865 #else
866   signal_remove("irssi init finished", (SIGNAL_FUNC) sig_init_finished);
867 #endif
868
869   signal_emit("chat protocol deinit", 1, chat_protocol_find("SILC"));
870
871   silc_hash_free(sha1hash);
872
873   silc_queue_deinit();
874   silc_server_deinit();
875   silc_channels_deinit();
876   silc_queries_deinit();
877   silc_expandos_deinit();
878   silc_lag_deinit();
879   silc_chatnets_deinit();
880
881   chat_protocol_unregister("SILC");
882
883   if (irssi_pubkey)
884     silc_pkcs_public_key_free(irssi_pubkey);
885   if (irssi_privkey)
886     silc_pkcs_private_key_free(irssi_privkey);
887   silc_client_free(silc_client);
888 }