author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Sat, 13 Dec 2008 15:40:12 +0100 | |
changeset 10 | 311ea5fa60dd |
parent 8 | 3152de03758e |
child 11 | a769385a59c6 |
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", |
|
52 |
"!op", |
|
53 |
NULL, |
|
54 |
}; |
|
55 |
||
8
3152de03758e
A critical change was introduced here...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
3
diff
changeset
|
56 |
char *parse (MSG * message) |
0 | 57 |
{ |
58 |
static char msg[513]; |
|
59 |
int cmd = -1; |
|
60 |
int i; |
|
61 |
char *token; |
|
62 |
char *parameters; |
|
63 |
||
64 |
time_t t; |
|
65 |
struct tm *timeptr; |
|
66 |
static int counter = 0; |
|
67 |
||
68 |
/* |
|
69 |
* default message |
|
70 |
*/ |
|
71 |
snprintf (msg, 512, "PRIVMSG %s :%s.\r\n", message->channel, gettext ("Request cannot be performed")); |
|
72 |
||
73 |
/* |
|
74 |
* PRIVATE MESSAGES |
|
75 |
*/ |
|
76 |
if (!strcmp (message->channel, message->nick)) |
|
77 |
message->channel = message->user; |
|
78 |
||
79 |
if (strstr (message->line, message->nick)) { |
|
80 |
if (*message->line != '!') { |
|
81 |
/* |
|
82 |
* DEAD - LOCK - CHECK |
|
83 |
*/ |
|
84 |
if (strcmp (message->user, message->nick)) { |
|
85 |
snprintf (msg, 512, "PRIVMSG %s :%s, %s?\r\n", message->channel, gettext ("What's up"), message->user); |
|
86 |
if (counter++ > 3) |
|
87 |
return NULL; |
|
88 |
return msg; |
|
89 |
} |
|
90 |
/* |
|
91 |
* DEAD - LOCK - CHECK ENDS |
|
92 |
*/ |
|
93 |
} |
|
94 |
return NULL; |
|
95 |
} |
|
96 |
||
97 |
counter = 0; |
|
98 |
||
99 |
/* |
|
100 |
* NO BOT Commands |
|
101 |
*/ |
|
102 |
if (*message->line != '!') { |
|
103 |
return NULL; |
|
104 |
} |
|
105 |
||
106 |
i = 0; |
|
107 |
token = strtok (message->line, " "); |
|
108 |
while (Bot_Commands[i]) { |
|
109 |
if (!strcmp (token, Bot_Commands[i])) { |
|
110 |
switch (i) { |
|
111 |
case 0: /* !help */ |
|
112 |
if ((token = strtok (NULL, "\r\n"))) |
|
113 |
cmd = atoi (token); |
|
114 |
if ((cmd > 0) && (cmd < ITEMS)) |
|
115 |
snprintf (msg, 512, "PRIVMSG %s :%s\r\n", message->channel, COMMAND_LIST[cmd]); |
|
116 |
else |
|
117 |
snprintf (msg, 512, "PRIVMSG %s :%s\r\n", message->channel, COMMAND_LIST[0]); |
|
118 |
return msg; |
|
119 |
||
120 |
case 1: /* !join */ |
|
121 |
if ((token = strtok (NULL, "\r\n"))) |
|
122 |
snprintf (msg, 512, "JOIN %s\r\n", token); |
|
123 |
return msg; |
|
124 |
||
125 |
case 2: /* !leave */ |
|
126 |
if (*message->channel != '#') |
|
127 |
return NULL; |
|
128 |
snprintf (msg, 512, "PART %s :Leaving.\r\n", message->channel); |
|
129 |
return msg; |
|
130 |
||
131 |
case 3: /* !add */ |
|
132 |
if ((token = strtok (NULL, " "))) { |
|
133 |
if ((parameters = strtok (NULL, "\r\n"))) { |
|
134 |
snprintf (msg, 512, "PRIVMSG %s :%s, %s\r\n", |
|
135 |
message->channel, message->user, db_insert (DATABASE_FILE, token, parameters, 0)); |
|
136 |
} else { |
|
3
a82a978f23f7
unreachable code removed and gitignore added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
137 |
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
|
138 |
gettext ("I need more parameters to add"), message->user); |
0 | 139 |
} |
140 |
} |
|
141 |
return msg; |
|
142 |
||
143 |
case 4: /* !replace */ |
|
144 |
if ((token = strtok (NULL, " "))) { |
|
145 |
if ((parameters = strtok (NULL, "\r\n"))) { |
|
146 |
snprintf (msg, 512, "PRIVMSG %s :%s, %s\r\n", |
|
147 |
message->channel, message->user, db_insert (DATABASE_FILE, token, parameters, 1)); |
|
148 |
} else { |
|
3
a82a978f23f7
unreachable code removed and gitignore added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
149 |
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
|
150 |
gettext ("I need more parameters to replace"), message->user); |
0 | 151 |
} |
152 |
} |
|
153 |
return msg; |
|
154 |
||
155 |
case 5: /* !delete */ |
|
156 |
if ((token = strtok (NULL, "\r\n"))) { |
|
157 |
snprintf (msg, 512, "PRIVMSG %s :%s, %s\r\n", |
|
158 |
message->channel, message->user, db_remove (DATABASE_FILE, token)); |
|
159 |
} else { |
|
3
a82a978f23f7
unreachable code removed and gitignore added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
160 |
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
|
161 |
message->user); |
0 | 162 |
} |
163 |
return msg; |
|
164 |
||
165 |
case 6: /* !count */ |
|
3
a82a978f23f7
unreachable code removed and gitignore added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
166 |
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
|
167 |
db_lookup (DATABASE_FILE, "mcbot.cgi")); |
0 | 168 |
return msg; |
169 |
||
170 |
case 7: /* !search */ |
|
171 |
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
|
172 |
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
|
173 |
db_lookup (DATABASE_FILE, token)); |
0 | 174 |
} else { |
175 |
snprintf (msg, 512, |
|
3
a82a978f23f7
unreachable code removed and gitignore added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
176 |
"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
|
177 |
message->user); |
0 | 178 |
} |
179 |
return msg; |
|
180 |
||
181 |
case 8: /* !info */ |
|
3
a82a978f23f7
unreachable code removed and gitignore added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
182 |
snprintf (msg, 512, "PRIVMSG %s :I am MCBot-%1.2f and my source code can be found on %s\r\n", |
0 | 183 |
message->channel, VERSION, "http://largo.homelinux.org/svn/c/mcbot/trunk"); |
184 |
return msg; |
|
185 |
||
186 |
case 9: /* !ping */ |
|
187 |
if ((token = strtok (NULL, "\r\n"))) |
|
188 |
snprintf (msg, 512, "PRIVMSG %s :PING 0815\r\n", token); |
|
189 |
return msg; |
|
190 |
||
191 |
case 10: /* !on */ |
|
3
a82a978f23f7
unreachable code removed and gitignore added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
192 |
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
|
193 |
message->channel); |
0 | 194 |
return msg; |
195 |
||
196 |
case 11: /* !off */ |
|
3
a82a978f23f7
unreachable code removed and gitignore added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
197 |
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
|
198 |
message->channel); |
0 | 199 |
return msg; |
200 |
||
201 |
case 12: /* !debug */ |
|
3
a82a978f23f7
unreachable code removed and gitignore added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
202 |
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
|
203 |
message->user, message->email, message->channel, message->line); |
0 | 204 |
return msg; |
205 |
||
206 |
case 13: /* !vaccum */ |
|
207 |
snprintf (msg, 512, "PRIVMSG %s :%s\r\n", message->channel, db_vaccuum (DATABASE_FILE)); |
|
208 |
return msg; |
|
209 |
||
210 |
case 14: /* !logout */ |
|
3
a82a978f23f7
unreachable code removed and gitignore added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
211 |
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
|
212 |
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
|
213 |
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
|
214 |
gettext ("Bye, have a nice day!")); |
0 | 215 |
} |
216 |
} |
|
217 |
/* |
|
218 |
* the returned message is either the default one or the generated one |
|
219 |
*/ |
|
220 |
return msg; |
|
221 |
||
222 |
case 15: /* !who */ |
|
223 |
if ((token = strtok (NULL, "\r\n")) != NULL) { |
|
224 |
snprintf (msg, 512, "WHO %s\r\n", token); |
|
225 |
} |
|
226 |
return msg; |
|
227 |
||
228 |
case 16: /* !whois */ |
|
229 |
if ((token = strtok (NULL, "\r\n")) != NULL) { |
|
230 |
snprintf (msg, 512, "WHOIS %s\r\n", token); |
|
231 |
} |
|
232 |
return msg; |
|
3
a82a978f23f7
unreachable code removed and gitignore added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
233 |
|
0 | 234 |
case 17: /* time */ |
235 |
t = time (NULL); |
|
236 |
timeptr = localtime (&t); |
|
237 |
if ((token = malloc (81))) { |
|
238 |
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
|
239 |
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
|
240 |
message->user); |
0 | 241 |
free (token); |
242 |
} |
|
243 |
return msg; |
|
3
a82a978f23f7
unreachable code removed and gitignore added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
244 |
|
0 | 245 |
case 18: /* tell */ |
246 |
if ((token = strtok (NULL, " "))) { |
|
247 |
if ((parameters = strtok (NULL, "\r\n"))) { |
|
248 |
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
|
249 |
('*' == *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
|
250 |
parameters)); |
0 | 251 |
} |
252 |
} |
|
253 |
return msg; |
|
3
a82a978f23f7
unreachable code removed and gitignore added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
254 |
|
0 | 255 |
case 19: /* op */ |
256 |
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
|
257 |
if (strstr (message->email, db_lookup (DATABASE_FILE, "mcbot.email"))) |
0 | 258 |
snprintf (msg, 512, "MODE %s +o %s\r\n", message->channel, token); |
259 |
} |
|
260 |
return msg; |
|
261 |
} |
|
262 |
} |
|
263 |
i++; |
|
264 |
} |
|
265 |
||
266 |
return NULL; |
|
267 |
} |