Porting to new Toolkit API.
[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 - 2006 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
35 #include "signals.h"
36 #include "levels.h"
37 #include "settings.h"
38 #include "fe-common/core/printtext.h"
39 #include "fe-common/core/fe-channels.h"
40 #include "fe-common/core/keyboard.h"
41 #include "fe-common/silc/module-formats.h"
42
43 /* Command line option variables */
44 static char *opt_pkcs = NULL;
45 static int opt_bits = 0;
46
47 static int idletag = -1;
48
49 /* SILC Client */
50 SilcClient silc_client = NULL;
51 extern SilcClientOperations ops;
52
53 /* Our keypair */
54 SilcPublicKey irssi_pubkey;
55 SilcPrivateKey irssi_privkey;
56
57 /* Default hash function */
58 SilcHash sha1hash;
59
60 void silc_expandos_init(void);
61 void silc_expandos_deinit(void);
62
63 void silc_lag_init(void);
64 void silc_lag_deinit(void);
65
66 static int my_silc_scheduler(void)
67 {
68   silc_client_run_one(silc_client);
69   return 1;
70 }
71
72 static CHATNET_REC *create_chatnet(void)
73 {
74   return g_malloc0(sizeof(CHATNET_REC));
75 }
76
77 static SERVER_SETUP_REC *create_server_setup(void)
78 {
79   return g_malloc0(sizeof(SERVER_SETUP_REC));
80 }
81
82 static CHANNEL_SETUP_REC *create_channel_setup(void)
83 {
84   return g_malloc0(sizeof(CHANNEL_SETUP_REC));
85 }
86
87 static SERVER_CONNECT_REC *create_server_connect(void)
88 {
89   return g_malloc0(sizeof(SILC_SERVER_CONNECT_REC));
90 }
91
92 static void destroy_server_connect(SERVER_CONNECT_REC *conn)
93 {
94
95 }
96
97 /* Checks user information and saves them to the config file it they
98    do not exist there already. */
99
100 static void silc_init_userinfo(void)
101 {
102   const char *set, *nick, *user_name, *str;
103   char *tmp;
104
105   /* check if nick/username/realname wasn't read from setup.. */
106   set = settings_get_str("real_name");
107   if (set == NULL || *set == '\0') {
108     str = g_getenv("SILCNAME");
109     if (!str)
110       str = g_getenv("IRCNAME");
111     settings_set_str("real_name",
112                      str != NULL ? str : silc_get_real_name());
113   }
114
115   /* username */
116   user_name = settings_get_str("user_name");
117   if (user_name == NULL || *user_name == '\0') {
118     str = g_getenv("SILCUSER");
119     if (!str)
120       str = g_getenv("IRCUSER");
121     settings_set_str("user_name",
122                      str != NULL ? str : silc_get_username());
123
124     user_name = settings_get_str("user_name");
125   }
126
127   /* nick */
128   nick = settings_get_str("nick");
129   if (nick == NULL || *nick == '\0') {
130     str = g_getenv("SILCNICK");
131     if (!str)
132       str = g_getenv("IRCNICK");
133     settings_set_str("nick", str != NULL ? str : user_name);
134
135     nick = settings_get_str("nick");
136   }
137
138   /* alternate nick */
139   set = settings_get_str("alternate_nick");
140   if (set == NULL || *set == '\0') {
141     tmp = g_strconcat(nick, "_", NULL);
142     settings_set_str("alternate_nick", tmp);
143     g_free(tmp);
144   }
145
146   /* host name */
147   set = settings_get_str("hostname");
148   if (set == NULL || *set == '\0') {
149     str = g_getenv("SILCHOST");
150     if (!str)
151       str = g_getenv("IRCHOST");
152     if (str != NULL)
153       settings_set_str("hostname", str);
154   }
155 }
156
157 #ifdef SILC_DEBUG
158 static bool i_debug;
159 static bool silc_irssi_debug_print(char *file, char *function, int line,
160                                    char *message, void *context)
161 {
162   printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
163             "DEBUG: %s:%d: %s", function, line, message);
164   return TRUE;
165 }
166 #endif
167
168 static void sig_setup_changed(void)
169 {
170 #ifdef SILC_DEBUG
171   bool debug = settings_get_bool("debug");
172   if (debug) {
173     const char *debug_string = settings_get_str("debug_string");
174     i_debug = TRUE;
175     silc_log_debug(TRUE);
176     if (strlen(debug_string))
177       silc_log_set_debug_string(debug_string);
178     silc_log_set_debug_callbacks(silc_irssi_debug_print, NULL, NULL, NULL);
179     return;
180   }
181   if (i_debug)
182     silc_log_debug(FALSE);
183 #endif
184 }
185
186 /* Log callbacks */
187
188 static bool silc_log_misc(SilcLogType type, char *message, void *context)
189 {
190   printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "%s: %s",
191             (type == SILC_LOG_INFO ? "[Info]" :
192              type == SILC_LOG_WARNING ? "[Warning]" : "[Error]"), message);
193   return TRUE;
194 }
195
196 static void silc_nickname_format_parse(const char *nickname,
197                                        char **ret_nickname)
198 {
199   char nick[128 + 1];
200   silc_parse_userfqdn(nickname, nick, sizeof(nick), NULL, 0);
201   *ret_nickname = strdup(nick);
202 }
203
204 static void silc_register_cipher(SilcClient client, const char *cipher)
205 {
206   int i;
207
208   if (cipher) {
209     for (i = 0; silc_default_ciphers[i].name; i++)
210       if (!strcmp(silc_default_ciphers[i].name, cipher)) {
211         silc_cipher_register(&(silc_default_ciphers[i]));
212         break;
213       }
214
215     if (!silc_cipher_is_supported(cipher)) {
216       SILC_LOG_ERROR(("Unknown cipher `%s'", cipher));
217       exit(1);
218     }
219   }
220
221   /* Register other defaults */
222   silc_cipher_register_default();
223 }
224
225 static void silc_register_hash(SilcClient client, const char *hash)
226 {
227   int i;
228
229   if (hash) {
230     for (i = 0; silc_default_hash[i].name; i++)
231       if (!strcmp(silc_default_hash[i].name, hash)) {
232         silc_hash_register(&(silc_default_hash[i]));
233         break;
234       }
235
236     if (!silc_hash_is_supported(hash)) {
237       SILC_LOG_ERROR(("Unknown hash function `%s'", hash));
238       exit(1);
239     }
240   }
241
242   /* Register other defaults */
243   silc_hash_register_default();
244 }
245
246 static void silc_register_hmac(SilcClient client, const char *hmac)
247 {
248   int i;
249
250   if (hmac) {
251     for (i = 0; silc_default_hmacs[i].name; i++)
252       if (!strcmp(silc_default_hmacs[i].name, hmac)) {
253         silc_hmac_register(&(silc_default_hmacs[i]));
254         break;
255       }
256
257     if (!silc_hmac_is_supported(hmac)) {
258       SILC_LOG_ERROR(("Unknown HMAC `%s'", hmac));
259       exit(1);
260     }
261   }
262
263   /* Register other defaults */
264   silc_hmac_register_default();
265 }
266
267 /* Finalize init. Init finish signal calls this. */
268
269 void silc_opt_callback(poptContext con,
270                        enum poptCallbackReason reason,
271                        const struct poptOption *opt,
272                        const char *arg, void *data)
273 {
274 #if 0
275   if (strcmp(opt->longName, "nick") == 0) {
276     g_free(silc_client->nickname);
277     silc_client->nickname = g_strdup(arg);
278   }
279
280   if (strcmp(opt->longName, "hostname") == 0) {
281     silc_free(silc_client->hostname);
282     silc_client->hostname = g_strdup(arg);
283   }
284 #endif /* 0 */
285
286   if (strcmp(opt->longName, "list-ciphers") == 0) {
287     silc_cipher_register_default();
288     silc_client_list_ciphers();
289     exit(0);
290   }
291
292   if (strcmp(opt->longName, "list-hash-funcs") == 0) {
293     silc_hash_register_default();
294     silc_client_list_hash_funcs();
295     exit(0);
296   }
297
298   if (strcmp(opt->longName, "list-hmacs") == 0) {
299     silc_hmac_register_default();
300     silc_client_list_hmacs();
301     exit(0);
302   }
303
304   if (strcmp(opt->longName, "list-pkcs") == 0) {
305     silc_pkcs_register_default();
306     silc_client_list_pkcs();
307     exit(0);
308   }
309
310   if (strcmp(opt->longName, "debug") == 0) {
311     silc_log_debug(TRUE);
312     silc_log_debug_hexdump(TRUE);
313     silc_log_set_debug_string(arg);
314 #ifndef SILC_DEBUG
315     fprintf(stdout,
316             "Run-time debugging is not enabled. To enable it recompile\n"
317             "the client with --enable-debug configuration option.\n");
318     sleep(1);
319 #endif
320   }
321
322   if (strcmp(opt->longName, "create-key-pair") == 0) {
323     /* Create new key pair and exit */
324     silc_cipher_register_default();
325     silc_pkcs_register_default();
326     silc_hash_register_default();
327     silc_hmac_register_default();
328     silc_create_key_pair(opt_pkcs, opt_bits, NULL, NULL,
329                          NULL, NULL, NULL, NULL, TRUE);
330     exit(0);
331   }
332
333   if (strcmp(opt->longName, "passphrase-change") == 0) {
334     /* Change the passphrase of the private key file */
335     silc_cipher_register_default();
336     silc_pkcs_register_default();
337     silc_hash_register_default();
338     silc_hmac_register_default();
339     silc_change_private_key_passphrase(arg, NULL, NULL);
340     exit(0);
341   }
342
343   if (strcmp(opt->longName, "show-key") == 0) {
344     /* Dump the key */
345     silc_cipher_register_default();
346     silc_pkcs_register_default();
347     silc_hash_register_default();
348     silc_hmac_register_default();
349     silc_show_public_key_file((char *)arg);
350     exit(0);
351   }
352 }
353
354 static void sig_init_finished(void)
355 {
356   /* Check ~/.silc directory and public and private keys */
357   if (!silc_client_check_silc_dir())
358     exit(1);
359
360   /* Load public and private key */
361   if (!silc_client_load_keys(silc_client))
362     exit(1);
363
364   /* Initialize the SILC client */
365   if (!silc_client_init(silc_client, settings_get_str("user_name"),
366                         silc_net_localhost(), settings_get_str("real_name")))
367     exit(1);
368
369   /* register SILC scheduler */
370   idletag = g_timeout_add(5, (GSourceFunc) my_silc_scheduler, NULL);
371 }
372
373 /* Init SILC. Called from src/fe-text/silc.c */
374
375 void silc_core_init(void)
376 {
377   static struct poptOption silc_options[] = {
378     { NULL, '\0', POPT_ARG_CALLBACK, (void *)&silc_opt_callback, '\0', NULL },
379     { "list-ciphers", 0, POPT_ARG_NONE, NULL, 0,
380       "List supported ciphers", NULL },
381     { "list-hash-funcs", 0, POPT_ARG_NONE, NULL, 0,
382       "List supported hash functions", NULL },
383     { "list-hmacs", 0, POPT_ARG_NONE, NULL, 0,
384       "List supported HMACs", NULL },
385     { "list-pkcs", 0, POPT_ARG_NONE, NULL, 0,
386       "List supported PKCSs", NULL },
387 #ifdef SILC_DEBUG
388     { "debug", 'd', POPT_ARG_STRING, NULL, 0,
389       "Enable debugging", "STRING" },
390 #endif /* SILC_DEBUG */
391     { "create-key-pair", 'C', POPT_ARG_NONE, NULL, 0,
392       "Create new public key pair", NULL },
393     { "pkcs", 0, POPT_ARG_STRING, &opt_pkcs, 0,
394       "Set the PKCS of the public key pair (-C)", "PKCS" },
395     { "bits", 0, POPT_ARG_INT, &opt_bits, 0,
396       "Set the length of the public key pair (-C)", "VALUE" },
397     { "passphrase-change", 'P', POPT_ARG_STRING, NULL, 0,
398       "Change the passphrase of private key file", "FILE" },
399     { "show-key", 'S', POPT_ARG_STRING, NULL, 0,
400       "Show the contents of the public key", "FILE" },
401     { NULL, '\0', 0, NULL }
402   };
403
404   CHAT_PROTOCOL_REC *rec;
405   SilcClientParams params;
406   const char *def_cipher, *def_hash, *def_hmac;
407
408   args_register(silc_options);
409
410   /* Settings */
411   settings_add_bool("server", "skip_motd", FALSE);
412   settings_add_str("server", "alternate_nick", NULL);
413   settings_add_bool("server", "use_auto_addr", FALSE);
414   settings_add_str("server", "auto_bind_ip", "");
415   settings_add_str("server", "auto_public_ip", "");
416   settings_add_int("server", "auto_bind_port", 0);
417   settings_add_str("server", "crypto_default_cipher", SILC_DEFAULT_CIPHER);
418   settings_add_str("server", "crypto_default_hash", SILC_DEFAULT_HASH);
419   settings_add_str("server", "crypto_default_hmac", SILC_DEFAULT_HMAC);
420   settings_add_int("server", "key_exchange_timeout_secs", 120);
421   settings_add_int("server", "key_exchange_rekey_secs", 3600);
422   settings_add_int("server", "connauth_request_secs", 2);
423   settings_add_int("server", "heartbeat", 300);
424   settings_add_bool("server", "ignore_message_signatures", FALSE);
425   settings_add_str("server", "session_filename", "session.$chatnet");
426
427   /* Requested Attributes settings */
428   settings_add_bool("silc", "attr_allow", TRUE);
429   settings_add_str("silc", "attr_vcard", "");
430   settings_add_str("silc", "attr_services", "");
431   settings_add_str("silc", "attr_status_mood", "NORMAL");
432   settings_add_str("silc", "attr_status_text", "");
433   settings_add_str("silc", "attr_status_message", NULL);
434   settings_add_str("silc", "attr_preferred_language", "");
435   settings_add_str("silc", "attr_preferred_contact", "CHAT");
436   settings_add_bool("silc", "attr_timezone", TRUE);
437   settings_add_str("silc", "attr_geolocation", "");
438   settings_add_str("silc", "attr_device_info", NULL);
439   settings_add_str("silc", "attr_public_keys", "");
440
441 #ifdef SILC_DEBUG
442   settings_add_bool("debug", "debug", FALSE);
443   settings_add_str("debug", "debug_string", "");
444 #endif
445
446   signal_add("setup changed", (SIGNAL_FUNC) sig_setup_changed);
447   signal_add("irssi init finished", (SIGNAL_FUNC) sig_init_finished);
448
449   silc_init_userinfo();
450
451   /* Initialize client parameters */
452   memset(&params, 0, sizeof(params));
453   strcat(params.nickname_format, "%n@%h%a");
454   params.nickname_parse = silc_nickname_format_parse;
455   params.rekey_secs = settings_get_int("key_exchange_rekey_secs");
456   params.connauth_request_secs = settings_get_int("connauth_request_secs");
457
458   /* Allocate SILC client */
459   silc_client = silc_client_alloc(&ops, &params, NULL, silc_version_string);
460
461   /* Get the ciphers and stuff from config file */
462   def_cipher = settings_get_str("crypto_default_cipher");
463   def_hash = settings_get_str("crypto_default_hash");
464   def_hmac = settings_get_str("crypto_default_hmac");
465   silc_register_cipher(silc_client, def_cipher);
466   silc_register_hash(silc_client, def_hash);
467   silc_register_hmac(silc_client, def_hmac);
468   silc_pkcs_register_default();
469
470   silc_log_set_callback(SILC_LOG_INFO, silc_log_misc, NULL);
471   silc_log_set_callback(SILC_LOG_WARNING, silc_log_misc, NULL);
472   silc_log_set_callback(SILC_LOG_ERROR, silc_log_misc, NULL);
473   silc_log_set_callback(SILC_LOG_FATAL, silc_log_misc, NULL);
474
475   /* Register SILC to the irssi */
476   rec = g_new0(CHAT_PROTOCOL_REC, 1);
477   rec->name = "SILC";
478   rec->fullname = "Secure Internet Live Conferencing";
479   rec->chatnet = "silcnet";
480   rec->create_chatnet = create_chatnet;
481   rec->create_server_setup = create_server_setup;
482   rec->create_channel_setup = create_channel_setup;
483   rec->create_server_connect = create_server_connect;
484   rec->destroy_server_connect = destroy_server_connect;
485   rec->server_init_connect = silc_server_init_connect;
486   rec->server_connect = silc_server_connect;
487   rec->channel_create = (CHANNEL_REC *(*) (SERVER_REC *, const char *,
488                                            const char *, int))
489     silc_channel_create;
490   rec->query_create = (QUERY_REC *(*) (const char *, const char *, int))
491     silc_query_create;
492
493   chat_protocol_register(rec);
494   g_free(rec);
495
496   silc_queue_init();
497   silc_server_init();
498   silc_channels_init();
499   silc_queries_init();
500   silc_expandos_init();
501   silc_lag_init();
502   silc_chatnets_init();
503
504   module_register("silc", "core");
505 }
506
507 /* Deinit SILC. Called from src/fe-text/silc.c */
508
509 void silc_core_deinit(void)
510 {
511   if (idletag != -1)
512     g_source_remove(idletag);
513
514   signal_emit("chat protocol deinit", 1,
515         chat_protocol_find("SILC"));
516   signal_remove("setup changed", (SIGNAL_FUNC) sig_setup_changed);
517   signal_remove("irssi init finished", (SIGNAL_FUNC) sig_init_finished);
518
519   silc_queue_deinit();
520   silc_server_deinit();
521   silc_channels_deinit();
522   silc_queries_deinit();
523   silc_expandos_deinit();
524   silc_lag_deinit();
525   silc_chatnets_deinit();
526
527   chat_protocol_unregister("SILC");
528
529   silc_pkcs_private_key_free(irssi_privkey);
530   silc_pkcs_public_key_free(irssi_pubkey);
531   silc_client_free(silc_client);
532 }