]>
Commit | Line | Data |
---|---|---|
2ace9480 | 1 | /* Thales - IRC to Relational Database Gateway |
2 | * Copyright (C) 2002 Lucas Nussbaum <lucas@lucas-nussbaum.net> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, write to the Free Software | |
16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
17 | */ | |
18 | ||
19 | #include "thales.h" | |
20 | #include "process.h" | |
21 | #include "messages.h" | |
22 | #include "memory.h" | |
23 | #include "log.h" | |
24 | #include "misc.h" | |
25 | ||
26 | extern Message messages[]; | |
27 | extern int verbose; | |
28 | extern char inbuf[BUFSIZE]; | |
29 | ||
30 | /*************************************************************************/ | |
31 | /*************************************************************************/ | |
32 | ||
33 | /* split_buf: Split a buffer into arguments and store the arguments in an | |
34 | * argument vector pointed to by argv (which will be malloc'd | |
35 | * as necessary); return the argument count. If colon_special | |
36 | * is non-zero, then treat a parameter with a leading ':' as | |
37 | * the last parameter of the line, per the IRC RFC. Destroys | |
38 | * the buffer by side effect. | |
39 | */ | |
40 | ||
41 | int split_buf(char *buf, char ***argv, int colon_special) | |
42 | { | |
43 | int argvsize = 8; | |
44 | int argc; | |
45 | char *s; | |
46 | ||
47 | *argv = (char **) scalloc(sizeof(char *) * argvsize, 1); | |
48 | argc = 0; | |
49 | while (*buf) | |
50 | { | |
51 | if (argc == argvsize) | |
52 | { | |
53 | argvsize += 8; | |
54 | *argv = (char **) srealloc(*argv, sizeof(char *) * argvsize); | |
55 | } | |
56 | if (*buf == ':') | |
57 | { | |
58 | (*argv)[argc++] = buf + 1; | |
59 | buf = ""; | |
60 | } | |
61 | else | |
62 | { | |
63 | s = strpbrk(buf, " "); | |
64 | if (s) | |
65 | { | |
66 | *s++ = 0; | |
67 | while (*s == ' ') | |
68 | s++; | |
69 | } | |
70 | else | |
71 | { | |
72 | s = buf + strlen(buf); | |
73 | } | |
74 | (*argv)[argc++] = buf; | |
75 | buf = s; | |
76 | } | |
77 | } | |
78 | return argc; | |
79 | } | |
80 | ||
81 | /*************************************************************************/ | |
82 | ||
83 | /* process: Main processing routine. Takes the string in inbuf (global | |
84 | * variable) and does something appropriate with it. */ | |
85 | ||
86 | void process() | |
87 | { | |
88 | char source[64]; | |
89 | char cmd[64]; | |
90 | char buf[512]; /* Longest legal IRC command line */ | |
91 | char *s; | |
92 | int ac; /* Parameters for the command */ | |
93 | char **av; | |
94 | Message *m; | |
95 | ||
96 | ||
97 | /* If debugging, log the buffer. */ | |
98 | if (verbose) | |
99 | log("<IRC : %s", inbuf); | |
100 | ||
101 | /* First make a copy of the buffer so we have the original in case we | |
102 | * crash - in that case, we want to know what we crashed on. */ | |
103 | strscpy(buf, inbuf, sizeof(buf)); | |
104 | ||
105 | /* Split the buffer into pieces. */ | |
106 | if (*buf == ':') | |
107 | { | |
108 | s = strpbrk(buf, " "); | |
109 | if (!s) | |
110 | return; | |
111 | *s = 0; | |
112 | while (isspace(*++s)) | |
113 | ; | |
114 | strscpy(source, buf + 1, sizeof(source)); | |
115 | memmove(buf, s, strlen(s) + 1); | |
116 | } | |
117 | else | |
118 | { | |
119 | *source = 0; | |
120 | } | |
121 | if (!*buf) | |
122 | return; | |
123 | s = strpbrk(buf, " "); | |
124 | if (s) | |
125 | { | |
126 | *s = 0; | |
127 | while (isspace(*++s)) | |
128 | ; | |
129 | } | |
130 | else | |
131 | s = buf + strlen(buf); | |
132 | strscpy(cmd, buf, sizeof(cmd)); | |
133 | ac = split_buf(s, &av, 1); | |
134 | ||
135 | /* Do something with the message. */ | |
136 | m = find_message(cmd); | |
137 | if (m) | |
138 | { | |
139 | if (m->func) | |
140 | m->func(source, ac, av); | |
141 | } | |
142 | else | |
143 | { | |
144 | log("IRC : unknown message from server (%s)", inbuf); | |
145 | } | |
146 | ||
147 | /* Free argument list we created */ | |
148 | free(av); | |
149 | } | |
150 | ||
151 | /*************************************************************************/ |