author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Sat, 13 Dec 2008 15:40:22 +0100 | |
changeset 12 | 213c3d4abc66 |
parent 11 | a769385a59c6 |
child 13 | d3554afaa768 |
permissions | -rw-r--r-- |
0 | 1 |
/** |
2 |
* $Id: parse.c 153 2008-04-27 07:26:15Z mbroeker $ |
|
3 |
* $URL: http://localhost/svn/c/mcbot/trunk/src/parse.c $ |
|
4 |
* |
|
5 |
*/ |
|
6 |
||
7 |
#include <stdio.h> |
|
8 |
#include <stdlib.h> |
|
9 |
#include <string.h> |
|
10 |
#include <time.h> |
|
11 |
||
12 |
#include <mcbot.h> |
|
13 |
#include <database.h> |
|
14 |
||
15 |
#include <locale.h> |
|
16 |
#include <libintl.h> |
|
17 |
||
18 |
#ifndef DATABASE_FILE |
|
19 |
#define DATABASE_FILE "/var/lib/nobody/data/mcbot.dat" |
|
20 |
#endif |
|
21 |
||
22 |
const |
|
23 |
char *COMMAND_LIST[] = { |
|
24 |
"!help Known Commands: join(1), leave(2), add(3), replace(4), delete(5), list(6), search(7), info(8)\r\n", |
|
25 |
"!join: Joins a new channel\r\n", |
|
26 |
"!leave: Parts from the current channel\r\n", |
|
27 |
"!add: adds an entry\r\n", |
|
28 |
"!replace: replaces an entry\r\n", |
|
29 |
"!delete: deletes an entry\r\n", |
|
30 |
"!list: lists the number of stored values\r\n", |
|
31 |
"!search: searches an entry up\r\n", |
|
32 |
"!info: Prints the current Bot-Version\r\n", |
|
33 |
"!ping: pings an host\r\n", |
|
34 |
"!on: enables autolearning mode\r\n", |
|
35 |
"!off: disables autolearning\r\n", |
|
36 |
"!debug: prints some debug infos\r\n", |
|
37 |
"!vaccuum: reorganizes the database\r\n", |
|
38 |
"!logout: Protected logout function\r\n", |
|
39 |
}; |
|
40 |
||
41 |
const |
|
42 |
char ITEMS = 14; |
|
43 |
||
44 |
const |
|
45 |
char *Bot_Commands[] = { |
|
46 |
"!help", "!join", "!leave", |
|
47 |
"!add", "!replace", "!delete", |
|
48 |
"!list", "!search", "!info", |
|
49 |
"!ping", "!on", "!off", |
|
50 |
"!debug", "!vaccuum", "!logout", |
|
51 |
"!who", "!whois", "!time", "!tell", |
|
11
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
52 |
"!op", NULL |
0 | 53 |
}; |
54 |
||
8
3152de03758e
A critical change was introduced here...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
3
diff
changeset
|
55 |
char *parse (MSG * message) |
0 | 56 |
{ |
57 |
static char msg[513]; |
|
58 |
int cmd = -1; |
|
59 |
int i; |
|
60 |
char *token; |
|
61 |
char *parameters; |
|
62 |
||
63 |
time_t t; |
|
64 |
struct tm *timeptr; |
|
65 |
static int counter = 0; |
|
66 |
||
67 |
/* |
|
68 |
* default message |
|
69 |
*/ |
|
70 |
snprintf (msg, 512, "PRIVMSG %s :%s.\r\n", message->channel, gettext ("Request cannot be performed")); |
|
71 |
||
72 |
/* |
|
73 |
* PRIVATE MESSAGES |
|
74 |
*/ |
|
75 |
if (!strcmp (message->channel, message->nick)) |
|
76 |
message->channel = message->user; |
|
77 |
||
78 |
if (strstr (message->line, message->nick)) { |
|
79 |
if (*message->line != '!') { |
|
80 |
/* |
|
81 |
* DEAD - LOCK - CHECK |
|
82 |
*/ |
|
83 |
if (strcmp (message->user, message->nick)) { |
|
84 |
snprintf (msg, 512, "PRIVMSG %s :%s, %s?\r\n", message->channel, gettext ("What's up"), message->user); |
|
85 |
if (counter++ > 3) |
|
86 |
return NULL; |
|
87 |
return msg; |
|
88 |
} |
|
89 |
/* |
|
90 |
* DEAD - LOCK - CHECK ENDS |
|
91 |
*/ |
|
92 |
} |
|
93 |
return NULL; |
|
94 |
} |
|
95 |
||
96 |
counter = 0; |
|
97 |
||
98 |
/* |
|
99 |
* NO BOT Commands |
|
100 |
*/ |
|
101 |
if (*message->line != '!') { |
|
102 |
return NULL; |
|
103 |
} |
|
104 |
||
105 |
i = 0; |
|
106 |
token = strtok (message->line, " "); |
|
107 |
while (Bot_Commands[i]) { |
|
108 |
if (!strcmp (token, Bot_Commands[i])) { |
|
109 |
switch (i) { |
|
110 |
case 0: /* !help */ |
|
111 |
if ((token = strtok (NULL, "\r\n"))) |
|
112 |
cmd = atoi (token); |
|
113 |
if ((cmd > 0) && (cmd < ITEMS)) |
|
114 |
snprintf (msg, 512, "PRIVMSG %s :%s\r\n", message->channel, COMMAND_LIST[cmd]); |
|
115 |
else |
|
116 |
snprintf (msg, 512, "PRIVMSG %s :%s\r\n", message->channel, COMMAND_LIST[0]); |
|
117 |
return msg; |
|
118 |
||
119 |
case 1: /* !join */ |
|
120 |
if ((token = strtok (NULL, "\r\n"))) |
|
121 |
snprintf (msg, 512, "JOIN %s\r\n", token); |
|
122 |
return msg; |
|
123 |
||
124 |
case 2: /* !leave */ |
|
125 |
if (*message->channel != '#') |
|
126 |
return NULL; |
|
127 |
snprintf (msg, 512, "PART %s :Leaving.\r\n", message->channel); |
|
128 |
return msg; |
|
129 |
||
130 |
case 3: /* !add */ |
|
131 |
if ((token = strtok (NULL, " "))) { |
|
132 |
if ((parameters = strtok (NULL, "\r\n"))) { |
|
133 |
snprintf (msg, 512, "PRIVMSG %s :%s, %s\r\n", |
|
134 |
message->channel, message->user, db_insert (DATABASE_FILE, token, parameters, 0)); |
|
135 |
} else { |
|
3
a82a978f23f7
unreachable code removed and gitignore added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
136 |
snprintf (msg, 512, "PRIVMSG %s :%s, %s!\r\n", message->channel, |
a82a978f23f7
unreachable code removed and gitignore added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
137 |
gettext ("I need more parameters to add"), message->user); |
0 | 138 |
} |
139 |
} |
|
140 |
return msg; |
|
141 |
||
142 |
case 4: /* !replace */ |
|
143 |
if ((token = strtok (NULL, " "))) { |
|
144 |
if ((parameters = strtok (NULL, "\r\n"))) { |
|
145 |
snprintf (msg, 512, "PRIVMSG %s :%s, %s\r\n", |
|
146 |
message->channel, message->user, db_insert (DATABASE_FILE, token, parameters, 1)); |
|
147 |
} else { |
|
3
a82a978f23f7
unreachable code removed and gitignore added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
148 |
snprintf (msg, 512, "PRIVMSG %s :%s, %s!\r\n", message->channel, |
a82a978f23f7
unreachable code removed and gitignore added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
149 |
gettext ("I need more parameters to replace"), message->user); |
0 | 150 |
} |
151 |
} |
|
152 |
return msg; |
|
153 |
||
154 |
case 5: /* !delete */ |
|
155 |
if ((token = strtok (NULL, "\r\n"))) { |
|
156 |
snprintf (msg, 512, "PRIVMSG %s :%s, %s\r\n", |
|
157 |
message->channel, message->user, db_remove (DATABASE_FILE, token)); |
|
158 |
} else { |
|
3
a82a978f23f7
unreachable code removed and gitignore added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
159 |
snprintf (msg, 512, "PRIVMSG %s :%s, %s!\r\n", message->channel, gettext ("I need a key to delete"), |
a82a978f23f7
unreachable code removed and gitignore added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
160 |
message->user); |
0 | 161 |
} |
162 |
return msg; |
|
163 |
||
164 |
case 6: /* !count */ |
|
3
a82a978f23f7
unreachable code removed and gitignore added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
165 |
snprintf (msg, 512, "PRIVMSG %s :%s %s\r\n", message->channel, db_elements (DATABASE_FILE), |
a82a978f23f7
unreachable code removed and gitignore added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
166 |
db_lookup (DATABASE_FILE, "mcbot.cgi")); |
0 | 167 |
return msg; |
168 |
||
169 |
case 7: /* !search */ |
|
170 |
if ((token = strtok (NULL, "\r\n"))) { |
|
3
a82a978f23f7
unreachable code removed and gitignore added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
171 |
snprintf (msg, 512, "PRIVMSG %s :%s, %s\r\n", message->channel, message->user, |
a82a978f23f7
unreachable code removed and gitignore added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
172 |
db_lookup (DATABASE_FILE, token)); |
0 | 173 |
} else { |
174 |
snprintf (msg, 512, |
|
3
a82a978f23f7
unreachable code removed and gitignore added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
175 |
"PRIVMSG %s :%s, %s!\r\n", message->channel, gettext ("I need a key to lookup"), |
a82a978f23f7
unreachable code removed and gitignore added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
176 |
message->user); |
0 | 177 |
} |
178 |
return msg; |
|
179 |
||
180 |
case 8: /* !info */ |
|
3
a82a978f23f7
unreachable code removed and gitignore added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
181 |
snprintf (msg, 512, "PRIVMSG %s :I am MCBot-%1.2f and my source code can be found on %s\r\n", |
0 | 182 |
message->channel, VERSION, "http://largo.homelinux.org/svn/c/mcbot/trunk"); |
183 |
return msg; |
|
184 |
||
185 |
case 9: /* !ping */ |
|
186 |
if ((token = strtok (NULL, "\r\n"))) |
|
187 |
snprintf (msg, 512, "PRIVMSG %s :PING 0815\r\n", token); |
|
188 |
return msg; |
|
189 |
||
190 |
case 10: /* !on */ |
|
3
a82a978f23f7
unreachable code removed and gitignore added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
191 |
snprintf (msg, 512, "PRIVMSG %s :%s %s.\r\n", message->user, gettext ("Autolearn enabled for channel"), |
a82a978f23f7
unreachable code removed and gitignore added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
192 |
message->channel); |
0 | 193 |
return msg; |
194 |
||
195 |
case 11: /* !off */ |
|
3
a82a978f23f7
unreachable code removed and gitignore added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
196 |
snprintf (msg, 512, "PRIVMSG %s :%s %s.\r\n", message->user, gettext ("Autolearn disabled for channel"), |
a82a978f23f7
unreachable code removed and gitignore added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
197 |
message->channel); |
0 | 198 |
return msg; |
199 |
||
200 |
case 12: /* !debug */ |
|
3
a82a978f23f7
unreachable code removed and gitignore added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
201 |
snprintf (msg, 512, "PRIVMSG %s :USER: %s EMAIL: %s CHANNEL: %s LINE: %s\r\n", message->channel, |
a82a978f23f7
unreachable code removed and gitignore added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
202 |
message->user, message->email, message->channel, message->line); |
0 | 203 |
return msg; |
204 |
||
205 |
case 13: /* !vaccum */ |
|
206 |
snprintf (msg, 512, "PRIVMSG %s :%s\r\n", message->channel, db_vaccuum (DATABASE_FILE)); |
|
207 |
return msg; |
|
208 |
||
209 |
case 14: /* !logout */ |
|
3
a82a978f23f7
unreachable code removed and gitignore added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
210 |
if (strstr (message->user, db_lookup (DATABASE_FILE, "mcbot.user"))) { |
a82a978f23f7
unreachable code removed and gitignore added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
211 |
if (strstr (message->email, db_lookup (DATABASE_FILE, "mcbot.email"))) { |
a82a978f23f7
unreachable code removed and gitignore added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
212 |
snprintf (msg, 512, "PRIVMSG %s :%s!\r\nQUIT\r\n", message->channel, |
a82a978f23f7
unreachable code removed and gitignore added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
213 |
gettext ("Bye, have a nice day!")); |
0 | 214 |
} |
215 |
} |
|
216 |
/* |
|
217 |
* the returned message is either the default one or the generated one |
|
218 |
*/ |
|
219 |
return msg; |
|
220 |
||
221 |
case 15: /* !who */ |
|
222 |
if ((token = strtok (NULL, "\r\n")) != NULL) { |
|
223 |
snprintf (msg, 512, "WHO %s\r\n", token); |
|
224 |
} |
|
225 |
return msg; |
|
226 |
||
227 |
case 16: /* !whois */ |
|
228 |
if ((token = strtok (NULL, "\r\n")) != NULL) { |
|
229 |
snprintf (msg, 512, "WHOIS %s\r\n", token); |
|
230 |
} |
|
231 |
return msg; |
|
3
a82a978f23f7
unreachable code removed and gitignore added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
232 |
|
0 | 233 |
case 17: /* time */ |
234 |
t = time (NULL); |
|
235 |
timeptr = localtime (&t); |
|
236 |
if ((token = malloc (81))) { |
|
237 |
strftime (token, 80, "%I:%M:%S %p", timeptr); |
|
3
a82a978f23f7
unreachable code removed and gitignore added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
238 |
snprintf (msg, 512, "PRIVMSG %s :%s %s, %s!\r\n", message->channel, gettext ("It is"), token, |
a82a978f23f7
unreachable code removed and gitignore added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
239 |
message->user); |
0 | 240 |
free (token); |
241 |
} |
|
242 |
return msg; |
|
3
a82a978f23f7
unreachable code removed and gitignore added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
243 |
|
0 | 244 |
case 18: /* tell */ |
245 |
if ((token = strtok (NULL, " "))) { |
|
246 |
if ((parameters = strtok (NULL, "\r\n"))) { |
|
247 |
snprintf (msg, 512, "PRIVMSG %s :%s, %s\r\n", |
|
3
a82a978f23f7
unreachable code removed and gitignore added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
248 |
('*' == *token) ? ++token : message->channel, token, db_lookup (DATABASE_FILE, |
a82a978f23f7
unreachable code removed and gitignore added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
249 |
parameters)); |
0 | 250 |
} |
251 |
} |
|
252 |
return msg; |
|
3
a82a978f23f7
unreachable code removed and gitignore added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
253 |
|
0 | 254 |
case 19: /* op */ |
255 |
if ((token = strtok (NULL, "\r\n")) != NULL) { |
|
3
a82a978f23f7
unreachable code removed and gitignore added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
256 |
if (strstr (message->email, db_lookup (DATABASE_FILE, "mcbot.email"))) |
0 | 257 |
snprintf (msg, 512, "MODE %s +o %s\r\n", message->channel, token); |
258 |
} |
|
259 |
return msg; |
|
260 |
} |
|
261 |
} |
|
262 |
i++; |
|
263 |
} |
|
264 |
||
265 |
return NULL; |
|
266 |
} |