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