]> jfr.im git - solanum.git/blame - ircd/msgbuf.c
make build reproducible
[solanum.git] / ircd / msgbuf.c
CommitLineData
a8e69f5d
AC
1/*
2 * charybdis - an advanced ircd.
3 * Copyright (c) 2016 William Pitcock <nenolod@dereferenced.org>.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice is present in all copies.
8 *
9 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
10 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
11 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
12 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
13 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
14 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
15 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
16 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
17 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
18 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
19 * POSSIBILITY OF SUCH DAMAGE.
20 */
21
22#include "stdinc.h"
23#include "ircd_defs.h"
24#include "msgbuf.h"
25
26/*
27 * parse a message into a MsgBuf.
28 * returns 0 on success, 1 on error.
29 */
30int
31msgbuf_parse(struct MsgBuf *msgbuf, char *line)
32{
33 char *ch;
34 char *parv[MAXPARA];
35 size_t n_para;
36 int i;
37
38 /* skip any leading spaces */
39 for (ch = line; *ch && *ch == ' '; ch++)
40 ;
41
42 msgbuf_init(msgbuf);
43
44 if (*ch == '@')
45 {
08006c16 46 char *t = ch + 1;
a8e69f5d
AC
47
48 ch = strchr(ch, ' ');
49 if (ch != NULL)
50 {
269dd686 51 while (1)
a8e69f5d
AC
52 {
53 char *next = strchr(t, ';');
54 char *eq = strchr(t, '=');
55
56 if (next != NULL)
08006c16 57 {
a8e69f5d
AC
58 *next = '\0';
59
08006c16
AC
60 if (eq > next)
61 eq = NULL;
62 }
a8e69f5d
AC
63
64 if (eq != NULL)
08006c16 65 *eq++ = '\0';
a8e69f5d 66
08006c16 67 if (*t && *t != ' ')
d670fe52 68 msgbuf_append_tag(msgbuf, t, eq, 0);
08006c16
AC
69 else
70 break;
269dd686
AC
71
72 if (next != NULL)
73 t = next + 1;
74 else
75 break;
a8e69f5d 76 }
08006c16
AC
77
78 *ch++ = '\0';
a8e69f5d
AC
79 }
80 }
81
82 /* skip any whitespace between tags and origin */
83 for (; *ch && *ch == ' '; ch++)
84 ;
85
86 if (*ch == ':')
87 {
88 ch++;
89 msgbuf->origin = ch;
90
91 char *end = strchr(ch, ' ');
92 if (end == NULL)
93 return 1;
94
95 *end = '\0';
96
97 for (ch = end + 1; *ch && *ch == ' '; ch++)
98 ;
99 }
100
101 if (*ch == '\0')
102 return 1;
103
104 n_para = rb_string_to_array(ch, parv, MAXPARA);
105 if (n_para == 0)
106 return 1;
107
108 msgbuf->cmd = parv[0];
269dd686 109 for (i = 0; i < n_para; i++)
a8e69f5d
AC
110 msgbuf_append_para(msgbuf, parv[i]);
111
112 return 0;
113}