[gaim-migrate @ 4156]
[pidgin-git.git] / src / aim.c
blobb345236bf002aece638221e15e4dcd99cca18b7d
1 /*
2 * gaim
4 * Copyright (C) 1998-1999, Mark Spencer <markster@marko.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 #ifdef GAIM_PLUGINS
26 #ifndef _WIN32
27 #include <dlfcn.h>
28 #endif
29 #endif /* GAIM_PLUGINS */
30 #include <gtk/gtk.h>
31 #ifndef _WIN32
32 #include <gdk/gdkx.h>
33 #include <unistd.h>
34 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
37 #include <sys/un.h>
38 #include <sys/wait.h>
39 #endif /* !_WIN32 */
40 #include <gdk/gdk.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <errno.h>
44 #include <stdio.h>
45 #include <string.h>
46 #include <stdarg.h>
47 #include <stdlib.h>
48 #include <ctype.h>
49 #include "prpl.h"
50 #include "gaim.h"
51 #include "gaim-socket.h"
52 #if HAVE_SIGNAL_H
53 #include <signal.h>
54 #endif
55 #include "locale.h"
56 #include <getopt.h>
58 #ifdef _WIN32
59 #include "win32dep.h"
60 #endif
62 static GtkWidget *name;
63 static GtkWidget *pass;
65 GList *log_conversations = NULL;
66 GList *buddy_pounces = NULL;
67 GSList *away_messages = NULL;
68 GList *conversations = NULL;
69 GSList *message_queue = NULL;
70 GSList *unread_message_queue = NULL;
71 GSList *away_time_queue = NULL;
73 GtkWidget *mainwindow = NULL;
75 int opt_away = 0;
76 char *opt_away_arg = NULL;
77 char *opt_rcfile_arg = NULL;
78 int opt_debug = 0;
79 #ifdef _WIN32
80 int opt_gdebug = 0;
81 #endif
83 #if HAVE_SIGNAL_H
85 * Lists of signals we wish to catch and those we wish to ignore.
86 * Each list terminated with -1
88 static int catch_sig_list[] = {
89 SIGSEGV,
90 SIGHUP,
91 SIGINT,
92 SIGTERM,
93 SIGQUIT,
94 SIGCHLD,
98 static int ignore_sig_list[] = {
99 SIGPIPE,
102 #endif
105 void cancel_logon(void)
107 #ifdef GAIM_PLUGINS
108 /* first we tell those who have requested it we're quitting */
109 plugin_event(event_quit);
111 /* then we remove everyone in a mass suicide */
112 remove_all_plugins();
113 #endif /* GAIM_PLUGINS */
114 #ifdef USE_PERL
115 perl_end();
116 #endif
118 gtk_main_quit();
121 static int snd_tmout;
122 int logins_not_muted = 1;
123 static void sound_timeout()
125 logins_not_muted = 1;
126 gtk_timeout_remove(snd_tmout);
129 /* we need to do this for Oscar because serv_login only starts the login
130 * process, it doesn't end there. gaim_setup will be called later from
131 * oscar.c, after the buddy list is made and serv_finish_login is called */
132 void gaim_setup(struct gaim_connection *gc)
134 if ((sound_options & OPT_SOUND_LOGIN) && (sound_options & OPT_SOUND_SILENT_SIGNON)) {
135 logins_not_muted = 0;
136 snd_tmout = gtk_timeout_add(10000, (GtkFunction)sound_timeout, NULL);
141 static void dologin(GtkWidget *widget, GtkWidget *w)
143 struct aim_user *u;
144 const char *username = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(name)->entry));
145 const char *password = gtk_entry_get_text(GTK_ENTRY(pass));
147 if (!strlen(username)) {
148 do_error_dialog(_("Please enter your login."), NULL, GAIM_ERROR);
149 return;
152 /* if there is more than one user of the same name, then fuck
153 * them, they just have to use the account editor to sign in
154 * the second one */
156 u = find_user(username, -1);
157 if (!u)
158 u = new_user(username, DEFAULT_PROTO, OPT_USR_REM_PASS);
159 g_snprintf(u->password, sizeof u->password, "%s", password);
160 save_prefs();
161 serv_login(u);
164 /* <name> is a comma-separated list of names, or NULL
165 if NULL and there is at least one user defined in .gaimrc, try to login.
166 if not NULL, parse <name> into separate strings, look up each one in
167 .gaimrc and, if it's there, try to login.
168 returns: 0 if successful
169 -1 if no user was found that had a saved password
171 static int dologin_named(char *name)
173 struct aim_user *u;
174 char **names, **n;
175 int retval = -1;
177 if (name !=NULL) { /* list of names given */
178 names = g_strsplit(name, ",", 32);
179 for (n = names; *n != NULL; n++) {
180 u = find_user(*n, -1);
181 if (u) { /* found a user */
182 if (u->options & OPT_USR_REM_PASS) {
183 retval = 0;
184 serv_login(u);
188 g_strfreev(names);
189 } else { /* no name given, use default */
190 u = (struct aim_user *)aim_users->data;
191 if (u->options & OPT_USR_REM_PASS) {
192 retval = 0;
193 serv_login(u);
197 return retval;
201 static void doenter(GtkWidget *widget, GtkWidget *w)
203 if (widget == name) {
204 gtk_entry_set_text(GTK_ENTRY(pass), "");
205 gtk_entry_select_region(GTK_ENTRY(GTK_COMBO(name)->entry), 0, 0);
206 gtk_widget_grab_focus(pass);
207 } else if (widget == pass) {
208 dologin(widget, w);
213 static void combo_changed(GtkWidget *w, GtkWidget *combo)
215 const char *txt = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(combo)->entry));
216 struct aim_user *u;
218 u = find_user(txt, -1);
220 if (u && u->options & OPT_USR_REM_PASS) {
221 gtk_entry_set_text(GTK_ENTRY(pass), u->password);
222 } else {
223 gtk_entry_set_text(GTK_ENTRY(pass), "");
228 static GList *combo_user_names()
230 GSList *usr = aim_users;
231 GList *tmp = NULL;
232 struct aim_user *u;
234 if (!usr)
235 return g_list_append(NULL, "<New User>");
237 while (usr) {
238 u = (struct aim_user *)usr->data;
239 tmp = g_list_append(tmp, u->username);
240 usr = usr->next;
243 return tmp;
246 void show_login()
248 GdkPixbuf *icon;
249 GtkWidget *image;
250 GtkWidget *vbox;
251 GtkWidget *button;
252 GtkWidget *hbox;
253 GtkWidget *label;
254 GtkWidget *vbox2;
255 GList *tmp;
257 /* Do we already have a main window opened? If so, bring it back, baby... ribs... yeah */
258 if (mainwindow) {
259 gtk_window_present(GTK_WINDOW(mainwindow));
260 return;
263 mainwindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
265 gtk_window_set_wmclass(GTK_WINDOW(mainwindow), "login", "Gaim");
266 gtk_window_set_policy(GTK_WINDOW(mainwindow), FALSE, FALSE, TRUE);
267 gtk_window_set_title(GTK_WINDOW(mainwindow), _("Gaim - Login"));
268 gtk_widget_realize(mainwindow);
269 gdk_window_set_group(mainwindow->window, mainwindow->window);
270 gtk_container_set_border_width(GTK_CONTAINER(mainwindow), 5);
271 gtk_signal_connect(GTK_OBJECT(mainwindow), "delete_event",
272 GTK_SIGNAL_FUNC(cancel_logon), mainwindow);
275 icon = gaim_pixbuf(NULL, "gaim.png");
276 if (icon) {
277 gtk_window_set_icon(GTK_WINDOW(mainwindow), icon);
278 gdk_pixbuf_unref(icon);
281 vbox = gtk_vbox_new(FALSE, 0);
282 gtk_container_add(GTK_CONTAINER(mainwindow), vbox);
284 image = gaim_pixmap(NULL, "logo.png");
285 gtk_box_pack_start(GTK_BOX(vbox), image, FALSE, FALSE, 0);
287 vbox2 = gtk_vbox_new(FALSE, 0);
288 gtk_container_set_border_width(GTK_CONTAINER(vbox2), 5);
290 label = gtk_label_new(_("Screen Name:"));
291 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
292 gtk_box_pack_start(GTK_BOX(vbox2), label, FALSE, FALSE, 0);
294 name = gtk_combo_new();
295 tmp = combo_user_names();
296 gtk_combo_set_popdown_strings(GTK_COMBO(name), tmp);
297 g_list_free(tmp);
298 gtk_signal_connect(GTK_OBJECT(GTK_COMBO(name)->entry), "activate",
299 GTK_SIGNAL_FUNC(doenter), mainwindow);
300 gtk_signal_connect(GTK_OBJECT(GTK_COMBO(name)->entry), "changed",
301 GTK_SIGNAL_FUNC(combo_changed), name);
302 gtk_box_pack_start(GTK_BOX(vbox2), name, FALSE, TRUE, 0);
303 gtk_box_pack_start(GTK_BOX(vbox), vbox2, FALSE, TRUE, 0);
305 vbox2 = gtk_vbox_new(FALSE, 0);
306 gtk_container_set_border_width(GTK_CONTAINER(vbox2), 5);
308 label = gtk_label_new(_("Password:"));
309 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
310 gtk_box_pack_start(GTK_BOX(vbox2), label, FALSE, FALSE, 0);
312 pass = gtk_entry_new();
313 gtk_entry_set_visibility(GTK_ENTRY(pass), FALSE);
314 gtk_signal_connect(GTK_OBJECT(pass), "activate", GTK_SIGNAL_FUNC(doenter), mainwindow);
315 gtk_box_pack_start(GTK_BOX(vbox2), pass, FALSE, TRUE, 0);
316 gtk_box_pack_start(GTK_BOX(vbox), vbox2, FALSE, TRUE, 0);
318 /* Now for the button box */
319 hbox = gtk_hbox_new(TRUE, 0);
320 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 5);
322 /* And now for the buttons */
323 button = gaim_pixbuf_button("Accounts", "accounts.png", GAIM_BUTTON_VERTICAL);
324 gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE);
325 gtk_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(account_editor), mainwindow);
326 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
328 #ifdef NO_MULTI
329 gtk_widget_set_sensitive(GTK_WIDGET(button), FALSE);
330 #endif
332 button = gaim_pixbuf_button("Settings", "preferences.png", GAIM_BUTTON_VERTICAL);
333 gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE);
334 gtk_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(show_prefs), mainwindow);
335 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
337 button = gaim_pixbuf_button("Sign On", "signon.png", GAIM_BUTTON_VERTICAL);
338 gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE);
339 gtk_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(dologin), mainwindow);
340 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
342 #ifdef _WIN32
343 /* Register newly created window with systray module */
344 wgaim_created_loginwin(GTK_WIDGET(mainwindow));
345 #endif
347 /* Now grab the focus that we need */
348 if (aim_users) {
349 struct aim_user *c = (struct aim_user *)aim_users->data;
350 if (c->options & OPT_USR_REM_PASS) {
351 combo_changed(NULL, name);
352 gtk_widget_grab_focus(button);
353 } else {
354 gtk_widget_grab_focus(pass);
356 } else {
357 gtk_widget_grab_focus(name);
360 /* And raise the curtain! */
361 gtk_widget_show_all(mainwindow);
365 #if HAVE_SIGNAL_H
366 void sighandler(int sig)
368 switch (sig) {
369 case SIGHUP:
370 debug_printf("caught signal %d\n", sig);
371 signoff_all(NULL, NULL);
372 break;
373 case SIGSEGV:
374 core_quit();
375 #ifndef DEBUG
376 fprintf(stderr, "Gaim has segfaulted and attempted to dump a core file.\n"
377 "This is a bug in the software and has happened through\n"
378 "no fault of your own.\n\n"
379 "It is possible that this bug is already fixed in CVS.\n"
380 "You can get a tarball of CVS from the Gaim website, at\n"
381 WEBSITE "gaim-CVS.tar.gz.\n\n"
382 "If you are already using CVS, or can reproduce the crash\n"
383 "using the CVS version, please notify the gaim maintainers\n"
384 "by reporting a bug at\n"
385 WEBSITE "bug.php\n\n"
386 "Please make sure to specify what you were doing at the time,\n"
387 "and post the backtrace from the core file. If you do not know\n"
388 "how to get the backtrace, please get instructions at\n"
389 WEBSITE "gdb.php. If you need further\n"
390 "assistance, please IM either RobFlynn or SeanEgn and\n"
391 "they can help you.\n");
392 #else
393 fprintf(stderr, "Oh no! Segmentation fault!\n");
394 g_on_error_query (g_get_prgname());
395 exit(1);
396 #endif
397 abort();
398 break;
399 case SIGCHLD:
400 clean_pid();
401 #if HAVE_SIGNAL_H
402 signal(SIGCHLD, sighandler); /* restore signal catching on this one! */
403 #endif
404 break;
405 default:
406 debug_printf("caught signal %d\n", sig);
407 signoff_all(NULL, NULL);
408 #ifdef GAIM_PLUGINS
409 remove_all_plugins();
410 #endif
411 if (gtk_main_level())
412 gtk_main_quit();
413 core_quit();
414 exit(0);
417 #endif
419 #ifndef _WIN32
420 static gboolean socket_readable(GIOChannel *source, GIOCondition cond, gpointer ud)
422 guchar type;
423 guchar subtype;
424 guint32 len;
425 guchar *data;
426 guint32 x;
428 debug_printf("Core says: ");
429 g_io_channel_read(source, &type, sizeof(type), &x);
430 if (x == 0) {
431 debug_printf("CORE IS GONE!\n");
432 g_io_channel_close(source);
433 return FALSE;
435 debug_printf("%d ", type);
436 g_io_channel_read(source, &subtype, sizeof(subtype), &x);
437 if (x == 0) {
438 debug_printf("CORE IS GONE!\n");
439 g_io_channel_close(source);
440 return FALSE;
442 debug_printf("%d ", subtype);
443 g_io_channel_read(source, (guchar *)&len, sizeof(len), &x);
444 if (x == 0) {
445 debug_printf("CORE IS GONE!\n");
446 g_io_channel_close(source);
447 return FALSE;
449 debug_printf("(%d bytes)\n", len);
451 data = g_malloc(len);
452 g_io_channel_read(source, data, len, &x);
453 if (x != len) {
454 debug_printf("CORE IS GONE! (read %d/%d bytes)\n", x, len);
455 g_free(data);
456 g_io_channel_close(source);
457 return FALSE;
460 g_free(data);
461 return TRUE;
464 static int ui_main()
466 GIOChannel *channel;
467 int UI_fd;
468 char name[256];
469 GList *icons = NULL;
470 GdkPixbuf *icon = NULL;
472 /* use the nice PNG icon for all the windows */
473 icon = gdk_pixbuf_new_from_file(DATADIR G_DIR_SEPARATOR_S "pixmaps" G_DIR_SEPARATOR_S "gaim.png",NULL);
474 if (icon) {
475 icons = g_list_append(icons,icon);
476 gtk_window_set_default_icon_list(icons);
477 g_object_unref(G_OBJECT(icon));
478 } else {
479 debug_printf("Failed to load icon from %s" G_DIR_SEPARATOR_S "pixmaps" G_DIR_SEPARATOR_S "gaim.png\n",DATADIR);
482 g_snprintf(name, sizeof(name), "%s" G_DIR_SEPARATOR_S "gaim_%s.%d", g_get_tmp_dir(), g_get_user_name(), gaim_session);
484 UI_fd = gaim_connect_to_session(0);
485 if (UI_fd < 0)
486 return 1;
488 channel = g_io_channel_unix_new(UI_fd);
489 g_io_add_watch(channel, G_IO_IN | G_IO_HUP | G_IO_ERR, socket_readable, NULL);
490 return 0;
492 #endif /* _WIN32 */
494 static void set_first_user(char *name)
496 struct aim_user *u;
498 u = find_user(name, -1);
500 if (!u) { /* new user */
501 u = g_new0(struct aim_user, 1);
502 g_snprintf(u->username, sizeof(u->username), "%s", name);
503 u->protocol = DEFAULT_PROTO;
504 aim_users = g_slist_prepend(aim_users, u);
505 } else { /* user already exists */
506 aim_users = g_slist_remove(aim_users, u);
507 aim_users = g_slist_prepend(aim_users, u);
509 save_prefs();
512 #ifdef _WIN32
513 /* WIN32 print and log handlers */
515 static void gaim_dummy_print( const gchar* string ) {
516 return;
519 static void gaim_dummy_log_handler (const gchar *domain,
520 GLogLevelFlags flags,
521 const gchar *msg,
522 gpointer user_data) {
523 return;
526 static void gaim_log_handler (const gchar *domain,
527 GLogLevelFlags flags,
528 const gchar *msg,
529 gpointer user_data) {
530 debug_printf("%s - %s\n", domain, msg);
531 g_log_default_handler(domain, flags, msg, user_data);
533 #endif /* _WIN32 */
535 /* FUCKING GET ME A TOWEL! */
536 #ifdef _WIN32
537 int gaim_main(int argc, char *argv[])
538 #else
539 int main(int argc, char *argv[])
540 #endif
542 int opt_acct = 0, opt_help = 0, opt_version = 0, opt_login = 0, opt_nologin = 0, dologin_ret = -1;
543 char *opt_user_arg = NULL, *opt_login_arg = NULL;
544 #if HAVE_SIGNAL_H
545 int sig_indx; /* for setting up signal catching */
546 sigset_t sigset;
547 void (*prev_sig_disp)();
548 #endif
549 int opt, opt_user = 0;
550 int i;
552 struct option long_options[] = {
553 {"acct", no_argument, NULL, 'a'},
554 /*{"away", optional_argument, NULL, 'w'}, */
555 {"help", no_argument, NULL, 'h'},
556 /*{"login", optional_argument, NULL, 'l'}, */
557 {"loginwin", no_argument, NULL, 'n'},
558 {"user", required_argument, NULL, 'u'},
559 {"file", required_argument, NULL, 'f'},
560 {"debug", no_argument, NULL, 'd'},
561 {"version", no_argument, NULL, 'v'},
562 {0, 0, 0, 0}
565 #ifdef DEBUG
566 opt_debug = 1;
567 #endif
569 #ifdef ENABLE_NLS
570 bindtextdomain(PACKAGE, LOCALEDIR);
571 bind_textdomain_codeset(PACKAGE, "UTF-8");
572 textdomain(PACKAGE);
573 #endif
575 #if HAVE_SIGNAL_H
576 /* Let's not violate any PLA's!!!! */
577 /* jseymour: whatever the fsck that means */
578 /* Robot101: for some reason things like gdm like to block *
579 * useful signals like SIGCHLD, so we unblock all the ones we *
580 * declare a handler for. thanks JSeymour and Vann. */
581 if (sigemptyset(&sigset)) {
582 char errmsg[BUFSIZ];
583 sprintf(errmsg, "Warning: couldn't initialise empty signal set");
584 perror(errmsg);
586 for(sig_indx = 0; catch_sig_list[sig_indx] != -1; ++sig_indx) {
587 if((prev_sig_disp = signal(catch_sig_list[sig_indx], sighandler)) == SIG_ERR) {
588 char errmsg[BUFSIZ];
589 sprintf(errmsg, "Warning: couldn't set signal %d for catching",
590 catch_sig_list[sig_indx]);
591 perror(errmsg);
593 if(sigaddset(&sigset, catch_sig_list[sig_indx])) {
594 char errmsg[BUFSIZ];
595 sprintf(errmsg, "Warning: couldn't include signal %d for unblocking",
596 catch_sig_list[sig_indx]);
597 perror(errmsg);
600 for(sig_indx = 0; ignore_sig_list[sig_indx] != -1; ++sig_indx) {
601 if((prev_sig_disp = signal(ignore_sig_list[sig_indx], SIG_IGN)) == SIG_ERR) {
602 char errmsg[BUFSIZ];
603 sprintf(errmsg, "Warning: couldn't set signal %d to ignore",
604 ignore_sig_list[sig_indx]);
605 perror(errmsg);
609 if (sigprocmask(SIG_UNBLOCK, &sigset, NULL)) {
610 char errmsg[BUFSIZ];
611 sprintf(errmsg, "Warning: couldn't unblock signals");
612 perror(errmsg);
614 #endif
616 for (i = 0; i < argc; i++) {
617 /* --login option */
618 if (strstr(argv[i], "--l") == argv[i]) {
619 char *equals;
620 opt_login = 1;
621 if ((equals = strchr(argv[i], '=')) != NULL) {
622 /* --login=NAME */
623 opt_login_arg = g_strdup(equals + 1);
624 if (strlen(opt_login_arg) == 0) {
625 g_free(opt_login_arg);
626 opt_login_arg = NULL;
628 } else if (i + 1 < argc && argv[i + 1][0] != '-') {
629 /* --login NAME */
630 opt_login_arg = g_strdup(argv[i + 1]);
631 strcpy(argv[i + 1], " ");
633 strcpy(argv[i], " ");
635 /* -l option */
636 else if (strstr(argv[i], "-l") == argv[i]) {
637 opt_login = 1;
638 if (strlen(argv[i]) > 2) {
639 /* -lNAME */
640 opt_login_arg = g_strdup(argv[i] + 2);
641 } else if (i + 1 < argc && argv[i + 1][0] != '-') {
642 /* -l NAME */
643 opt_login_arg = g_strdup(argv[i + 1]);
644 strcpy(argv[i + 1], " ");
646 strcpy(argv[i], " ");
648 /* --away option */
649 else if (strstr(argv[i], "--aw") == argv[i]) {
650 char *equals;
651 opt_away = 1;
652 if ((equals = strchr(argv[i], '=')) != NULL) {
653 /* --away=MESG */
654 opt_away_arg = g_strdup(equals + 1);
655 if (strlen(opt_away_arg) == 0) {
656 g_free(opt_away_arg);
657 opt_away_arg = NULL;
659 } else if (i + 1 < argc && argv[i + 1][0] != '-') {
660 /* --away MESG */
661 opt_away_arg = g_strdup(argv[i + 1]);
662 strcpy(argv[i + 1], " ");
664 strcpy(argv[i], " ");
666 /* -w option */
667 else if (strstr(argv[i], "-w") == argv[i]) {
668 opt_away = 1;
669 if (strlen(argv[i]) > 2) {
670 /* -wMESG */
671 opt_away_arg = g_strdup(argv[i] + 2);
672 } else if (i + 1 < argc && argv[i + 1][0] != '-') {
673 /* -w MESG */
674 opt_away_arg = g_strdup(argv[i + 1]);
675 strcpy(argv[i + 1], " ");
677 strcpy(argv[i], " ");
681 if (opt_login) {
682 printf ("--login given with arg %s\n",
683 opt_login_arg ? opt_login_arg : "NULL");
684 exit(0);
688 gtk_set_locale();
689 gtk_init(&argc, &argv);
691 /* scan command-line options */
692 opterr = 1;
693 while ((opt = getopt_long(argc, argv,
694 #ifndef _WIN32
695 "adhu:f:vn",
696 #else
697 "adghu:f:vn",
698 #endif
699 long_options, NULL)) != -1) {
700 switch (opt) {
701 case 'u': /* set user */
702 opt_user = 1;
703 opt_user_arg = g_strdup(optarg);
704 break;
705 case 'a': /* account editor */
706 opt_acct = 1;
707 break;
708 case 'd': /* debug */
709 opt_debug = 1;
710 break;
711 case 'f':
712 opt_rcfile_arg = g_strdup(optarg);
713 break;
714 case 'v': /* version */
715 opt_version = 1;
716 break;
717 case 'h': /* help */
718 opt_help = 1;
719 break;
720 case 'n': /* don't autologin */
721 opt_nologin = 1;
722 break;
723 #ifdef _WIN32
724 case 'g': /* debug GTK and GLIB */
725 opt_gdebug = 1;
726 break;
727 #endif
728 case '?':
729 default:
730 show_usage(1, argv[0]);
731 return 0;
732 break;
736 #ifdef _WIN32
737 /* We don't want a console window.. */
739 * Any calls to the glib logging functions, result in a call to AllocConsole().
740 * ME and 98 will in such cases produce a console window (2000 not), despite
741 * being built as a windows app rather than a console app. So we should either
742 * ignore messages by setting dummy log handlers, or redirect messages.
743 * This requires setting handlers for all domains (any lib which uses g_logging).
746 g_log_set_handler ("Gdk", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION,
747 (opt_gdebug ? gaim_log_handler : gaim_dummy_log_handler),
748 NULL);
749 g_log_set_handler ("Gtk", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION,
750 (opt_gdebug ? gaim_log_handler : gaim_dummy_log_handler),
751 NULL);
752 g_log_set_handler ("GLib", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION,
753 (opt_gdebug ? gaim_log_handler : gaim_dummy_log_handler),
754 NULL);
755 g_log_set_handler ("GModule", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION,
756 (opt_gdebug ? gaim_log_handler : gaim_dummy_log_handler),
757 NULL);
758 g_log_set_handler ("GLib-GObject", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION,
759 (opt_gdebug ? gaim_log_handler : gaim_dummy_log_handler),
760 NULL);
761 g_log_set_handler ("GThread", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION,
762 (opt_gdebug ? gaim_log_handler : gaim_dummy_log_handler),
763 NULL);
765 /* g_print also makes a call to AllocConsole(), therefore a handler needs to be
766 set here aswell */
767 if(!opt_debug)
768 g_set_print_handler( gaim_dummy_print );
770 #endif
772 /* show help message */
773 if (opt_help) {
774 show_usage(0, argv[0]);
775 return 0;
777 /* show version message */
778 if (opt_version) {
779 printf("Gaim %s\n",VERSION);
780 return 0;
783 #if GAIM_PLUGINS || USE_PERL
784 gaim_probe_plugins();
785 #endif
787 #ifdef _WIN32
788 /* Various win32 initializations */
789 wgaim_init();
790 #endif
792 load_prefs();
793 core_main();
794 #ifndef _WIN32
795 ui_main();
796 #endif
798 /* set the default username */
799 if (opt_user_arg != NULL) {
800 set_first_user(opt_user_arg);
801 g_free(opt_user_arg);
802 opt_user_arg = NULL;
805 if (misc_options & OPT_MISC_DEBUG)
806 show_debug();
808 static_proto_init();
810 /* deal with --login */
811 if (opt_login) {
812 dologin_ret = dologin_named(opt_login_arg);
813 if (opt_login_arg != NULL) {
814 g_free(opt_login_arg);
815 opt_login_arg = NULL;
819 if (!opt_acct && !opt_nologin && gaim_session == 0)
820 auto_login();
822 if (opt_acct) {
823 account_editor(NULL, NULL);
824 } else if ((dologin_ret == -1) && !connections)
825 show_login();
827 gtk_main();
828 core_quit();
829 /* don't need ui_quit here because ui doesn't create anything */
830 #ifdef _WIN32
831 wgaim_cleanup();
832 #endif
833 return 0;