]> jfr.im git - irc/thales.git/blob - src/messages.c
cosmetics updates
[irc/thales.git] / src / messages.c
1 /* GNU 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 "messages.h"
21 #include "send.h"
22 #include "misc.h"
23 #include "compat.h"
24 #include "memory.h"
25 #include "log.h"
26 #include "actions.h"
27 #include "sockutil.h"
28
29 extern char *ServerName;
30 extern char *RemoteServer;
31 extern time_t start_time;
32
33 extern unsigned int nbusers;
34 extern unsigned int nbusers_max;
35 extern unsigned int nbchans;
36 extern unsigned int nbchans_max;
37
38 /* List of messages is at the bottom of the file. */
39
40 /* PING - handled */
41 static void m_ping(char *source, int ac, char **av)
42 {
43 if (ac < 1)
44 return;
45 send_cmd(ServerName, "PONG %s %s", ac > 1 ? av[1] : ServerName, av[0]);
46 }
47
48 /* SERVER - forwarded */
49 static void m_server(char *source, int ac, char **av)
50 {
51 if (*source)
52 {
53 do_server(av[0], av[2], source);
54 }
55 else
56 {
57 #ifdef IRCD_UNREAL
58 do_server(av[0], strchr(av[2], ' ') + 1, ServerName);
59 #else
60 do_server(av[0], av[2], ServerName);
61 #endif
62 }
63 }
64
65 /* SQUIT - forwarded */
66 static void m_squit(char *source, int ac, char **av)
67 {
68 do_squit(av[0]);
69 }
70
71 /* NICK - forwarded */
72 static void m_nick(char *source, int ac, char **av)
73 {
74 if (*source)
75 do_nick_chg(av[0], source);
76 else
77 do_nick_new(ac, av);
78 }
79
80 /* KICK - forwarded */
81 static void m_kick(char *source, int ac, char **av)
82 {
83 do_part(av[0], av[1]); /* kicks are handled like parts */
84 }
85
86 /* PART - forwarded */
87 static void m_part(char *source, int ac, char **av)
88 {
89 do_part(av[0], source);
90
91 }
92
93 /* ERROR - handled */
94 static void m_error(char *source, int ac, char **av)
95 {
96 fatal(av[0][0] == ':' ? av[0] + 1 : av[0]);
97 }
98
99 /* JOIN - forwarded */
100 static void m_join(char *source, int ac, char **av)
101 {
102 #ifdef IRCD_ULTI28
103 char *pos, *oldpos;
104 /* LAME ultimate 2.8 with multiple channels in the same JOIN ... */
105 oldpos = av[0];
106 while ((pos = strchr(oldpos, ',')) != NULL)
107 {
108 *pos = '\0';
109 do_join(oldpos, source);
110 oldpos = pos + 1;
111 }
112 do_join(oldpos, source);
113 #else
114 if (!strcmp(av[0], "0"))
115 do_partall(source);
116 else
117 do_join(av[0], source);
118 #endif
119 }
120
121 /* QUIT - forwarded */
122 static void m_quit(char *source, int ac, char **av)
123 {
124 do_quit(source);
125 }
126
127 /* SJOIN - forwarded */
128 static void m_sjoin(char *source, int ac, char **av)
129 {
130
131 #if defined(IRCD_SEQUANA)||defined(IRCD_BAHAMUT)||defined(IRCD_ULTIMATE)||defined(IRCD_IRCDRU)
132 if (ac == 2)
133 do_join(av[1], source); /* like a join */
134 else
135 #endif
136 do_sjoin(av[1], av[ac - 1], &av[2], (ac > 3) ? ac - 3 : 0);
137 }
138
139 /* SVSMODE - forwarded */
140 static void m_svsmode(char *source, int ac, char **av)
141 {
142 if (*av[0] != '#')
143 #if defined(IRCD_SEQUANA)||defined(IRCD_BAHAMUT)||defined(IRCD_ULTIMATE)||defined(IRCD_IRCDRU)||defined(IRCD_HYBRID)
144 do_umode(av[0], av[2]);
145 #elif defined(IRCD_UNREAL)||defined(IRCD_ULTI28)
146 do_umode(av[0], av[1]);
147 #endif
148 else
149 do_cmode(av[0], &av[1], ac - 2);
150 }
151
152 /* MODE - forwarded */
153 static void m_mode(char *source, int ac, char **av)
154 {
155 if (*av[0] != '#')
156 do_umode(av[0], av[1]);
157 else
158 do_cmode(av[0], &av[1], ac - 1);
159 }
160
161 /* TOPIC - forwarded */
162 static void m_topic(char *source, int ac, char **av)
163 {
164 #ifdef IRCD_HYBRID
165 do_topic(source, av[0], av[1]);
166 #else
167 do_topic(av[0], av[1], av[2], av[3]);
168 #endif /* IRCD_HYBRID */
169 }
170
171 /* TBURST - forwarded -iwes */
172 #ifdef IRCD_HYBRID
173 static void m_tburst(char *source, int ac, char **av)
174 {
175 do_tburst(av[1], av[2], av[3], av[4]);
176 }
177 #endif /* IRCD_HYBRID */
178
179 /* KILL - forwarded */
180 static void m_kill(char *source, int ac, char **av)
181 {
182 do_quit(av[0]);
183 }
184
185 /* AWAY - forwarded */
186 static void m_away(char *source, int ac, char **av)
187 {
188 if (!ac)
189 do_away_unset(source);
190 else
191 do_away_set(source, av[0]);
192 }
193
194 /* VERSION - handled */
195 static void m_version(char *source, int ac, char **av)
196 {
197 if (source)
198 send_cmd(ServerName, "351 %s GNU Thales %s %s",
199 source, VERSION, ServerName);
200 }
201
202 /* STATS - handled */
203 static void m_stats(char *source, int ac, char **av)
204 {
205 if (ac < 1)
206 return;
207 switch (*av[0])
208 {
209 case 'l':
210 send_cmd(NULL,
211 "211 %s Server SendBuf SentBytes SentMsgs RecvBuf RecvBytes RecvMsgs ConnTime",
212 source);
213 send_cmd(NULL, "211 %s %s %d %d %d %d %d %d %ld", source,
214 RemoteServer, write_buffer_len(), total_written, -1,
215 read_buffer_len(), total_read, -1, time(NULL) - start_time);
216 send_cmd(NULL, "219 %s l :End of /STATS report.", source);
217 break;
218 case 'u':
219 {
220 int uptime;
221 uptime = time(NULL) - start_time;
222 send_cmd(NULL, "242 %s :GNU Thales up %d day%s, %02d:%02d:%02d",
223 source, uptime / 86400, (uptime / 86400 == 1) ? "" : "s",
224 (uptime / 3600) % 24, (uptime / 60) % 60, uptime % 60);
225 send_cmd(NULL, "219 %s u :End of /STATS report.", source);
226 break;
227 }
228 default:
229 send_cmd(NULL, "219 %s %c :End of /STATS report.", source, *av[0]);
230 break;
231 }
232 }
233
234 /* LUSERS - forwarded */
235 static void m_lusers(char *source, int ac, char **av)
236 {
237 do_lusers(source);
238 }
239
240 /* MOTD - handled */
241 static void m_motd(char *source, int ac, char **av)
242 {
243 send_cmd(ServerName, "375 %s :- %s Message of the Day", source,
244 ServerName);
245 send_cmd(ServerName, "372 %s :- _____ _ _", source);
246 send_cmd(ServerName, "372 %s :-|_ _| |__ __ _| | ___ ___", source);
247 send_cmd(ServerName, "372 %s :- | | | '_ \\ / _` | |/ _ \\/ __|",
248 source);
249 send_cmd(ServerName, "372 %s :- | | | | | | (_| | | __/\\__ \\",
250 source);
251 send_cmd(ServerName, "372 %s :- |_| |_| |_|\\__,_|_|\\___||___/",
252 source);
253 send_cmd(ServerName, "372 %s :- v. %s", source, VERSION);
254 send_cmd(ServerName, "372 %s :- (c)2002-2004 Lucas Nussbaum", source);
255 send_cmd(ServerName, "372 %s :-", source);
256 send_cmd(ServerName, "372 %s :-GNU Thales is an IRC to MySQL gateway.",
257 source);
258 send_cmd(ServerName, "372 %s :- More info is available on", source);
259 send_cmd(ServerName, "372 %s :- http://www.gnu.org/software/thales/",
260 source);
261 send_cmd(ServerName, "376 %s :End of /MOTD command.", source);
262 }
263
264 /* ADMIN - handled */
265 static void m_admin(char *source, int ac, char **av)
266 {
267 send_cmd(ServerName, "256 %s :Administrative info about %s", source,
268 ServerName);
269 send_cmd(ServerName, "257 %s :thales-%s : IRC to MySQL Gateway", source,
270 VERSION);
271 send_cmd(ServerName,
272 "258 %s :Written by Lucas Nussbaum <lucas@lucas-nussbaum.net>",
273 source);
274 send_cmd(ServerName,
275 "259 %s :See http://www.gnu.org/software/thales/ for more info.",
276 source);
277 }
278
279 /* TIME - handled */
280 static char *months[] = {
281 "January", "February", "March", "April",
282 "May", "June", "July", "August",
283 "September", "October", "November", "December"
284 };
285
286 static char *weekdays[] = {
287 "Sunday", "Monday", "Tuesday", "Wednesday",
288 "Thursday", "Friday", "Saturday"
289 };
290
291 static void m_time(char *source, int ac, char **av)
292 {
293 static char buf[80], plus;
294 struct tm *lt, *gm;
295 struct tm gmbuf;
296 int minswest;
297 time_t clock;
298
299 time(&clock);
300 gm = gmtime(&clock);
301 memcpy((char *) &gmbuf, (char *) gm, sizeof(gmbuf));
302 gm = &gmbuf;
303 lt = localtime(&clock);
304
305 if (lt->tm_yday == gm->tm_yday)
306 minswest =
307 (gm->tm_hour - lt->tm_hour) * 60 + (gm->tm_min - lt->tm_min);
308 else if (lt->tm_yday > gm->tm_yday)
309 minswest = (gm->tm_hour - (lt->tm_hour + 24)) * 60;
310 else
311 minswest = ((gm->tm_hour + 24) - lt->tm_hour) * 60;
312
313 plus = (minswest > 0) ? '-' : '+';
314 if (minswest < 0)
315 minswest = -minswest;
316
317 (void) snprintf(buf, sizeof(buf),
318 "%s %s %d %04d -- %02d:%02d %c%02d:%02d",
319 weekdays[lt->tm_wday], months[lt->tm_mon], lt->tm_mday,
320 lt->tm_year + 1900, lt->tm_hour, lt->tm_min, plus,
321 minswest / 60, minswest % 60);
322
323 send_cmd(ServerName, "391 %s %s :%s", source, ServerName, buf);
324 }
325
326
327 /*
328 * IRCD specific commands
329 */
330
331 #if defined(IRCD_UNREAL)||defined(IRCD_ULTI28)
332
333 /* CHGHOST - forwarded */
334 static void m_chghost(char *source, int ac, char **av)
335 {
336 do_defhost(av[0], av[1]);
337 }
338
339 /* CHGNAME - forwarded */
340 static void m_chgname(char *source, int ac, char **av)
341 {
342 do_defname(av[0], av[1]);
343 }
344
345 /* CHGIDENT - forwarded */
346 static void m_chgident(char *source, int ac, char **av)
347 {
348 do_defname(av[0], av[1]);
349 }
350
351 /* SETNAME - forwarded */
352 static void m_setname(char *source, int ac, char **av)
353 {
354 do_defname(source, av[0]);
355 }
356
357 /* SETIDENT - forwarded */
358 static void m_setident(char *source, int ac, char **av)
359 {
360 do_defident(source, av[0]);
361 }
362
363 #endif
364
365 #ifdef IRCD_UNREAL
366
367 /* SDESC - forwarded */
368 static void m_sdesc(char *source, int ac, char **av)
369 {
370 do_sdesc(source, av[0]);
371 }
372 #endif
373
374 #if defined(IRCD_UNREAL)||defined(IRCD_ULTI28)
375 /* SETHOST - forwarded */
376 static void m_sethost(char *source, int ac, char **av)
377 {
378 do_defhost(source, av[0]);
379 }
380 #endif
381
382 #if defined(IRCD_ULTIMATE)
383
384 /* SETHOST - forwarded */
385 static void m_sethost(char *source, int ac, char **av)
386 {
387 do_defhost(av[0], av[1]);
388 }
389
390 #endif
391
392 #ifdef IRCD_HYBRID
393
394 /* REALHOST - forwarded */
395 static void m_realhost(char *source, int ac, char **av)
396 {
397 mylog("NBPARAMS : %d", ac);
398 if (ac > 1)
399 do_realhost(av[0], av[1]);
400 else
401 do_realhost(av[0], "");
402 }
403 #endif
404
405 /*************************************************************************/
406 Message messages[] = {
407
408 {"PASS", NULL}
409 ,
410 {"PING", m_ping}
411 ,
412 {"SERVER", m_server}
413 ,
414 {"VERSION", m_version}
415 ,
416 {"SQUIT", m_squit}
417 ,
418 {"NICK", m_nick}
419 ,
420 {"KILL", m_kill}
421 ,
422 {"SJOIN", m_sjoin}
423 ,
424 {"KICK", m_kick}
425 ,
426 {"SVSMODE", m_svsmode}
427 ,
428 {"PART", m_part}
429 ,
430 {"MODE", m_mode}
431 ,
432 {"TOPIC", m_topic}
433 ,
434 #ifdef IRCD_HYBRID
435 {"TBURST", m_tburst}
436 ,
437 #endif /* IRCD_HYBRID */
438 {"QUIT", m_quit}
439 ,
440 {"STATS", m_stats}
441 ,
442 {"JOIN", m_join}
443 ,
444 {"AWAY", m_away}
445 ,
446 {"ERROR", m_error}
447 ,
448 {"SQLINE", NULL}
449 ,
450 {"NOTICE", NULL}
451 ,
452 {"GNOTICE", NULL}
453 ,
454 {"SZLINE", NULL}
455 ,
456 {"CAPAB", NULL}
457 ,
458 {"SVINFO", NULL}
459 ,
460 {"SGLINE", NULL}
461 ,
462 {"UNSGLINE", NULL}
463 ,
464 {"UNSZLINE", NULL}
465 ,
466 {"AKILL", NULL}
467 ,
468 {"GLOBOPS", NULL}
469 ,
470 {"WALLOPS", NULL}
471 ,
472 {"SVSKILL", NULL}
473 ,
474 {"RAKILL", NULL}
475 ,
476 {"CHATOPS", NULL}
477 ,
478 {"LUSERS", m_lusers}
479 ,
480 {"ADMIN", m_admin}
481 ,
482 {"MOTD", m_motd}
483 ,
484 {"TIME", m_time}
485 ,
486
487 #ifdef IRCD_ULTI28
488 {"VCTRL", NULL}
489 ,
490 {"PROTOCTL", NULL}
491 ,
492 {"SNETINFO", NULL}
493 ,
494 {"CHGHOST", m_chghost}
495 ,
496 {"CHGIDENT", m_chgident}
497 ,
498 {"CHGNAME", m_chgname}
499 ,
500 {"SETHOST", m_sethost}
501 ,
502 {"SETIDENT", m_setident}
503 ,
504 {"SETNAME", m_setname}
505 ,
506 {"FAILOPS", NULL}
507 ,
508 {"GCONNECT", NULL}
509 ,
510 {"NETINFO", NULL}
511 ,
512 {"ADG", NULL}
513 ,
514 {"NETG", NULL}
515 ,
516 {"NETGLOBAL", NULL}
517 ,
518 {"GNOTICE", NULL}
519 ,
520 {"GLINE", NULL}
521 ,
522 {"GOPER", NULL}
523 ,
524 {"GNOTICE", NULL}
525 ,
526 {"GOPER", NULL}
527 ,
528 {"GCLIENT", NULL}
529 ,
530 #endif
531 #ifdef IRCD_ULTIMATE
532 {"VCTRL", NULL}
533 ,
534 {"GCONNECT", NULL}
535 ,
536 {"NETGLOBAL", NULL}
537 ,
538 {"GNOTICE", NULL}
539 ,
540 {"GOPER", NULL}
541 ,
542 {"GNOTICE", NULL}
543 ,
544 {"GOPER", NULL}
545 ,
546 {"NETCTRL", NULL}
547 ,
548 {"EOBURST", NULL}
549 ,
550 {"NETINFO", NULL}
551 ,
552 {"SETHOST", m_sethost}
553 ,
554 {"SMODE", m_mode}
555 ,
556 #endif
557 #ifdef IRCD_UNREAL
558 {"HELP", NULL}
559 ,
560 {"PROTOCTL", NULL}
561 ,
562 {"TKL", NULL}
563 ,
564 {"SMO", NULL}
565 ,
566 {"SDESC", m_sdesc}
567 ,
568 {"SVS2MODE", m_svsmode}
569 ,
570 {"GOPER", NULL}
571 ,
572 {"NETINFO", NULL}
573 ,
574 {"CHGHOST", m_chghost}
575 ,
576 {"CHGIDENT", m_chgident}
577 ,
578 {"CHGNAME", m_chgname}
579 ,
580 {"SETHOST", m_sethost}
581 ,
582 {"SETIDENT", m_setident}
583 ,
584 {"SETNAME", m_setname}
585 ,
586 #endif
587 #ifdef IRCD_HYBRID
588 {"REALHOST", m_realhost}
589 ,
590 #endif
591 {NULL}
592 };
593
594 /*************************************************************************/
595
596 Message *find_message(const char *name)
597 {
598 Message *m;
599 for (m = messages; m->name; m++)
600 if (stricmp(name, m->name) == 0)
601 return m;
602 return NULL;
603 }
604
605 /*************************************************************************/