Fri, 31 Jan 2003 06:47:07 +0000
[gaim-migrate @ 4764]
aim.c -> main.c
| 4489 | 1 | /* |
| 2 | * gaim | |
| 3 | * | |
| 4 | * Copyright (C) 1998-1999, Mark Spencer <markster@marko.net> | |
| 5 | * | |
| 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. | |
| 10 | * | |
| 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. | |
| 15 | * | |
| 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 | |
| 19 | * | |
| 20 | */ | |
| 21 | ||
| 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> | |
| 57 | ||
| 58 | #ifdef _WIN32 | |
| 59 | #include "win32dep.h" | |
| 60 | #endif | |
| 61 | ||
| 62 | static GtkWidget *name; | |
| 63 | static GtkWidget *pass; | |
| 64 | ||
| 65 | GList *log_conversations = NULL; | |
| 66 | GList *buddy_pounces = NULL; | |
| 67 | GSList *away_messages = NULL; | |
| 68 | GSList *message_queue = NULL; | |
| 69 | GSList *unread_message_queue = NULL; | |
| 70 | GSList *away_time_queue = NULL; | |
| 71 | ||
| 72 | GtkWidget *mainwindow = NULL; | |
| 73 | ||
| 74 | int opt_away = 0; | |
| 75 | char *opt_away_arg = NULL; | |
| 76 | char *opt_rcfile_arg = NULL; | |
| 77 | int opt_debug = 0; | |
| 78 | #ifdef _WIN32 | |
| 79 | int opt_gdebug = 0; | |
| 80 | #endif | |
| 81 | ||
| 82 | #if HAVE_SIGNAL_H | |
| 83 | /* | |
| 84 | * Lists of signals we wish to catch and those we wish to ignore. | |
| 85 | * Each list terminated with -1 | |
| 86 | */ | |
| 87 | static int catch_sig_list[] = { | |
| 88 | SIGSEGV, | |
| 89 | SIGHUP, | |
| 90 | SIGINT, | |
| 91 | SIGTERM, | |
| 92 | SIGQUIT, | |
| 93 | SIGCHLD, | |
| 94 | -1 | |
| 95 | }; | |
| 96 | ||
| 97 | static int ignore_sig_list[] = { | |
| 98 | SIGPIPE, | |
| 99 | -1 | |
| 100 | }; | |
| 101 | #endif | |
| 102 | ||
| 103 | void do_quit() | |
| 104 | { | |
| 105 | /* captain's log, stardate... */ | |
| 106 | system_log(log_quit, NULL, NULL, OPT_LOG_BUDDY_SIGNON | OPT_LOG_MY_SIGNON); | |
| 107 | ||
| 108 | /* the self destruct sequence has been initiated */ | |
| 109 | plugin_event(event_quit); | |
| 110 | ||
| 111 | /* transmission ends */ | |
| 112 | signoff_all(); | |
| 113 | ||
| 114 | /* record what we have before we blow it away... */ | |
| 115 | save_prefs(); | |
| 116 | ||
| 117 | #ifdef GAIM_PLUGINS | |
| 118 | /* jettison cargo */ | |
| 119 | remove_all_plugins(); | |
| 120 | #endif | |
| 121 | ||
| 122 | #ifdef USE_PERL | |
| 123 | /* yup, perl too */ | |
| 124 | perl_end(); | |
| 125 | #endif | |
| 126 | ||
| 127 | #ifdef USE_SM | |
| 128 | /* unplug */ | |
| 129 | session_end(); | |
| 130 | #endif | |
| 131 | ||
| 132 | /* and end it all... */ | |
| 133 | gtk_main_quit(); | |
| 134 | } | |
| 135 | ||
| 136 | static int snd_tmout; | |
| 137 | int logins_not_muted = 1; | |
| 138 | static gboolean sound_timeout(gpointer data) | |
| 139 | { | |
| 140 | logins_not_muted = 1; | |
| 141 | g_source_remove(snd_tmout); | |
| 142 | return FALSE; | |
| 143 | } | |
| 144 | ||
| 145 | /* we need to do this for Oscar because serv_login only starts the login | |
| 146 | * process, it doesn't end there. gaim_setup will be called later from | |
| 147 | * oscar.c, after the buddy list is made and serv_finish_login is called */ | |
| 148 | void gaim_setup(struct gaim_connection *gc) | |
| 149 | { | |
| 150 | if ((sound_options & OPT_SOUND_LOGIN) && (sound_options & OPT_SOUND_SILENT_SIGNON)) { | |
| 151 | logins_not_muted = 0; | |
| 152 | snd_tmout = g_timeout_add(10000, sound_timeout, NULL); | |
| 153 | } | |
| 154 | } | |
| 155 | ||
| 156 | static gboolean domiddleclick(GtkWidget *w, GdkEventButton *event, gpointer null) | |
| 157 | { | |
| 158 | if (event->button != 2) | |
| 159 | return FALSE; | |
| 160 | ||
| 161 | auto_login(); | |
| 162 | return TRUE; | |
| 163 | } | |
| 164 | ||
| 165 | static void dologin(GtkWidget *widget, GtkWidget *w) | |
| 166 | { | |
| 167 | struct aim_user *u; | |
| 168 | const char *username = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(name)->entry)); | |
| 169 | const char *password = gtk_entry_get_text(GTK_ENTRY(pass)); | |
| 170 | ||
| 171 | if (!strlen(username)) { | |
| 172 | do_error_dialog(_("Please enter your login."), NULL, GAIM_ERROR); | |
| 173 | return; | |
| 174 | } | |
| 175 | ||
| 176 | /* if there is more than one user of the same name, then fuck | |
| 177 | * them, they just have to use the account editor to sign in | |
| 178 | * the second one */ | |
| 179 | ||
| 180 | u = find_user(username, -1); | |
| 181 | if (!u) | |
| 182 | u = new_user(username, DEFAULT_PROTO, OPT_USR_REM_PASS); | |
| 183 | g_snprintf(u->password, sizeof u->password, "%s", password); | |
| 184 | save_prefs(); | |
| 185 | serv_login(u); | |
| 186 | } | |
| 187 | ||
| 188 | /* <name> is a comma-separated list of names, or NULL | |
| 189 | if NULL and there is at least one user defined in .gaimrc, try to login. | |
| 190 | if not NULL, parse <name> into separate strings, look up each one in | |
| 191 | .gaimrc and, if it's there, try to login. | |
| 192 | returns: 0 if successful | |
| 193 | -1 if no user was found that had a saved password | |
| 194 | */ | |
| 195 | static int dologin_named(char *name) | |
| 196 | { | |
| 197 | struct aim_user *u; | |
| 198 | char **names, **n; | |
| 199 | int retval = -1; | |
| 200 | ||
| 201 | if (name !=NULL) { /* list of names given */ | |
| 202 | names = g_strsplit(name, ",", 32); | |
| 203 | for (n = names; *n != NULL; n++) { | |
| 204 | u = find_user(*n, -1); | |
| 205 | if (u) { /* found a user */ | |
| 206 | if (u->options & OPT_USR_REM_PASS) { | |
| 207 | retval = 0; | |
| 208 | serv_login(u); | |
| 209 | } | |
| 210 | } | |
| 211 | } | |
| 212 | g_strfreev(names); | |
| 213 | } else { /* no name given, use default */ | |
| 214 | u = (struct aim_user *)aim_users->data; | |
| 215 | if (u->options & OPT_USR_REM_PASS) { | |
| 216 | retval = 0; | |
| 217 | serv_login(u); | |
| 218 | } | |
| 219 | } | |
| 220 | ||
| 221 | return retval; | |
| 222 | } | |
| 223 | ||
| 224 | ||
| 225 | static void doenter(GtkWidget *widget, GtkWidget *w) | |
| 226 | { | |
| 227 | if (widget == name) { | |
| 228 | gtk_entry_set_text(GTK_ENTRY(pass), ""); | |
| 229 | gtk_entry_select_region(GTK_ENTRY(GTK_COMBO(name)->entry), 0, 0); | |
| 230 | gtk_widget_grab_focus(pass); | |
| 231 | } else if (widget == pass) { | |
| 232 | dologin(widget, w); | |
| 233 | } | |
| 234 | } | |
| 235 | ||
| 236 | ||
| 237 | static void combo_changed(GtkWidget *w, GtkWidget *combo) | |
| 238 | { | |
| 239 | const char *txt = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(combo)->entry)); | |
| 240 | struct aim_user *u; | |
| 241 | ||
| 242 | u = find_user(txt, -1); | |
| 243 | ||
| 244 | if (u && u->options & OPT_USR_REM_PASS) { | |
| 245 | gtk_entry_set_text(GTK_ENTRY(pass), u->password); | |
| 246 | } else { | |
| 247 | gtk_entry_set_text(GTK_ENTRY(pass), ""); | |
| 248 | } | |
| 249 | } | |
| 250 | ||
| 251 | ||
| 252 | static GList *combo_user_names() | |
| 253 | { | |
| 254 | GSList *usr = aim_users; | |
| 255 | GList *tmp = NULL; | |
| 256 | struct aim_user *u; | |
| 257 | ||
| 258 | if (!usr) | |
| 259 | return g_list_append(NULL, _("<New User>")); | |
| 260 | ||
| 261 | while (usr) { | |
| 262 | u = (struct aim_user *)usr->data; | |
| 263 | tmp = g_list_append(tmp, u->username); | |
| 264 | usr = usr->next; | |
| 265 | } | |
| 266 | ||
| 267 | return tmp; | |
| 268 | } | |
| 269 | ||
| 270 | static void login_window_closed(GtkWidget *w, GdkEvent *ev, gpointer d) | |
| 271 | { | |
| 272 | if(docklet_count) { | |
| 273 | #if _WIN32 | |
| 274 | wgaim_systray_minimize(mainwindow); | |
| 275 | #endif | |
| 276 | gtk_widget_hide(mainwindow); | |
| 277 | } else | |
| 278 | do_quit(); | |
| 279 | } | |
| 280 | ||
| 281 | void show_login() | |
| 282 | { | |
| 283 | GdkPixbuf *icon; | |
| 284 | GtkWidget *image; | |
| 285 | GtkWidget *vbox; | |
| 286 | GtkWidget *button; | |
| 287 | GtkWidget *hbox; | |
| 288 | GtkWidget *label; | |
| 289 | GtkWidget *vbox2; | |
| 290 | GList *tmp; | |
| 291 | ||
| 292 | /* Do we already have a main window opened? If so, bring it back, baby... ribs... yeah */ | |
| 293 | if (mainwindow) { | |
| 294 | gtk_window_present(GTK_WINDOW(mainwindow)); | |
| 295 | return; | |
| 296 | } | |
| 297 | ||
| 298 | mainwindow = gtk_window_new(GTK_WINDOW_TOPLEVEL); | |
| 299 | ||
| 300 | gtk_window_set_role(GTK_WINDOW(mainwindow), "login"); | |
| 301 | gtk_window_set_policy(GTK_WINDOW(mainwindow), FALSE, FALSE, TRUE); | |
| 302 | gtk_window_set_title(GTK_WINDOW(mainwindow), _("Gaim - Login")); | |
| 303 | gtk_widget_realize(mainwindow); | |
| 304 | gdk_window_set_group(mainwindow->window, mainwindow->window); | |
| 305 | gtk_container_set_border_width(GTK_CONTAINER(mainwindow), 5); | |
| 306 | g_signal_connect(G_OBJECT(mainwindow), "delete_event", | |
| 307 | G_CALLBACK(login_window_closed), mainwindow); | |
| 308 | ||
| 309 | ||
| 310 | icon = gaim_pixbuf(NULL, "gaim.png"); | |
| 311 | if (icon) { | |
| 312 | gtk_window_set_icon(GTK_WINDOW(mainwindow), icon); | |
| 313 | gdk_pixbuf_unref(icon); | |
| 314 | } | |
| 315 | ||
| 316 | vbox = gtk_vbox_new(FALSE, 0); | |
| 317 | gtk_container_add(GTK_CONTAINER(mainwindow), vbox); | |
| 318 | ||
| 319 | image = gaim_pixmap(NULL, "logo.png"); | |
| 320 | gtk_box_pack_start(GTK_BOX(vbox), image, FALSE, FALSE, 0); | |
| 321 | ||
| 322 | vbox2 = gtk_vbox_new(FALSE, 0); | |
| 323 | gtk_container_set_border_width(GTK_CONTAINER(vbox2), 5); | |
| 324 | ||
| 325 | label = gtk_label_new(_("Screen Name:")); | |
| 326 | gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5); | |
| 327 | gtk_box_pack_start(GTK_BOX(vbox2), label, FALSE, FALSE, 0); | |
| 328 | ||
| 329 | name = gtk_combo_new(); | |
| 330 | tmp = combo_user_names(); | |
| 331 | gtk_combo_set_popdown_strings(GTK_COMBO(name), tmp); | |
| 332 | g_list_free(tmp); | |
| 333 | g_signal_connect(G_OBJECT(GTK_COMBO(name)->entry), "activate", | |
| 334 | G_CALLBACK(doenter), mainwindow); | |
| 335 | g_signal_connect(G_OBJECT(GTK_COMBO(name)->entry), "changed", | |
| 336 | G_CALLBACK(combo_changed), name); | |
| 337 | gtk_box_pack_start(GTK_BOX(vbox2), name, FALSE, TRUE, 0); | |
| 338 | gtk_box_pack_start(GTK_BOX(vbox), vbox2, FALSE, TRUE, 0); | |
| 339 | ||
| 340 | vbox2 = gtk_vbox_new(FALSE, 0); | |
| 341 | gtk_container_set_border_width(GTK_CONTAINER(vbox2), 5); | |
| 342 | ||
| 343 | label = gtk_label_new(_("Password:")); | |
| 344 | gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5); | |
| 345 | gtk_box_pack_start(GTK_BOX(vbox2), label, FALSE, FALSE, 0); | |
| 346 | ||
| 347 | pass = gtk_entry_new(); | |
| 348 | gtk_entry_set_visibility(GTK_ENTRY(pass), FALSE); | |
| 349 | g_signal_connect(G_OBJECT(pass), "activate", | |
| 350 | G_CALLBACK(doenter), mainwindow); | |
| 351 | gtk_box_pack_start(GTK_BOX(vbox2), pass, FALSE, TRUE, 0); | |
| 352 | gtk_box_pack_start(GTK_BOX(vbox), vbox2, FALSE, TRUE, 0); | |
| 353 | ||
| 354 | /* Now for the button box */ | |
| 355 | hbox = gtk_hbox_new(TRUE, 0); | |
| 356 | gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 5); | |
| 357 | ||
| 358 | /* And now for the buttons */ | |
| 359 | button = gaim_pixbuf_button("Accounts", "accounts.png", GAIM_BUTTON_VERTICAL); | |
| 360 | gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE); | |
| 361 | g_signal_connect(G_OBJECT(button), "clicked", | |
| 362 | G_CALLBACK(account_editor), mainwindow); | |
| 363 | gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0); | |
| 364 | ||
| 365 | #ifdef NO_MULTI | |
| 366 | gtk_widget_set_sensitive(GTK_WIDGET(button), FALSE); | |
| 367 | #endif | |
| 368 | ||
| 369 | button = gaim_pixbuf_button("Settings", "preferences.png", GAIM_BUTTON_VERTICAL); | |
| 370 | gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE); | |
| 371 | g_signal_connect(G_OBJECT(button), "clicked", | |
| 372 | G_CALLBACK(show_prefs), mainwindow); | |
| 373 | gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0); | |
| 374 | ||
| 375 | button = gaim_pixbuf_button("Sign On", "signon.png", GAIM_BUTTON_VERTICAL); | |
| 376 | gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE); | |
| 377 | g_signal_connect(G_OBJECT(button), "clicked", | |
| 378 | G_CALLBACK(dologin), mainwindow); | |
| 379 | g_signal_connect(G_OBJECT(button), "button-press-event", G_CALLBACK(domiddleclick), NULL); | |
| 380 | gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0); | |
| 381 | ||
| 382 | #ifdef _WIN32 | |
| 383 | /* Register newly created window with systray module */ | |
| 384 | wgaim_created_loginwin(GTK_WIDGET(mainwindow)); | |
| 385 | #endif | |
| 386 | ||
| 387 | /* Now grab the focus that we need */ | |
| 388 | if (aim_users) { | |
| 389 | struct aim_user *c = (struct aim_user *)aim_users->data; | |
| 390 | if (c->options & OPT_USR_REM_PASS) { | |
| 391 | combo_changed(NULL, name); | |
| 392 | gtk_widget_grab_focus(button); | |
| 393 | } else { | |
| 394 | gtk_widget_grab_focus(pass); | |
| 395 | } | |
| 396 | } else { | |
| 397 | gtk_widget_grab_focus(name); | |
| 398 | } | |
| 399 | ||
| 400 | /* And raise the curtain! */ | |
| 401 | gtk_widget_show_all(mainwindow); | |
| 402 | ||
| 403 | } | |
| 404 | ||
| 405 | #if HAVE_SIGNAL_H | |
| 406 | void sighandler(int sig) | |
| 407 | { | |
| 408 | switch (sig) { | |
| 409 | case SIGHUP: | |
| 410 | debug_printf("caught signal %d\n", sig); | |
| 411 | signoff_all(NULL, NULL); | |
| 412 | break; | |
| 413 | case SIGSEGV: | |
| 414 | core_quit(); | |
| 415 | #ifndef DEBUG | |
| 416 | fprintf(stderr, "Gaim has segfaulted and attempted to dump a core file.\n" | |
| 417 | "This is a bug in the software and has happened through\n" | |
| 418 | "no fault of your own.\n\n" | |
| 419 | "It is possible that this bug is already fixed in CVS.\n" | |
| 420 | "You can get a tarball of CVS from the Gaim website, at\n" | |
| 421 | WEBSITE "gaim-CVS.tar.gz.\n\n" | |
| 422 | "If you are already using CVS, or can reproduce the crash\n" | |
| 423 | "using the CVS version, please notify the gaim maintainers\n" | |
| 424 | "by reporting a bug at\n" | |
| 425 | WEBSITE "bug.php\n\n" | |
| 426 | "Please make sure to specify what you were doing at the time,\n" | |
| 427 | "and post the backtrace from the core file. If you do not know\n" | |
| 428 | "how to get the backtrace, please get instructions at\n" | |
| 429 | WEBSITE "gdb.php. If you need further\n" | |
| 430 | "assistance, please IM either RobFlynn or SeanEgn and\n" | |
| 431 | "they can help you.\n"); | |
| 432 | #else | |
| 433 | fprintf(stderr, "Oh no! Segmentation fault!\n"); | |
| 434 | g_on_error_query (g_get_prgname()); | |
| 435 | exit(1); | |
| 436 | #endif | |
| 437 | abort(); | |
| 438 | break; | |
| 439 | case SIGCHLD: | |
| 440 | clean_pid(); | |
| 441 | #if HAVE_SIGNAL_H | |
| 442 | signal(SIGCHLD, sighandler); /* restore signal catching on this one! */ | |
| 443 | #endif | |
| 444 | break; | |
| 445 | default: | |
| 446 | debug_printf("caught signal %d\n", sig); | |
| 447 | signoff_all(NULL, NULL); | |
| 448 | #ifdef GAIM_PLUGINS | |
| 449 | remove_all_plugins(); | |
| 450 | #endif | |
| 451 | if (gtk_main_level()) | |
| 452 | gtk_main_quit(); | |
| 453 | core_quit(); | |
| 454 | exit(0); | |
| 455 | } | |
| 456 | } | |
| 457 | #endif | |
| 458 | ||
| 459 | #ifndef _WIN32 | |
| 460 | static gboolean socket_readable(GIOChannel *source, GIOCondition cond, gpointer ud) | |
| 461 | { | |
| 462 | guchar type; | |
| 463 | guchar subtype; | |
| 464 | guint32 len; | |
| 465 | guchar *data; | |
| 466 | guint32 x; | |
| 467 | ||
| 468 | debug_printf("Core says: "); | |
| 469 | g_io_channel_read(source, &type, sizeof(type), &x); | |
| 470 | if (x == 0) { | |
| 471 | debug_printf("CORE IS GONE!\n"); | |
| 472 | g_io_channel_close(source); | |
| 473 | return FALSE; | |
| 474 | } | |
| 475 | debug_printf("%d ", type); | |
| 476 | g_io_channel_read(source, &subtype, sizeof(subtype), &x); | |
| 477 | if (x == 0) { | |
| 478 | debug_printf("CORE IS GONE!\n"); | |
| 479 | g_io_channel_close(source); | |
| 480 | return FALSE; | |
| 481 | } | |
| 482 | debug_printf("%d ", subtype); | |
| 483 | g_io_channel_read(source, (guchar *)&len, sizeof(len), &x); | |
| 484 | if (x == 0) { | |
| 485 | debug_printf("CORE IS GONE!\n"); | |
| 486 | g_io_channel_close(source); | |
| 487 | return FALSE; | |
| 488 | } | |
| 489 | debug_printf("(%d bytes)\n", len); | |
| 490 | ||
| 491 | data = g_malloc(len); | |
| 492 | g_io_channel_read(source, data, len, &x); | |
| 493 | if (x != len) { | |
| 494 | debug_printf("CORE IS GONE! (read %d/%d bytes)\n", x, len); | |
| 495 | g_free(data); | |
| 496 | g_io_channel_close(source); | |
| 497 | return FALSE; | |
| 498 | } | |
| 499 | ||
| 500 | g_free(data); | |
| 501 | return TRUE; | |
| 502 | } | |
| 503 | #endif /* _WIN32 */ | |
| 504 | ||
| 505 | static int ui_main() | |
| 506 | { | |
| 507 | #ifndef _WIN32 | |
| 508 | GIOChannel *channel; | |
| 509 | int UI_fd; | |
| 510 | char name[256]; | |
| 511 | GList *icons = NULL; | |
| 512 | GdkPixbuf *icon = NULL; | |
| 513 | char *icon_path; | |
| 514 | #endif | |
| 515 | if (current_smiley_theme == NULL) { | |
| 516 | smiley_theme_probe(); | |
| 517 | if (smiley_themes) { | |
| 518 | struct smiley_theme *smile = smiley_themes->data; | |
| 519 | load_smiley_theme(smile->path, TRUE); | |
| 520 | } | |
| 521 | } | |
| 522 | ||
| 523 | gaim_set_win_ui_ops(gaim_get_gtk_window_ui_ops()); | |
| 524 | ||
| 525 | setup_stock(); | |
| 526 | ||
| 527 | #ifndef _WIN32 | |
| 528 | /* use the nice PNG icon for all the windows */ | |
| 529 | icon_path = g_build_filename(DATADIR, "pixmaps", "gaim.png", NULL); | |
| 530 | icon = gdk_pixbuf_new_from_file(icon_path, NULL); | |
| 531 | g_free(icon_path); | |
| 532 | if (icon) { | |
| 533 | icons = g_list_append(icons,icon); | |
| 534 | gtk_window_set_default_icon_list(icons); | |
| 535 | g_object_unref(G_OBJECT(icon)); | |
| 536 | } else { | |
| 537 | debug_printf("Failed to load icon from %s" G_DIR_SEPARATOR_S "pixmaps" G_DIR_SEPARATOR_S "gaim.png\n",DATADIR); | |
| 538 | } | |
| 539 | ||
| 540 | g_snprintf(name, sizeof(name), "%s" G_DIR_SEPARATOR_S "gaim_%s.%d", g_get_tmp_dir(), g_get_user_name(), gaim_session); | |
| 541 | UI_fd = gaim_connect_to_session(0); | |
| 542 | if (UI_fd < 0) | |
| 543 | return 1; | |
| 544 | ||
| 545 | channel = g_io_channel_unix_new(UI_fd); | |
| 546 | g_io_add_watch(channel, G_IO_IN | G_IO_HUP | G_IO_ERR, socket_readable, NULL); | |
| 547 | #endif | |
| 548 | return 0; | |
| 549 | } | |
| 550 | ||
| 551 | static void set_first_user(char *name) | |
| 552 | { | |
| 553 | struct aim_user *u; | |
| 554 | ||
| 555 | u = find_user(name, -1); | |
| 556 | ||
| 557 | if (!u) { /* new user */ | |
| 558 | u = g_new0(struct aim_user, 1); | |
| 559 | g_snprintf(u->username, sizeof(u->username), "%s", name); | |
| 560 | u->protocol = DEFAULT_PROTO; | |
| 561 | aim_users = g_slist_prepend(aim_users, u); | |
| 562 | } else { /* user already exists */ | |
| 563 | aim_users = g_slist_remove(aim_users, u); | |
| 564 | aim_users = g_slist_prepend(aim_users, u); | |
| 565 | } | |
| 566 | save_prefs(); | |
| 567 | } | |
| 568 | ||
| 569 | #ifdef _WIN32 | |
| 570 | /* WIN32 print and log handlers */ | |
| 571 | ||
| 572 | static void gaim_dummy_print( const gchar* string ) { | |
| 573 | return; | |
| 574 | } | |
| 575 | ||
| 576 | static void gaim_dummy_log_handler (const gchar *domain, | |
| 577 | GLogLevelFlags flags, | |
| 578 | const gchar *msg, | |
| 579 | gpointer user_data) { | |
| 580 | return; | |
| 581 | } | |
| 582 | ||
| 583 | static void gaim_log_handler (const gchar *domain, | |
| 584 | GLogLevelFlags flags, | |
| 585 | const gchar *msg, | |
| 586 | gpointer user_data) { | |
| 587 | debug_printf("%s - %s\n", domain, msg); | |
| 588 | g_log_default_handler(domain, flags, msg, user_data); | |
| 589 | } | |
| 590 | #endif /* _WIN32 */ | |
| 591 | ||
| 592 | /* FUCKING GET ME A TOWEL! */ | |
| 593 | #ifdef _WIN32 | |
| 594 | int gaim_main(int argc, char *argv[]) | |
| 595 | #else | |
| 596 | int main(int argc, char *argv[]) | |
| 597 | #endif | |
| 598 | { | |
| 599 | int opt_acct = 0, opt_help = 0, opt_version = 0, opt_login = 0, opt_nologin = 0, dologin_ret = -1; | |
| 600 | char *opt_user_arg = NULL, *opt_login_arg = NULL; | |
| 601 | char *opt_session_arg = NULL; | |
| 602 | #if HAVE_SIGNAL_H | |
| 603 | int sig_indx; /* for setting up signal catching */ | |
| 604 | sigset_t sigset; | |
| 605 | void (*prev_sig_disp)(); | |
| 606 | #endif | |
| 607 | int opt, opt_user = 0; | |
| 608 | int i; | |
| 609 | ||
| 610 | struct option long_options[] = { | |
| 611 | {"acct", no_argument, NULL, 'a'}, | |
| 612 | /*{"away", optional_argument, NULL, 'w'}, */ | |
| 613 | {"help", no_argument, NULL, 'h'}, | |
| 614 | /*{"login", optional_argument, NULL, 'l'}, */ | |
| 615 | {"loginwin", no_argument, NULL, 'n'}, | |
| 616 | {"user", required_argument, NULL, 'u'}, | |
| 617 | {"file", required_argument, NULL, 'f'}, | |
| 618 | {"debug", no_argument, NULL, 'd'}, | |
| 619 | {"version", no_argument, NULL, 'v'}, | |
| 620 | {"session", required_argument, NULL, 's'}, | |
| 621 | {0, 0, 0, 0} | |
| 622 | }; | |
| 623 | ||
| 624 | #ifdef DEBUG | |
| 625 | opt_debug = 1; | |
| 626 | #endif | |
| 627 | ||
| 628 | #ifdef ENABLE_NLS | |
| 629 | bindtextdomain(PACKAGE, LOCALEDIR); | |
| 630 | bind_textdomain_codeset(PACKAGE, "UTF-8"); | |
| 631 | textdomain(PACKAGE); | |
| 632 | #endif | |
| 633 | ||
| 634 | #if HAVE_SIGNAL_H | |
| 635 | /* Let's not violate any PLA's!!!! */ | |
| 636 | /* jseymour: whatever the fsck that means */ | |
| 637 | /* Robot101: for some reason things like gdm like to block * | |
| 638 | * useful signals like SIGCHLD, so we unblock all the ones we * | |
| 639 | * declare a handler for. thanks JSeymour and Vann. */ | |
| 640 | if (sigemptyset(&sigset)) { | |
| 641 | char errmsg[BUFSIZ]; | |
| 642 | sprintf(errmsg, "Warning: couldn't initialise empty signal set"); | |
| 643 | perror(errmsg); | |
| 644 | } | |
| 645 | for(sig_indx = 0; catch_sig_list[sig_indx] != -1; ++sig_indx) { | |
| 646 | if((prev_sig_disp = signal(catch_sig_list[sig_indx], sighandler)) == SIG_ERR) { | |
| 647 | char errmsg[BUFSIZ]; | |
| 648 | sprintf(errmsg, "Warning: couldn't set signal %d for catching", | |
| 649 | catch_sig_list[sig_indx]); | |
| 650 | perror(errmsg); | |
| 651 | } | |
| 652 | if(sigaddset(&sigset, catch_sig_list[sig_indx])) { | |
| 653 | char errmsg[BUFSIZ]; | |
| 654 | sprintf(errmsg, "Warning: couldn't include signal %d for unblocking", | |
| 655 | catch_sig_list[sig_indx]); | |
| 656 | perror(errmsg); | |
| 657 | } | |
| 658 | } | |
| 659 | for(sig_indx = 0; ignore_sig_list[sig_indx] != -1; ++sig_indx) { | |
| 660 | if((prev_sig_disp = signal(ignore_sig_list[sig_indx], SIG_IGN)) == SIG_ERR) { | |
| 661 | char errmsg[BUFSIZ]; | |
| 662 | sprintf(errmsg, "Warning: couldn't set signal %d to ignore", | |
| 663 | ignore_sig_list[sig_indx]); | |
| 664 | perror(errmsg); | |
| 665 | } | |
| 666 | } | |
| 667 | ||
| 668 | if (sigprocmask(SIG_UNBLOCK, &sigset, NULL)) { | |
| 669 | char errmsg[BUFSIZ]; | |
| 670 | sprintf(errmsg, "Warning: couldn't unblock signals"); | |
| 671 | perror(errmsg); | |
| 672 | } | |
| 673 | #endif | |
| 674 | ||
| 675 | for (i = 0; i < argc; i++) { | |
| 676 | /* --login option */ | |
| 677 | if (strstr(argv[i], "--l") == argv[i]) { | |
| 678 | char *equals; | |
| 679 | opt_login = 1; | |
| 680 | if ((equals = strchr(argv[i], '=')) != NULL) { | |
| 681 | /* --login=NAME */ | |
| 682 | opt_login_arg = g_strdup(equals + 1); | |
| 683 | if (strlen(opt_login_arg) == 0) { | |
| 684 | g_free(opt_login_arg); | |
| 685 | opt_login_arg = NULL; | |
| 686 | } | |
| 687 | } else if (i + 1 < argc && argv[i + 1][0] != '-') { | |
| 688 | /* --login NAME */ | |
| 689 | opt_login_arg = g_strdup(argv[i + 1]); | |
| 690 | strcpy(argv[i + 1], " "); | |
| 691 | } | |
| 692 | strcpy(argv[i], " "); | |
| 693 | } | |
| 694 | /* -l option */ | |
| 695 | else if (strstr(argv[i], "-l") == argv[i]) { | |
| 696 | opt_login = 1; | |
| 697 | if (strlen(argv[i]) > 2) { | |
| 698 | /* -lNAME */ | |
| 699 | opt_login_arg = g_strdup(argv[i] + 2); | |
| 700 | } else if (i + 1 < argc && argv[i + 1][0] != '-') { | |
| 701 | /* -l NAME */ | |
| 702 | opt_login_arg = g_strdup(argv[i + 1]); | |
| 703 | strcpy(argv[i + 1], " "); | |
| 704 | } | |
| 705 | strcpy(argv[i], " "); | |
| 706 | } | |
| 707 | /* --away option */ | |
| 708 | else if (strstr(argv[i], "--aw") == argv[i]) { | |
| 709 | char *equals; | |
| 710 | opt_away = 1; | |
| 711 | if ((equals = strchr(argv[i], '=')) != NULL) { | |
| 712 | /* --away=MESG */ | |
| 713 | opt_away_arg = g_strdup(equals + 1); | |
| 714 | if (strlen(opt_away_arg) == 0) { | |
| 715 | g_free(opt_away_arg); | |
| 716 | opt_away_arg = NULL; | |
| 717 | } | |
| 718 | } else if (i + 1 < argc && argv[i + 1][0] != '-') { | |
| 719 | /* --away MESG */ | |
| 720 | opt_away_arg = g_strdup(argv[i + 1]); | |
| 721 | strcpy(argv[i + 1], " "); | |
| 722 | } | |
| 723 | strcpy(argv[i], " "); | |
| 724 | } | |
| 725 | /* -w option */ | |
| 726 | else if (strstr(argv[i], "-w") == argv[i]) { | |
| 727 | opt_away = 1; | |
| 728 | if (strlen(argv[i]) > 2) { | |
| 729 | /* -wMESG */ | |
| 730 | opt_away_arg = g_strdup(argv[i] + 2); | |
| 731 | } else if (i + 1 < argc && argv[i + 1][0] != '-') { | |
| 732 | /* -w MESG */ | |
| 733 | opt_away_arg = g_strdup(argv[i + 1]); | |
| 734 | strcpy(argv[i + 1], " "); | |
| 735 | } | |
| 736 | strcpy(argv[i], " "); | |
| 737 | } | |
| 738 | } | |
| 739 | /* | |
| 740 | if (opt_login) { | |
| 741 | printf ("--login given with arg %s\n", | |
| 742 | opt_login_arg ? opt_login_arg : "NULL"); | |
| 743 | exit(0); | |
| 744 | } | |
| 745 | */ | |
| 746 | ||
| 747 | gtk_set_locale(); | |
| 748 | gtk_init(&argc, &argv); | |
| 749 | ||
| 750 | /* scan command-line options */ | |
| 751 | opterr = 1; | |
| 752 | while ((opt = getopt_long(argc, argv, | |
| 753 | #ifndef _WIN32 | |
| 754 | "adhu:f:vns:", | |
| 755 | #else | |
| 756 | "adghu:f:vn", | |
| 757 | #endif | |
| 758 | long_options, NULL)) != -1) { | |
| 759 | switch (opt) { | |
| 760 | case 'u': /* set user */ | |
| 761 | opt_user = 1; | |
| 762 | opt_user_arg = g_strdup(optarg); | |
| 763 | break; | |
| 764 | case 'a': /* account editor */ | |
| 765 | opt_acct = 1; | |
| 766 | break; | |
| 767 | case 'd': /* debug */ | |
| 768 | opt_debug = 1; | |
| 769 | break; | |
| 770 | case 'f': | |
| 771 | opt_rcfile_arg = g_strdup(optarg); | |
| 772 | break; | |
| 773 | case 's': /* use existing session ID */ | |
| 774 | opt_session_arg = g_strdup(optarg); | |
| 775 | break; | |
| 776 | case 'v': /* version */ | |
| 777 | opt_version = 1; | |
| 778 | break; | |
| 779 | case 'h': /* help */ | |
| 780 | opt_help = 1; | |
| 781 | break; | |
| 782 | case 'n': /* don't autologin */ | |
| 783 | opt_nologin = 1; | |
| 784 | break; | |
| 785 | #ifdef _WIN32 | |
| 786 | case 'g': /* debug GTK and GLIB */ | |
| 787 | opt_gdebug = 1; | |
| 788 | break; | |
| 789 | #endif | |
| 790 | case '?': | |
| 791 | default: | |
| 792 | show_usage(1, argv[0]); | |
| 793 | return 0; | |
| 794 | break; | |
| 795 | } | |
| 796 | } | |
| 797 | ||
| 798 | #ifdef _WIN32 | |
| 799 | /* We don't want a console window.. */ | |
| 800 | /* | |
| 801 | * Any calls to the glib logging functions, result in a call to AllocConsole(). | |
| 802 | * ME and 98 will in such cases produce a console window (2000 not), despite | |
| 803 | * being built as a windows app rather than a console app. So we should either | |
| 804 | * ignore messages by setting dummy log handlers, or redirect messages. | |
| 805 | * This requires setting handlers for all domains (any lib which uses g_logging). | |
| 806 | */ | |
| 807 | ||
| 808 | g_log_set_handler (NULL, G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION, | |
| 809 | (opt_gdebug ? gaim_log_handler : gaim_dummy_log_handler), | |
| 810 | NULL); | |
| 811 | g_log_set_handler ("Gdk", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION, | |
| 812 | (opt_gdebug ? gaim_log_handler : gaim_dummy_log_handler), | |
| 813 | NULL); | |
| 814 | g_log_set_handler ("Gtk", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION, | |
| 815 | (opt_gdebug ? gaim_log_handler : gaim_dummy_log_handler), | |
| 816 | NULL); | |
| 817 | g_log_set_handler ("GLib", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION, | |
| 818 | (opt_gdebug ? gaim_log_handler : gaim_dummy_log_handler), | |
| 819 | NULL); | |
| 820 | g_log_set_handler ("GModule", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION, | |
| 821 | (opt_gdebug ? gaim_log_handler : gaim_dummy_log_handler), | |
| 822 | NULL); | |
| 823 | g_log_set_handler ("GLib-GObject", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION, | |
| 824 | (opt_gdebug ? gaim_log_handler : gaim_dummy_log_handler), | |
| 825 | NULL); | |
| 826 | g_log_set_handler ("GThread", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION, | |
| 827 | (opt_gdebug ? gaim_log_handler : gaim_dummy_log_handler), | |
| 828 | NULL); | |
| 829 | ||
| 830 | /* g_print also makes a call to AllocConsole(), therefore a handler needs to be | |
| 831 | set here aswell */ | |
| 832 | if(!opt_debug) | |
| 833 | g_set_print_handler( gaim_dummy_print ); | |
| 834 | ||
| 835 | #endif | |
| 836 | ||
| 837 | /* show help message */ | |
| 838 | if (opt_help) { | |
| 839 | show_usage(0, argv[0]); | |
| 840 | return 0; | |
| 841 | } | |
| 842 | /* show version message */ | |
| 843 | if (opt_version) { | |
| 844 | printf("Gaim %s\n",VERSION); | |
| 845 | return 0; | |
| 846 | } | |
| 847 | ||
| 848 | #if GAIM_PLUGINS || USE_PERL | |
| 849 | gaim_probe_plugins(); | |
| 850 | #endif | |
| 851 | ||
| 852 | #ifdef _WIN32 | |
| 853 | /* Various win32 initializations */ | |
| 854 | wgaim_init(); | |
| 855 | #endif | |
| 856 | ||
| 857 | load_prefs(); | |
| 858 | core_main(); | |
| 859 | ui_main(); | |
| 860 | ||
| 861 | #ifdef USE_SM | |
| 862 | session_init(argv[0], opt_session_arg); | |
| 863 | #endif | |
| 864 | if (opt_session_arg != NULL) { | |
| 865 | g_free(opt_session_arg); | |
| 866 | opt_session_arg = NULL; | |
| 867 | }; | |
| 868 | ||
| 869 | /* set the default username */ | |
| 870 | if (opt_user_arg != NULL) { | |
| 871 | set_first_user(opt_user_arg); | |
| 872 | g_free(opt_user_arg); | |
| 873 | opt_user_arg = NULL; | |
| 874 | } | |
| 875 | ||
| 876 | if (misc_options & OPT_MISC_DEBUG) | |
| 877 | show_debug(); | |
| 878 | ||
| 879 | static_proto_init(); | |
| 880 | ||
| 881 | /* deal with --login */ | |
| 882 | if (opt_login) { | |
| 883 | dologin_ret = dologin_named(opt_login_arg); | |
| 884 | if (opt_login_arg != NULL) { | |
| 885 | g_free(opt_login_arg); | |
| 886 | opt_login_arg = NULL; | |
| 887 | } | |
| 888 | } | |
| 889 | ||
| 890 | if (!opt_acct && !opt_nologin && gaim_session == 0) | |
| 891 | auto_login(); | |
| 892 | ||
| 893 | if (opt_acct) { | |
| 894 | account_editor(NULL, NULL); | |
| 895 | } else if ((dologin_ret == -1) && !connections) | |
| 896 | show_login(); | |
| 897 | ||
| 898 | gtk_main(); | |
| 899 | core_quit(); | |
| 900 | /* don't need ui_quit here because ui doesn't create anything */ | |
| 901 | #ifdef _WIN32 | |
| 902 | wgaim_cleanup(); | |
| 903 | #endif | |
| 904 | return 0; | |
| 905 | ||
| 906 | } |