src/main.c
changeset 0 586472add385
child 6 7eb12be31bb5
equal deleted inserted replaced
-1:000000000000 0:586472add385
       
     1 /**
       
     2  *  $Id: main.c 51 2008-01-10 00:19:39Z mbroeker $
       
     3  * $URL: http://localhost/svn/c/mcbot/trunk/src/main.c $
       
     4  *
       
     5  */
       
     6 
       
     7 #include <stdio.h>
       
     8 #include <stdlib.h>
       
     9 #include <string.h>
       
    10 
       
    11 #include <sys/types.h>
       
    12 #include <sys/socket.h>
       
    13 #include <netinet/in.h>
       
    14 #include <arpa/inet.h>
       
    15 #include <netdb.h>
       
    16 
       
    17 #include <signal.h>
       
    18 #include <locale.h>
       
    19 #include <libintl.h>
       
    20 
       
    21 #include <mcbot.h>
       
    22 #include <config.h>
       
    23 
       
    24 #ifndef CONFIG_FILE
       
    25 #define CONFIG_FILE "/var/lib/nobody/.mcbotrc"
       
    26 #endif
       
    27 
       
    28 #ifndef LOCALE_PATH
       
    29 #define LOCALE_PATH "/var/lib/nobody/data/locale"
       
    30 #endif
       
    31 
       
    32 short active = 1;
       
    33 
       
    34 void sigproc ()
       
    35 {
       
    36     signal (SIGTERM, sigproc);
       
    37     active = 0;
       
    38 }
       
    39 
       
    40 int main (int argc, char **argv)
       
    41 {
       
    42     UC uc;
       
    43     MSG message;
       
    44     char buf[513];
       
    45     char *msg;
       
    46     char *command;
       
    47     int len;
       
    48 
       
    49     if (bindtextdomain ("mcbot", LOCALE_PATH) != NULL) {
       
    50         (void)textdomain ("mcbot");
       
    51         (void)setlocale (LC_MESSAGES, "");
       
    52     }
       
    53 
       
    54     printf ("mcbot-%1.2f\n", VERSION);
       
    55 
       
    56     if ((len = config (&uc, CONFIG_FILE)) != 0) {
       
    57         switch (len) {
       
    58         case -1:
       
    59             printf ("You need to create a config file %s\n", CONFIG_FILE);
       
    60             break;
       
    61         case -2:
       
    62             printf
       
    63                 ("You need at least entries for user, password, server and channel in your config file %s\n",
       
    64                  CONFIG_FILE);
       
    65             break;
       
    66         }
       
    67         return len;
       
    68     }
       
    69 
       
    70     if (uc.nick)
       
    71         message.nick = uc.nick;
       
    72     else
       
    73         return -1;
       
    74 
       
    75     if (!(message.stream = irc_connect (uc.server, uc.port)))
       
    76         return EXIT_FAILURE;
       
    77 
       
    78     if ((len = irc_login (message.stream, uc.nick, uc.pass)) != 0) {
       
    79         switch (len) {
       
    80         case IRC_GENERAL_ERROR:
       
    81             printf ("GENERAL ERROR\n");
       
    82             break;
       
    83         case IRC_LOGIN_ERROR:
       
    84             printf ("LOGIN ERROR\n");
       
    85             break;
       
    86         default:
       
    87             printf ("Unknown Error %d\n", len);
       
    88         }
       
    89         return len;
       
    90     }
       
    91 
       
    92     signal (SIGTERM, sigproc);
       
    93 
       
    94     if (uc.channel) {
       
    95         fprintf (message.stream, "JOIN :%s\r\n", uc.channel);
       
    96         if (uc.topic)
       
    97             fprintf (message.stream, "TOPIC %s :%s\r\n", uc.channel, uc.topic);
       
    98     }
       
    99 
       
   100     while (active) {
       
   101         if (fgets (buf, 512, message.stream) == NULL) {
       
   102             active = 0;
       
   103             break;
       
   104         }
       
   105 
       
   106         if ((command = irc_parsemessage (buf, &message))) {
       
   107             printf ("%10s %s %s\n", command, message.channel, message.line);
       
   108         } else {
       
   109             if ((msg = parse (&message, &active)) != NULL) {
       
   110                 fprintf (message.stream, "%s\r\n", msg);
       
   111                 printf ("%10s %s", "WRITE", msg);
       
   112             }
       
   113         }
       
   114         *buf = 0;
       
   115     }
       
   116     printf ("\n\nClosing Connection\n\n");
       
   117     fclose (message.stream);
       
   118 
       
   119     /*
       
   120      * cleanup
       
   121      */
       
   122 
       
   123     if (uc.nick)
       
   124         free (uc.nick);
       
   125     if (uc.pass)
       
   126         free (uc.pass);
       
   127     if (uc.server)
       
   128         free (uc.server);
       
   129     if (uc.channel)
       
   130         free (uc.channel);
       
   131     if (uc.topic)
       
   132         free (uc.topic);
       
   133 
       
   134     return EXIT_SUCCESS;
       
   135 }