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