]> jfr.im git - solanum.git/blame - ircd/msgbuf.c
free server_p->certfp, allocated in newconf.c
[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"
691adddd
AC
25#include "client.h"
26#include "ircd.h"
a8e69f5d
AC
27
28/*
29 * parse a message into a MsgBuf.
30 * returns 0 on success, 1 on error.
31 */
32int
33msgbuf_parse(struct MsgBuf *msgbuf, char *line)
34{
35 char *ch;
3656fa83 36 char *parv[MAXPARA + 1];
a8e69f5d 37 size_t n_para;
a8e69f5d
AC
38
39 /* skip any leading spaces */
40 for (ch = line; *ch && *ch == ' '; ch++)
41 ;
42
43 msgbuf_init(msgbuf);
44
45 if (*ch == '@')
46 {
08006c16 47 char *t = ch + 1;
a8e69f5d
AC
48
49 ch = strchr(ch, ' ');
50 if (ch != NULL)
51 {
269dd686 52 while (1)
a8e69f5d
AC
53 {
54 char *next = strchr(t, ';');
55 char *eq = strchr(t, '=');
56
57 if (next != NULL)
08006c16 58 {
a8e69f5d
AC
59 *next = '\0';
60
08006c16
AC
61 if (eq > next)
62 eq = NULL;
63 }
a8e69f5d
AC
64
65 if (eq != NULL)
08006c16 66 *eq++ = '\0';
a8e69f5d 67
08006c16 68 if (*t && *t != ' ')
d670fe52 69 msgbuf_append_tag(msgbuf, t, eq, 0);
08006c16
AC
70 else
71 break;
269dd686
AC
72
73 if (next != NULL)
74 t = next + 1;
75 else
76 break;
a8e69f5d 77 }
08006c16
AC
78
79 *ch++ = '\0';
a8e69f5d 80 }
6d16f66b
AJ
81 else
82 ch = t;
a8e69f5d
AC
83 }
84
85 /* skip any whitespace between tags and origin */
86 for (; *ch && *ch == ' '; ch++)
87 ;
88
89 if (*ch == ':')
90 {
91 ch++;
92 msgbuf->origin = ch;
93
94 char *end = strchr(ch, ' ');
95 if (end == NULL)
96 return 1;
97
98 *end = '\0';
99
100 for (ch = end + 1; *ch && *ch == ' '; ch++)
101 ;
102 }
103
104 if (*ch == '\0')
105 return 1;
106
107 n_para = rb_string_to_array(ch, parv, MAXPARA);
108 if (n_para == 0)
109 return 1;
110
111 msgbuf->cmd = parv[0];
66769bc1 112 for (size_t i = 0; i < n_para; i++)
a8e69f5d
AC
113 msgbuf_append_para(msgbuf, parv[i]);
114
115 return 0;
116}
4a13e3f1 117
4d2f2030
AC
118static int
119msgbuf_has_matching_tags(struct MsgBuf *msgbuf, unsigned int capmask)
120{
66769bc1 121 for (size_t i = 0; i < msgbuf->n_tags; i++)
4d2f2030
AC
122 {
123 if ((msgbuf->tags[i].capmask & capmask) != 0)
124 return 1;
125 }
126
127 return 0;
128}
129
4a13e3f1
AC
130static void
131msgbuf_unparse_tags(char *buf, size_t buflen, struct MsgBuf *msgbuf, unsigned int capmask)
132{
4d2f2030
AC
133 if (!msgbuf_has_matching_tags(msgbuf, capmask))
134 return;
135
4a13e3f1
AC
136 *buf = '@';
137
66769bc1 138 for (size_t i = 0; i < msgbuf->n_tags; i++)
4a13e3f1
AC
139 {
140 if ((msgbuf->tags[i].capmask & capmask) == 0)
141 continue;
142
143 if (i != 0)
144 rb_strlcat(buf, ";", buflen);
145
146 rb_strlcat(buf, msgbuf->tags[i].key, buflen);
147
148 /* XXX properly handle escaping */
149 if (msgbuf->tags[i].value)
150 {
151 rb_strlcat(buf, "=", buflen);
152 rb_strlcat(buf, msgbuf->tags[i].value, buflen);
153 }
154 }
155
156 rb_strlcat(buf, " ", buflen);
157}
158
33085472 159void
4a13e3f1
AC
160msgbuf_unparse_prefix(char *buf, size_t buflen, struct MsgBuf *msgbuf, unsigned int capmask)
161{
4a13e3f1
AC
162 memset(buf, 0, buflen);
163
164 if (msgbuf->n_tags > 0)
165 msgbuf_unparse_tags(buf, buflen, msgbuf, capmask);
166
691adddd 167 rb_snprintf_append(buf, buflen, ":%s ", msgbuf->origin != NULL ? msgbuf->origin : me.name);
5559c3cf
AC
168
169 if (msgbuf->cmd != NULL)
170 rb_snprintf_append(buf, buflen, "%s ", msgbuf->cmd);
71c875fb
AC
171
172 if (msgbuf->target != NULL)
173 rb_snprintf_append(buf, buflen, "%s ", msgbuf->target);
4a13e3f1
AC
174}
175
176/*
177 * unparse a pure MsgBuf into a buffer.
178 * if origin is NULL, me.name will be used.
179 * cmd may not be NULL.
180 * returns 0 on success, 1 on error.
181 */
182int
183msgbuf_unparse(char *buf, size_t buflen, struct MsgBuf *msgbuf, unsigned int capmask)
184{
4a13e3f1
AC
185 msgbuf_unparse_prefix(buf, buflen, msgbuf, capmask);
186
66769bc1 187 for (size_t i = msgbuf->cmd != NULL ? 0 : 1; i < msgbuf->n_para; i++)
4a13e3f1
AC
188 {
189 if (i == (msgbuf->n_para - 1))
190 {
191 if (strchr(msgbuf->para[i], ' ') != NULL)
192 rb_snprintf_append(buf, buflen, ":%s", msgbuf->para[i]);
193 else
194 rb_strlcat(buf, msgbuf->para[i], buflen);
195 }
196 else
197 rb_strlcat(buf, msgbuf->para[i], buflen);
198 }
199
200 return 0;
201}
8f64d325
AC
202
203/*
204 * unparse a MsgBuf stem + format string into a buffer
205 * if origin is NULL, me.name will be used.
206 * cmd may not be NULL.
207 * returns 0 on success, 1 on error.
208 */
209int
210msgbuf_vunparse_fmt(char *buf, size_t buflen, struct MsgBuf *head, unsigned int capmask, const char *fmt, va_list va)
211{
212 char *ws;
213 size_t prefixlen;
214
215 msgbuf_unparse_prefix(buf, buflen, head, capmask);
216 prefixlen = strlen(buf);
217
218 ws = buf + prefixlen;
219 vsnprintf(ws, buflen - prefixlen, fmt, va);
220
221 return 0;
222}
223
224/*
225 * unparse a MsgBuf stem + format string into a buffer (with va_list handling)
226 * if origin is NULL, me.name will be used.
227 * cmd may not be NULL.
228 * returns 0 on success, 1 on error.
229 */
230int
231msgbuf_unparse_fmt(char *buf, size_t buflen, struct MsgBuf *head, unsigned int capmask, const char *fmt, ...)
232{
233 va_list va;
234 int res;
235
236 va_start(va, fmt);
237 res = msgbuf_vunparse_fmt(buf, buflen, head, capmask, fmt, va);
238 va_end(va);
239
240 return res;
241}