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