diff options
author | beber <beber> | 2005-10-09 00:15:09 +0000 |
---|---|---|
committer | beber <beber> | 2005-10-09 00:15:09 +0000 |
commit | 26891a781c9f6571a365c2fe048135ec53fce333 (patch) | |
tree | 004d44184047c8ff30ad5670bf711350803ae615 /src | |
parent | and use it (diff) | |
download | espik-26891a781c9f6571a365c2fe048135ec53fce333.tar.xz |
Avoid repeated jobs !!
And do things better (all snprintf)
Diffstat (limited to 'src')
-rw-r--r-- | src/espik_irc.c | 98 |
1 files changed, 51 insertions, 47 deletions
diff --git a/src/espik_irc.c b/src/espik_irc.c index f3e2ef9..491b94b 100644 --- a/src/espik_irc.c +++ b/src/espik_irc.c @@ -20,6 +20,12 @@ typedef struct char* (*format) (char *dest, char *msg); } cmd_t; +typedef struct +{ + char* buf; + int len; +} buf_t; + cmd_t cmd_tab[] = { { "JOIN", irc_join }, { "PART", irc_part }, @@ -66,26 +72,32 @@ unsigned short sendmsg_len (char* msg) return (strlen (msg) + 2); } -char* irc_privmsg (char* chan, char* msg) +static buf_t make_buffer(int len) { - char* out; - unsigned int len; + buf_t buf; - len = strlen ("PRIVMSG") + 1 /* space */ - + strlen (chan) + 2 /* space: */ - + strlen (msg) + 1; /* EOF */ + buf.buf = malloc (sizeof(char) * len); + buf.len = len; - out = malloc (sizeof(char) * len); + return (buf); +} - snprintf (out, len, "PRIVMSG %s :%s", chan, msg); +char* irc_privmsg (char* chan, char* msg) +{ + buf_t out; - return (out); + out = make_buffer (strlen ("PRIVMSG") + 1 /* "PRIVMSG " */ + + strlen (chan) + 2 /* ":<channel> " */ + + strlen (msg) + 1); /* "<msg>EOF" */ + + snprintf (out.buf, out.len, "PRIVMSG %s :%s", chan, msg); + + return (out.buf); } char* irc_join (char* chan __UNUSED__, char* msg) { - char* out; - int len; + buf_t out; /* COuld also by '&' and so one ... * have to look on RFC, dev will be done with # only */ @@ -96,57 +108,52 @@ char* irc_join (char* chan __UNUSED__, char* msg) return (0); } - len = strlen ("JOIN") + 1 - + strlen (msg) + 1; - out = malloc (sizeof(char) * len); + out = make_buffer (strlen ("JOIN") + 1 /* "JOIN " */ + + strlen (msg) + 1); /* "<msg>EOF" */ - snprintf (out, len, "JOIN %s", msg); + snprintf (out.buf, out.len, "JOIN %s", msg); - return (out); + return (out.buf); } char* irc_part (char* chan, char* msg) { - char* out; - int len; + buf_t out; if (msg[0] != '#') { - len = strlen ("PART") + 1 - + strlen (chan) + 2 - + strlen (msg) + 1; - printf ("irc_part: len: %d\n", len); - out = malloc (sizeof(char) * len); - snprintf (out, len, "PART %s :%s", chan, msg); + out = make_buffer (strlen ("PART") + 1 /* "PART " */ + + strlen (chan) + 2 /* ":<chan> " */ + + strlen (msg) + 1); /* "<msg>EOF" */ + + snprintf (out.buf, out.len, "PART %s :%s", chan, msg); } else { char** sep; sep = separate_commandline (msg); - len = strlen ("PART") + 1 - + strlen (sep[0]) + 2 - + strlen (sep[1]) + 1; - out = malloc (sizeof(char) * len); - snprintf (out, len, "PART %s :%s", sep[0], sep[1]); + + out = make_buffer (strlen ("PART") + 1 /* "PART " */ + + strlen (sep[0]) + 2 /* ":<chan> " */ + + strlen (sep[1]) + 1); /* "<msg>EOF" */ + + snprintf (out.buf, out.len, "PART %s :%s", sep[0], sep[1]); } - return (out); + return (out.buf); } char* irc_quit (char* chan __UNUSED__, char* msg) { - char* out; - int len; - - len = strlen ("QUIT") + 2 - + strlen (msg) + 1; + buf_t out; - out = malloc (sizeof (char) * len); + out = make_buffer (strlen ("QUIT") + 2 /* "QUIT :" */ + + strlen (msg) + 1); /* "<msg>EOF" */ - snprintf (out, len, "QUIT :%s", msg); + snprintf (out.buf, out.len, "QUIT :%s", msg); - printf ("msg : %s\nout: %s\n", msg, out); + printf ("msg : %s\nout: %s\n", msg, out.buf); /* espik_raw_send (out); @@ -155,7 +162,7 @@ char* irc_quit (char* chan __UNUSED__, char* msg) irc_disconnect(sock); */ - return (out); + return (out.buf); } char* irc_raw (char* chan __UNUSED__, char* msg) @@ -165,19 +172,16 @@ char* irc_raw (char* chan __UNUSED__, char* msg) char* irc_nick (char* chan __UNUSED__, char* nick) { - char* out; - int len; - - len = strlen ("NICK") + 1 - + strlen (nick) + 1; + buf_t out; - out = malloc (sizeof (char*) * len); + out = make_buffer (strlen ("NICK") + 1 /* "NICK " */ + + strlen (nick) + 1); /* "<nick>EOF" */ - snprintf (out, len, "NICK %s", nick); + snprintf (out.buf, out.len, "NICK %s", nick); printf ("NICK CHANGED REQUEST: %s\n", nick); - return (out); + return (out.buf); } /* |