Debian Codegraph @

modules/transport/rfc1459/parse.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 #include "rfc1459.h"
28
29 /* parses a standard 2.8.21 style IRC stream */
30 void irc_parse(char *line)
31 {
32 sourceinfo_t *si;
33 char *pos;
34 char *origin = NULL;
35 char *command = NULL;
36 char *message = NULL;
37 char *parv[MAXPARC + 1];
38 static char coreLine[BUFSIZE];
39 int parc = 0;
40 unsigned int i;
41 pcommand_t *pcmd;
42
43 /* clear the parv */
44 for (i = 0; i <= MAXPARC; i++)
45 parv[i] = NULL;
46
47 si = sourceinfo_create();
48 si->connection = curr_uplink->conn;
49 si->output_limit = MAX_IRC_OUTPUT_LINES;
50
51 if (line != NULL)
52 {
53 /* sometimes we'll get a blank line with just a \n on it...
54 * catch those here... they'll core us later on if we don't
55 */
56 if (*line == '\n')
57 goto cleanup;
58 if (*line == '\000')
59 goto cleanup;
60
61 /* copy the original line so we know what we crashed on */
62 memset((char *)&coreLine, '\0', BUFSIZE);
63 mowgli_strlcpy(coreLine, line, BUFSIZE);
64
65 slog(LG_RAWDATA, "-> %s", line);
66
67 /* find the first space */
68 if ((pos = strchr(line, ' ')))
69 {
70 *pos = '\0';
71 pos++;
72 /* if it starts with a : we have a prefix/origin
73 * pull the origin off into `origin', and have pos for the
74 * command, message will be the part afterwards
75 */
76 if (*line == ':')
77 {
78 origin = line + 1;
79
80 si->s = server_find(origin);
81 si->su = user_find(origin);
82
83 if ((message = strchr(pos, ' ')))
84 {
85 *message = '\0';
86 message++;
87 command = pos;
88 }
89 else
90 {
91 command = pos;
92 message = NULL;
93 }
94 }
95 else
96 {
97 if (me.recvsvr)
98 {
99 origin = me.actual;
100 si->s = server_find(origin);
101 }
102 message = pos;
103 command = line;
104 }
105 }
106 else
107 {
108 if (me.recvsvr)
109 {
110 origin = me.actual;
111 si->s = server_find(origin);
112 }
113 command = line;
114 message = NULL;
115 }
116 if (!si->s && !si->su && me.recvsvr)
117 {
118 slog(LG_DEBUG, "irc_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, "irc_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, "irc_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 /* take the command through the hash table */
152 if ((pcmd = pcommand_find(command)))
153 {
154 if (si->su && !(pcmd->sourcetype & MSRC_USER))
155 {
156 slog(LG_INFO, "irc_parse(): user %s sent disallowed command %s", si->su->nick, pcmd->token);
157 goto cleanup;
158 }
159 else if (si->s && !(pcmd->sourcetype & MSRC_SERVER))
160 {
161 slog(LG_INFO, "irc_parse(): server %s sent disallowed command %s", si->s->name, pcmd->token);
162 goto cleanup;
163 }
164 else if (!me.recvsvr && !(pcmd->sourcetype & MSRC_UNREG))
165 {
166 slog(LG_INFO, "irc_parse(): unregistered server sent disallowed command %s", pcmd->token);
167 goto cleanup;
168 }
169 if (parc < pcmd->minparc)
170 {
171 slog(LG_INFO, "irc_parse(): insufficient parameters for command %s", pcmd->token);
172 goto cleanup;
173 }
174 if (pcmd->handler)
175 {
176 pcmd->handler(si, parc, parv);
177 }
178 }
179 }
180
181 cleanup:
182 object_unref(si);
183 }
184
185 /* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs
186 * vim:ts=8
187 * vim:sw=8
188 * vim:noexpandtab
189 */