msgbuf: implement msgbuf_parse()

This commit is contained in:
William Pitcock 2016-02-10 00:08:58 -06:00
parent 88b427b61d
commit a8e69f5dfc
4 changed files with 109 additions and 2 deletions

View File

@ -46,7 +46,7 @@ struct MsgBuf {
* parse a message into a MsgBuf.
* returns 0 on success, 1 on error.
*/
int msgbuf_parse(struct MsgBuf *msgbuf, const char *line);
int msgbuf_parse(struct MsgBuf *msgbuf, char *line);
/*
* unparse a pure MsgBuf into a buffer.

View File

@ -42,6 +42,7 @@ libircd_la_SOURCES = \
match.c \
modules.c \
monitor.c \
msgbuf.c \
newconf.c \
operhash.c \
packet.c \

View File

@ -141,7 +141,7 @@ am_libircd_la_OBJECTS = authd.lo bandbi.lo blacklist.lo cache.lo \
extban.lo getopt.lo hash.lo hook.lo hostmask.lo \
ipv4_from_ipv6.lo irc_radixtree.lo irc_dictionary.lo ircd.lo \
ircd_parser.lo ircd_lexer.lo ircd_signal.lo listener.lo \
logger.lo match.lo modules.lo monitor.lo newconf.lo \
logger.lo match.lo modules.lo monitor.lo msgbuf.lo newconf.lo \
operhash.lo packet.lo parse.lo privilege.lo ratelimit.lo \
reject.lo restart.lo s_auth.lo s_conf.lo s_newconf.lo \
s_serv.lo s_user.lo scache.lo send.lo snomask.lo sslproc.lo \
@ -454,6 +454,7 @@ libircd_la_SOURCES = \
match.c \
modules.c \
monitor.c \
msgbuf.c \
newconf.c \
operhash.c \
packet.c \
@ -646,6 +647,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/match.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/modules.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/monitor.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/msgbuf.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/newconf.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/operhash.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/packet.Plo@am__quote@

104
ircd/msgbuf.c Normal file
View File

@ -0,0 +1,104 @@
/*
* charybdis - an advanced ircd.
* Copyright (c) 2016 William Pitcock <nenolod@dereferenced.org>.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice is present in all copies.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "stdinc.h"
#include "ircd_defs.h"
#include "msgbuf.h"
/*
* parse a message into a MsgBuf.
* returns 0 on success, 1 on error.
*/
int
msgbuf_parse(struct MsgBuf *msgbuf, char *line)
{
char *ch;
char *parv[MAXPARA];
size_t n_para;
int i;
/* skip any leading spaces */
for (ch = line; *ch && *ch == ' '; ch++)
;
msgbuf_init(msgbuf);
if (*ch == '@')
{
char *t = ch++;
ch = strchr(ch, ' ');
if (ch != NULL)
{
while (t < ch)
{
char *next = strchr(t, ';');
char *eq = strchr(t, '=');
if (next != NULL)
*next = '\0';
if (eq > next)
eq = NULL;
if (eq != NULL)
*eq = '\0';
msgbuf_append_tag(msgbuf, t, eq);
t = next + 1;
}
}
}
/* skip any whitespace between tags and origin */
for (; *ch && *ch == ' '; ch++)
;
if (*ch == ':')
{
ch++;
msgbuf->origin = ch;
char *end = strchr(ch, ' ');
if (end == NULL)
return 1;
*end = '\0';
for (ch = end + 1; *ch && *ch == ' '; ch++)
;
}
if (*ch == '\0')
return 1;
n_para = rb_string_to_array(ch, parv, MAXPARA);
if (n_para == 0)
return 1;
msgbuf->cmd = parv[0];
msgbuf->n_para = n_para - 1;
for (i = 1; i < n_para; i++)
msgbuf_append_para(msgbuf, parv[i]);
return 0;
}