00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef lint
00021 static const char rcsid[] = "$Id: ircparse.c,v 1.4 2004-10-17 05:14:06 stdarg Exp $";
00022 #endif
00023
00024 #include <eggdrop/eggdrop.h>
00025
00026
00027 static void add_arg(irc_msg_t *msg, char *text)
00028 {
00029 if (msg->nargs >= IRC_MSG_NSTATIC_ARGS) {
00030 if (msg->nargs > IRC_MSG_NSTATIC_ARGS) {
00031 msg->args = realloc(msg->args, sizeof(char *) * (msg->nargs+1));
00032 }
00033 else {
00034 msg->args = malloc(sizeof(char *) * (IRC_MSG_NSTATIC_ARGS+1));
00035 memcpy(msg->args, msg->static_args, sizeof(char *) * IRC_MSG_NSTATIC_ARGS);
00036 }
00037 }
00038 msg->args[msg->nargs] = text;
00039 msg->nargs++;
00040 }
00041
00042
00043
00044
00045 void irc_msg_parse(char *text, irc_msg_t *msg)
00046 {
00047 char *space;
00048
00049 memset(msg, 0, sizeof(*msg));
00050 msg->args = msg->static_args;
00051 if (*text == ':') {
00052 msg->prefix = text+1;
00053 space = strchr(msg->prefix, ' ');
00054 if (!space) return;
00055 *space = 0;
00056 text = space+1;
00057 }
00058 msg->cmd = text;
00059 space = strchr(text, ' ');
00060 if (!space) return;
00061 *space = 0;
00062 text = space+1;
00063
00064 while (*text && (*text != ':')) {
00065 space = strchr(text, ' ');
00066 add_arg(msg, text);
00067 if (space) {
00068 *space = 0;
00069 text = space+1;
00070 }
00071 else break;
00072 }
00073 if (*text == ':') add_arg(msg, text+1);
00074 add_arg(msg, NULL);
00075 msg->nargs--;
00076 }
00077
00078
00079 void irc_msg_restore(irc_msg_t *msg)
00080 {
00081 int i;
00082 for (i = 0; i < msg->nargs; i++) {
00083 if (msg->args[i][-1] == ':') msg->args[i][-2] = ' ';
00084 else msg->args[i][-1] = ' ';
00085 }
00086 }
00087
00088
00089 void irc_msg_cleanup(irc_msg_t *msg)
00090 {
00091 if (msg->args && msg->args != msg->static_args) free(msg->args);
00092 msg->args = msg->static_args;
00093 msg->nargs = 0;
00094 }