comparison mcabber/src/main.c @ 1598:a087125d8fc8

Replace libjabber with loudmouth
author franky
date Sun, 11 Oct 2009 15:38:32 +0200
parents 4f59a414217e
children dcd5d4c75199
comparison
equal deleted inserted replaced
1597:4f59a414217e 1598:a087125d8fc8
29 #include <sys/types.h> 29 #include <sys/types.h>
30 #include <sys/wait.h> 30 #include <sys/wait.h>
31 #include <glib.h> 31 #include <glib.h>
32 #include <config.h> 32 #include <config.h>
33 33
34 #include "jabglue.h"
35 #include "screen.h" 34 #include "screen.h"
36 #include "settings.h" 35 #include "settings.h"
37 #include "roster.h" 36 #include "roster.h"
38 #include "commands.h" 37 #include "commands.h"
39 #include "histolog.h" 38 #include "histolog.h"
40 #include "hooks.h" 39 #include "hooks.h"
41 #include "utils.h" 40 #include "utils.h"
42 #include "pgp.h" 41 #include "pgp.h"
43 #include "otr.h" 42 #include "otr.h"
44 #include "fifo.h" 43 #include "fifo.h"
44 #include "xmpp.h"
45 45
46 #ifdef ENABLE_HGCSET 46 #ifdef ENABLE_HGCSET
47 # include "hgcset.h" 47 # include "hgcset.h"
48 #endif 48 #endif
49 49
50 #ifndef WAIT_ANY 50 #ifndef WAIT_ANY
51 # define WAIT_ANY -1 51 # define WAIT_ANY -1
52 #endif 52 #endif
53 53
54 static unsigned int terminate_ui; 54 static unsigned int terminate_ui;
55 GMainLoop *main_loop = NULL;
55 56
56 static struct termios *backup_termios; 57 static struct termios *backup_termios;
57 58
58 char *mcabber_version(void) 59 char *mcabber_version(void)
59 { 60 {
64 ver = g_strdup(PACKAGE_VERSION); 65 ver = g_strdup(PACKAGE_VERSION);
65 #endif 66 #endif
66 return ver; 67 return ver;
67 } 68 }
68 69
69 void mcabber_connect(void)
70 {
71 const char *username, *password, *resource, *servername;
72 const char *proxy_host;
73 const char *resource_prefix = PACKAGE_NAME;
74 char *dynresource = NULL;
75 char *fjid;
76 int ssl;
77 int sslverify = -1;
78 const char *sslvopt = NULL, *cafile = NULL, *capath = NULL, *ciphers = NULL;
79 static char *cafile_xp, *capath_xp;
80 unsigned int port;
81
82 servername = settings_opt_get("server");
83 username = settings_opt_get("username");
84 password = settings_opt_get("password");
85 resource = settings_opt_get("resource");
86 proxy_host = settings_opt_get("proxy_host");
87
88 // Free the ca*_xp strings if they've already been set.
89 g_free(cafile_xp);
90 g_free(capath_xp);
91 cafile_xp = capath_xp = NULL;
92
93 if (!servername) {
94 scr_LogPrint(LPRINT_NORMAL, "Server name has not been specified!");
95 return;
96 }
97 if (!username) {
98 scr_LogPrint(LPRINT_NORMAL, "User name has not been specified!");
99 return;
100 }
101 if (!password) {
102 scr_LogPrint(LPRINT_NORMAL, "Password has not been specified!");
103 return;
104 }
105
106 port = (unsigned int) settings_opt_get_int("port");
107
108 ssl = settings_opt_get_int("ssl");
109 sslvopt = settings_opt_get("ssl_verify");
110 if (sslvopt)
111 sslverify = settings_opt_get_int("ssl_verify");
112 cafile = settings_opt_get("ssl_cafile");
113 capath = settings_opt_get("ssl_capath");
114 ciphers = settings_opt_get("ssl_ciphers");
115
116 #if !defined(HAVE_OPENSSL) && !defined(HAVE_GNUTLS)
117 if (ssl) {
118 scr_LogPrint(LPRINT_LOGNORM, "** Error: SSL is NOT available, "
119 "do not set the option 'ssl'.");
120 return;
121 } else if (sslvopt || cafile || capath || ciphers) {
122 scr_LogPrint(LPRINT_LOGNORM, "** Warning: SSL is NOT available, "
123 "ignoring ssl-related settings");
124 ssl = sslverify = 0;
125 cafile = capath = ciphers = NULL;
126 }
127 #elif defined HAVE_GNUTLS
128 if (ssl && sslverify != 0) {
129 scr_LogPrint(LPRINT_LOGNORM, "** Error: SSL certificate checking "
130 "is not supported yet with GnuTLS.");
131 scr_LogPrint(LPRINT_LOGNORM,
132 " * Please set 'ssl_verify' to 0 explicitly!");
133 return;
134 }
135 #endif
136 cafile_xp = expand_filename(cafile);
137 capath_xp = expand_filename(capath);
138 cw_set_ssl_options(sslverify, cafile_xp, capath_xp, ciphers, servername);
139 // We can't free the ca*_xp variables now, because they're not duplicated
140 // in cw_set_ssl_options().
141
142 if (!resource)
143 resource = resource_prefix;
144
145 if (!settings_opt_get("disable_random_resource")) {
146 #if HAVE_ARC4RANDOM
147 dynresource = g_strdup_printf("%s.%08x", resource, arc4random());
148 #else
149 unsigned int tab[2];
150 srand(time(NULL));
151 tab[0] = (unsigned int) (0xffff * (rand() / (RAND_MAX + 1.0)));
152 tab[1] = (unsigned int) (0xffff * (rand() / (RAND_MAX + 1.0)));
153 dynresource = g_strdup_printf("%s.%04x%04x", resource, tab[0], tab[1]);
154 #endif
155 resource = dynresource;
156 }
157
158 /* Connect to server */
159 scr_LogPrint(LPRINT_NORMAL|LPRINT_DEBUG, "Connecting to server: %s",
160 servername);
161 if (port)
162 scr_LogPrint(LPRINT_NORMAL|LPRINT_DEBUG, " using port %d", port);
163 scr_LogPrint(LPRINT_NORMAL|LPRINT_DEBUG, " resource %s", resource);
164
165 if (proxy_host) {
166 int proxy_port = settings_opt_get_int("proxy_port");
167 if (proxy_port <= 0 || proxy_port > 65535) {
168 scr_LogPrint(LPRINT_LOGNORM, "Invalid proxy port: %d", proxy_port);
169 } else {
170 const char *proxy_user, *proxy_pass;
171 proxy_user = settings_opt_get("proxy_user");
172 proxy_pass = settings_opt_get("proxy_pass");
173 // Proxy initialization
174 cw_setproxy(proxy_host, proxy_port, proxy_user, proxy_pass);
175 scr_LogPrint(LPRINT_NORMAL|LPRINT_DEBUG, " using proxy %s:%d",
176 proxy_host, proxy_port);
177 }
178 }
179
180 fjid = compose_jid(username, servername, resource);
181 #if defined(HAVE_LIBOTR)
182 otr_init(fjid);
183 #endif
184 jc = jb_connect(fjid, servername, port, ssl, password);
185 g_free(fjid);
186 g_free(dynresource);
187
188 if (!jc)
189 scr_LogPrint(LPRINT_LOGNORM, "Error connecting to (%s)", servername);
190
191 jb_reset_keepalive();
192 }
193
194 static void mcabber_terminate(const char *msg) 70 static void mcabber_terminate(const char *msg)
195 { 71 {
196 fifo_deinit(); 72 fifo_deinit();
197 jb_disconnect(); 73 xmpp_disconnect();
198 scr_TerminateCurses(); 74 scr_TerminateCurses();
199 75
200 // Restore term settings, if needed. 76 // Restore term settings, if needed.
201 if (backup_termios) 77 if (backup_termios)
202 tcsetattr(fileno(stdin), TCSAFLUSH, backup_termios); 78 tcsetattr(fileno(stdin), TCSAFLUSH, backup_termios);
257 new.c_lflag &= ~ECHO; 133 new.c_lflag &= ~ECHO;
258 if (tcsetattr(fileno(stdin), TCSAFLUSH, &new) != 0) return NULL; 134 if (tcsetattr(fileno(stdin), TCSAFLUSH, &new) != 0) return NULL;
259 135
260 /* Read the password. */ 136 /* Read the password. */
261 printf("Please enter %s: ", what); 137 printf("Please enter %s: ", what);
262 fgets(password, passsize, stdin); 138 if (fgets(password, passsize, stdin) == NULL) return NULL;
263 139
264 /* Restore terminal. */ 140 /* Restore terminal. */
265 tcsetattr(fileno(stdin), TCSAFLUSH, &orig); 141 tcsetattr(fileno(stdin), TCSAFLUSH, &orig);
266 printf("\n"); 142 printf("\n");
267 backup_termios = NULL; 143 backup_termios = NULL;
286 static void compile_options(void) 162 static void compile_options(void)
287 { 163 {
288 puts("Installation data directory: " DATA_DIR "\n"); 164 puts("Installation data directory: " DATA_DIR "\n");
289 #ifdef HAVE_UNICODE 165 #ifdef HAVE_UNICODE
290 puts("Compiled with unicode support."); 166 puts("Compiled with unicode support.");
291 #endif
292 #ifdef HAVE_OPENSSL
293 puts("Compiled with OpenSSL support.");
294 #elif defined HAVE_GNUTLS
295 puts("Compiled with GnuTLS support.");
296 #endif 167 #endif
297 #ifdef HAVE_GPGME 168 #ifdef HAVE_GPGME
298 puts("Compiled with GPG support."); 169 puts("Compiled with GPG support.");
299 #endif 170 #endif
300 #ifdef HAVE_LIBOTR 171 #ifdef HAVE_LIBOTR
374 void mcabber_set_terminate_ui(void) 245 void mcabber_set_terminate_ui(void)
375 { 246 {
376 terminate_ui = TRUE; 247 terminate_ui = TRUE;
377 } 248 }
378 249
250 gboolean mcabber_loop()
251 {
252 keycode kcode;
253
254 if (terminate_ui) {
255 g_main_loop_quit(main_loop);
256 return FALSE;
257 }
258 scr_DoUpdate();
259 scr_Getch(&kcode);
260
261 if (kcode.value != ERR) {
262 process_key(kcode);
263 } else {
264 scr_CheckAutoAway(FALSE);
265
266 if (update_roster)
267 scr_DrawRoster();
268
269 hk_mainloop();
270 }
271 return TRUE;
272 }
273
379 int main(int argc, char **argv) 274 int main(int argc, char **argv)
380 { 275 {
381 char *configFile = NULL; 276 char *configFile = NULL;
382 const char *optstring; 277 const char *optstring;
383 int optval, optval2; 278 int optval, optval2;
384 unsigned int ping;
385 int ret; 279 int ret;
386 keycode kcode;
387 280
388 credits(); 281 credits();
389 282
390 signal(SIGTERM, sig_handler); 283 signal(SIGTERM, sig_handler);
391 signal(SIGINT, sig_handler); 284 signal(SIGINT, sig_handler);
482 375
483 optstring = settings_opt_get("events_command"); 376 optstring = settings_opt_get("events_command");
484 if (optstring) 377 if (optstring)
485 hk_ext_cmd_init(optstring); 378 hk_ext_cmd_init(optstring);
486 379
487 ping = 40;
488 if (settings_opt_get("pinginterval"))
489 ping = (unsigned int) settings_opt_get_int("pinginterval");
490 jb_set_keepalive_delay(ping);
491 scr_LogPrint(LPRINT_DEBUG, "Ping interval established: %d secs", ping);
492
493 if (settings_opt_get_int("hide_offline_buddies") > 0) { // XXX Deprecated 380 if (settings_opt_get_int("hide_offline_buddies") > 0) { // XXX Deprecated
494 scr_RosterDisplay("ofdna"); 381 scr_RosterDisplay("ofdna");
495 scr_LogPrint(LPRINT_LOGNORM, 382 scr_LogPrint(LPRINT_LOGNORM,
496 "* Warning: 'hide_offline_buddies' is deprecated."); 383 "* Warning: 'hide_offline_buddies' is deprecated.");
497 } else { 384 } else {
509 fifo_init(settings_opt_get("fifo_name")); 396 fifo_init(settings_opt_get("fifo_name"));
510 397
511 /* Load previous roster state */ 398 /* Load previous roster state */
512 hlog_load_state(); 399 hlog_load_state();
513 400
401 main_loop = g_main_loop_new(NULL, TRUE);
402
514 if (ret < 0) { 403 if (ret < 0) {
515 scr_LogPrint(LPRINT_NORMAL, "No configuration file has been found."); 404 scr_LogPrint(LPRINT_NORMAL, "No configuration file has been found.");
516 scr_ShowBuddyWindow(); 405 scr_ShowBuddyWindow();
517 } else { 406 } else {
518 /* Connection */ 407 /* Connection */
519 mcabber_connect(); 408 xmpp_connect();
520 } 409 }
521 410
522 scr_LogPrint(LPRINT_DEBUG, "Entering into main loop..."); 411 scr_LogPrint(LPRINT_DEBUG, "Entering into main loop...");
523 412
524 while (!terminate_ui) { 413 g_timeout_add(10, mcabber_loop, NULL);
525 scr_DoUpdate(); 414 g_main_loop_run(main_loop);
526 scr_Getch(&kcode);
527
528 if (kcode.value != ERR) {
529 process_key(kcode);
530 } else {
531 scr_CheckAutoAway(FALSE);
532
533 if (update_roster)
534 scr_DrawRoster();
535
536 jb_main();
537 hk_mainloop();
538 }
539 }
540 415
541 scr_TerminateCurses(); 416 scr_TerminateCurses();
542 fifo_deinit(); 417 fifo_deinit();
543 #ifdef HAVE_LIBOTR 418 #ifdef HAVE_LIBOTR
544 otr_terminate(); 419 otr_terminate();
545 #endif 420 #endif
546 jb_disconnect(); 421 xmpp_disconnect();
547 #ifdef HAVE_GPGME 422 #ifdef HAVE_GPGME
548 gpg_terminate(); 423 gpg_terminate();
549 #endif 424 #endif
550 #if defined(WITH_ENCHANT) || defined(WITH_ASPELL) 425 #if defined(WITH_ENCHANT) || defined(WITH_ASPELL)
551 /* Deinitialize spelling */ 426 /* Deinitialize spelling */