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