ChangeLog mcbot-0.95-1
authorMarkus Bröker <mbroeker@largo.dyndns.tv>
Sat, 13 Dec 2008 15:40:28 +0100
changeset 15 f19c1f9b4cd3
parent 14 fe8adc56b109
child 16 33245bf7873a
ChangeLog mcbot-0.95-1 * current_channel changed from pointer type to char [41] * many constants added in parse.c and irc.c * codebase reduced committer: Markus Bröker <mbroeker@largo.homelinux.org>
debian/changelog
include/irc.h
include/mcbot.h
src/CMakeLists.txt
src/irc.c
src/main.c
src/parse.c
--- a/debian/changelog
+++ b/debian/changelog
@@ -13,5 +13,6 @@
   * A simple garbage collector fills the memory leaks...
   * compat.c added for XOPEN_SOURCE features - not finished yet
   * SOURCE_URL points to the proper git-location.
+  * many small improvements, reduced codebase
 
- -- Markus Broeker <mbroeker@largo.homelinux.org>  Sat, 10 Aug 2008 20:00:54 +0200
+ -- Markus Broeker <mbroeker@largo.homelinux.org>  Mon, 11 Aug 2008 00:00:54 +0200
--- a/include/irc.h
+++ b/include/irc.h
@@ -19,7 +19,7 @@
     char *command;
     char *channel;
     char *line;
-    char *current_channel;
+    char current_channel[41];
 };
 
 typedef struct Message MSG;
--- a/include/mcbot.h
+++ b/include/mcbot.h
@@ -7,8 +7,8 @@
 #ifndef MCBOT_H
 #define MCBOT_H
 
-#ifndef VERSION
-#define VERSION 0.0
+#ifndef BOT_VERSION
+#define BOT_VERSION 0.95
 #endif
 
 #ifndef IRC_H
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -10,7 +10,7 @@
 # C-Definitions
 ADD_DEFINITIONS(-Wall -O2 -ansi -pedantic)
 ADD_DEFINITIONS(-D_XOPEN_SOURCE=500)
-ADD_DEFINITIONS(-DVERSION=0.95)
+ADD_DEFINITIONS(-DBOT_VERSION=0.95)
 
 # Includes
 INCLUDE_DIRECTORIES(../include)
--- a/src/irc.c
+++ b/src/irc.c
@@ -20,6 +20,19 @@
 #include <compat.h>
 #include <irc.h>
 
+#define  NOTICE  0
+#define    MODE  1
+#define    JOIN  2
+#define    PART  3
+#define   TOPIC  4
+#define    PING  5
+#define  ENOMEM  6
+#define   ERROR  7
+#define VERSION  8
+#define PRIVMSG  9
+#define    QUIT 10
+#define    NICK 11
+
 #define VERSION_STRING "MCBOT on GNU/LINUX"
 
 const char *IRC_Commands[] = {
@@ -163,10 +176,7 @@
     message->channel = message->line = NULL;
 
     token = strtok (theLine, " ");
-    if (*token != ':') {
-        /*
-         * SERVER MESSAGES
-         */
+    if (*token != ':') {        /* SERVER MESSAGES */
         if ((ptr = strtok (NULL, "\r\n")) != NULL)
             ++ptr;
         message->line = ptr;
@@ -175,7 +185,6 @@
 
     message->user = ++token;
     message->command = strtok (NULL, " ");
-
     message->line = strtok (NULL, "\r\n");
 
     return message->command;
@@ -197,7 +206,7 @@
         while (IRC_Commands[i] != NULL) {
             if (strcmp (IRC_Commands[i], command) == 0) {
                 switch (i) {
-                case 0:        /* NOTICE */
+                case NOTICE:
                     if (message->line == NULL) {
                         message->channel = "*";
                         message->line = "*";
@@ -207,22 +216,22 @@
                             ++message->line;
                     }
                     return command;
-                case 1:        /* MODE */
+                case MODE:
                     message->channel = strtok (message->line, " ");
                     message->line = strtok (NULL, "\r\n");
                     return command;
-                case 2:        /* JOIN */
-                case 3:        /* PART */
+                case JOIN:
+                case PART:
                     if ((message->channel = strchr (message->line, ':')))
                         ++message->channel;
                     message->line = message->user;
                     return command;
-                case 4:        /* TOPIC */
+                case TOPIC:
                     message->channel = strtok (message->line, " ");
                     if ((message->line = strtok (NULL, "\r\n")))
                         ++message->line;
                     return command;
-                case 5:        /* PING */
+                case PING:
 #ifdef DEBUG
                     /*
                      * DONT NERVE WITH PING PONG MESSAGES
@@ -236,29 +245,29 @@
 #else
                     return NULL;
 #endif
-                case 6:        /* ENOMEM */
-                case 7:        /* ERROR */
+                case ENOMEM:
+                case ERROR:
                     return command;
-                case 8:        /* VERSION */
+                case VERSION:
                     if ((ptr = strchr (message->user, ' ')))
                         *ptr = '\0';
                     return command;
-                case 9:        /* PRIVMSG */
+                case PRIVMSG:
                     if ((message->email = strchr (message->user, '=')))
                         ++message->email;
                     if ((ptr = strchr (message->user, '!')))
                         *ptr = '\0';
 
                     message->channel = strtok (message->line, " ");
-                    message->current_channel = message->channel;
+                    strncpy (message->current_channel, message->channel, 40);
                     message->line = strtok (NULL, "\r\n");
                     message->line++;
                     printf ("%10s %s %s :%s\n", "READ", message->command, message->channel, message->line);
                     return NULL;
-                case 10:       /* QUIT */
+                case QUIT:
                     message->channel = message->user;
                     return command;
-                case 11:       /* NICK */
+                case NICK:
                     message->channel = message->user;
                     return command;
                 }
@@ -300,20 +309,8 @@
             case 328:          /* INFORMATION */
             case 332:          /* TOPIC OF CHANNEL */
             case 333:          /* NAMES IN CHANNEL */
-                message->channel = strtok (message->line, " ");
-                message->line = strtok (NULL, "\r\n");
-                return command;
             case 351:          /* SVERSION */
-                message->channel = strtok (message->line, " ");
-                message->line = strtok (NULL, "\r\n");
-                return command;
             case 352:          /* WHO LIST */
-                message->channel = strtok (message->line, " ");
-                message->line = strtok (NULL, "\r\n");
-                /*
-                 * MORE THAN 3 LINES AND YOU WILL be KICKED
-                 */
-                return command;
             case 353:
             case 365:
             case 366:          /* END OF NAMES */
@@ -328,18 +325,19 @@
             case 403:          /* THAT CHANNEL DOESN'T EXIST */
             case 412:          /* NO TEXT TO SEND */
             case 441:          /* THEY AREN'T ON THIS CHANNEL */
+                message->channel = strtok (message->line, " ");
+                message->line = strtok (NULL, "\r\n");
+                fprintf (message->stream, "PRIVMSG %s :%s\r\n", message->current_channel, message->line);
                 return command;
             case 474:
             case 475:
             case 476:
             case 477:
             case 482:
-                message->channel = strtok (message->line, " ");
-                message->line = strtok (NULL, "\r\n");
-                return command;
             case 901:          /* notify or some crap */
                 message->channel = strtok (message->line, " ");
                 message->line = strtok (NULL, "\r\n");
+                fprintf (message->stream, "PRIVMSG %s :%s\r\n", message->current_channel, message->line);
                 return command;
             default:
                 printf ("DEBUG   %s", line);
--- a/src/main.c
+++ b/src/main.c
@@ -42,7 +42,7 @@
         (void)setlocale (LC_MESSAGES, "");
     }
 
-    printf ("mcbot-%1.2f\n", VERSION);
+    printf ("mcbot-%1.2f\n", BOT_VERSION);
 
     if ((len = config (&uc, CONFIG_FILE)) != 0) {
         switch (len) {
@@ -84,7 +84,7 @@
 
     if (uc.channel) {
         fprintf (message.stream, "JOIN :%s\r\n", uc.channel);
-        message.current_channel = uc.channel;
+        strncpy (message.current_channel, uc.channel, 40);
         if (uc.topic)
             fprintf (message.stream, "TOPIC %s :%s\r\n", uc.channel, uc.topic);
     }
--- a/src/parse.c
+++ b/src/parse.c
@@ -23,6 +23,27 @@
 #define SOURCE_URL "http://largo.homelinux.org/cgi-bin/gitweb.cgi?p=net/mcbot.git"
 #endif
 
+#define    HELP  0
+#define    JOIN  1
+#define   LEAVE  2
+#define     ADD  3
+#define REPLACE  4
+#define  DELETE  5
+#define    LIST  6
+#define  SEARCH  7
+#define    INFO  8
+#define    PING  9
+#define      ON 10
+#define     OFF 11
+#define   DEBUG 12
+#define VACCUUM 13
+#define  LOGOUT 14
+#define     WHO 15
+#define   WHOIS 16
+#define    TIME 17
+#define    TELL 18
+#define      OP 19
+
 const
 char *COMMAND_LIST[] = {
     "!help      Known Commands: join(1), leave(2), add(3), replace(4), delete(5), list(6), search(7), info(8)\r\n",
@@ -43,7 +64,7 @@
 };
 
 const
-char ITEMS = 14;
+char VISIBLE_ITEMS = 14;
 
 const
 char *Bot_Commands[] = {
@@ -111,27 +132,27 @@
     while (Bot_Commands[i]) {
         if (!strcmp (token, Bot_Commands[i])) {
             switch (i) {
-            case 0:            /* !help */
+            case HELP:
                 if ((token = strtok (NULL, "\r\n")))
                     cmd = atoi (token);
-                if ((cmd > 0) && (cmd < ITEMS))
+                if ((cmd > 0) && (cmd < VISIBLE_ITEMS))
                     snprintf (msg, 512, "PRIVMSG %s :%s\r\n", message->channel, COMMAND_LIST[cmd]);
                 else
                     snprintf (msg, 512, "PRIVMSG %s :%s\r\n", message->channel, COMMAND_LIST[0]);
                 return msg;
 
-            case 1:            /* !join */
+            case JOIN:
                 if ((token = strtok (NULL, "\r\n")))
                     snprintf (msg, 512, "JOIN %s\r\n", token);
                 return msg;
 
-            case 2:            /* !leave */
+            case LEAVE:
                 if (*message->channel != '#')
                     return NULL;
                 snprintf (msg, 512, "PART %s :Leaving.\r\n", message->channel);
                 return msg;
 
-            case 3:            /* !add */
+            case ADD:
                 if ((token = strtok (NULL, " "))) {
                     if ((parameters = strtok (NULL, "\r\n"))) {
                         snprintf (msg, 512, "PRIVMSG %s :%s, %s\r\n",
@@ -143,7 +164,7 @@
                 }
                 return msg;
 
-            case 4:            /* !replace */
+            case REPLACE:
                 if ((token = strtok (NULL, " "))) {
                     if ((parameters = strtok (NULL, "\r\n"))) {
                         snprintf (msg, 512, "PRIVMSG %s :%s, %s\r\n",
@@ -155,7 +176,7 @@
                 }
                 return msg;
 
-            case 5:            /* !delete */
+            case DELETE:
                 if ((token = strtok (NULL, "\r\n"))) {
                     snprintf (msg, 512, "PRIVMSG %s :%s, %s\r\n",
                               message->channel, message->user, db_remove (DATABASE_FILE, token));
@@ -165,12 +186,12 @@
                 }
                 return msg;
 
-            case 6:            /* !count */
+            case LIST:
                 snprintf (msg, 512, "PRIVMSG %s :%s %s\r\n", message->channel, db_elements (DATABASE_FILE),
                           db_lookup (DATABASE_FILE, "mcbot.cgi"));
                 return msg;
 
-            case 7:            /* !search */
+            case SEARCH:
                 if ((token = strtok (NULL, "\r\n"))) {
                     snprintf (msg, 512, "PRIVMSG %s :%s, %s\r\n", message->channel, message->user,
                               db_lookup (DATABASE_FILE, token));
@@ -181,36 +202,36 @@
                 }
                 return msg;
 
-            case 8:            /* !info */
+            case INFO:
                 snprintf (msg, 512, "PRIVMSG %s :I am MCBot-%1.2f and my source code can be found on %s\r\n",
-                          message->channel, VERSION, SOURCE_URL);
+                          message->channel, BOT_VERSION, SOURCE_URL);
                 return msg;
 
-            case 9:            /* !ping */
+            case PING:
                 if ((token = strtok (NULL, "\r\n")))
                     snprintf (msg, 512, "PRIVMSG %s :PING 0815\r\n", token);
                 return msg;
 
-            case 10:           /* !on */
+            case ON:
                 snprintf (msg, 512, "PRIVMSG %s :%s %s.\r\n", message->user, gettext ("Autolearn enabled for channel"),
                           message->channel);
                 return msg;
 
-            case 11:           /* !off */
+            case OFF:
                 snprintf (msg, 512, "PRIVMSG %s :%s %s.\r\n", message->user, gettext ("Autolearn disabled for channel"),
                           message->channel);
                 return msg;
 
-            case 12:           /* !debug */
+            case DEBUG:
                 snprintf (msg, 512, "PRIVMSG %s :USER: %s EMAIL: %s CHANNEL: %s LINE: %s\r\n", message->channel,
                           message->user, message->email, message->channel, message->line);
                 return msg;
 
-            case 13:           /* !vaccum */
+            case VACCUUM:
                 snprintf (msg, 512, "PRIVMSG %s :%s\r\n", message->channel, db_vaccuum (DATABASE_FILE));
                 return msg;
 
-            case 14:           /* !logout */
+            case LOGOUT:
                 if (strstr (message->user, db_lookup (DATABASE_FILE, "mcbot.user"))) {
                     if (strstr (message->email, db_lookup (DATABASE_FILE, "mcbot.email"))) {
                         snprintf (msg, 512, "PRIVMSG %s :%s!\r\nQUIT\r\n", message->channel,
@@ -222,19 +243,19 @@
                  */
                 return msg;
 
-            case 15:           /* !who */
+            case WHO:
                 if ((token = strtok (NULL, "\r\n")) != NULL) {
                     snprintf (msg, 512, "WHO %s\r\n", token);
                 }
                 return msg;
 
-            case 16:           /* !whois */
+            case WHOIS:
                 if ((token = strtok (NULL, "\r\n")) != NULL) {
                     snprintf (msg, 512, "WHOIS %s\r\n", token);
                 }
                 return msg;
 
-            case 17:           /* time */
+            case TIME:
                 t = time (NULL);
                 timeptr = localtime (&t);
                 if ((token = malloc (81))) {
@@ -245,7 +266,7 @@
                 }
                 return msg;
 
-            case 18:           /* tell */
+            case TELL:
                 if ((token = strtok (NULL, " "))) {
                     if ((parameters = strtok (NULL, "\r\n"))) {
                         if (*token == '*')
@@ -256,7 +277,7 @@
                 }
                 return msg;
 
-            case 19:           /* op */
+            case OP:
                 if ((token = strtok (NULL, "\r\n")) != NULL) {
                     if (strstr (message->email, db_lookup (DATABASE_FILE, "mcbot.email")))
                         snprintf (msg, 512, "MODE %s +o %s\r\n", message->channel, token);