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