Yattm - unified GTK instant-messaging client logo
   [Generated for version 0.2-17 - Mon Jan 6 19:01:23 GMT+1 2003]

Home - Main Page - Data Structures - File List - Data Fields - Globals

main.c

Go to the documentation of this file.
00001 /*
00002  * Yattm
00003  *
00004  * Copyright (C) 1999, Torrey Searle <tsearle@uci.edu>
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  */
00021 
00022 /*
00023  * main.c
00024  * yipee
00025  */
00026 #ifdef HAVE_CONFIG_H
00027 #  include <config.h>
00028 #endif
00029 
00030 #include "intl.h"
00031 
00032 #include <sys/stat.h>
00033 #ifdef __MINGW32__
00034 #include <winsock2.h>
00035 #else
00036 #include <sys/un.h>
00037 #endif
00038 #include <fcntl.h>
00039 #if defined( _WIN32 )
00040 #include <direct.h>
00041 #define mkdir( x, y )   _mkdir( x )
00042 #endif
00043 #include <string.h>
00044 #include <stdlib.h>
00045 #ifdef HAVE_GETOPT_H
00046 #include <getopt.h>
00047 #endif
00048 #include <errno.h>
00049 #include "status.h"
00050 #include "plugin.h"
00051 #include "gtk_globals.h"
00052 #include "dialog.h"
00053 #include "util.h"
00054 #include "sound.h"
00055 #include "smileys.h"
00056 #include "libproxy/libproxy.h"
00057 #include "console_session.h"
00058 
00059 #include <locale.h>
00060 
00061 #define PACKAGE "yattm"
00062 
00063 
00064 static void eb_cli_ver ()
00065 {
00066   printf("Yattm v%s\n", VERSION);
00067   printf("Copyright (C) 1999 Torrey Searle\n");
00068   printf("Yattm comes with NO WARRANTY, to the extent permitted"
00069      " by law.\n");
00070   printf("You may redistribute copies of Yattm under the terms of the\n");
00071   printf("GNU General Public License.  For more information about these\n");
00072   printf("matters, see the file named COPYING.\n");
00073   printf("\nYattm is brought to you by (in no order): Torrey Searle, \n");
00074   printf("Jared Peterson, Ben Rigas, Alex Wheeler, Robert Lazzurs, Meredydd Luff,\n");
00075   printf("Vance Lankhaar, Erik Inge Bolsø, Colin Leroy, Philip Tellis, Jeff Miller,\n");
00076   printf("and Troy Morrison.\n");
00077   printf("\nFor more information on Yattm, visit the website at\n");
00078   printf("         http://www.yattm.com/\n");
00079   return;
00080 }
00081 
00082 struct option_help
00083 {
00084   const char   s_opt;
00085   const char * l_opt;
00086   const char * help;
00087 };
00088 
00089 static struct option_help options [] =
00090 {
00091   {'h', "help",       "Display this help and exit"},
00092   {'v', "version",    "Output version information and exit"},
00093   {'g', "geometry",   "Set position of main window"},
00094   {'d', "config-dir", "Specify configuration directory"},
00095   {'D', "disable-server",    "Disable console message server"},
00096   {'c', "contact",    "Specify contact to send a message to via console server"},
00097   {'m', "message",    "Specify the message to send via console server"},
00098   {' ', NULL,        "-c and -m must be used toegether"},
00099   {'\0', NULL,         NULL}
00100 };
00101 
00102 
00103 
00104 static void eb_cli_help (const char * cmd)
00105 {
00106   unsigned int i = 0;
00107 
00108   printf("Usage: %s [OPTION]...\n", cmd);
00109   printf("An integrated instant messenger program.\n");
00110   printf("Supports AIM, ICQ, MSN, Yahoo!, Jabber, and IRC.\n\n");
00111 
00112 #if defined (HAVE_GETOPT) || defined (HAVE_GETOPT_LONG)
00113 
00114 for (i = 0; options [i].help != NULL; i++)
00115 {
00116 #ifdef HAVE_GETOPT_LONG
00117 
00118   printf ("  %c%c  %2s%15s  %s\n",
00119       (options [i].s_opt == ' ' ? ' ' : '-'),
00120       options [i].s_opt,
00121       (options [i].l_opt == NULL ? "  " : "--"),
00122       options [i].l_opt,
00123       options [i].help);
00124 
00125 #else /* HAVE_GETOPT_LONG */
00126 
00127   /* Leaves HAVE_GETOPT */
00128   printf ("  %c%c  %s\n",
00129       (options [i].s_opt == ' ' ? ' ' : '-'),
00130       options [i].s_opt,
00131       options [i].help);
00132 
00133 #endif /* HAVE_GETOPT_LONG */
00134 }
00135 
00136 #endif /* defined (HAVE_GETOPT) || defined (HAVE_GETOPT_LONG) */
00137 
00138   return;
00139 }
00140 
00141 
00142 /* Global variable, referenced in gtk_globals.h */
00143 char config_dir[1024] = "";
00144 GtkWidget *current_parent_widget = NULL;
00145 
00146 
00147 static void start_login(gboolean new)
00148 {
00149     extern gint do_login_on_startup ;
00150 
00151     if (new)
00152     {
00153         eb_new_user();
00154     }
00155     else
00156     {
00157         eb_status_window();
00158         if(do_login_on_startup) eb_sign_on_all(NULL, NULL) ;
00159     }
00160 }
00161  
00162 int main(int argc, char *argv[])
00163 {
00164 
00165     int c = 0;
00166     int sock;
00167     int len;
00168 #ifndef __MINGW32__
00169     struct sockaddr_un local;
00170     int disable_console_server = FALSE;
00171 #else
00172     struct sockaddr_in local;
00173     int disable_console_server = TRUE;
00174 #endif
00175     char message[4096] = "";
00176     char contact[256] = "";
00177     gchar buff[1024];
00178     struct stat stat_buf;
00179     gboolean accounts_success = FALSE;
00180 
00181     GtkWidget *wid;
00182     pid_t pid;
00183     const char * cmd = argv [0];
00184 
00185     setlocale(LC_ALL, "");
00186     bindtextdomain (PACKAGE, LOCALEDIR);
00187     textdomain (PACKAGE);
00188 
00189     geometry[0] = 0;
00190 
00191 /* Setup default value for config_dir */
00192 #if defined( _WIN32 )
00193     strcpy(config_dir, "C:\\.yattm\\");
00194 #else
00195     gtk_set_locale ();
00196     g_snprintf(config_dir, 1024, "%s/.yattm/",getenv("HOME"));
00197 #endif
00198 
00199 #if defined ( HAVE_GETOPT ) || defined ( HAVE_GETOPT_LONG )
00200     while (1) 
00201     {
00202 #ifdef HAVE_GETOPT_LONG
00203         static struct option long_options[] = 
00204         {
00205             {"version", no_argument, NULL, 'v'},
00206         {"help", no_argument, NULL, 'h'},
00207             {"geometry", required_argument, NULL, 'g'},
00208             {"activate-goad-server", required_argument, NULL, 'a'},
00209             {"disable-server", no_argument, NULL, 'D'},
00210             {"contact", required_argument, NULL, 'c'},
00211             {"message", required_argument, NULL, 'm'},
00212             {"config-dir", required_argument, NULL, 'd'},
00213             {0, 0, 0, 0}
00214         };
00215     
00216         int option_index = 0;
00217 
00218         c = getopt_long(argc, argv, "vhg:a:d:Dc:m:", long_options, &option_index);
00219 #else /* HAVE_GETOPT_LONG */
00220 /* Either had getopt_long or getopt or both so getopt should be here */
00221         c = getopt (argc, argv, "vhg:a:d:Dc:m:");
00222 #endif
00223         /* Detect the end of the options. */
00224 
00225         if (c == -1)
00226         {
00227             break;
00228         }
00229 
00230 
00231         switch (c) 
00232         {
00233             case 'v':           /* version information */
00234                 eb_cli_ver ();
00235                 exit(0);
00236             break;
00237 
00238             case 'h':           /* help information */
00239             case '?':
00240                 eb_cli_help (cmd);
00241             exit (0);
00242                 break;
00243 
00244             case 'g':
00245             strcpy(geometry, optarg);
00246             break;
00247 
00248 
00249             case ':':
00250             printf("Try 'yattm --help' for more information.\n");
00251             exit(0);
00252 
00253             case 'a':
00254             printf("hi\n");
00255             break;
00256 
00257             case 'D':
00258             printf("Disabling console message server.\n");
00259             disable_console_server = TRUE;
00260             break;
00261 
00262             case 'c':
00263             strcpy(contact, optarg);
00264             break;
00265 
00266             case 'm':
00267             strcpy(message, optarg);
00268             break;
00269 
00270             case 'd':
00271             strcpy(config_dir, optarg);
00272             /*Make sure we have directory delimiter */
00273 #if defined( _WIN32 )
00274             if(config_dir[strlen(config_dir)-1]!='\\')
00275                 strcat(config_dir, "\\");
00276 #else
00277             if(config_dir[strlen(config_dir)-1]!='/')
00278                 strcat(config_dir, "/");
00279 #endif
00280             if(stat(config_dir, &stat_buf)==-1)
00281             {
00282                 perror(config_dir);
00283                 exit(errno);
00284             }
00285             if(!S_ISDIR(stat_buf.st_mode))
00286             {
00287                 printf("config-dir %s is not a directory!\n", config_dir);
00288                 exit(1);
00289             }
00290             break;
00291 
00292         }
00293     } 
00294 
00295 #endif
00296 
00297     if(strlen(message) > 0 && strlen(contact) > 0)
00298     {
00299 #ifndef __MINGW32__
00300         struct sockaddr_un remote;
00301 #else
00302         struct sockaddr_in remote;
00303 #endif
00304         short length;
00305         int ret;
00306 
00307 #ifndef __MINGW32__
00308         sock = socket(AF_UNIX, SOCK_STREAM, 0);
00309         strcpy(remote.sun_path, config_dir);
00310         strcat(remote.sun_path, "eb_socket");
00311         remote.sun_family = AF_UNIX;
00312         len = strlen(remote.sun_path) + sizeof(remote.sun_family) +1;
00313 #else
00314         sock = socket(AF_INET, SOCK_STREAM, 0);
00315 #endif
00316         if(connect(sock, (struct sockaddr*)&remote, len) == -1 )
00317         {
00318             perror("connect");
00319             exit(1);
00320         }
00321         length = strlen(contact)+1;
00322         write(sock, &length, sizeof(short));
00323         write(sock, contact, length);
00324         length = strlen(message)+1;
00325         write(sock, &length, sizeof(short));
00326         write(sock, message, length);
00327         read(sock, &ret, sizeof(int));
00328         close(sock);
00329         exit(ret);
00330     }
00331 
00332     sound_init();
00333 
00334     gtk_set_locale();
00335     gtk_init(&argc, &argv);
00336     
00337 /*  g_snprintf(buff, 1024, "%s",config_dir);*/
00338 
00339     /* Mizhi 04-04-2001 */
00340     g_snprintf(buff, 1024, "%s.lock", config_dir);
00341     if ((pid = create_lock_file(buff)) > 0) {
00342 #ifndef __MINGW32__
00343         struct sockaddr_un remote;
00344 #else
00345         struct sockaddr_in remote;
00346 #endif
00347         short length;
00348         int ret;
00349 #ifndef __MINGW32__
00350         sock = socket(AF_UNIX, SOCK_STREAM, 0);
00351         strcpy(remote.sun_path, config_dir);
00352         strcat(remote.sun_path, "eb_socket");
00353         remote.sun_family = AF_UNIX;
00354         len = strlen(remote.sun_path) + sizeof(remote.sun_family) +1;
00355 #else
00356         sock = socket(AF_INET, SOCK_STREAM, 0);
00357 #endif
00358         if(connect(sock, (struct sockaddr*)&remote, len) == -1 )
00359         {
00360             perror("connect");
00361             g_snprintf(buff, 1024, _("Yattm already running, pid=%d\n"), pid);
00362             wid = do_error_dialog(buff, _("Already logged on"));
00363             /* UGLY UGLY HACK... */ 
00364             while (GTK_CHECK_TYPE(wid, GTK_TYPE_WINDOW) && GTK_WIDGET_VISIBLE(wid)){while (g_main_iteration(FALSE));}
00365             exit(1);
00366         }
00367         length = strlen("focus-yattm")+1;
00368         write(sock, &length, sizeof(short));
00369         write(sock, "focus-yattm", length);
00370         read(sock, &ret, sizeof(int));
00371         close(sock);
00372         exit(ret);
00373     }
00374 
00375     gtk_set_locale ();
00376     g_snprintf(buff, 1024, "%s",config_dir);
00377 
00378     mkdir(buff, 0700);
00379     g_snprintf(buff, 1024, "%slogs",config_dir);
00380     mkdir(buff, 0700);
00381 
00382     if(!disable_console_server) {
00383 #ifndef __MINGW32__
00384         sock = socket(AF_UNIX, SOCK_STREAM, 0);
00385         strcpy(local.sun_path, config_dir);
00386         strcat(local.sun_path, "eb_socket");
00387         unlink(local.sun_path);
00388         local.sun_family = AF_UNIX;
00389         len = strlen(local.sun_path) + sizeof(local.sun_family) +1;
00390 #else
00391         sock = socket(AF_INET, SOCK_STREAM, 0);
00392 #endif
00393         if(bind(sock, (struct sockaddr *)&local, len) == -1)
00394         {
00395             perror("Unable to handle console connections.  bind");
00396             exit(1);
00397         }
00398         if(listen(sock, 5) == -1)
00399         {
00400             perror("Unable to handle console connections. listen");
00401             exit(1);
00402         }
00403         gdk_input_add(sock, GDK_INPUT_READ, console_session_init, NULL);
00404     }
00405 
00406     //proxy_set_proxy( PROXY_NONE, "", 0 );
00407     //proxy_set_proxy(PROXY_SOCKS5,"24.0.201.225",1080);
00408 
00409     eb_read_prefs();
00410     /* Initalize the menus that are available through the plugin_api */
00411     init_menus();
00412     /* Load all the modules in the module_path preference, details can be found in the preferences module tab */
00413     load_modules();
00414 
00415     accounts_success = load_accounts();
00416     if(accounts_success)
00417     {
00418         load_contacts();
00419     }
00420 
00421     start_login(!accounts_success);
00422 
00423         init_smileys();
00424     gtk_main();
00425 
00426     unload_modules(); // Need to unload what we load
00427     sound_shutdown();
00428     g_snprintf(buff, 1024, "%s.lock", config_dir);
00429 #ifndef __MINGW32__
00430     if(!disable_console_server)
00431         unlink(local.sun_path);
00432         delete_lock_file(buff);
00433 #endif
00434     return 0;
00435 
00436 }

Contact: Andy Maloney     [Documentation generated by doxygen]