updates.
[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 "version_internal.h"
33 #include "version.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 bool opt_create_keypair = FALSE;
45 static bool opt_list_ciphers = FALSE;
46 static bool opt_list_hash = FALSE;
47 static bool opt_list_hmac = FALSE;
48 static bool opt_list_pkcs = FALSE;
49 static bool opt_version = FALSE;
50 static char *opt_debug = FALSE;
51 static char *opt_pkcs = NULL;
52 static char *opt_keyfile = NULL;
53 static int opt_bits = 0;
54
55 static int idletag;
56
57 SilcClient silc_client = NULL;
58 SilcClientConfig silc_config = NULL;
59 extern SilcClientOperations ops;
60 extern bool silc_debug;
61 extern bool silc_debug_hexdump;
62 #ifdef SILC_SIM
63 /* SIM (SILC Module) table */
64 SilcSimContext **sims = NULL;
65 uint32 sims_count = 0;
66 #endif
67
68 static int my_silc_scheduler(void)
69 {
70   silc_client_run_one(silc_client);
71   return 1;
72 }
73
74 static CHATNET_REC *create_chatnet(void)
75 {
76   return g_malloc0(sizeof(CHATNET_REC));
77 }
78
79 static SERVER_SETUP_REC *create_server_setup(void)
80 {
81   return g_malloc0(sizeof(SERVER_SETUP_REC));
82 }
83
84 static CHANNEL_SETUP_REC *create_channel_setup(void)
85 {
86   return g_malloc0(sizeof(CHANNEL_SETUP_REC));
87 }
88
89 static SERVER_CONNECT_REC *create_server_connect(void)
90 {
91   return g_malloc0(sizeof(SILC_SERVER_CONNECT_REC));
92 }
93
94 /* Checks user information and saves them to the config file it they
95    do not exist there already. */
96
97 static void silc_init_userinfo(void)
98 {
99   const char *set, *nick, *user_name;
100   char *str;   
101         
102   /* check if nick/username/realname wasn't read from setup.. */
103   set = settings_get_str("real_name");
104   if (set == NULL || *set == '\0') {
105     str = g_getenv("SILCNAME");
106     if (!str)
107       str = g_getenv("IRCNAME");
108     settings_set_str("real_name",
109                      str != NULL ? str : g_get_real_name());
110   }
111  
112   /* username */
113   user_name = settings_get_str("user_name");
114   if (user_name == NULL || *user_name == '\0') {
115     str = g_getenv("SILCUSER");
116     if (!str)
117       str = g_getenv("IRCUSER");
118     settings_set_str("user_name",
119                      str != NULL ? str : g_get_user_name());
120     
121     user_name = settings_get_str("user_name");
122   }
123
124   /* nick */
125   /* Actually take SILCUSER or IRCUSER since nickname cannot be set
126      beforehand in SILC (XXX auto-nicking support should be added to Irssi). */
127   nick = settings_get_str("nick");
128   if (nick == NULL || *nick == '\0') {
129     str = g_getenv("SILCUSER");
130     if (!str)
131       str = g_getenv("IRCUSER");
132     settings_set_str("nick", str != NULL ? str : user_name);
133     
134     nick = settings_get_str("nick");
135   }
136                 
137   /* alternate nick */
138   set = settings_get_str("alternate_nick");
139   if (set == NULL || *set == '\0') {
140     if (strlen(nick) < 9)
141       str = g_strconcat(nick, "_", NULL);
142     else { 
143       str = g_strdup(nick);
144       str[strlen(str)-1] = '_';
145     }
146     settings_set_str("alternate_nick", str);
147     g_free(str);
148   }
149
150   /* host name */
151   set = settings_get_str("hostname");
152   if (set == NULL || *set == '\0') {
153     str = g_getenv("SILCHOST");
154     if (!str)
155       str = g_getenv("IRCHOST");
156     if (str != NULL)
157       settings_set_str("hostname", str);
158   }
159 }
160
161 /* Log callbacks */
162
163 static void silc_log_info(char *message)
164 {
165   fprintf(stderr, "%s\n", message);
166 }
167
168 static void silc_log_warning(char *message)
169 {
170   fprintf(stderr, "%s\n", message);
171 }
172
173 static void silc_log_error(char *message)
174 {
175   fprintf(stderr, "%s\n", message);
176 }
177
178 /* Init SILC. Called from src/fe-text/silc.c */
179
180 void silc_core_init(void)
181 {
182   static struct poptOption options[] = {
183     { "create-key-pair", 'C', POPT_ARG_NONE, &opt_create_keypair, 0, 
184       "Create new public key pair", NULL },
185     { "pkcs", 0, POPT_ARG_STRING, &opt_pkcs, 0, 
186       "Set the PKCS of the public key pair", "PKCS" },
187     { "bits", 0, POPT_ARG_INT, &opt_bits, 0, 
188       "Set the length of the public key pair", "VALUE" },
189     { "show-key", 'S', POPT_ARG_STRING, &opt_keyfile, 0, 
190       "Show the contents of the public key", "FILE" },
191     { "list-ciphers", 'C', POPT_ARG_NONE, &opt_list_ciphers, 0,
192       "List supported ciphers", NULL },
193     { "list-hash-funcs", 'H', POPT_ARG_NONE, &opt_list_hash, 0,
194       "List supported hash functions", NULL },
195     { "list-hmacs", 'H', POPT_ARG_NONE, &opt_list_hmac, 0,
196       "List supported HMACs", NULL },
197     { "list-pkcs", 'P', POPT_ARG_NONE, &opt_list_pkcs, 0,
198       "List supported PKCSs", NULL },
199     { "debug", 'd', POPT_ARG_STRING, &opt_debug, 0,
200       "Enable debugging", "STRING" },
201     { "version", 'V', POPT_ARG_NONE, &opt_version, 0,
202       "Show version", NULL },
203     { NULL, '\0', 0, NULL }
204   };
205
206   args_register(options);
207 }
208
209 static void silc_nickname_format_parse(const char *nickname,
210                                        char **ret_nickname)
211 {
212   silc_parse_userfqdn(nickname, ret_nickname, NULL);
213 }
214
215 /* Finalize init. Called from src/fe-text/silc.c */
216
217 void silc_core_init_finish(void)
218 {
219   CHAT_PROTOCOL_REC *rec;
220   SilcClientParams params;
221
222   if (opt_create_keypair == TRUE) {
223     /* Create new key pair and exit */
224     silc_cipher_register_default();
225     silc_pkcs_register_default();
226     silc_hash_register_default();
227     silc_hmac_register_default();
228     silc_client_create_key_pair(opt_pkcs, opt_bits, 
229                                 NULL, NULL, NULL, NULL, NULL);
230     exit(0);
231   }
232
233   if (opt_keyfile) {
234     /* Dump the key */
235     silc_cipher_register_default();
236     silc_pkcs_register_default();
237     silc_hash_register_default();
238     silc_hmac_register_default();
239     silc_client_show_key(opt_keyfile);
240     exit(0);
241   }
242
243   if (opt_list_ciphers) {
244     silc_cipher_register_default();
245     silc_client_list_ciphers();
246     exit(0);
247   }
248
249   if (opt_list_hash) {
250     silc_hash_register_default();
251     silc_client_list_hash_funcs();
252     exit(0);
253   }
254
255   if (opt_list_hmac) {
256     silc_hmac_register_default();
257     silc_client_list_hmacs();
258     exit(0);
259   }
260
261   if (opt_list_pkcs) {
262     silc_pkcs_register_default();
263     silc_client_list_pkcs();
264     exit(0);
265   }
266
267   if (opt_version) {
268     printf("SILC Secure Internet Live Conferencing, version %s "
269            "(base: SILC Toolkit %s)\n", silc_dist_version, silc_version);
270     printf("(c) 1997 - 2001 Pekka Riikonen <priikone@silcnet.org>\n");
271     exit(0); 
272   }
273
274   if (opt_debug) {
275     silc_debug = TRUE;
276     silc_debug_hexdump = TRUE;
277     silc_log_set_debug_string(opt_debug);
278     silc_log_set_callbacks(silc_log_info, silc_log_warning,
279                            silc_log_error, NULL);
280 #ifndef SILC_DEBUG
281     fprintf(stdout, 
282             "Run-time debugging is not enabled. To enable it recompile\n"
283             "the client with --enable-debug configuration option.\n");
284 #endif
285   }
286
287   /* Do some irssi initializing */
288   settings_add_bool("server", "skip_motd", FALSE);
289   settings_add_str("server", "alternate_nick", NULL);
290   
291   /* Initialize the auto_addr variables Is "server" the best choice for
292    * this?  No existing category seems to apply.
293    */
294   
295   settings_add_bool("server", "use_auto_addr", FALSE);
296   settings_add_str("server", "auto_bind_ip", "");
297   settings_add_str("server", "auto_public_ip", "");
298   settings_add_int("server", "auto_bind_port", 0);
299                                 
300   silc_init_userinfo();
301
302   /* Initialize client parameters */
303   memset(&params, 0, sizeof(params));
304   strcat(params.nickname_format, "%n@%h%a");
305   params.nickname_parse = silc_nickname_format_parse;
306
307   /* Allocate SILC client */
308   silc_client = silc_client_alloc(&ops, &params, NULL, silc_version_string);
309
310   /* Load local config file */
311   silc_config = silc_client_config_alloc(SILC_CLIENT_HOME_CONFIG_FILE);
312
313   /* Get user information */
314   silc_client->username = g_strdup(settings_get_str("user_name"));
315   silc_client->hostname = silc_net_localhost();
316   silc_client->realname = g_strdup(settings_get_str("real_name"));
317
318   /* Register all configured ciphers, PKCS and hash functions. */
319   if (silc_config) {
320     silc_config->client = silc_client;
321     if (!silc_client_config_register_ciphers(silc_config))
322       silc_cipher_register_default();
323     if (!silc_client_config_register_pkcs(silc_config))
324       silc_pkcs_register_default();
325     if (!silc_client_config_register_hashfuncs(silc_config))
326       silc_hash_register_default();
327     if (!silc_client_config_register_hmacs(silc_config))
328       silc_hmac_register_default();
329   } else {
330     /* Register default ciphers, pkcs, hash funtions and hmacs. */
331     silc_cipher_register_default();
332     silc_pkcs_register_default();
333     silc_hash_register_default();
334     silc_hmac_register_default();
335   }
336
337   /* Check ~/.silc directory and public and private keys */
338   if (silc_client_check_silc_dir() == FALSE) {
339     idletag = -1;
340     return;
341   }
342
343   /* Load public and private key */
344   if (silc_client_load_keys(silc_client) == FALSE) {
345     idletag = -1;
346     return;
347   }
348
349   /* Initialize the SILC client */
350   if (!silc_client_init(silc_client)) {
351     idletag = -1;
352     return;
353   }
354
355   /* Register SILC to the irssi */
356   rec = g_new0(CHAT_PROTOCOL_REC, 1);
357   rec->name = "SILC";
358   rec->fullname = "Secure Internet Live Conferencing";
359   rec->chatnet = "silcnet";
360   rec->create_chatnet = create_chatnet;
361   rec->create_server_setup = create_server_setup;
362   rec->create_channel_setup = create_channel_setup;
363   rec->create_server_connect = create_server_connect;
364   rec->server_connect = (SERVER_REC *(*) (SERVER_CONNECT_REC *))
365     silc_server_connect; 
366   rec->channel_create = (CHANNEL_REC *(*) (SERVER_REC *, const char *, int))
367     silc_channel_create;
368   rec->query_create = (QUERY_REC *(*) (const char *, const char *, int))
369     silc_query_create;
370   
371   chat_protocol_register(rec);
372   g_free(rec);
373
374   silc_server_init();
375   silc_channels_init();
376   silc_queries_init();
377
378   idletag = g_timeout_add(5, (GSourceFunc) my_silc_scheduler, NULL);
379 }
380
381 /* Deinit SILC. Called from src/fe-text/silc.c */
382
383 void silc_core_deinit(void)
384 {
385   if (idletag != -1) {
386     signal_emit("chat protocol deinit", 1,
387                 chat_protocol_find("SILC"));
388     
389     silc_server_deinit();
390     silc_channels_deinit();
391     silc_queries_deinit();
392     
393     chat_protocol_unregister("SILC");
394     
395     g_source_remove(idletag);
396   }
397   
398   g_free(silc_client->username);
399   g_free(silc_client->realname);
400   silc_client_free(silc_client);
401 }