Debian Codegraph @

modules/transport/p10.c

raw
1 /*
2 * atheme-services: A collection of minimalist IRC services
3 * parse.c: Parsing of IRC messages.
4 *
5 * Copyright (c) 2005-2007 Atheme Project (http://www.atheme.org)
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
12 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
13 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
14 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
15 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
16 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
17 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
19 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
20 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
21 * POSSIBILITY OF SUCH DAMAGE.
22 */
23
24 #include "atheme.h"
25 #include "uplink.h"
26 #include "pmodule.h"
27
28 DECLARE_MODULE_V1
29 (
30 "transport/p10", true, _modinit, _moddeinit,
31 PACKAGE_VERSION,
32 VENDOR_STRING
33 );
34
35 /* parses a P10 IRC stream */
36 static void p10_parse(char *line)
37 {
38 sourceinfo_t *si;
39 char *pos;
40 char *origin = NULL;
41 char *command = NULL;
42 char *message = NULL;
43 char *parv[MAXPARC + 1];
44 static char coreLine[BUFSIZE];
45 int parc = 0;
46 unsigned int i;
47 pcommand_t *pcmd;
48
49 /* clear the parv */
50 for (i = 0; i <= MAXPARC; i++)
51 parv[i] = NULL;
52
53 si = sourceinfo_create();
54 si->connection = curr_uplink->conn;
55 si->output_limit = MAX_IRC_OUTPUT_LINES;
56
57 if (line != NULL)
58 {
59 /* sometimes we'll get a blank line with just a \n on it...
60 * catch those here... they'll core us later on if we don't
61 */
62 if (*line == '\n')
63 goto cleanup;
64 if (*line == '\000')
65 goto cleanup;
66
67 /* copy the original line so we know what we crashed on */
68 memset((char *)&coreLine, '\0', BUFSIZE);
69 mowgli_strlcpy(coreLine, line, BUFSIZE);
70
71 slog(LG_RAWDATA, "-> %s", line);
72
73 /* find the first space */
74 if ((pos = strchr(line, ' ')))
75 {
76 *pos = '\0';
77 pos++;
78 /* if it starts with a : we have a prefix/origin
79 * pull the origin off into `origin', and have pos for the
80 * command, message will be the part afterwards
81 */
82 if (*line == ':' || me.recvsvr)
83 {
84 origin = line;
85 if (*origin == ':')
86 {
87 origin++;
88 si->s = server_find(origin);
89 si->su = user_find_named(origin);
90 }
91 else
92 {
93 si->s = server_find(origin);
94 si->su = user_find(origin);
95 }
96
97 if ((message = strchr(pos, ' ')))
98 {
99 *message = '\0';
100 message++;
101 command = pos;
102 }
103 else
104 {
105 command = pos;
106 message = NULL;
107 }
108 }
109 else
110 {
111 message = pos;
112 command = line;
113 }
114 }
115
116 if (!si->s && !si->su && me.recvsvr)
117 {
118 slog(LG_DEBUG, "p10_parse(): got message from nonexistent user or server: %s", origin);
119 goto cleanup;
120 }
121 if (si->s == me.me)
122 {
123 slog(LG_INFO, "p10_parse(): got message supposedly from myself %s: %s", si->s->name, coreLine);
124 goto cleanup;
125 }
126 if (si->su != NULL && si->su->server == me.me)
127 {
128 slog(LG_INFO, "p10_parse(): got message supposedly from my own client %s: %s", si->su->nick, coreLine);
129 goto cleanup;
130 }
131 si->smu = si->su != NULL ? si->su->myuser : NULL;
132
133 /* okay, the nasty part is over, now we need to make a
134 * parv out of what's left
135 */
136
137 if (message)
138 {
139 if (*message == ':')
140 {
141 message++;
142 parv[0] = message;
143 parc = 1;
144 }
145 else
146 parc = tokenize(message, parv);
147 }
148 else
149 parc = 0;
150
151 /* now we should have origin (or NULL), command, and a parv
152 * with it's accompanying parc... let's make ABSOLUTELY sure
153 */
154 if (!command)
155 {
156 slog(LG_DEBUG, "p10_parse(): command not found: %s", coreLine);
157 goto cleanup;
158 }
159
160 /* take the command through the hash table */
161 if ((pcmd = pcommand_find(command)))
162 {
163 if (si->su && !(pcmd->sourcetype & MSRC_USER))
164 {
165 slog(LG_INFO, "p10_parse(): user %s sent disallowed command %s", si->su->nick, pcmd->token);
166 goto cleanup;
167 }
168 else if (si->s && !(pcmd->sourcetype & MSRC_SERVER))
169 {
170 slog(LG_INFO, "p10_parse(): server %s sent disallowed command %s", si->s->name, pcmd->token);
171 goto cleanup;
172 }
173 else if (!me.recvsvr && !(pcmd->sourcetype & MSRC_UNREG))
174 {
175 slog(LG_INFO, "p10_parse(): unregistered server sent disallowed command %s", pcmd->token);
176 goto cleanup;
177 }
178 if (parc < pcmd->minparc)
179 {
180 slog(LG_INFO, "p10_parse(): insufficient parameters for command %s", pcmd->token);
181 goto cleanup;
182 }
183 if (pcmd->handler)
184 {
185 pcmd->handler(si, parc, parv);
186 }
187 }
188 }
189
190 cleanup:
191 object_unref(si);
192 }
193
194 void (*default_parse)(char *line) = NULL;
195
196 void _modinit(module_t *m)
197 {
198 MODULE_TRY_REQUEST_DEPENDENCY(m, "transport/rfc1459");
199
200 default_parse = parse;
201 parse = &p10_parse;
202 }
203
204 void _moddeinit(module_unload_intent_t intent)
205 {
206 parse = default_parse;
207 }
208
209 /* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs
210 * vim:ts=8
211 * vim:sw=8
212 * vim:noexpandtab
213 */