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
29 #endif /* GAIM_PLUGINS */
34 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
41 #include <sys/types.h>
51 #include "gaim-socket.h"
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
;
76 char *opt_away_arg
= NULL
;
77 char *opt_rcfile_arg
= NULL
;
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
[] = {
98 static int ignore_sig_list
[] = {
105 void cancel_logon(void)
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 */
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
)
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
);
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
156 u
= find_user(username
, -1);
158 u
= new_user(username
, DEFAULT_PROTO
, OPT_USR_REM_PASS
);
159 g_snprintf(u
->password
, sizeof u
->password
, "%s", password
);
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
)
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
) {
189 } else { /* no name given, use default */
190 u
= (struct aim_user
*)aim_users
->data
;
191 if (u
->options
& OPT_USR_REM_PASS
) {
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
) {
213 static void combo_changed(GtkWidget
*w
, GtkWidget
*combo
)
215 const char *txt
= gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(combo
)->entry
));
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
);
223 gtk_entry_set_text(GTK_ENTRY(pass
), "");
228 static GList
*combo_user_names()
230 GSList
*usr
= aim_users
;
235 return g_list_append(NULL
, "<New User>");
238 u
= (struct aim_user
*)usr
->data
;
239 tmp
= g_list_append(tmp
, u
->username
);
257 /* Do we already have a main window opened? If so, bring it back, baby... ribs... yeah */
259 gtk_window_present(GTK_WINDOW(mainwindow
));
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");
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
);
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);
329 gtk_widget_set_sensitive(GTK_WIDGET(button
), FALSE
);
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);
343 /* Register newly created window with systray module */
344 wgaim_created_loginwin(GTK_WIDGET(mainwindow
));
347 /* Now grab the focus that we need */
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
);
354 gtk_widget_grab_focus(pass
);
357 gtk_widget_grab_focus(name
);
360 /* And raise the curtain! */
361 gtk_widget_show_all(mainwindow
);
366 void sighandler(int sig
)
370 debug_printf("caught signal %d\n", sig
);
371 signoff_all(NULL
, NULL
);
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");
393 fprintf(stderr
, "Oh no! Segmentation fault!\n");
394 g_on_error_query (g_get_prgname());
402 signal(SIGCHLD
, sighandler
); /* restore signal catching on this one! */
406 debug_printf("caught signal %d\n", sig
);
407 signoff_all(NULL
, NULL
);
409 remove_all_plugins();
411 if (gtk_main_level())
420 static gboolean
socket_readable(GIOChannel
*source
, GIOCondition cond
, gpointer ud
)
428 debug_printf("Core says: ");
429 g_io_channel_read(source
, &type
, sizeof(type
), &x
);
431 debug_printf("CORE IS GONE!\n");
432 g_io_channel_close(source
);
435 debug_printf("%d ", type
);
436 g_io_channel_read(source
, &subtype
, sizeof(subtype
), &x
);
438 debug_printf("CORE IS GONE!\n");
439 g_io_channel_close(source
);
442 debug_printf("%d ", subtype
);
443 g_io_channel_read(source
, (guchar
*)&len
, sizeof(len
), &x
);
445 debug_printf("CORE IS GONE!\n");
446 g_io_channel_close(source
);
449 debug_printf("(%d bytes)\n", len
);
451 data
= g_malloc(len
);
452 g_io_channel_read(source
, data
, len
, &x
);
454 debug_printf("CORE IS GONE! (read %d/%d bytes)\n", x
, len
);
456 g_io_channel_close(source
);
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
);
475 icons
= g_list_append(icons
,icon
);
476 gtk_window_set_default_icon_list(icons
);
477 g_object_unref(G_OBJECT(icon
));
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);
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
);
494 static void set_first_user(char *name
)
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
);
513 /* WIN32 print and log handlers */
515 static void gaim_dummy_print( const gchar
* string
) {
519 static void gaim_dummy_log_handler (const gchar
*domain
,
520 GLogLevelFlags flags
,
522 gpointer user_data
) {
526 static void gaim_log_handler (const gchar
*domain
,
527 GLogLevelFlags flags
,
529 gpointer user_data
) {
530 debug_printf("%s - %s\n", domain
, msg
);
531 g_log_default_handler(domain
, flags
, msg
, user_data
);
535 /* FUCKING GET ME A TOWEL! */
537 int gaim_main(int argc
, char *argv
[])
539 int main(int argc
, char *argv
[])
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
;
545 int sig_indx
; /* for setting up signal catching */
547 void (*prev_sig_disp
)();
549 int opt
, opt_user
= 0;
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'},
570 bindtextdomain(PACKAGE
, LOCALEDIR
);
571 bind_textdomain_codeset(PACKAGE
, "UTF-8");
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
)) {
583 sprintf(errmsg
, "Warning: couldn't initialise empty signal set");
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
) {
589 sprintf(errmsg
, "Warning: couldn't set signal %d for catching",
590 catch_sig_list
[sig_indx
]);
593 if(sigaddset(&sigset
, catch_sig_list
[sig_indx
])) {
595 sprintf(errmsg
, "Warning: couldn't include signal %d for unblocking",
596 catch_sig_list
[sig_indx
]);
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
) {
603 sprintf(errmsg
, "Warning: couldn't set signal %d to ignore",
604 ignore_sig_list
[sig_indx
]);
609 if (sigprocmask(SIG_UNBLOCK
, &sigset
, NULL
)) {
611 sprintf(errmsg
, "Warning: couldn't unblock signals");
616 for (i
= 0; i
< argc
; i
++) {
618 if (strstr(argv
[i
], "--l") == argv
[i
]) {
621 if ((equals
= strchr(argv
[i
], '=')) != NULL
) {
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] != '-') {
630 opt_login_arg
= g_strdup(argv
[i
+ 1]);
631 strcpy(argv
[i
+ 1], " ");
633 strcpy(argv
[i
], " ");
636 else if (strstr(argv
[i
], "-l") == argv
[i
]) {
638 if (strlen(argv
[i
]) > 2) {
640 opt_login_arg
= g_strdup(argv
[i
] + 2);
641 } else if (i
+ 1 < argc
&& argv
[i
+ 1][0] != '-') {
643 opt_login_arg
= g_strdup(argv
[i
+ 1]);
644 strcpy(argv
[i
+ 1], " ");
646 strcpy(argv
[i
], " ");
649 else if (strstr(argv
[i
], "--aw") == argv
[i
]) {
652 if ((equals
= strchr(argv
[i
], '=')) != NULL
) {
654 opt_away_arg
= g_strdup(equals
+ 1);
655 if (strlen(opt_away_arg
) == 0) {
656 g_free(opt_away_arg
);
659 } else if (i
+ 1 < argc
&& argv
[i
+ 1][0] != '-') {
661 opt_away_arg
= g_strdup(argv
[i
+ 1]);
662 strcpy(argv
[i
+ 1], " ");
664 strcpy(argv
[i
], " ");
667 else if (strstr(argv
[i
], "-w") == argv
[i
]) {
669 if (strlen(argv
[i
]) > 2) {
671 opt_away_arg
= g_strdup(argv
[i
] + 2);
672 } else if (i
+ 1 < argc
&& argv
[i
+ 1][0] != '-') {
674 opt_away_arg
= g_strdup(argv
[i
+ 1]);
675 strcpy(argv
[i
+ 1], " ");
677 strcpy(argv
[i
], " ");
682 printf ("--login given with arg %s\n",
683 opt_login_arg ? opt_login_arg : "NULL");
689 gtk_init(&argc
, &argv
);
691 /* scan command-line options */
693 while ((opt
= getopt_long(argc
, argv
,
699 long_options
, NULL
)) != -1) {
701 case 'u': /* set user */
703 opt_user_arg
= g_strdup(optarg
);
705 case 'a': /* account editor */
708 case 'd': /* debug */
712 opt_rcfile_arg
= g_strdup(optarg
);
714 case 'v': /* version */
720 case 'n': /* don't autologin */
724 case 'g': /* debug GTK and GLIB */
730 show_usage(1, argv
[0]);
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
),
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
),
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
),
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
),
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
),
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
),
765 /* g_print also makes a call to AllocConsole(), therefore a handler needs to be
768 g_set_print_handler( gaim_dummy_print
);
772 /* show help message */
774 show_usage(0, argv
[0]);
777 /* show version message */
779 printf("Gaim %s\n",VERSION
);
783 #if GAIM_PLUGINS || USE_PERL
784 gaim_probe_plugins();
788 /* Various win32 initializations */
798 /* set the default username */
799 if (opt_user_arg
!= NULL
) {
800 set_first_user(opt_user_arg
);
801 g_free(opt_user_arg
);
805 if (misc_options
& OPT_MISC_DEBUG
)
810 /* deal with --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)
823 account_editor(NULL
, NULL
);
824 } else if ((dologin_ret
== -1) && !connections
)
829 /* don't need ui_quit here because ui doesn't create anything */