ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/ermyth/modules/protocol/ratbox.C
Revision: 1.8
Committed: Sat Sep 22 14:27:28 2007 UTC (16 years, 8 months ago) by pippijn
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.7: +8 -12 lines
Log Message:
split up ermyth into ermyth-modules, libermyth (currently just ermyth-util) and ermyth-core

File Contents

# Content
1 /**
2 * ratbox.C: This file contains protocol support for ratbox-based ircd.
3 *
4 * Copyright © 2007 Pippijn van Steenhoven / The Ermyth Team
5 * Rights to this code are as documented in COPYING.
6 *
7 *
8 * Portions of this file were derived from sources bearing the following license:
9 * Copyright © 2003-2004 E. Will et al.
10 * Copyright © 2005-2006 Atheme Development Group
11 * Rights to this code are documented in doc/pod/license.pod.
12 *
13 * $Id: ratbox.C,v 1.7 2007-09-16 18:54:44 pippijn Exp $
14 */
15
16 #include <boost/foreach.hpp>
17
18 #include "atheme.h"
19 #include <util/time.h>
20 #include <libermyth.h>
21 #include "servers.h"
22 #include <account/myuser.h>
23 #include "uplink.h"
24 #include "pmodule.h"
25 #include "protocol/ratbox.h"
26
27 static char const rcsid[] = "$Id: ratbox.C,v 1.7 2007-09-16 18:54:44 pippijn Exp $";
28
29 /* *INDENT-OFF* */
30
31 namespace protocol
32 {
33 static ircd_t Ratbox = {
34 "Ratbox (1.0 or later)", /* IRCd name */
35 "$$", /* TLD Prefix, used by Global. */
36 true, /* Whether or not we use IRCNet/TS6 UID */
37 false, /* Whether or not we use RCOMMAND */
38 false, /* Whether or not we support channel owners. */
39 false, /* Whether or not we support channel protection. */
40 false, /* Whether or not we support halfops. */
41 false, /* Whether or not we use P10 */
42 false, /* Whether or not we use vHosts. */
43 0, /* Oper-only cmodes */
44 0, /* Integer flag for owner channel flag. */
45 0, /* Integer flag for protect channel flag. */
46 0, /* Integer flag for halfops. */
47 "+", /* Mode we set for owner. */
48 "+", /* Mode we set for protect. */
49 "+", /* Mode we set for halfops. */
50 PROTOCOL_RATBOX, /* Protocol type */
51 0, /* Permanent cmodes */
52 "beI", /* Ban-like cmodes */
53 'e', /* Except mchar */
54 'I', /* Invex mchar */
55 IRCD_CIDR_BANS /* Flags */
56 };
57
58 struct ratbox_handler : handler
59 {
60 ratbox_handler ();
61 virtual ~ratbox_handler ();
62
63 virtual unsigned int server_login (void);
64 virtual void introduce_nick (user_t *u);
65 virtual void invite_sts (user_t *source, user_t *target, channel_t *channel);
66 virtual void quit_sts (user_t *u, char const * const reason);
67 virtual void wallops_sts (char const * const text);
68 virtual void join_sts (channel_t *c, user_t *u, bool isnew, char const * const modes);
69 virtual void chan_lowerts (channel_t *c, user_t *u);
70 virtual void kick (char const * const from, char const * const channel, char const * const to, char const * const reason);
71 virtual void privmsg (char const * const from, char const * const target, char const * const fmt, ...);
72 virtual void notice_user_sts (user_t *from, user_t *target, char const * const text);
73 virtual void notice_global_sts (user_t *from, char const * const mask, char const * const text);
74 virtual void notice_channel_sts (user_t *from, channel_t *target, char const * const text);
75 virtual void wallchops (user_t *sender, channel_t *channel, char const * const message);
76 virtual void numeric_sts (char const * const from, int numeric, char const * const target, char const * const fmt, ...);
77 virtual void skill (char const * const from, char const * const nick, char const * const fmt, ...);
78 virtual void part_sts (channel_t *c, user_t *u);
79 virtual void kline_sts (char const * const server, char const * const user, char const * const host, long duration, char const * const reason);
80 virtual void unkline_sts (char const * const server, char const * const user, char const * const host);
81 virtual void topic_sts (channel_t *c, char const * const setter, time_t ts, time_t prevts, char const * const topic);
82 virtual void mode_sts (char const * const sender, channel_t *target, char const * const modes);
83 virtual void ping_sts (void);
84 virtual void ircd_on_login (char const * const origin, char const * const user, char const * const wantedhost);
85 virtual bool ircd_on_logout (char const * const origin, char const * const user, char const * const wantedhost);
86 virtual void jupe (char const * const server, char const * const reason);
87 virtual void fnc_sts (user_t *source, user_t *u, char const * const newnick, int type);
88 virtual void holdnick_sts (user_t *source, int duration, char const * const nick, myuser_t *account);
89 };
90
91 static cmode_t ratbox_mode_list[] = {
92 { 'i', CMODE_INVITE },
93 { 'm', CMODE_MOD },
94 { 'n', CMODE_NOEXT },
95 { 'p', CMODE_PRIV },
96 { 's', CMODE_SEC },
97 { 't', CMODE_TOPIC },
98 { '\0', 0 }
99 };
100
101 static extmode_t ratbox_ignore_mode_list[] = {
102 { '\0', 0 }
103 };
104
105 static cmode_t ratbox_status_mode_list[] = {
106 { 'o', CMODE_OP },
107 { 'v', CMODE_VOICE },
108 { '\0', 0 }
109 };
110
111 static cmode_t ratbox_prefix_mode_list[] = {
112 { '@', CMODE_OP },
113 { '+', CMODE_VOICE },
114 { '\0', 0 }
115 };
116
117 static bool use_rserv_support = false;
118 static bool use_tb = false;
119 static bool use_rsfnc = false;
120
121 static void server_eob(server_t *s);
122
123 static char ts6sid[3 + 1] = "";
124
125 /* *INDENT-ON* */
126
127 /* login to our uplink */
128 unsigned int
129 ratbox_handler::server_login (void)
130 {
131 int ret = 1;
132
133 if (!me.numeric)
134 {
135 ircd->uses_uid = false;
136 ret = sts ("PASS %s :TS", curr_uplink->pass);
137 }
138 else if (strlen (me.numeric) == 3 && isdigit (*me.numeric))
139 {
140 ircd->uses_uid = true;
141 ret = sts ("PASS %s TS 6 :%s", curr_uplink->pass, me.numeric);
142 }
143 else
144 {
145 slog (LG_ERROR, "Invalid numeric (SID) %s", me.numeric);
146 }
147 if (ret == 1)
148 return 1;
149
150 me.bursting = true;
151
152 sts ("CAPAB :QS EX IE KLN UNKLN ENCAP TB SERVICES");
153 sts ("SERVER %s 1 :%s", me.name, me.desc);
154 sts ("SVINFO %d 3 0 :%ld", ircd->uses_uid ? 6 : 5, NOW);
155
156 return 0;
157 }
158
159 /* introduce a client */
160 void
161 ratbox_handler::introduce_nick (user_t *u)
162 {
163 if (ircd->uses_uid)
164 sts (":%s UID %s 1 %ld +%s%s%s %s %s 0 %s :%s", me.numeric, u->nick, u->ts, "io", chansvs.fantasy ? "" : "D", use_rserv_support ? "S" : "", u->user, u->host, u->uid, u->gecos);
165 else
166 sts ("NICK %s 1 %ld +%s%s%s %s %s %s :%s", u->nick, u->ts, "io", chansvs.fantasy ? "" : "D", use_rserv_support ? "S" : "", u->user, u->host, me.name, u->gecos);
167 }
168
169 /* invite a user to a channel */
170 void
171 ratbox_handler::invite_sts (user_t *sender, user_t *target, channel_t *channel)
172 {
173 /* some older TSora ircds require the sender to be
174 * on the channel, but hyb7/ratbox don't
175 * let's just assume it's not necessary -- jilles */
176 sts (":%s INVITE %s %s", CLIENT_NAME (sender), CLIENT_NAME (target), channel->name);
177 }
178
179 void
180 ratbox_handler::quit_sts (user_t *u, char const * const reason)
181 {
182 if (!me.connected)
183 return;
184
185 sts (":%s QUIT :%s", CLIENT_NAME (u), reason);
186 }
187
188 /* WALLOPS wrapper */
189 void
190 ratbox_handler::wallops_sts (char const * const text)
191 {
192 sts (":%s WALLOPS :%s", ME, text);
193 }
194
195 /* join a channel */
196 void
197 ratbox_handler::join_sts (channel_t *c, user_t *u, bool isnew, char const * const modes)
198 {
199 if (isnew)
200 sts (":%s SJOIN %ld %s %s :@%s", ME, c->ts, c->name, modes, CLIENT_NAME (u));
201 else
202 sts (":%s SJOIN %ld %s + :@%s", ME, c->ts, c->name, CLIENT_NAME (u));
203 }
204
205 void
206 ratbox_handler::chan_lowerts (channel_t *c, user_t *u)
207 {
208 slog (LG_DEBUG, "ratbox_chan_lowerts(): lowering TS for %s to %ld", c->name, (long) c->ts);
209 sts (":%s SJOIN %ld %s %s :@%s", ME, c->ts, c->name, channel_modes (c, true), CLIENT_NAME (u));
210 if (ircd->uses_uid)
211 chanban_clear (c);
212 }
213
214 /* kicks a user from a channel */
215 void
216 ratbox_handler::kick (char const * const from, char const * const channel, char const * const to, char const * const reason)
217 {
218 channel_t *chan = channel_find (channel);
219 user_t *user = user_find (to);
220 user_t *from_p = user_find (from);
221
222 if (!chan || !user)
223 return;
224
225 if (chan->ts != 0 || chanuser_find (chan, from_p))
226 sts (":%s KICK %s %s :%s", CLIENT_NAME (from_p), channel, CLIENT_NAME (user), reason);
227 else
228 sts (":%s KICK %s %s :%s", ME, channel, CLIENT_NAME (user), reason);
229
230 chanuser_delete (chan, user);
231 }
232
233 /* PRIVMSG wrapper */
234 void
235 ratbox_handler::privmsg (char const * const from, char const * const target, char const * const fmt, ...)
236 {
237 va_list ap;
238 char buf[BUFSIZE];
239 user_t *u = user_find (from);
240 user_t *t = user_find (target);
241
242 if (!u)
243 return;
244
245 va_start (ap, fmt);
246 vsnprintf (buf, BUFSIZE, fmt, ap);
247 va_end (ap);
248
249 /* If this is to a channel, it's the snoop channel so chanserv
250 * is on it -- jilles
251 *
252 * Well, now it's operserv, but yes it's safe to assume that
253 * the source would be able to send to whatever target it is
254 * sending to. --nenolod
255 */
256 sts (":%s PRIVMSG %s :%s", CLIENT_NAME (u), t ? CLIENT_NAME (t) : target, buf);
257 }
258
259 /* NOTICE wrapper */
260 void
261 ratbox_handler::notice_user_sts (user_t *from, user_t *target, char const * const text)
262 {
263 sts (":%s NOTICE %s :%s", from ? CLIENT_NAME (from) : ME, CLIENT_NAME (target), text);
264 }
265
266 void
267 ratbox_handler::notice_global_sts (user_t *from, char const * const mask, char const * const text)
268 {
269 if (!strcmp (mask, "*"))
270 foreach (tld_t *tld, tld_t::list)
271 sts (":%s NOTICE %s*%s :%s", from ? CLIENT_NAME (from) : ME, ircd->tldprefix, tld->name, text);
272 else
273 sts (":%s NOTICE %s%s :%s", from ? CLIENT_NAME (from) : ME, ircd->tldprefix, mask, text);
274 }
275
276 void
277 ratbox_handler::notice_channel_sts (user_t *from, channel_t *target, char const * const text)
278 {
279 if (from == NULL || chanuser_find (target, from))
280 sts (":%s NOTICE %s :%s", from ? CLIENT_NAME (from) : ME, target->name, text);
281 else
282 /* not on channel, let's send it from the server
283 * hyb6 won't accept this, oh well, they'll have to
284 * enable join_chans -- jilles */
285 sts (":%s NOTICE %s :[%s:%s] %s", ME, target->name, from->nick, target->name, text);
286 }
287
288 void
289 ratbox_handler::wallchops (user_t *sender, channel_t *channel, char const * const message)
290 {
291 if (chanuser_find (channel, sender))
292 sts (":%s NOTICE @%s :%s", CLIENT_NAME (sender), channel->name, message);
293 else /* do not join for this, everyone would see -- jilles */
294 handler::wallchops (sender, channel, message);
295 }
296
297 /* numeric wrapper */
298 void
299 ratbox_handler::numeric_sts (char const * const from, int numeric, char const * const target, char const * const fmt, ...)
300 {
301 va_list ap;
302 char buf[BUFSIZE];
303 user_t *t = user_find (target);
304
305 va_start (ap, fmt);
306 vsnprintf (buf, BUFSIZE, fmt, ap);
307 va_end (ap);
308
309 sts (":%s %d %s %s", ME, numeric, CLIENT_NAME (t), buf);
310 }
311
312 /* KILL wrapper */
313 void
314 ratbox_handler::skill (char const * const from, char const * const nick, char const * const fmt, ...)
315 {
316 va_list ap;
317 char buf[BUFSIZE];
318 user_t *killer = user_find (from);
319 user_t *victim = user_find (nick);
320
321 va_start (ap, fmt);
322 vsnprintf (buf, BUFSIZE, fmt, ap);
323 va_end (ap);
324
325 sts (":%s KILL %s :%s!%s!%s (%s)", killer ? CLIENT_NAME (killer) : ME, victim ? CLIENT_NAME (victim) : nick, from, from, from, buf);
326 }
327
328 /* PART wrapper */
329 void
330 ratbox_handler::part_sts (channel_t *c, user_t *u)
331 {
332 sts (":%s PART %s", CLIENT_NAME (u), c->name);
333 }
334
335 /* server-to-server KLINE wrapper */
336 void
337 ratbox_handler::kline_sts (char const * const server, char const * const user, char const * const host, long duration, char const * const reason)
338 {
339 if (!me.connected)
340 return;
341
342 sts (":%s KLINE %s %ld %s %s :%s", CLIENT_NAME (opersvs.me->me), server, duration, user, host, reason);
343 }
344
345 /* server-to-server UNKLINE wrapper */
346 void
347 ratbox_handler::unkline_sts (char const * const server, char const * const user, char const * const host)
348 {
349 if (!me.connected)
350 return;
351
352 sts (":%s UNKLINE %s %s %s", CLIENT_NAME (opersvs.me->me), server, user, host);
353 }
354
355 /* topic wrapper */
356 void
357 ratbox_handler::topic_sts (channel_t *c, char const * const setter, time_t ts, time_t prevts, char const * const topic)
358 {
359 int joined = 0;
360
361 if (!me.connected || !c)
362 return;
363
364 /* If possible, try to use TB
365 * Note that because TOPIC does not contain topicTS, it may be
366 * off a few seconds on other servers, hence the 60 seconds here.
367 * -- jilles */
368 if (use_tb && *topic != '\0')
369 {
370 /* Restoring old topic */
371 if (ts < prevts || prevts == 0)
372 {
373 if (prevts != 0 && ts + 60 > prevts)
374 ts = prevts - 60;
375 sts (":%s TB %s %ld %s :%s", ME, c->name, ts, setter, topic);
376 c->topicts = ts;
377 return;
378 }
379 /* Tweaking a topic */
380 else if (ts == prevts)
381 {
382 ts -= 60;
383 sts (":%s TB %s %ld %s :%s", ME, c->name, ts, setter, topic);
384 c->topicts = ts;
385 return;
386 }
387 }
388 /* We have to be on channel to change topic.
389 * We cannot nicely change topic from the server:
390 * :server.name TOPIC doesn't propagate and TB requires
391 * us to specify an older topicts.
392 * -- jilles
393 */
394 if (!chanuser_find (c, chansvs.me->me))
395 {
396 sts (":%s SJOIN %ld %s + :@%s", ME, c->ts, c->name, CLIENT_NAME (chansvs.me->me));
397 joined = 1;
398 }
399 sts (":%s TOPIC %s :%s", CLIENT_NAME (chansvs.me->me), c->name, topic);
400 if (joined)
401 sts (":%s PART %s :Topic set", CLIENT_NAME (chansvs.me->me), c->name);
402 c->topicts = NOW;
403 }
404
405 /* mode wrapper */
406 void
407 ratbox_handler::mode_sts (char const * const sender, channel_t *target, char const * const modes)
408 {
409 user_t *u = user_find (sender);
410
411 if (!me.connected || !u)
412 return;
413
414 if (ircd->uses_uid)
415 sts (":%s TMODE %ld %s %s", CLIENT_NAME (u), target->ts, target->name, modes);
416 else
417 sts (":%s MODE %s %s", CLIENT_NAME (u), target->name, modes);
418 }
419
420 /* ping wrapper */
421 void
422 ratbox_handler::ping_sts (void)
423 {
424 if (!me.connected)
425 return;
426
427 sts ("PING :%s", me.name);
428 }
429
430 /* protocol-specific stuff to do on login */
431 void
432 ratbox_handler::ircd_on_login (char const * const origin, char const * const user, char const * const wantedhost)
433 {
434 user_t *u = user_find (origin);
435
436 if (!me.connected || !use_rserv_support || !u)
437 return;
438
439 sts (":%s ENCAP * SU %s %s", ME, CLIENT_NAME (u), user);
440 }
441
442 /* protocol-specific stuff to do on login */
443 bool
444 ratbox_handler::ircd_on_logout (char const * const origin, char const * const user, char const * const wantedhost)
445 {
446 user_t *u = user_find (origin);
447
448 if (!me.connected || !use_rserv_support || !u)
449 return false;
450
451 sts (":%s ENCAP * SU %s", ME, CLIENT_NAME (u));
452 return false;
453 }
454
455 void
456 ratbox_handler::jupe (char const * const server, char const * const reason)
457 {
458 if (!me.connected)
459 return;
460
461 server_delete (server);
462 sts (":%s SQUIT %s :%s", CLIENT_NAME (opersvs.me->me), server, reason);
463 sts (":%s SERVER %s 2 :(H) %s", me.name, server, reason);
464 }
465
466 void
467 ratbox_handler::fnc_sts (user_t *source, user_t *u, char const * const newnick, int type)
468 {
469 if (use_rsfnc)
470 /* XXX assumes whole net has it -- jilles */
471 sts (":%s ENCAP %s RSFNC %s %s %lu %lu", ME, u->server->name, CLIENT_NAME (u), newnick, (unsigned long) (NOW - 60), (unsigned long) u->ts);
472 else
473 handler::fnc_sts (source, u, newnick, type);
474 }
475
476 void
477 ratbox_handler::holdnick_sts (user_t *source, int duration, char const * const nick, myuser_t *account)
478 {
479 if (duration == 0)
480 return; /* can't do this safely */
481 sts (":%s ENCAP * RESV %d %s 0 :Reserved by %s for nickname owner (%s)", CLIENT_NAME (source), duration > 300 ? 300 : duration, nick, source->nick, account != NULL ? account->name : nick);
482 }
483
484 static void
485 m_topic (sourceinfo_t *si, int parc, char *parv[])
486 {
487 channel_t *c = channel_find (parv[0]);
488
489 if (c == NULL)
490 return;
491
492 handle_topic_from (si, c, si->su->nick, NOW, parv[1]);
493 }
494
495 static void
496 m_tb (sourceinfo_t *si, int parc, char *parv[])
497 {
498 channel_t *c = channel_find (parv[0]);
499 time_t ts = atol (parv[1]);
500
501 if (c == NULL)
502 return;
503
504 if (c->topic != NULL && c->topicts <= ts)
505 {
506 slog (LG_DEBUG, "m_tb(): ignoring newer topic on %s", c->name);
507 return;
508 }
509
510 handle_topic_from (si, c, parc > 3 ? parv[2] : si->s->name, ts, parv[parc - 1]);
511 }
512
513 static void
514 m_ping (sourceinfo_t *si, int parc, char *parv[])
515 {
516 /* reply to PING's */
517 sts (":%s PONG %s %s", ME, me.name, parv[0]);
518 }
519
520 static void
521 m_pong (sourceinfo_t *si, int parc, char *parv[])
522 {
523 server_t *s;
524
525 /* someone replied to our PING */
526 if (!parv[0])
527 return;
528 s = server_find (parv[0]);
529 if (s == NULL)
530 return;
531 handle_eob (s);
532
533 if (irccasecmp (me.actual, parv[0]))
534 return;
535
536 me.uplinkpong = NOW;
537
538 /* -> :test.projectxero.net PONG test.projectxero.net :shrike.malkier.net */
539 if (me.bursting)
540 {
541 #ifdef HAVE_GETTIMEOFDAY
542 e_time (burstime, &burstime);
543
544 slog (LG_INFO, "m_pong(): finished synching with uplink (%d %s)", (tv2ms (&burstime) > 1000) ? (tv2ms (&burstime) / 1000) : tv2ms (&burstime), (tv2ms (&burstime) > 1000) ? "s" : "ms");
545
546 wallops ("Finished synching to network in %d %s.", (tv2ms (&burstime) > 1000) ? (tv2ms (&burstime) / 1000) : tv2ms (&burstime), (tv2ms (&burstime) > 1000) ? "s" : "ms");
547 #else
548 slog (LG_INFO, "m_pong(): finished synching with uplink");
549 wallops ("Finished synching to network.");
550 #endif
551
552 me.bursting = false;
553 }
554 }
555
556 static void
557 m_privmsg (sourceinfo_t *si, int parc, char *parv[])
558 {
559 if (parc != 2)
560 return;
561
562 handle_message (si, parv[0], false, parv[1]);
563 }
564
565 static void
566 m_notice (sourceinfo_t *si, int parc, char *parv[])
567 {
568 if (parc != 2)
569 return;
570
571 handle_message (si, parv[0], true, parv[1]);
572 }
573
574 static void
575 m_sjoin (sourceinfo_t *si, int parc, char *parv[])
576 {
577 /* -> :proteus.malkier.net SJOIN 1073516550 #shrike +tn :@sycobuny @+rakaur */
578
579 channel_t *c;
580 bool keep_new_modes = true;
581 unsigned int userc;
582 char *userv[256];
583 unsigned int i;
584 time_t ts;
585 char *p;
586
587 /* :origin SJOIN ts chan modestr [key or limits] :users */
588 c = channel_find (parv[1]);
589 ts = atol (parv[0]);
590
591 if (!c)
592 {
593 slog (LG_DEBUG, "m_sjoin(): new channel: %s", parv[1]);
594 c = channel_add (parv[1], ts, si->s);
595 }
596
597 if (ts == 0 || c->ts == 0)
598 {
599 if (c->ts != 0)
600 slog (LG_INFO, "m_sjoin(): server %s changing TS on %s from %ld to 0", si->s->name, c->name, (long) c->ts);
601 c->ts = 0;
602 c->callback.tschange (c);
603 }
604 else if (ts < c->ts)
605 {
606 chanuser_t *cu;
607 node_t *n;
608
609 /* the TS changed. a TS change requires the following things
610 * to be done to the channel: reset all modes to nothing, remove
611 * all status modes on known users on the channel (including ours),
612 * and set the new TS.
613 *
614 * if the source does TS6, also remove all bans
615 * note that JOIN does not do this
616 */
617
618 clear_simple_modes (c);
619 if (si->s->sid != NULL)
620 chanban_clear (c);
621
622 LIST_FOREACH (n, c->members.head)
623 {
624 cu = (chanuser_t *) n->data;
625 if (cu->user->server == me.me)
626 {
627 /* it's a service, reop */
628 sts (":%s PART %s :Reop", CLIENT_NAME (cu->user), c->name);
629 sts (":%s SJOIN %ld %s + :@%s", ME, ts, c->name, CLIENT_NAME (cu->user));
630 cu->modes = CMODE_OP;
631 }
632 else
633 cu->modes = 0;
634 }
635
636 slog (LG_DEBUG, "m_sjoin(): TS changed for %s (%ld -> %ld)", c->name, c->ts, ts);
637
638 c->ts = ts;
639 c->callback.tschange (c);
640 }
641 else if (ts > c->ts)
642 keep_new_modes = false;
643
644 if (keep_new_modes)
645 channel_mode (NULL, c, parc - 3, parv + 2);
646
647 userc = sjtoken (parv[parc - 1], ' ', userv);
648
649 if (keep_new_modes)
650 for (i = 0; i < userc; i++)
651 chanuser_add (c, userv[i]);
652 else
653 for (i = 0; i < userc; i++)
654 {
655 p = userv[i];
656 while (*p == '@' || *p == '%' || *p == '+')
657 p++;
658 /* XXX for TS5 we should mark them deopped
659 * if they were opped and drop modes from them
660 * -- jilles */
661 chanuser_add (c, p);
662 }
663
664 if (c->nummembers == 0 && !(c->modes & ircd->perm_mode))
665 channel_delete (c);
666 }
667
668 static void
669 m_join (sourceinfo_t *si, int parc, char *parv[])
670 {
671 /* -> :1JJAAAAAB JOIN 1127474195 #test +tn */
672 bool keep_new_modes = true;
673 node_t *n, *tn;
674 channel_t *c;
675 chanuser_t *cu;
676 time_t ts;
677
678 /* JOIN 0 is really a part from all channels */
679 /* be sure to allow joins to TS 0 channels -- jilles */
680 if (parv[0][0] == '0' && parc <= 2)
681 {
682 LIST_FOREACH_SAFE (n, tn, si->su->channels.head)
683 {
684 cu = (chanuser_t *) n->data;
685 chanuser_delete (cu->chan, si->su);
686 }
687 return;
688 }
689
690 /* :user JOIN ts chan modestr [key or limits] */
691 c = channel_find (parv[1]);
692 ts = atol (parv[0]);
693
694 if (!c)
695 {
696 slog (LG_DEBUG, "m_join(): new channel: %s", parv[1]);
697 c = channel_add (parv[1], ts, si->su->server);
698 }
699
700 if (ts == 0 || c->ts == 0)
701 {
702 if (c->ts != 0)
703 slog (LG_INFO, "m_join(): server %s changing TS on %s from %ld to 0", si->su->server->name, c->name, (long) c->ts);
704 c->ts = 0;
705 c->callback.tschange (c);
706 }
707 else if (ts < c->ts)
708 {
709 /* the TS changed. a TS change requires the following things
710 * to be done to the channel: reset all modes to nothing, remove
711 * all status modes on known users on the channel (including ours),
712 * and set the new TS.
713 */
714 clear_simple_modes (c);
715
716 LIST_FOREACH (n, c->members.head)
717 {
718 cu = (chanuser_t *) n->data;
719 if (cu->user->server == me.me)
720 {
721 /* it's a service, reop */
722 sts (":%s PART %s :Reop", CLIENT_NAME (cu->user), c->name);
723 sts (":%s SJOIN %ld %s + :@%s", ME, ts, c->name, CLIENT_NAME (cu->user));
724 cu->modes = CMODE_OP;
725 }
726 else
727 cu->modes = 0;
728 }
729 slog (LG_DEBUG, "m_join(): TS changed for %s (%ld -> %ld)", c->name, c->ts, ts);
730 c->ts = ts;
731 c->callback.tschange (c);
732 }
733 else if (ts > c->ts)
734 keep_new_modes = false;
735
736 if (keep_new_modes)
737 channel_mode (NULL, c, parc - 2, parv + 2);
738
739 chanuser_add (c, CLIENT_NAME (si->su));
740 }
741
742 static void
743 m_bmask (sourceinfo_t *si, int parc, char *parv[])
744 {
745 unsigned int ac, i;
746 char *av[256];
747 channel_t *c = channel_find (parv[1]);
748 int type;
749
750 /* :1JJ BMASK 1127474361 #services b :*!*@*evil* *!*eviluser1@* */
751 if (!c)
752 {
753 slog (LG_DEBUG, "m_bmask(): got bmask for unknown channel");
754 return;
755 }
756
757 if (atol (parv[0]) > c->ts)
758 return;
759
760 type = *parv[2];
761 if (!strchr (ircd->ban_like_modes, type))
762 {
763 slog (LG_DEBUG, "m_bmask(): got unknown type '%c'", type);
764 return;
765 }
766
767 ac = sjtoken (parv[parc - 1], ' ', av);
768
769 for (i = 0; i < ac; i++)
770 chanban_add (c, av[i], type);
771 }
772
773 static void
774 m_part (sourceinfo_t *si, int parc, char *parv[])
775 {
776 int chanc;
777 char *chanv[256];
778 int i;
779
780 chanc = sjtoken (parv[0], ',', chanv);
781 for (i = 0; i < chanc; i++)
782 {
783 slog (LG_DEBUG, "m_part(): user left channel: %s -> %s", si->su->nick, chanv[i]);
784
785 chanuser_delete (channel_find (chanv[i]), si->su);
786 }
787 }
788
789 static void
790 m_nick (sourceinfo_t *si, int parc, char *parv[])
791 {
792 server_t *s;
793 user_t *u;
794
795 /* got the right number of args for an introduction? */
796 if (parc == 8)
797 {
798 s = server_find (parv[6]);
799 if (!s)
800 {
801 slog (LG_DEBUG, "m_nick(): new user on nonexistant server: %s", parv[6]);
802 return;
803 }
804
805 slog (LG_DEBUG, "m_nick(): new user on `%s': %s", s->name, parv[0]);
806
807 u = user_add (parv[0], parv[4], parv[5], NULL, NULL, NULL, parv[7], s, atoi (parv[2]));
808
809 user_mode (u, parv[3]);
810
811 /* If server is not yet EOB we will do this later.
812 * This avoids useless "please identify" -- jilles */
813 if (s->flags & SF_EOB)
814 handle_nickchange (user_find (parv[0]));
815 }
816
817 /* if it's only 2 then it's a nickname change */
818 else if (parc == 2)
819 {
820 if (!si->su)
821 {
822 slog (LG_DEBUG, "m_nick(): server trying to change nick: %s", si->s != NULL ? si->s->name : "<none>");
823 return;
824 }
825
826 slog (LG_DEBUG, "m_nick(): nickname change from `%s': %s", si->su->nick, parv[0]);
827
828 user_changenick (si->su, parv[0], atoi (parv[1]));
829
830 /* It could happen that our PING arrived late and the
831 * server didn't acknowledge EOB yet even though it is
832 * EOB; don't send double notices in that case -- jilles */
833 if (si->su->server->flags & SF_EOB)
834 handle_nickchange (si->su);
835 }
836 else
837 {
838 int i;
839 slog (LG_DEBUG, "m_nick(): got NICK with wrong number of params");
840
841 for (i = 0; i < parc; i++)
842 slog (LG_DEBUG, "m_nick(): parv[%d] = %s", i, parv[i]);
843 }
844 }
845
846 static void
847 m_uid (sourceinfo_t *si, int parc, char *parv[])
848 {
849 server_t *s;
850 user_t *u;
851
852 /* got the right number of args for an introduction? */
853 if (parc == 9)
854 {
855 s = si->s;
856 slog (LG_DEBUG, "m_uid(): new user on `%s': %s", s->name, parv[0]);
857
858 u = user_add (parv[0], parv[4], parv[5], NULL, parv[6], parv[7], parv[8], s, atoi (parv[2]));
859
860 user_mode (u, parv[3]);
861
862 /* If server is not yet EOB we will do this later.
863 * This avoids useless "please identify" -- jilles
864 */
865 if (s->flags & SF_EOB)
866 handle_nickchange (user_find (parv[0]));
867 }
868 else
869 {
870 int i;
871 slog (LG_DEBUG, "m_uid(): got UID with wrong number of params");
872
873 for (i = 0; i < parc; i++)
874 slog (LG_DEBUG, "m_uid(): parv[%d] = %s", i, parv[i]);
875 }
876 }
877
878 static void
879 m_quit (sourceinfo_t *si, int parc, char *parv[])
880 {
881 slog (LG_DEBUG, "m_quit(): user leaving: %s", si->su->nick);
882
883 /* user_delete() takes care of removing channels and so forth */
884 user_delete (si->su);
885 }
886
887 static void
888 m_mode (sourceinfo_t *si, int parc, char *parv[])
889 {
890 if (*parv[0] == '#')
891 channel_mode (NULL, channel_find (parv[0]), parc - 1, &parv[1]);
892 else
893 user_mode (user_find (parv[0]), parv[1]);
894 }
895
896 static void
897 m_tmode (sourceinfo_t *si, int parc, char *parv[])
898 {
899 channel_t *c;
900
901 /* -> :1JJAAAAAB TMODE 1127511579 #new +o 2JJAAAAAB */
902 c = channel_find (parv[1]);
903 if (c == NULL)
904 {
905 slog (LG_DEBUG, "m_tmode(): nonexistent channel %s", parv[1]);
906 return;
907 }
908
909 if (atol (parv[0]) > c->ts)
910 return;
911
912 channel_mode (NULL, c, parc - 2, &parv[2]);
913 }
914
915 static void
916 m_kick (sourceinfo_t *si, int parc, char *parv[])
917 {
918 user_t *u = user_find (parv[1]);
919 channel_t *c = channel_find (parv[0]);
920
921 /* -> :rakaur KICK #shrike rintaun :test */
922 slog (LG_DEBUG, "m_kick(): user was kicked: %s -> %s", parv[1], parv[0]);
923
924 if (!u)
925 {
926 slog (LG_DEBUG, "m_kick(): got kick for nonexistant user %s", parv[1]);
927 return;
928 }
929
930 if (!c)
931 {
932 slog (LG_DEBUG, "m_kick(): got kick in nonexistant channel: %s", parv[0]);
933 return;
934 }
935
936 if (!chanuser_find (c, u))
937 {
938 slog (LG_DEBUG, "m_kick(): got kick for %s not in %s", u->nick, c->name);
939 return;
940 }
941
942 chanuser_delete (c, u);
943
944 /* if they kicked us, let's rejoin */
945 if (is_internal_client (u))
946 {
947 slog (LG_DEBUG, "m_kick(): %s got kicked from %s; rejoining", u->nick, parv[0]);
948 join (parv[0], u->nick);
949 }
950 }
951
952 static void
953 m_kill (sourceinfo_t *si, int parc, char *parv[])
954 {
955 handle_kill (si, parv[0], parc > 1 ? parv[1] : "<No reason given>");
956 }
957
958 static void
959 m_squit (sourceinfo_t *si, int parc, char *parv[])
960 {
961 slog (LG_DEBUG, "m_squit(): server leaving: %s from %s", parv[0], parv[1]);
962 server_delete (parv[0]);
963 }
964
965 static void
966 m_server (sourceinfo_t *si, int parc, char *parv[])
967 {
968 server_t *s;
969
970 slog (LG_DEBUG, "m_server(): new server: %s", parv[0]);
971 s = handle_server (si, parv[0], si->s || !ircd->uses_uid ? NULL : ts6sid, atoi (parv[1]), parv[2]);
972
973 if (s != NULL && s->uplink != me.me)
974 {
975 /* elicit PONG for EOB detection; pinging uplink is
976 * already done elsewhere -- jilles
977 */
978 sts (":%s PING %s %s", ME, me.name, s->name);
979 }
980 }
981
982 static void
983 m_sid (sourceinfo_t *si, int parc, char *parv[])
984 {
985 /* -> :1JJ SID file. 2 00F :telnet server */
986 server_t *s;
987
988 slog (LG_DEBUG, "m_sid(): new server: %s", parv[0]);
989 s = handle_server (si, parv[0], parv[2], atoi (parv[1]), parv[3]);
990
991 if (s != NULL && s->uplink != me.me)
992 {
993 /* elicit PONG for EOB detection; pinging uplink is
994 * already done elsewhere -- jilles
995 */
996 sts (":%s PING %s %s", ME, me.name, s->sid);
997 }
998 }
999
1000 static void
1001 m_stats (sourceinfo_t *si, int parc, char *parv[])
1002 {
1003 handle_stats (si->su, parv[0][0]);
1004 }
1005
1006 static void
1007 m_admin (sourceinfo_t *si, int parc, char *parv[])
1008 {
1009 handle_admin (si->su);
1010 }
1011
1012 static void
1013 m_version (sourceinfo_t *si, int parc, char *parv[])
1014 {
1015 handle_version (si->su);
1016 }
1017
1018 static void
1019 m_info (sourceinfo_t *si, int parc, char *parv[])
1020 {
1021 handle_info (si->su);
1022 }
1023
1024 static void
1025 m_whois (sourceinfo_t *si, int parc, char *parv[])
1026 {
1027 handle_whois (si->su, parv[1]);
1028 }
1029
1030 static void
1031 m_trace (sourceinfo_t *si, int parc, char *parv[])
1032 {
1033 handle_trace (si->su, parv[0], parc >= 2 ? parv[1] : NULL);
1034 }
1035
1036 static void
1037 m_away (sourceinfo_t *si, int parc, char *parv[])
1038 {
1039 handle_away (si->su, parc >= 1 ? parv[0] : NULL);
1040 }
1041
1042 static void
1043 m_pass (sourceinfo_t *si, int parc, char *parv[])
1044 {
1045 /* TS5: PASS mypassword :TS
1046 * TS6: PASS mypassword TS 6 :sid */
1047 if (strcmp (curr_uplink->pass, parv[0]))
1048 {
1049 slog (LG_INFO, "m_pass(): password mismatch from uplink; aborting");
1050 runflags |= RF_SHUTDOWN;
1051 }
1052
1053 if (ircd->uses_uid && parc > 3 && atoi (parv[2]) >= 6)
1054 strlcpy (ts6sid, parv[3], sizeof (ts6sid));
1055 else
1056 {
1057 if (ircd->uses_uid)
1058 {
1059 slog (LG_INFO, "m_pass(): uplink does not support TS6, falling back to TS5");
1060 ircd->uses_uid = false;
1061 }
1062 ts6sid[0] = '\0';
1063 }
1064 }
1065
1066 static void
1067 m_error (sourceinfo_t *si, int parc, char *parv[])
1068 {
1069 slog (LG_INFO, "m_error(): error from server: %s", parv[0]);
1070 }
1071
1072 static void
1073 m_encap (sourceinfo_t *si, int parc, char *parv[])
1074 {
1075 user_t *u;
1076
1077 if (match (parv[0], me.name))
1078 return;
1079 if (!irccasecmp (parv[1], "LOGIN"))
1080 {
1081 /* :jilles ENCAP * LOGIN jilles */
1082 if (!use_rserv_support)
1083 return;
1084 if (parc < 3)
1085 return;
1086 u = si->su;
1087 if (u == NULL)
1088 return;
1089 if (u->server->flags & SF_EOB)
1090 {
1091 slog (LG_DEBUG, "m_encap(): %s sent ENCAP LOGIN for user %s outside burst", u->server->name, si->su->nick);
1092 return;
1093 }
1094 handle_burstlogin (u, parv[2]);
1095 }
1096 }
1097
1098 static void
1099 m_capab (sourceinfo_t *si, int parc, char *parv[])
1100 {
1101 char *p;
1102
1103 use_rserv_support = false;
1104 use_tb = false;
1105 use_rsfnc = false;
1106 for (p = strtok (parv[0], " "); p != NULL; p = strtok (NULL, " "))
1107 {
1108 if (!irccasecmp (p, "SERVICES"))
1109 {
1110 slog (LG_DEBUG, "m_capab(): uplink has rserv extensions, enabling support.");
1111 use_rserv_support = true;
1112 }
1113 if (!irccasecmp (p, "TB"))
1114 {
1115 slog (LG_DEBUG, "m_capab(): uplink does topic bursting, using if appropriate.");
1116 use_tb = true;
1117 }
1118 if (!irccasecmp (p, "RSFNC"))
1119 {
1120 slog (LG_DEBUG, "m_capab(): uplink does RSFNC, assuming whole net has it.");
1121 use_rsfnc = true;
1122 }
1123 }
1124
1125 /* Now we know whether or not we should enable services support,
1126 * so burst the clients.
1127 * --nenolod
1128 */
1129 services_init ();
1130 }
1131
1132 static void
1133 m_motd (sourceinfo_t *si, int parc, char *parv[])
1134 {
1135 handle_motd (si->su);
1136 }
1137
1138 /* Server ended their burst: warn all their users if necessary -- jilles */
1139 static void
1140 server_eob (server_t *s)
1141 {
1142 node_t *n;
1143
1144 LIST_FOREACH (n, s->userlist.head)
1145 {
1146 handle_nickchange ((user_t *) n->data);
1147 }
1148 }
1149
1150 static pcommand_t const pcommands[] = {
1151 { "PING", m_ping, 1, MSRC_USER | MSRC_SERVER },
1152 { "PONG", m_pong, 1, MSRC_SERVER },
1153 { "PRIVMSG", m_privmsg, 2, MSRC_USER },
1154 { "NOTICE", m_notice, 2, MSRC_UNREG | MSRC_USER | MSRC_SERVER },
1155 { "SJOIN", m_sjoin, 4, MSRC_SERVER },
1156 { "PART", m_part, 1, MSRC_USER },
1157 { "NICK", m_nick, 2, MSRC_USER | MSRC_SERVER },
1158 { "QUIT", m_quit, 1, MSRC_USER },
1159 { "MODE", m_mode, 2, MSRC_USER | MSRC_SERVER },
1160 { "KICK", m_kick, 2, MSRC_USER | MSRC_SERVER },
1161 { "KILL", m_kill, 1, MSRC_USER | MSRC_SERVER },
1162 { "SQUIT", m_squit, 1, MSRC_USER | MSRC_SERVER },
1163 { "SERVER", m_server, 3, MSRC_UNREG | MSRC_SERVER },
1164 { "STATS", m_stats, 2, MSRC_USER },
1165 { "ADMIN", m_admin, 1, MSRC_USER },
1166 { "VERSION", m_version, 1, MSRC_USER },
1167 { "INFO", m_info, 1, MSRC_USER },
1168 { "WHOIS", m_whois, 2, MSRC_USER },
1169 { "TRACE", m_trace, 1, MSRC_USER },
1170 { "AWAY", m_away, 0, MSRC_USER },
1171 { "JOIN", m_join, 1, MSRC_USER },
1172 { "PASS", m_pass, 1, MSRC_UNREG },
1173 { "ERROR", m_error, 1, MSRC_UNREG | MSRC_SERVER },
1174 { "TOPIC", m_topic, 2, MSRC_USER },
1175 { "TB", m_tb, 3, MSRC_SERVER },
1176 { "ENCAP", m_encap, 2, MSRC_USER | MSRC_SERVER },
1177 { "CAPAB", m_capab, 1, MSRC_UNREG },
1178 { "UID", m_uid, 9, MSRC_SERVER },
1179 { "BMASK", m_bmask, 4, MSRC_SERVER },
1180 { "TMODE", m_tmode, 3, MSRC_USER | MSRC_SERVER },
1181 { "SID", m_sid, 4, MSRC_SERVER },
1182 { "MOTD", m_motd, 1, MSRC_USER },
1183 };
1184
1185 ratbox_handler::ratbox_handler ()
1186 {
1187 mode_list = ratbox_mode_list;
1188 ignore_mode_list = ratbox_ignore_mode_list;
1189 status_mode_list = ratbox_status_mode_list;
1190 prefix_mode_list = ratbox_prefix_mode_list;
1191
1192 ircd = &Ratbox;
1193
1194 pcommand_add (&pcommands);
1195
1196 server_t::callback.eob.attach (server_eob);
1197 }
1198
1199 ratbox_handler::~ratbox_handler ()
1200 {
1201 server_t::callback.eob.detach (server_eob);
1202
1203 pcommand_delete (&pcommands);
1204
1205 ircd = NULL;
1206
1207 prefix_mode_list = NULL;
1208 status_mode_list = NULL;
1209 ignore_mode_list = NULL;
1210 mode_list = NULL;
1211 }
1212 } // namespace protocol
1213
1214 #define FACREG_TYPE protocol::ratbox_handler
1215 #define FACREG_TYPE_NAME "ratbox"
1216 #define FACREG_INTERFACE_TYPE protocol::handler
1217 #include <ermyth/factory_reg.h>