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