]> jfr.im git - solanum.git/blob - ircd/msgbuf.c
free server_p->certfp, allocated in newconf.c
[solanum.git] / ircd / msgbuf.c
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 #include "client.h"
26 #include "ircd.h"
27
28 /*
29 * parse a message into a MsgBuf.
30 * returns 0 on success, 1 on error.
31 */
32 int
33 msgbuf_parse(struct MsgBuf *msgbuf, char *line)
34 {
35 char *ch;
36 char *parv[MAXPARA + 1];
37 size_t n_para;
38
39 /* skip any leading spaces */
40 for (ch = line; *ch && *ch == ' '; ch++)
41 ;
42
43 msgbuf_init(msgbuf);
44
45 if (*ch == '@')
46 {
47 char *t = ch + 1;
48
49 ch = strchr(ch, ' ');
50 if (ch != NULL)
51 {
52 while (1)
53 {
54 char *next = strchr(t, ';');
55 char *eq = strchr(t, '=');
56
57 if (next != NULL)
58 {
59 *next = '\0';
60
61 if (eq > next)
62 eq = NULL;
63 }
64
65 if (eq != NULL)
66 *eq++ = '\0';
67
68 if (*t && *t != ' ')
69 msgbuf_append_tag(msgbuf, t, eq, 0);
70 else
71 break;
72
73 if (next != NULL)
74 t = next + 1;
75 else
76 break;
77 }
78
79 *ch++ = '\0';
80 }
81 else
82 ch = t;
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];
112 for (size_t i = 0; i < n_para; i++)
113 msgbuf_append_para(msgbuf, parv[i]);
114
115 return 0;
116 }
117
118 static int
119 msgbuf_has_matching_tags(struct MsgBuf *msgbuf, unsigned int capmask)
120 {
121 for (size_t i = 0; i < msgbuf->n_tags; i++)
122 {
123 if ((msgbuf->tags[i].capmask & capmask) != 0)
124 return 1;
125 }
126
127 return 0;
128 }
129
130 static void
131 msgbuf_unparse_tags(char *buf, size_t buflen, struct MsgBuf *msgbuf, unsigned int capmask)
132 {
133 if (!msgbuf_has_matching_tags(msgbuf, capmask))
134 return;
135
136 *buf = '@';
137
138 for (size_t i = 0; i < msgbuf->n_tags; i++)
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
159 void
160 msgbuf_unparse_prefix(char *buf, size_t buflen, struct MsgBuf *msgbuf, unsigned int capmask)
161 {
162 memset(buf, 0, buflen);
163
164 if (msgbuf->n_tags > 0)
165 msgbuf_unparse_tags(buf, buflen, msgbuf, capmask);
166
167 rb_snprintf_append(buf, buflen, ":%s ", msgbuf->origin != NULL ? msgbuf->origin : me.name);
168
169 if (msgbuf->cmd != NULL)
170 rb_snprintf_append(buf, buflen, "%s ", msgbuf->cmd);
171
172 if (msgbuf->target != NULL)
173 rb_snprintf_append(buf, buflen, "%s ", msgbuf->target);
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 */
182 int
183 msgbuf_unparse(char *buf, size_t buflen, struct MsgBuf *msgbuf, unsigned int capmask)
184 {
185 msgbuf_unparse_prefix(buf, buflen, msgbuf, capmask);
186
187 for (size_t i = msgbuf->cmd != NULL ? 0 : 1; i < msgbuf->n_para; i++)
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 }
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 */
209 int
210 msgbuf_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 */
230 int
231 msgbuf_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 }