safe buffers, a memory leak and cleanups
authorMarkus Bröker <mbroeker@largo.dyndns.tv>
Sat, 03 Jan 2009 21:41:51 +0100
changeset 33 56571d34d754
parent 32 89fb9c6611ca
child 34 900bc2a53fd7
safe buffers, a memory leak and cleanups sizeof(buffer) is always the right choice for a static buffer... The error handler must appear before the printf Useful make targets like deb and debclean Trivial changes like removing trailing white spaces committer: Markus Bröker <mbroeker@largo.homelinux.org>
Makefile
TODO
debian/changelog
include/mcbot.h
locale/de/mcbot.po
scripts/mcbot.cgi
scripts/runbot
src/CMakeLists.txt
src/common.h
src/config.c
src/database.c
src/irc.c
src/main.c
src/parse.c
--- a/Makefile
+++ b/Makefile
@@ -14,13 +14,20 @@
 build/Makefile: prepare
 	cd build && $(CMAKE) ../src
 
-.PHONY: distclean prepare beauty
+.PHONY: distclean debclean prepare beauty
 
 clean: build/Makefile
 	@$(MAKE) -C build clean
 
+deb: debclean
+	dpkg-buildpackage -r
+
+debclean:
+	fakeroot debian/rules clean
+
 distclean:
 	$(RMDIR) build
+	$(RM) locale/de/LC_MESSAGES/mcbot.mo
 
 install: build/Makefile
 	$(MAKE) -C build install
--- a/TODO
+++ b/TODO
@@ -10,8 +10,6 @@
 
 Todo:
 	* UTF-8 aware Character-Encoding
-	* Dynamic Loading of plugins with dlopen
-	* implementation of some fun stuff
 	* AUTO-RECONNECT must be implemented
 
 Author:
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,4 +1,4 @@
-mcbot (0.97-2) unstable; urgency=low
+mcbot (0.99-2) unstable; urgency=low
 
   * Initial Release
   * the location of mcbot.cgi is stored in the db
@@ -30,5 +30,8 @@
   * command must be freed on error - makes valgrind happy
   * mcbot needs a proper system account - nobody collides with selinux-policy
   * a system account is between 100 and 999
+  * fixed a memory leak and safe buffers
+  * common.h added - common types for the project
+  * useful addons to the built process
 
- -- Markus Broeker <mbroeker@largo.homelinux.org>  Fri, 26 Dec 2008 11:25:00 +0100
+ -- Markus Broeker <mbroeker@largo.homelinux.org>  Fri, 02 Jan 2009 11:00:00 +0100
--- a/include/mcbot.h
+++ b/include/mcbot.h
@@ -8,7 +8,7 @@
 #define MCBOT_H
 
 #ifndef BOT_VERSION
-#define BOT_VERSION 0.96
+#define BOT_VERSION 0.99
 #endif
 
 #ifdef __DATE__
--- a/locale/de/mcbot.po
+++ b/locale/de/mcbot.po
@@ -1,22 +1,20 @@
-# SOME DESCRIPTIVE TITLE.
-# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
-# This file is distributed under the same license as the PACKAGE package.
-# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
+# MCBOT Translation
+# Copyright (C) 2009 Markus Broeker
 #
 #, fuzzy
 msgid ""
 msgstr ""
-"Project-Id-Version: PACKAGE VERSION\n"
-"Report-Msgid-Bugs-To: \n"
-"POT-Creation-Date: 2007-12-26 15:44+0100\n"
-"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
+"Project-Id-Version: mcbot-0.99\n"
+"Report-Msgid-Bugs-To: mbroeker@largo.homelinux.org\n"
+"POT-Creation-Date: 2008-06-20 15:44+0100\n"
+"PO-Revision-Date: 2009-01-03 20:23+001\n"
 "Last-Translator: Markus Broeker <mbroeker@largo.homelinux.org>\n"
-"Language-Team: LANGUAGE <LL@li.org>\n"
+"Language-Team: translation <translation@largo.homelinux.org>\n"
 "MIME-Version: 1.0\n"
 "Content-Type: text/plain; charset=UTF-8\n"
 "Content-Transfer-Encoding: 8bit\n"
 
-#: parse.c:78
+#: parse.c:83
 msgid "Request cannot be performed"
 msgstr "Operation kann nicht durchgefuehrt werden"
 
@@ -24,34 +22,34 @@
 msgid "What's up"
 msgstr "Was ist los"
 
-#: parse.c:149
+#: parse.c:136
 msgid "I need more parameters to add"
 msgstr "Ich brauche zwei Parameter zum Anfuegen"
 
-#: parse.c:164
+#: parse.c:148
 msgid "I need more parameters to replace"
 msgstr "Ich brauche zwei Parameter zum Ersetzen"
 
-#: parse.c:177
+#: parse.c:159
 msgid "I need a key to delete"
 msgstr "Ich brauche einen Schluessel zum Loeschen"
 
-#: parse.c:195
+#: parse.c:174
 msgid "I need a key to lookup"
 msgstr "Ich brauche einen Parameter zum Aufsuchen"
 
-#: parse.c:214
+#: parse.c:192
 msgid "Autolearn enabled for channel"
 msgstr "Autolearn ist aktiviert im Channel"
 
-#: parse.c:220
+#: parse.c:197
 msgid "Autolearn disabled for channel"
 msgstr "Autolearn ist deaktiviert im Channel"
 
-#: parse.c:240
+#: parse.c:213
 msgid "Bye, have a nice day!"
 msgstr "Machts gut, ich brauch ne Pause!"
 
-#: parse.c:268
+#: parse.c:238
 msgid "It is"
 msgstr "Es ist"
--- a/scripts/mcbot.cgi
+++ b/scripts/mcbot.cgi
@@ -17,13 +17,13 @@
 	$b =~ s/^[\t ]+//;					# skip blanks
 	$b =~ s/[\n]+//;					# for html source :)
 
-   	if ( $b && !($a =~ /mcbot/)) {
-    	if($b =~ /^http/ ) {
-	    	print "$a - <a href=\"$b\">$b</a><br>";
-   		} else {
-    		print "$a - $b<br>";
-	    }
-   	}
+    if ( $b && !($a =~ /mcbot/)) {
+        if($b =~ /^http/ ) {
+            print "$a - <a href=\"$b\">$b</a><br>";
+        } else {
+            print "$a - $b<br>";
+        }
+    }
 }
 
 print "\n<br>$a\n";
--- a/scripts/runbot
+++ b/scripts/runbot
@@ -6,7 +6,7 @@
 # Required-Stop:     $remote_fs $syslog
 # Default-Start:     2 3 4 5
 # Default-Stop:      0 1 6
-# Short-Description: IRC BOT mcbot 
+# Short-Description: IRC BOT mcbot
 # Description:       IRC BOT mcbot
 ### END INIT INFO
 
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -8,9 +8,9 @@
 SET(CMAKE_C_FLAGS_RELEASE "-g -ggdb")
 
 # C-Definitions
-ADD_DEFINITIONS(-Wall -O2 -ansi -pedantic)
+ADD_DEFINITIONS(-Wall -O2 -ansi)
 ADD_DEFINITIONS(-D_XOPEN_SOURCE=500)
-ADD_DEFINITIONS(-DBOT_VERSION=0.97)
+ADD_DEFINITIONS(-DBOT_VERSION=0.99)
 
 # Includes
 INCLUDE_DIRECTORIES(../include)
@@ -23,7 +23,7 @@
 INSTALL(TARGETS mcbot RUNTIME DESTINATION /usr/sbin)
 INSTALL(TARGETS dbtool RUNTIME DESTINATION /usr/bin)
 INSTALL(DIRECTORY ../locale/ DESTINATION /var/lib/mcbot/data/locale)
-INSTALL(PROGRAMS ../scripts/runbot DESTINATION /etc/init.d/ 
+INSTALL(PROGRAMS ../scripts/runbot DESTINATION /etc/init.d/
 	RENAME mcbot)
 INSTALL(PROGRAMS ../scripts/mcbot.cgi DESTINATION /usr/lib/cgi-bin/)
 
@@ -33,7 +33,7 @@
     EXEC_PROGRAM(${MSGFMT_PROG} ../locale ARGS -o de/LC_MESSAGES/mcbot.mo de/mcbot.po)
 ELSE(MSGFMT_PROG)
 	MESSAGE(FATAL_ERROR "YOU NEED TO INSTALL gettext")
-ENDIF (MSGFMT_PROG)    
+ENDIF (MSGFMT_PROG)
 
 # Search required libraries
 FIND_LIBRARY(GDBM_LIB NAMES gdbm
new file mode 100644
--- /dev/null
+++ b/src/common.h
@@ -0,0 +1,9 @@
+/**
+ *  $Id: common.h 551 2009-02-01 10:28:39Z mbroeker $
+ */
+
+#ifndef COMMON_H
+#define COMMON_H
+
+#define DEFAULT_BUF_SIZE 512
+#endif
--- a/src/config.c
+++ b/src/config.c
@@ -10,6 +10,7 @@
 
 #include <compat.h>
 #include <config.h>
+#include "common.h"
 
 const
 char *CONFIG_OPTIONS[] = {
@@ -20,7 +21,7 @@
 int config (UC * uc, char *fname)
 {
     FILE *f;
-    char buffer[513];
+    char buffer[DEFAULT_BUF_SIZE];
     char **line;
     char *token;
     char *value;
@@ -41,8 +42,7 @@
     uc->port = 6667;
 
     while (!feof (f)) {
-        *buffer = '\0';
-        (void)fgets (buffer, 512, f);
+        (void)fgets (buffer, sizeof (buffer), f);
         token = buffer;
 
         while (*token == '\t')  /* Eat trailing tabs */
--- a/src/database.c
+++ b/src/database.c
@@ -10,6 +10,7 @@
 #include <gdbm.h>
 
 #include <database.h>
+#include "common.h"
 
 char *db_insert (char *file_name, char *name, char *msg, int mode)
 {
@@ -66,7 +67,7 @@
     GDBM_FILE dbf;
     datum content;
     datum key;
-    static char msg[513] = { 0 };
+    static char msg[DEFAULT_BUF_SIZE] = { 0 };
 
     if ((dbf = gdbm_open (file_name, 512, GDBM_READER, 0, 0)) == NULL)
         return "db_lookup error";
@@ -76,10 +77,10 @@
 
     content = gdbm_fetch (dbf, key);
     if (content.dptr != NULL) {
-        snprintf (msg, 512, "%s", content.dptr);
+        snprintf (msg, sizeof (msg), "%s", content.dptr);
         free (content.dptr);
     } else
-        snprintf (msg, 512, "I haven't heard anything about %s.", entry);
+        snprintf (msg, sizeof (msg), "I haven't heard anything about %s.", entry);
 
     gdbm_close (dbf);
     return msg;
@@ -106,7 +107,7 @@
     }
 
     gdbm_close (dbf);
-    snprintf (msg, 80, "I am holding %d %s in my database.", count, (count > 0) ? "rows" : "row");
+    snprintf (msg, sizeof (msg), "I am holding %d %s in my database.", count, (count > 0) ? "rows" : "row");
     return msg;
 }
 
@@ -136,7 +137,7 @@
     }
 
     gdbm_close (dbf);
-    snprintf (msg, 80, "I am holding %d %s in my database.", count, (count > 0) ? "rows" : "row");
+    snprintf (msg, sizeof (msg), "I am holding %d %s in my database.", count, (count > 0) ? "rows" : "row");
     return msg;
 }
 
@@ -151,6 +152,6 @@
     gdbm_reorganize (dbf);
 
     gdbm_close (dbf);
-    snprintf (msg, 80, "I reorganized the database.");
+    snprintf (msg, sizeof (msg), "I reorganized the database.");
     return msg;
 }
--- a/src/irc.c
+++ b/src/irc.c
@@ -19,20 +19,11 @@
 
 #include <compat.h>
 #include <irc.h>
+#include "common.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    KICK 12
+enum {
+    NOTICE, MODE, JOIN, PART, TOPIC, PING, ENOMEM, ERROR, VERSION, PRIVMSG, QUIT, NICK, KICK
+};
 
 #define VERSION_STRING "MCBOT on GNU/LINUX"
 
@@ -96,7 +87,7 @@
 int irc_login (FILE * stream, char *server, char *nick, char *password)
 {
     MSG message = { NULL, 0, 0, 0, 0, 0, 0 };
-    char msg[513];
+    char msg[DEFAULT_BUF_SIZE];
     char *user;
     struct passwd *pwd;
 
@@ -118,8 +109,7 @@
         fprintf (stream, "PRIVMSG NICKSERV :IDENTIFY %s\r\n", password);
 
     for (;;) {
-        *msg = '\0';
-        fgets (msg, 512, stream);
+        fgets (msg, sizeof (msg), stream);
         if ((user = irc_parsemessage (msg, &message)) != NULL)
             printf ("%10s %s\n", user, message.line);
 
@@ -267,7 +257,7 @@
                         *ptr = '\0';
 
                     message->channel = strtok (message->line, " ");
-                    strncpy (message->current_channel, message->channel, 40);
+                    strncpy (message->current_channel, message->channel, sizeof (message->current_channel));
                     message->line = strtok (NULL, "\r\n");
                     message->line++;
                     printf ("%10s %s %s :%s\n", "READ", message->command, message->channel, message->line);
--- a/src/main.c
+++ b/src/main.c
@@ -14,6 +14,7 @@
 
 #include <mcbot.h>
 #include <config.h>
+#include "common.h"
 
 #ifndef CONFIG_FILE
 #define CONFIG_FILE "/var/lib/mcbot/.mcbotrc"
@@ -35,7 +36,7 @@
 {
     UC uc;
     MSG message;
-    char buf[513];
+    char buf[DEFAULT_BUF_SIZE];
     char *msg;
     char *command;
     int len;
@@ -91,17 +92,15 @@
     }
 
     while (!feof (message.stream)) {
-        *buf = '\0';
-        fgets (buf, 512, message.stream);
+        fgets (buf, sizeof (buf), message.stream);
 
         if (!active)            /* the bot was killed with SIGTERM */
             break;
 
         if ((command = irc_parsemessage (buf, &message))) {
+            if (!strcmp (command, "ERROR") || !strcmp (command, "ENOMEM"))
+                break;
             printf ("%10s %s %s\n", command, message.channel, message.line);
-            if (!strcmp (command, "ERROR") || !strcmp (command, "ENOMEM")) {
-                break;
-            }
         } else {
             if ((msg = parse (&message)) != NULL) {
                 fprintf (message.stream, "%s\r\n", msg);
@@ -109,6 +108,7 @@
                 printf ("%10s %s", "WRITE", msg);
             }
         }
+        fflush (stdout);
     }
     printf ("\n\nClosing Connection\n\n");
     fclose (message.stream);
--- a/src/parse.c
+++ b/src/parse.c
@@ -11,6 +11,7 @@
 
 #include <mcbot.h>
 #include <database.h>
+#include "common.h"
 
 #include <locale.h>
 #include <libintl.h>
@@ -23,32 +24,10 @@
 #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
-#define    DEOP 20
-#define    KICK 21
-#define     BAN 22
-#define   UNBAN 23
-#define KICKBAN 24
-#define   HELLO 25
+enum {
+    HELP, JOIN, LEAVE, ADD, REPLACE, DELETE, LIST, SEARCH, INFO, PING, ON, OFF, DEBUG,
+    VACCUUM, LOGOUT, WHO, WHOIS, TIME, TELL, OP, DEOP, KICK, BAN, UNBAN, KICKBAN, HELLO
+};
 
 const
 char *COMMAND_LIST[] = {
@@ -89,7 +68,7 @@
 
 char *parse (MSG * message)
 {
-    static char msg[513];
+    static char msg[DEFAULT_BUF_SIZE];
     int cmd = -1;
     int i;
     char *token;
@@ -101,7 +80,7 @@
     /*
      * default message
      */
-    snprintf (msg, 512, "PRIVMSG %s :%s.\r\n", message->channel, gettext ("Request cannot be performed"));
+    snprintf (msg, sizeof (msg), "PRIVMSG %s :%s.\r\n", message->channel, gettext ("Request cannot be performed"));
 
     /*
      * PRIVATE MESSAGES
@@ -125,16 +104,16 @@
                 if ((token = strtok (NULL, "\r\n")))
                     cmd = atoi (token);
                 if ((cmd > 0) && (cmd < VISIBLE_ITEMS))
-                    snprintf (msg, 512, "PRIVMSG %s :%s\r\n", message->channel, COMMAND_LIST[cmd]);
+                    snprintf (msg, sizeof (msg), "PRIVMSG %s :%s\r\n", message->channel, COMMAND_LIST[cmd]);
                 else
-                    snprintf (msg, 512, "PRIVMSG %s :%s\r\n", message->channel, COMMAND_LIST[0]);
+                    snprintf (msg, sizeof (msg), "PRIVMSG %s :%s\r\n", message->channel, COMMAND_LIST[0]);
                 return msg;
 
             case JOIN:
                 if ((token = strtok (NULL, "\r\n")))
                     if (strstr (message->user, db_lookup (DATABASE_FILE, "mcbot.user"))) {
                         if (strstr (message->email, db_lookup (DATABASE_FILE, "mcbot.email")))
-                            snprintf (msg, 512, "JOIN %s\r\n", token);
+                            snprintf (msg, sizeof (msg), "JOIN %s\r\n", token);
                     }
                 return msg;
 
@@ -143,17 +122,17 @@
                     return NULL;
                 if (strstr (message->user, db_lookup (DATABASE_FILE, "mcbot.user"))) {
                     if (strstr (message->email, db_lookup (DATABASE_FILE, "mcbot.email")))
-                        snprintf (msg, 512, "PART %s :Leaving.\r\n", message->channel);
+                        snprintf (msg, sizeof (msg), "PART %s :Leaving.\r\n", message->channel);
                 }
                 return msg;
 
             case ADD:
                 if ((token = strtok (NULL, " "))) {
                     if ((parameters = strtok (NULL, "\r\n"))) {
-                        snprintf (msg, 512, "PRIVMSG %s :%s, %s\r\n",
+                        snprintf (msg, sizeof (msg), "PRIVMSG %s :%s, %s\r\n",
                                   message->channel, message->user, db_insert (DATABASE_FILE, token, parameters, 0));
                     } else {
-                        snprintf (msg, 512, "PRIVMSG %s :%s, %s!\r\n", message->channel,
+                        snprintf (msg, sizeof (msg), "PRIVMSG %s :%s, %s!\r\n", message->channel,
                                   gettext ("I need more parameters to add"), message->user);
                     }
                 }
@@ -162,10 +141,10 @@
             case REPLACE:
                 if ((token = strtok (NULL, " "))) {
                     if ((parameters = strtok (NULL, "\r\n"))) {
-                        snprintf (msg, 512, "PRIVMSG %s :%s, %s\r\n",
+                        snprintf (msg, sizeof (msg), "PRIVMSG %s :%s, %s\r\n",
                                   message->channel, message->user, db_insert (DATABASE_FILE, token, parameters, 1));
                     } else {
-                        snprintf (msg, 512, "PRIVMSG %s :%s, %s!\r\n", message->channel,
+                        snprintf (msg, sizeof (msg), "PRIVMSG %s :%s, %s!\r\n", message->channel,
                                   gettext ("I need more parameters to replace"), message->user);
                     }
                 }
@@ -173,63 +152,64 @@
 
             case DELETE:
                 if ((token = strtok (NULL, "\r\n"))) {
-                    snprintf (msg, 512, "PRIVMSG %s :%s, %s\r\n",
+                    snprintf (msg, sizeof (msg), "PRIVMSG %s :%s, %s\r\n",
                               message->channel, message->user, db_remove (DATABASE_FILE, token));
                 } else {
-                    snprintf (msg, 512, "PRIVMSG %s :%s, %s!\r\n", message->channel, gettext ("I need a key to delete"),
-                              message->user);
+                    snprintf (msg, sizeof (msg), "PRIVMSG %s :%s, %s!\r\n", message->channel,
+                              gettext ("I need a key to delete"), message->user);
                 }
                 return msg;
 
             case LIST:
-                snprintf (msg, 512, "PRIVMSG %s :%s %s\r\n", message->channel, db_elements (DATABASE_FILE),
+                snprintf (msg, sizeof (msg), "PRIVMSG %s :%s %s\r\n", message->channel, db_elements (DATABASE_FILE),
                           db_lookup (DATABASE_FILE, "mcbot.cgi"));
                 return msg;
 
             case SEARCH:
                 if ((token = strtok (NULL, "\r\n"))) {
-                    snprintf (msg, 512, "PRIVMSG %s :%s, %s\r\n", message->channel, message->user,
+                    snprintf (msg, sizeof (msg), "PRIVMSG %s :%s, %s\r\n", message->channel, message->user,
                               db_lookup (DATABASE_FILE, token));
                 } else {
-                    snprintf (msg, 512,
+                    snprintf (msg, sizeof (msg),
                               "PRIVMSG %s :%s, %s!\r\n", message->channel, gettext ("I need a key to lookup"),
                               message->user);
                 }
                 return msg;
 
             case INFO:
-                snprintf (msg, 512, "PRIVMSG %s :I am MCBot-%1.2f Build %s and my source code can be found at %s\r\n",
+                snprintf (msg, sizeof (msg),
+                          "PRIVMSG %s :I am MCBot-%1.2f Build %s and my source code can be found at %s\r\n",
                           message->channel, BOT_VERSION, BOT_BUILD, SOURCE_URL);
                 return msg;
 
             case PING:
                 if ((token = strtok (NULL, "\r\n")))
-                    snprintf (msg, 512, "PRIVMSG %s :PING 0815\r\n", token);
+                    snprintf (msg, sizeof (msg), "PRIVMSG %s :PING 0815\r\n", token);
                 return msg;
 
             case ON:
-                snprintf (msg, 512, "PRIVMSG %s :%s %s.\r\n", message->user, gettext ("Autolearn enabled for channel"),
-                          message->channel);
+                snprintf (msg, sizeof (msg), "PRIVMSG %s :%s %s.\r\n", message->user,
+                          gettext ("Autolearn enabled for channel"), message->channel);
                 return msg;
 
             case OFF:
-                snprintf (msg, 512, "PRIVMSG %s :%s %s.\r\n", message->user, gettext ("Autolearn disabled for channel"),
-                          message->channel);
+                snprintf (msg, sizeof (msg), "PRIVMSG %s :%s %s.\r\n", message->user,
+                          gettext ("Autolearn disabled for channel"), message->channel);
                 return msg;
 
             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);
+                snprintf (msg, sizeof (msg), "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 VACCUUM:
-                snprintf (msg, 512, "PRIVMSG %s :%s\r\n", message->channel, db_vaccuum (DATABASE_FILE));
+                snprintf (msg, sizeof (msg), "PRIVMSG %s :%s\r\n", message->channel, db_vaccuum (DATABASE_FILE));
                 return msg;
 
             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,
+                        snprintf (msg, sizeof (msg), "PRIVMSG %s :%s!\r\nQUIT\r\n", message->channel,
                                   gettext ("Bye, have a nice day!"));
                     }
                 }
@@ -240,13 +220,13 @@
 
             case WHO:
                 if ((token = strtok (NULL, "\r\n")) != NULL) {
-                    snprintf (msg, 512, "WHO %s\r\n", token);
+                    snprintf (msg, sizeof (msg), "WHO %s\r\n", token);
                 }
                 return msg;
 
             case WHOIS:
                 if ((token = strtok (NULL, "\r\n")) != NULL) {
-                    snprintf (msg, 512, "WHOIS %s\r\n", token);
+                    snprintf (msg, sizeof (msg), "WHOIS %s\r\n", token);
                 }
                 return msg;
 
@@ -255,8 +235,8 @@
                 timeptr = localtime (&t);
                 if ((token = malloc (81))) {
                     strftime (token, 80, "%I:%M:%S %p", timeptr);
-                    snprintf (msg, 512, "PRIVMSG %s :%s %s, %s!\r\n", message->channel, gettext ("It is"), token,
-                              message->user);
+                    snprintf (msg, sizeof (msg), "PRIVMSG %s :%s %s, %s!\r\n", message->channel, gettext ("It is"),
+                              token, message->user);
                     free (token);
                 }
                 return msg;
@@ -266,7 +246,7 @@
                     if ((parameters = strtok (NULL, "\r\n"))) {
                         if (*token == '*')
                             message->channel = ++token;
-                        snprintf (msg, 512, "PRIVMSG %s :%s, %s\r\n", message->channel, token,
+                        snprintf (msg, sizeof (msg), "PRIVMSG %s :%s, %s\r\n", message->channel, token,
                                   db_lookup (DATABASE_FILE, parameters));
                     }
                 }
@@ -276,7 +256,7 @@
                 if ((token = strtok (NULL, "\r\n")) != NULL) {
                     if (strstr (message->user, db_lookup (DATABASE_FILE, "mcbot.user"))) {
                         if (strstr (message->email, db_lookup (DATABASE_FILE, "mcbot.email")))
-                            snprintf (msg, 512, "MODE %s +o %s\r\n", message->channel, token);
+                            snprintf (msg, sizeof (msg), "MODE %s +o %s\r\n", message->channel, token);
                     }
                 }
                 return msg;
@@ -285,7 +265,7 @@
                 if ((token = strtok (NULL, "\r\n")) != NULL) {
                     if (strstr (message->user, db_lookup (DATABASE_FILE, "mcbot.user"))) {
                         if (strstr (message->email, db_lookup (DATABASE_FILE, "mcbot.email")))
-                            snprintf (msg, 512, "MODE %s -o %s\r\n", message->channel, token);
+                            snprintf (msg, sizeof (msg), "MODE %s -o %s\r\n", message->channel, token);
                     }
                 }
                 return msg;
@@ -294,7 +274,7 @@
                 if ((token = strtok (NULL, "\r\n")) != NULL) {
                     if (strstr (message->user, db_lookup (DATABASE_FILE, "mcbot.user"))) {
                         if (strstr (message->email, db_lookup (DATABASE_FILE, "mcbot.email")))
-                            snprintf (msg, 512, "KICK %s %s\r\n", message->channel, token);
+                            snprintf (msg, sizeof (msg), "KICK %s %s\r\n", message->channel, token);
                     }
                 }
                 return msg;
@@ -303,7 +283,7 @@
                 if ((token = strtok (NULL, "\r\n")) != NULL) {
                     if (strstr (message->user, db_lookup (DATABASE_FILE, "mcbot.user"))) {
                         if (strstr (message->email, db_lookup (DATABASE_FILE, "mcbot.email")))
-                            snprintf (msg, 512, "MODE %s +b %s\r\n", message->channel, token);
+                            snprintf (msg, sizeof (msg), "MODE %s +b %s\r\n", message->channel, token);
                     }
                 }
                 return msg;
@@ -312,7 +292,7 @@
                 if ((token = strtok (NULL, "\r\n")) != NULL) {
                     if (strstr (message->user, db_lookup (DATABASE_FILE, "mcbot.user"))) {
                         if (strstr (message->email, db_lookup (DATABASE_FILE, "mcbot.email")))
-                            snprintf (msg, 512, "MODE %s -b %s\r\n", message->channel, token);
+                            snprintf (msg, sizeof (msg), "MODE %s -b %s\r\n", message->channel, token);
                     }
                 }
                 return msg;
@@ -321,14 +301,15 @@
                 if ((token = strtok (NULL, "\r\n")) != NULL) {
                     if (strstr (message->user, db_lookup (DATABASE_FILE, "mcbot.user"))) {
                         if (strstr (message->email, db_lookup (DATABASE_FILE, "mcbot.email")))
-                            snprintf (msg, 512, "MODE %s +b %s\r\nKICK %s %s\r\n", message->channel, token,
+                            snprintf (msg, sizeof (msg), "MODE %s +b %s\r\nKICK %s %s\r\n", message->channel, token,
                                       message->channel, token);
                     }
                 }
                 return msg;
 
             case HELLO:
-                snprintf (msg, 512, "PRIVMSG %s :%s, %s?\r\n", message->channel, gettext ("What's up"), message->user);
+                snprintf (msg, sizeof (msg), "PRIVMSG %s :%s, %s?\r\n", message->channel, gettext ("What's up"),
+                          message->user);
                 return msg;
             }
         }