ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/ermyth/modules/protocol/hybrid.C
Revision: 1.6
Committed: Sun Sep 9 20:05:52 2007 UTC (19 years, 1 month ago) by pippijn
Content type: text/plain
Branch: MAIN
Changes since 1.5: +3 -2 lines
Log Message:
- changed configurations to the c++ stdlib
- more #defines to enum
- removed getopt.h and link.h from the system as they were unused
- reworked logstreams
- added an itoa with old syntax
- made klines objects
- moved some global variables into appropriate classes
- fixed boost.foreach's compiler workaround #if's
- allow other files to add exceptions with ADD_EXCEPTION
- changed mynick_t to c++ object
- moved servers.h out of atheme.h
- corrected PING from inspircd 1.2

File Contents

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