ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/ermyth/modules/protocol/inspircd11.C
Revision: 1.3
Committed: Sat Jul 21 13:23:21 2007 UTC (19 years, 4 months ago) by pippijn
Content type: text/plain
Branch: MAIN
Changes since 1.2: +10 -8 lines
Log Message:
- added rcsid to some files
- more documentation tweaks
- made most protocol commands local to phandler.C
- added ircd metadata (inspircd only for now)
- added inspircd swhois support

File Contents

# User Rev Content
1 pippijn 1.1 /*
2     * 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 spanning tree 1.1 branch inspircd.
6     *
7 pippijn 1.3 * $Id: inspircd11.C,v 1.2 2007-07-21 01:29:09 pippijn Exp $
8 pippijn 1.1 */
9    
10     #include "atheme.h"
11     #include <account/mychan.h>
12     #include <account/myuser.h>
13     #include "uplink.h"
14     #include "pmodule.h"
15     #include "protocol/inspircd.h"
16    
17 pippijn 1.3 static char const rcsid[] = "$Id$";
18 pippijn 1.1
19     struct inspircd11_listeners : callback::has_listeners
20     {
21     };
22    
23     inspircd11_listeners listeners;
24    
25     DECLARE_MODULE_V1 ("protocol/inspircd", true, _modinit, NULL, rcsid, "InspIRCd Core Team <http://www.inspircd.org/>");
26    
27     /* *INDENT-OFF* */
28    
29     ircd_t InspIRCd = {
30     "InspIRCd 1.1.x", /* IRCd name */
31     "$", /* TLD Prefix, used by Global. */
32     false, /* Whether or not we use IRCNet/TS6 UID */
33     false, /* Whether or not we use RCOMMAND */
34     true, /* Whether or not we support channel owners. */
35     true, /* Whether or not we support channel protection. */
36     true, /* Whether or not we support halfops. */
37     false, /* Whether or not we use P10 */
38     true, /* Whether or not we use vHosts. */
39     CMODE_OPERONLY, /* Oper-only cmodes */
40     CMODE_OWNER, /* Integer flag for owner channel flag. */
41     CMODE_PROTECT, /* Integer flag for protect channel flag. */
42     CMODE_HALFOP, /* Integer flag for halfops. */
43     "+q", /* Mode we set for owner. */
44     "+a", /* Mode we set for protect. */
45     "+h", /* Mode we set for halfops. */
46     PROTOCOL_INSPIRCD, /* Protocol type */
47     0, /* Permanent cmodes */
48     "beIg", /* Ban-like cmodes */
49     'e', /* Except mchar */
50     'I', /* Invex mchar */
51     IRCD_CIDR_BANS /* Flags */
52     };
53    
54     cmode_t inspircd_mode_list[] = {
55     { 'i', CMODE_INVITE },
56     { 'm', CMODE_MOD },
57     { 'n', CMODE_NOEXT },
58     { 'p', CMODE_PRIV },
59     { 's', CMODE_SEC },
60     { 't', CMODE_TOPIC },
61     { 'c', CMODE_NOCOLOR },
62     { 'M', CMODE_MODREG },
63     { 'R', CMODE_REGONLY },
64     { 'O', CMODE_OPERONLY },
65     { 'S', CMODE_STRIP },
66     { 'K', CMODE_NOKNOCK },
67     { 'V', CMODE_NOINVITE },
68     { 'C', CMODE_NOCTCP },
69     { 'N', CMODE_STICKY },
70     { 'G', CMODE_CENSOR },
71     { 'P', CMODE_NOCAPS },
72     { 'z', CMODE_SSLONLY },
73     { 'T', CMODE_NONOTICE },
74     { '\0', 0 }
75     };
76    
77     static bool check_flood(const char *, channel_t *, mychan_t *, user_t *, myuser_t *);
78     static bool check_jointhrottle(const char *, channel_t *, mychan_t *, user_t *, myuser_t *);
79     static bool check_forward(const char *, channel_t *, mychan_t *, user_t *, myuser_t *);
80     static bool check_rejoindelay(const char *, channel_t *, mychan_t *, user_t *, myuser_t *);
81    
82     extmode inspircd_ignore_mode_list[] = {
83     { 'f', check_flood },
84     { 'j', check_jointhrottle },
85     { 'L', check_forward },
86     { 'J', check_rejoindelay },
87     { '\0', 0 }
88     };
89    
90     cmode_t inspircd_status_mode_list[] = {
91     { 'q', CMODE_OWNER },
92     { 'a', CMODE_PROTECT },
93     { 'o', CMODE_OP },
94     { 'h', CMODE_HALFOP },
95     { 'v', CMODE_VOICE },
96     { '\0', 0 }
97     };
98    
99     cmode_t inspircd_prefix_mode_list[] = {
100     { '~', CMODE_OWNER },
101     { '&', CMODE_PROTECT },
102     { '@', CMODE_OP },
103     { '%', CMODE_HALFOP },
104     { '+', CMODE_VOICE },
105     { '\0', 0 }
106     };
107    
108     /* CAPABilities */
109     static bool has_servicesmod = false;
110     static bool has_globopsmod = false;
111     static bool has_svshold = false;
112     static int has_protocol = 0;
113    
114     #define PROTOCOL_SNONOTICE 1102 /* has SNONOTICE and OPERNOTICE commands */
115     #define PROTOCOL_NEWTS 1104 /* does not send "confirming" FMODEs on TS changes and uses clear/ignore semantics also for FMODE */
116    
117     /* *INDENT-ON* */
118    
119     static bool
120     check_flood (const char *value, channel_t *c, mychan_t *mc, user_t *u, myuser_t *mu)
121     {
122    
123     return *value == '*' ? check_jointhrottle (value + 1, c, mc, u, mu) : check_jointhrottle (value, c, mc, u, mu);
124     }
125    
126     static bool
127     check_jointhrottle (const char *value, channel_t *c, mychan_t *mc, user_t *u, myuser_t *mu)
128     {
129     const char *p, *arg2;
130    
131     p = value, arg2 = NULL;
132     while (*p != '\0')
133     {
134     if (*p == ':')
135     {
136     if (arg2 != NULL)
137     return false;
138     arg2 = p + 1;
139     }
140     else if (!isdigit (*p))
141     return false;
142     p++;
143     }
144     if (arg2 == NULL)
145     return false;
146     if (p - arg2 > 10 || arg2 - value - 1 > 10 || !atoi (value) || !atoi (arg2))
147     return false;
148     return true;
149     }
150    
151     static bool
152     check_forward (const char *value, channel_t *c, mychan_t *mc, user_t *u, myuser_t *mu)
153     {
154     channel_t *target_c;
155     mychan_t *target_mc;
156    
157     if (*value != '#' || strlen (value) > 50)
158     return false;
159     if (u == NULL && mu == NULL)
160     return true;
161     target_c = channel_find (value);
162     target_mc = mychan_find (value);
163     if (target_c == NULL && target_mc == NULL)
164     return false;
165     return true;
166     }
167    
168     static bool
169     check_rejoindelay (const char *value, channel_t *c, mychan_t *mc, user_t *u, myuser_t *mu)
170     {
171     const char *ch = value;
172    
173     while (*ch)
174     {
175     if (!isdigit (*ch))
176     return false;
177     ch++;
178     }
179    
180     if (atoi (value) <= 0)
181     {
182     return false;
183     }
184     else
185     {
186     return true;
187     }
188     }
189    
190    
191     /* login to our uplink */
192     static unsigned int
193     inspircd_server_login (void)
194     {
195     int ret;
196    
197     /* XXX This is a horrible hack, but I don't know how else */
198     sleep (3);
199    
200     ret = sts ("SERVER %s %s 0 :%s", me.name, curr_uplink->pass, me.desc);
201     if (ret == 1)
202     return 1;
203    
204     me.bursting = true;
205     sts ("BURST");
206     /* XXX: Being able to get this data as a char* would be nice - Brain */
207     sts (":%s VERSION :" PACKAGE_NAME "-%s. %s %s%s%s%s%s%s%s%s%s%s", me.name, version, me.name, (match_mapping) ? "A" : "", log_debug_enabled ()? "d" : "", (me.auth) ? "e" : "", (config_options.flood_msgs) ? "F" : "", (config_options.leave_chans) ? "l" : "", (config_options.join_chans) ? "j" : "", (chansvs.changets) ? "t" : "", (!match_mapping) ? "R" : "", (config_options.raw) ? "r" : "", (runflags & RF_LIVE) ? "n" : "");
208     services_init ();
209     return 0;
210     }
211    
212     /* introduce a client */
213     static void
214     inspircd_introduce_nick (user_t *u)
215     {
216     /* :services-dev.chatspike.net NICK 1133994664 OperServ chatspike.net chatspike.net services +oii 0.0.0.0 :Operator Server */
217     sts (":%s NICK %ld %s %s %s %s +%s 0.0.0.0 :%s", me.name, u->ts, u->nick, u->host, u->host, u->user, "io", u->gecos);
218     sts (":%s OPERTYPE Services", u->nick);
219     }
220    
221     static void
222     inspircd_quit_sts (user_t *u, const char *reason)
223     {
224     if (!me.connected)
225     return;
226    
227     sts (":%s QUIT :%s", u->nick, reason);
228     }
229    
230     /* WALLOPS wrapper */
231     static void
232     inspircd_wallops_sts (const char *text)
233     {
234     char *sendernick = NULL;
235     user_t *u;
236     node_t *n;
237    
238     if (has_protocol >= PROTOCOL_SNONOTICE)
239     {
240     /* XXX */
241     if (has_globopsmod)
242     sts (":%s SNONOTICE g :%s", me.name, text);
243     else
244     sts (":%s OPERNOTICE :%s", me.name, text);
245     return;
246     }
247    
248     if (me.me == NULL)
249     return;
250    
251     if (is_internal_client (user_find_named (opersvs.nick)))
252     {
253     sendernick = opersvs.nick;
254     }
255     else
256     {
257     LIST_FOREACH (n, me.me->userlist.head)
258     {
259     u = (user_t *) n->data;
260    
261     sendernick = u->nick;
262     break;
263     }
264     }
265    
266     if (sendernick == NULL)
267     {
268     /*
269     * this means we have no pseudoclients -- under present inspircd, servers cannot globops, and
270     * thus, we will need to bail -- slog, and let them know. --w00t
271     */
272     slog (LG_ERROR, "wallops_sts(): InspIRCD requires at least one pseudoclient module to be loaded to send wallops.");
273     return;
274     }
275    
276     sts (":%s GLOBOPS :%s", sendernick, text);
277     }
278    
279     /* join a channel */
280     static void
281     inspircd_join_sts (channel_t *c, user_t *u, bool isnew, char *modes)
282     {
283     if (isnew)
284     {
285     sts (":%s FJOIN %s %ld :@,%s", me.name, c->name, c->ts, u->nick);
286     if (modes[0] && modes[1])
287     sts (":%s FMODE %s %ld %s", me.name, c->name, c->ts, modes);
288     }
289     else
290     {
291     sts (":%s FJOIN %s %ld :@,%s", me.name, c->name, c->ts, u->nick);
292     }
293     }
294    
295     static void
296     inspircd_chan_lowerts (channel_t *c, user_t *u)
297     {
298     slog (LG_DEBUG, "inspircd_chan_lowerts(): lowering TS for %s to %ld", c->name, (long) c->ts);
299    
300     sts (":%s FJOIN %s %ld :@,%s", me.name, c->name, c->ts, u->nick);
301     sts (":%s FMODE %s %ld %s", me.name, c->name, c->ts, channel_modes (c, true));
302     }
303    
304     /* kicks a user from a channel */
305     static void
306     inspircd_kick (char *from, char *channel, char *to, char *reason)
307     {
308     channel_t *chan = channel_find (channel);
309     user_t *user = user_find (to);
310    
311     if (!chan || !user)
312     return;
313    
314     sts (":%s KICK %s %s :%s", from, channel, to, reason);
315    
316     chanuser_delete (chan, user);
317     }
318    
319 pippijn 1.3 // changes ircd metadata
320     static void
321     inspircd_mdchange (const char *target, const char *key, const char *value)
322     {
323     sts (":%s METADATA %s %s :%s", me.name, target, key, value);
324     }
325    
326 pippijn 1.1 /* PRIVMSG wrapper */
327     static void
328     inspircd_msg (const char *from, const char *target, const char *fmt, ...)
329     {
330     va_list ap;
331     char buf[BUFSIZE];
332    
333     va_start (ap, fmt);
334     vsnprintf (buf, BUFSIZE, fmt, ap);
335     va_end (ap);
336    
337     sts (":%s PRIVMSG %s :%s", from, target, buf);
338     }
339    
340     /* NOTICE wrapper */
341     static void
342     inspircd_notice_user_sts (user_t *from, user_t *target, const char *text)
343     {
344     sts (":%s NOTICE %s :%s", from ? from->nick : me.name, target->nick, text);
345     }
346    
347     static void
348     inspircd_notice_global_sts (user_t *from, const char *mask, const char *text)
349     {
350     sts (":%s NOTICE %s%s :%s", from ? from->nick : me.name, ircd->tldprefix, mask, text);
351     }
352    
353     static void
354     inspircd_notice_channel_sts (user_t *from, channel_t *target, const char *text)
355     {
356     sts (":%s NOTICE %s :%s", from ? from->nick : me.name, target->name, text);
357     }
358    
359     static void
360     inspircd_numeric_sts (char *from, int numeric, char *target, char *fmt, ...)
361     {
362     va_list ap;
363     char buf[BUFSIZE];
364    
365     va_start (ap, fmt);
366     vsnprintf (buf, BUFSIZE, fmt, ap);
367     va_end (ap);
368    
369     sts (":%s PUSH %s ::%s %d %s %s", me.name, target, from, numeric, target, buf);
370     }
371    
372     /* KILL wrapper */
373     static void
374     inspircd_skill (char *from, char *nick, char *fmt, ...)
375     {
376     va_list ap;
377     char buf[BUFSIZE];
378    
379     va_start (ap, fmt);
380     vsnprintf (buf, BUFSIZE, fmt, ap);
381     va_end (ap);
382    
383     sts (":%s KILL %s :[%s] Killed (%s (%s))", from, nick, me.name, from, buf);
384     }
385    
386     /* PART wrapper */
387     static void
388     inspircd_part_sts (channel_t *c, user_t *u)
389     {
390     sts (":%s PART %s :Leaving", u->nick, c->name);
391     }
392    
393     /* server-to-server KLINE wrapper */
394     static void
395     inspircd_kline_sts (char *server, char *user, char *host, long duration, char *reason)
396     {
397     if (!me.connected)
398     return;
399    
400     /* :services-dev.chatspike.net ADDLINE G test@test.com Brain 1133994664 0 :You are banned from this network */
401     sts (":%s ADDLINE G %s@%s %s %ld %ld :%s", me.name, user, host, opersvs.nick, time (NULL), duration, reason);
402     }
403    
404     /* server-to-server UNKLINE wrapper */
405     static void
406     inspircd_unkline_sts (char *server, char *user, char *host)
407     {
408     if (!me.connected)
409     return;
410    
411     /* I know this looks wrong, but it's really not. Trust me. --w00t */
412     sts (":%s GLINE %s@%s", opersvs.nick, user, host);
413     }
414    
415     /* topic wrapper */
416     static void
417     inspircd_topic_sts (channel_t *c, const char *setter, time_t ts, time_t prevts, const char *topic)
418     {
419     if (!me.connected || !c)
420     return;
421    
422     /* If possible, try to use FTOPIC
423     * Note that because TOPIC does not contain topicTS, it may be
424     * off a few seconds on other servers, hence the 60 seconds here.
425     * -- jilles */
426     /* Restoring old topic */
427     if (ts > prevts + 60 || prevts == 0)
428     {
429     sts (":%s FTOPIC %s %ld %s :%s", chansvs.nick, c->name, ts, setter, topic);
430     return;
431     }
432     /* Tweaking a topic */
433     else if (ts == prevts)
434     {
435     ts += 60;
436     sts (":%s FTOPIC %s %ld %s :%s", chansvs.nick, c->name, ts, setter, topic);
437     c->topicts = ts;
438     return;
439     }
440     sts (":%s TOPIC %s :%s", chansvs.nick, c->name, topic);
441     c->topicts = NOW;
442     }
443    
444     /* mode wrapper */
445     static void
446     inspircd_mode_sts (char *sender, channel_t *target, char *modes)
447     {
448     if (!me.connected)
449     return;
450    
451     if (has_protocol >= PROTOCOL_NEWTS)
452     {
453     /* FMODE from user is ok, use it */
454     sts (":%s FMODE %s %ld %s", sender, target->name, target->ts, modes);
455     }
456     else
457     {
458     sts (":%s MODE %s %s", sender, target->name, modes);
459     }
460     }
461    
462     /* ping wrapper */
463     static void
464     inspircd_ping_sts (void)
465     {
466     if (!me.connected)
467     return;
468    
469     sts (":%s PING :%s", me.name, curr_uplink->name);
470     }
471    
472     /* protocol-specific stuff to do on login */
473     static void
474     inspircd_on_login (char *origin, char *user, char *wantedhost)
475     {
476     if (!me.connected)
477     return;
478    
479     sts (":%s METADATA %s accountname :%s", me.name, origin, user);
480     }
481    
482     /* protocol-specific stuff to do on logout */
483     static bool
484     inspircd_on_logout (char *origin, char *user, char *wantedhost)
485     {
486     if (!me.connected)
487     return false;
488    
489     sts (":%s METADATA %s accountname :", me.name, origin);
490     return false;
491     }
492    
493     static void
494     inspircd_jupe (const char *server, const char *reason)
495     {
496     server_t *s;
497     if (!me.connected)
498     return;
499    
500     s = server_find (server);
501     if (s != NULL)
502     {
503     /* We need to wait for the RSQUIT to be processed -- jilles */
504     sts (":%s RSQUIT :%s", opersvs.nick, server);
505     s->flags |= SF_JUPE_PENDING;
506     }
507     else
508     {
509     /* Remove any previous jupe first */
510     sts (":%s SQUIT %s :%s", me.name, server, reason);
511     sts (":%s SERVER %s * 1 :%s", me.name, server, reason);
512     }
513     }
514    
515     static void
516     inspircd_sethost_sts (char *source, char *target, char *host)
517     {
518     if (!me.connected)
519     return;
520    
521     notice (source, target, "Setting your host to \2%s\2.", host);
522     sts (":%s CHGHOST %s %s", source, target, host);
523     }
524    
525     static void
526     inspircd_fnc_sts (user_t *source, user_t *u, char *newnick, int type)
527     {
528     /* svsnick can only be sent by a server */
529     sts (":%s SVSNICK %s %s %lu", me.name, u->nick, newnick, (unsigned long) (NOW - 60));
530     }
531    
532    
533     /* invite a user to a channel */
534     static void
535     inspircd_invite_sts (user_t *sender, user_t *target, channel_t *channel)
536     {
537     sts (":%s INVITE %s %s", sender->nick, target->nick, channel->name);
538     }
539    
540     static void
541     inspircd_holdnick_sts (user_t *source, int duration, const char *nick, myuser_t *account)
542     {
543     if (duration == 0)
544     {
545     /* remove SVSHOLD */
546     sts (":%s SVSHOLD %s", source->nick, nick);
547     }
548     else
549     {
550     sts (":%s SVSHOLD %s %ds :Registered nickname.", source->nick, nick, duration);
551     }
552     }
553    
554     static void
555     m_topic (sourceinfo_t *si, int parc, char *parv[])
556     {
557     channel_t *c = channel_find (parv[0]);
558    
559     if (!c)
560     return;
561    
562     handle_topic_from (si, c, si->su->nick, time (NULL), parv[1]);
563     }
564    
565     static void
566     m_ftopic (sourceinfo_t *si, int parc, char *parv[])
567     {
568     channel_t *c = channel_find (parv[0]);
569     time_t ts = atol (parv[1]);
570    
571     if (!c)
572     return;
573    
574     if (c->topic != NULL && c->topicts >= ts)
575     {
576     slog (LG_DEBUG, "m_ftopic(): ignoring older topic on %s", c->name);
577     return;
578     }
579    
580     handle_topic_from (si, c, parv[2], ts, parv[3]);
581     }
582    
583     static void
584     m_ping (sourceinfo_t *si, int parc, char *parv[])
585     {
586     /* reply to PING's */
587     sts (":%s PONG %s", me.name, parv[0]);
588     }
589    
590     static void
591     m_pong (sourceinfo_t *si, int parc, char *parv[])
592     {
593     handle_eob (si->s);
594    
595     if (irccasecmp (me.actual, si->s->name))
596     return;
597     me.uplinkpong = NOW;
598    
599     /* -> :test.projectxero.net PONG test.projectxero.net :shrike.malkier.net */
600     if (me.bursting)
601     {
602     #ifdef HAVE_GETTIMEOFDAY
603     e_time (burstime, &burstime);
604    
605     slog (LG_INFO, "m_pong(): finished synching with uplink (%d %s)", (tv2ms (&burstime) > 1000) ? (tv2ms (&burstime) / 1000) : tv2ms (&burstime), (tv2ms (&burstime) > 1000) ? "s" : "ms");
606    
607     wallops ("Finished synching to network in %d %s.", (tv2ms (&burstime) > 1000) ? (tv2ms (&burstime) / 1000) : tv2ms (&burstime), (tv2ms (&burstime) > 1000) ? "s" : "ms");
608     #else
609     slog (LG_INFO, "m_pong(): finished synching with uplink");
610     wallops ("Finished synching to network.");
611     #endif
612    
613     me.bursting = false;
614     }
615     }
616    
617     static void
618     m_privmsg (sourceinfo_t *si, int parc, char *parv[])
619     {
620     if (parc != 2)
621     return;
622    
623     handle_message (si, parv[0], false, parv[1]);
624     }
625    
626     static void
627     m_notice (sourceinfo_t *si, int parc, char *parv[])
628     {
629     if (parc != 2)
630     return;
631    
632     handle_message (si, parv[0], true, parv[1]);
633     }
634    
635     static void
636     m_fjoin (sourceinfo_t *si, int parc, char *parv[])
637     {
638     /* FJOIN #flaps 1234 :@,fanny +%,arse ,tits ,breasts &~,poontang */
639     channel_t *c;
640     unsigned int userc;
641     unsigned int i;
642     unsigned int j;
643     unsigned int nlen;
644     bool prefix = true;
645     bool keep_new_modes = true;
646     char *userv[256];
647     char prefixandnick[51];
648     time_t ts;
649    
650     c = channel_find (parv[0]);
651     ts = atol (parv[1]);
652    
653     if (!c)
654     {
655     slog (LG_DEBUG, "m_fjoin(): new channel: %s", parv[0]);
656     c = channel_add (parv[0], ts, si->s);
657     return_if_fail (c != NULL);
658     /* Tell the core to check mode locks now,
659     * otherwise it may only happen after the next
660     * mode change.
661     * Inspircd does not allow any redundant modes
662     * so this will not look ugly. -- jilles */
663     /* As discussed with Brain, if this is in a burst,
664     * an FMODE with the simple modes will follow so we
665     * can skip this. -- jilles */
666     if (!me.bursting)
667     channel_mode_va (NULL, c, 1, "+");
668     }
669    
670     if (ts < c->ts)
671     {
672     chanuser_t *cu;
673     node_t *n;
674    
675     /* the TS changed. a TS change requires us to do
676     * bugger all except update the TS, because in InspIRCd
677     * remote servers enforce the TS change - Brain
678     *
679     * This is no longer the case with 1.1, we need to bounce their modes
680     * as well as lowering the channel ts. Do both. -- w00t
681     */
682    
683     if (has_protocol >= PROTOCOL_NEWTS)
684     {
685     clear_simple_modes (c);
686     chanban_clear (c);
687     }
688     LIST_FOREACH (n, c->members.head)
689     {
690     cu = (chanuser_t *) n->data;
691     if (cu->user->server == me.me)
692     {
693     /* it's a service, reop */
694     sts (":%s FMODE %s %ld +o %s", me.name, c->name, ts, cu->user->nick);
695     cu->modes = CMODE_OP;
696     }
697     else
698     cu->modes = 0;
699     }
700    
701     c->ts = ts;
702     hook_call_event ("channel_tschange", c);
703     }
704     else if (ts > c->ts)
705     {
706     keep_new_modes = false; /* ignore statuses */
707     }
708    
709     /*
710     * ok, here's the difference from 1.0 -> 1.1:
711     * 1.0 sent p[3] and up as individual users, prefixed with their 'highest' prefix, @, % or +
712     * in 1.1, this is more complex. All prefixes are sent, with the additional caveat that modules
713     * can add their own prefixes (dangerous!) - therefore, don't just chanuser_add(), split the prefix
714     * out and ignore unknown prefixes (probably the safest option). --w00t
715     */
716     userc = sjtoken (parv[parc - 1], ' ', userv);
717    
718     /* loop over all the users in this fjoin */
719     for (i = 0; i < userc; i++)
720     {
721     nlen = 0;
722     prefix = true;
723    
724     slog (LG_DEBUG, "m_fjoin(): processing user: %s", userv[i]);
725    
726     /*
727     * ok, now look at the chars in the nick.. we have something like "@%,w00t", but need @%w00t.. and
728     * we also want to ignore unknown prefixes.. loop through the chars
729     */
730     for (; *userv[i]; userv[i]++)
731     {
732     /* does this char match a known prefix? */
733     for (j = 0; prefix_mode_list[j].mode; j++)
734     {
735     /* yup. add it to the 'final' combination (@%w00t) */
736     if (*userv[i] == prefix_mode_list[j].mode)
737     {
738     prefixandnick[nlen++] = *userv[i];
739     continue;
740     }
741     }
742    
743     /* it's not a known prefix char, have we reached the end of the prefixes? */
744     if (*userv[i] == ',')
745     {
746     /* yup, skip over the comma */
747     userv[i]++;
748    
749     /* if we're ignoring status (keep_new_modes is false) then just add them to chan here.. */
750     if (keep_new_modes == false)
751     {
752     /* This ignores the @%, and just adds 'w00t' to the chan */
753     chanuser_add (c, userv[i]);
754     }
755     else
756     {
757     /* else, we do care about their prefixes.. add '@%w00t' to the chan */
758     strlcpy (prefixandnick + nlen, userv[i], sizeof (prefixandnick) - nlen);
759     chanuser_add (c, prefixandnick);
760     }
761    
762     /* added them.. break out of this loop, which will move us to the next user */
763     break;
764     }
765     else
766     {
767     /* unknown prefix char */
768     }
769     }
770    
771     /* go to the next user */
772     }
773    
774     if (c->nummembers == 0 && !(c->modes & ircd->perm_mode))
775     channel_delete (c);
776     }
777    
778     static void
779     m_part (sourceinfo_t *si, int parc, char *parv[])
780     {
781     int chanc;
782     char *chanv[256];
783     int i;
784    
785     chanc = sjtoken (parv[0], ',', chanv);
786     for (i = 0; i < chanc; i++)
787     {
788     slog (LG_DEBUG, "m_part(): user left channel: %s -> %s", si->su->nick, chanv[i]);
789    
790     chanuser_delete (channel_find (chanv[i]), si->su);
791     }
792     }
793    
794     static void
795     m_nick (sourceinfo_t *si, int parc, char *parv[])
796     {
797     user_t *u;
798    
799     /* :services-dev.chatspike.net NICK 1133994664 DevNull chatspike.net chatspike.net services +i 0.0.0.0 :/dev/null -- message sink */
800     if (parc == 8)
801     {
802     slog (LG_DEBUG, "m_nick(): new user on `%s': %s", si->s->name, parv[1]);
803    
804     /* char *nick, char *user, char *host, char *vhost, char *ip, char *uid, char *gecos, server_t *server, unsigned int ts */
805     u = user_add (parv[1], parv[4], parv[2], parv[3], parv[6], NULL, parv[7], si->s, atol (parv[0]));
806     user_mode (u, parv[5]);
807    
808     /* If server is not yet EOB we will do this later.
809     * This avoids useless "please identify" -- jilles */
810     if (si->s->flags & SF_EOB)
811     handle_nickchange (u);
812     }
813     /* if it's only 1 then it's a nickname change */
814     else if (parc == 1)
815     {
816     if (!si->su)
817     {
818     slog (LG_DEBUG, "m_nick(): server trying to change nick: %s", si->s != NULL ? si->s->name : "<none>");
819     return;
820     }
821    
822     slog (LG_DEBUG, "m_nick(): nickname change from `%s': %s", si->su->nick, parv[0]);
823    
824     /* No TS here for some reason, hmm */
825     user_changenick (si->su, parv[0], si->su->ts);
826    
827     /* It could happen that our PING arrived late and the
828     * server didn't acknowledge EOB yet even though it is
829     * EOB; don't send double notices in that case -- jilles */
830     if (si->su->server->flags & SF_EOB)
831     handle_nickchange (si->su);
832     }
833     else
834     {
835     int i;
836     slog (LG_DEBUG, "m_nick(): got NICK with wrong number of params");
837    
838     for (i = 0; i < parc; i++)
839     slog (LG_DEBUG, "m_nick(): parv[%d] = %s", i, parv[i]);
840     }
841     }
842    
843     static void
844     m_quit (sourceinfo_t *si, int parc, char *parv[])
845     {
846     slog (LG_DEBUG, "m_quit(): user leaving: %s", si->su->nick);
847    
848     /* user_delete() takes care of removing channels and so forth */
849     user_delete (si->su);
850     }
851    
852     static void
853     m_saquit (sourceinfo_t *si, int parc, char *parv[])
854     {
855     user_t *u = user_find (parv[0]);
856    
857     slog (LG_DEBUG, "m_saquit(): user leaving: %s", parv[0]);
858    
859     /* user_delete() takes care of removing channels and so forth */
860     user_delete (u);
861     }
862    
863     static void
864     m_mode (sourceinfo_t *si, int parc, char *parv[])
865     {
866     if (*parv[0] == '#')
867     channel_mode (NULL, channel_find (parv[0]), parc - 1, &parv[1]);
868     else
869     user_mode (user_find (parv[0]), parv[1]);
870     }
871    
872     static void
873     m_fmode (sourceinfo_t *si, int parc, char *parv[])
874     {
875     channel_t *c;
876     bool onlydeop;
877     time_t ts;
878     const char *p;
879    
880     /* :server.moo FMODE #blarp tshere +ntsklLg keymoo 1337 secks */
881     if (*parv[0] == '#')
882     {
883     c = channel_find (parv[0]);
884     if (c == NULL)
885     {
886     slog (LG_DEBUG, "m_fmode(): nonexistant channel: %s", parv[0]);
887     return;
888     }
889     ts = atoi (parv[1]);
890     if (ts == c->ts && has_protocol < PROTOCOL_NEWTS)
891     {
892     onlydeop = true;
893     p = parv[2];
894     while (*p != '\0')
895     {
896     if (!strchr ("-qaohv", *p))
897     onlydeop = false;
898     p++;
899     }
900     if (onlydeop && si->s != NULL)
901     {
902     /* ignore redundant deops generated
903     * if we lower the TS of a channel
904     * scenario: user with autoop privs recreates
905     * channel
906     * XXX could this ignore other stuff too?
907     * -- jilles */
908     slog (LG_DEBUG, "m_fmode(): ignoring %s %s: incoming TS %ld is equal to our TS %ld, and only deops", parv[0], parv[2], ts, c->ts);
909     return;
910     }
911     }
912     else if (ts > c->ts)
913     {
914     if (has_protocol < PROTOCOL_NEWTS)
915     slog (LG_DEBUG, "m_fmode(): accepting but should bounce %s %s: incoming TS %ld is newer than our TS %ld", parv[0], parv[2], ts, c->ts);
916     else
917     return;
918     }
919     else if (ts < c->ts)
920     slog (LG_DEBUG, "m_fmode(): %s %s: incoming TS %ld is older than our TS %ld, possible desync", parv[0], parv[2], ts, c->ts);
921     channel_mode (NULL, c, parc - 2, &parv[2]);
922     }
923     else
924     user_mode (user_find (parv[0]), parv[2]);
925     }
926    
927     static void
928     m_kick (sourceinfo_t *si, int parc, char *parv[])
929     {
930     user_t *u = user_find (parv[1]);
931     channel_t *c = channel_find (parv[0]);
932    
933     /* -> :rakaur KICK #shrike rintaun :test */
934     slog (LG_DEBUG, "m_kick(): user was kicked: %s -> %s", parv[1], parv[0]);
935    
936     if (!u)
937     {
938     slog (LG_DEBUG, "m_kick(): got kick for nonexistant user %s", parv[1]);
939     return;
940     }
941    
942     if (!c)
943     {
944     slog (LG_DEBUG, "m_kick(): got kick in nonexistant channel: %s", parv[0]);
945     return;
946     }
947    
948     if (!chanuser_find (c, u))
949     {
950     slog (LG_DEBUG, "m_kick(): got kick for %s not in %s", u->nick, c->name);
951     return;
952     }
953    
954     chanuser_delete (c, u);
955    
956     /* if they kicked us, let's rejoin */
957     if (is_internal_client (u))
958     {
959     slog (LG_DEBUG, "m_kick(): %s got kicked from %s; rejoining", u->nick, parv[0]);
960     join (parv[0], u->nick);
961     }
962     }
963    
964     static void
965     m_kill (sourceinfo_t *si, int parc, char *parv[])
966     {
967     handle_kill (si, parv[0], parc > 1 ? parv[1] : "<No reason given>");
968     }
969    
970     static void
971     m_squit (sourceinfo_t *si, int parc, char *parv[])
972     {
973     slog (LG_DEBUG, "m_squit(): server leaving: %s", parv[0]);
974     server_delete (parv[0]);
975     }
976    
977     static void
978     m_server (sourceinfo_t *si, int parc, char *parv[])
979     {
980     server_t *s;
981    
982     slog (LG_DEBUG, "m_server(): new server: %s", parv[0]);
983     s = handle_server (si, parv[0], NULL, atoi (parv[2]), parv[3]);
984    
985     if (s != NULL && s->uplink != me.me)
986     {
987     /* elicit PONG for EOB detection; pinging uplink is
988     * already done elsewhere -- jilles
989     */
990     sts (":%s PING %s %s", me.name, me.name, s->name);
991     }
992     }
993    
994     static void
995     m_stats (sourceinfo_t *si, int parc, char *parv[])
996     {
997     handle_stats (si->su, parv[0][0]);
998     }
999    
1000     static void
1001     m_motd (sourceinfo_t *si, int parc, char *parv[])
1002     {
1003     handle_motd (si->su);
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_away (sourceinfo_t *si, int parc, char *parv[])
1014     {
1015     handle_away (si->su, parc >= 1 ? parv[0] : NULL);
1016     }
1017    
1018     static void
1019     m_join (sourceinfo_t *si, int parc, char *parv[])
1020     {
1021     channel_t *c;
1022    
1023     c = channel_find (parv[0]);
1024     if (!c)
1025     {
1026     slog (LG_DEBUG, "m_join(): new channel: %s (modes lost)", parv[0]);
1027     c = channel_add (parv[0], parc > 1 ? atol (parv[1]) : NOW, si->su->server);
1028     return_if_fail (c != NULL);
1029     channel_mode_va (NULL, c, 1, "+");
1030     }
1031     chanuser_add (c, si->su->nick);
1032     }
1033    
1034     static void
1035     m_sajoin (sourceinfo_t *si, int parc, char *parv[])
1036     {
1037     si->su = user_find (parv[0]);
1038     if (si->su == NULL)
1039     return;
1040     m_join (si, 1, &parv[1]);
1041     }
1042    
1043     static void
1044     m_sapart (sourceinfo_t *si, int parc, char *parv[])
1045     {
1046     si->su = user_find (parv[0]);
1047     if (si->su == NULL)
1048     return;
1049     m_part (si, 1, &parv[1]);
1050     }
1051    
1052     static void
1053     m_sanick (sourceinfo_t *si, int parc, char *parv[])
1054     {
1055     si->su = user_find (parv[0]);
1056     if (si->su == NULL)
1057     return;
1058     m_nick (si, 1, &parv[1]);
1059     }
1060    
1061     static void
1062     m_samode (sourceinfo_t *si, int parc, char *parv[])
1063     {
1064     /* note that SAMODE is not checked in any way before propagation,
1065     * and only works on channels, not users */
1066     channel_mode (NULL, channel_find (parv[0]), parc - 1, &parv[1]);
1067     }
1068    
1069     static void
1070     m_error (sourceinfo_t *si, int parc, char *parv[])
1071     {
1072     slog (LG_INFO, "m_error(): error from server: %s", parv[0]);
1073     }
1074    
1075     static void
1076     m_idle (sourceinfo_t *si, int parc, char *parv[])
1077     {
1078     if (parc == 1 && si->su != NULL)
1079     {
1080     sts (":%s IDLE %s %ld 0", parv[0], si->su->nick, NOW);
1081     }
1082     else
1083     {
1084     slog (LG_INFO, "m_idle(): Received an IDLE response but we didn't WHOIS anybody!");
1085     }
1086     }
1087    
1088     static void
1089     m_opertype (sourceinfo_t *si, int parc, char *parv[])
1090     {
1091     /*
1092     * Hope this works.. InspIRCd OPERTYPE means user is an oper, mark them so
1093     * Later, we may want to look at saving their OPERTYPE for informational
1094     * purposes, or not. --w00t
1095     */
1096     user_mode (si->su, "+o");
1097     }
1098    
1099     static void
1100     m_fhost (sourceinfo_t *si, int parc, char *parv[])
1101     {
1102     strlcpy (si->su->vhost, parv[0], HOSTLEN);
1103     }
1104    
1105     /*
1106     * :<source server> METADATA <channel|user> <key> :<value>
1107     * The sole piece of metadata we're interested in is 'accountname', set by Services,
1108     * and kept by ircd.
1109     *
1110     * :services.barafranca METADATA w00t accountname :w00t
1111     */
1112    
1113     static void
1114     m_metadata (sourceinfo_t *si, int parc, char *parv[])
1115     {
1116     user_t *u;
1117    
1118     if (!irccasecmp (parv[1], "accountname"))
1119     {
1120     /* find user */
1121     u = user_find (parv[0]);
1122    
1123     if (u == NULL)
1124     return;
1125    
1126     handle_burstlogin (u, parv[2]);
1127     }
1128     }
1129    
1130     /*
1131     * rsquit:
1132     * remote/request squit
1133     * when squitting a remote server, inspircd sends RSQUIT along the tree until it reaches the server that has
1134     * the server to be squit as a local connection, which should then close it's connection and send SQUIT back
1135     * to the rest of the network.
1136     */
1137     static void
1138     m_rsquit (sourceinfo_t *si, int parc, char *parv[])
1139     {
1140     sts (":%s SQUIT %s :Jupe removed by %s", me.name, parv[0], si->su->nick);
1141     }
1142    
1143     static void
1144     m_capab (sourceinfo_t *si, int parc, char *parv[])
1145     {
1146     int i, varc;
1147     char *varv[256];
1148    
1149     if (strcasecmp (parv[0], "START") == 0)
1150     {
1151     /* reset all our previously recieved CAPAB stuff */
1152     has_servicesmod = false;
1153     has_globopsmod = false;
1154     has_svshold = false;
1155     has_protocol = 0;
1156     }
1157     else if (strcasecmp (parv[0], "CAPABILITIES") == 0 && parc > 1)
1158     {
1159     varc = sjtoken (parv[1], ' ', varv);
1160     for (i = 0; i < varc; i++)
1161     {
1162     if (!strncmp (varv[i], "PROTOCOL=", 9))
1163     has_protocol = atoi (varv[i] + 9);
1164     /* XXX check/store HALFOP/CHANMAX/IDENTMAX */
1165     }
1166     }
1167     else if (strcasecmp (parv[0], "MODULES") == 0 && parc > 1)
1168     {
1169     if (strstr (parv[1], "m_services_account.so"))
1170     {
1171     has_servicesmod = true;
1172     }
1173     if (strstr (parv[1], "m_globops.so"))
1174     {
1175     has_globopsmod = true;
1176     }
1177     if (strstr (parv[1], "m_svshold.so"))
1178     {
1179     has_svshold = true;
1180     }
1181     }
1182     else if (strcasecmp (parv[0], "END") == 0)
1183     {
1184     if (has_globopsmod == false && has_protocol < PROTOCOL_SNONOTICE)
1185     {
1186     slog (LG_ERROR, "m_capab(): you didn't load m_globops into inspircd. " PACKAGE_NAME " support requires this module. exiting.");
1187     exit (EXIT_FAILURE);
1188     }
1189    
1190     if (has_servicesmod == false)
1191     {
1192     slog (LG_ERROR, "m_capab(): you didn't load m_services_account into inspircd. " PACKAGE_NAME " support requires this module. exiting.");
1193     exit (EXIT_FAILURE);
1194     }
1195    
1196     if (has_svshold == false)
1197     {
1198     slog (LG_INFO, "m_capab(): you didn't load m_svshold into inspircd. nickname enforcers will not work.");
1199     }
1200     }
1201     else
1202     {
1203     slog (LG_DEBUG, "m_capab(): unknown CAPAB type %s - out of date protocol module?", parv[0]);
1204     }
1205     }
1206    
1207     /* Server ended their burst: warn all their users if necessary -- jilles */
1208     static void
1209     server_eob (server_t *s)
1210     {
1211     node_t *n;
1212    
1213     LIST_FOREACH (n, s->userlist.head)
1214     {
1215     handle_nickchange ((user_t *) n->data);
1216     }
1217     }
1218    
1219     void
1220     _modinit (module_t *m)
1221     {
1222     /* Symbol relocation voodoo. */
1223     server_login = &inspircd_server_login;
1224     introduce_nick = &inspircd_introduce_nick;
1225     quit_sts = &inspircd_quit_sts;
1226     wallops_sts = &inspircd_wallops_sts;
1227     join_sts = &inspircd_join_sts;
1228     chan_lowerts = &inspircd_chan_lowerts;
1229     kick = &inspircd_kick;
1230 pippijn 1.3 mdchange = &inspircd_mdchange;
1231 pippijn 1.1 msg = &inspircd_msg;
1232     notice_user_sts = &inspircd_notice_user_sts;
1233     notice_global_sts = &inspircd_notice_global_sts;
1234     notice_channel_sts = &inspircd_notice_channel_sts;
1235     numeric_sts = &inspircd_numeric_sts;
1236     skill = &inspircd_skill;
1237     part_sts = &inspircd_part_sts;
1238     kline_sts = &inspircd_kline_sts;
1239     unkline_sts = &inspircd_unkline_sts;
1240     topic_sts = &inspircd_topic_sts;
1241     mode_sts = &inspircd_mode_sts;
1242     ping_sts = &inspircd_ping_sts;
1243     ircd_on_login = &inspircd_on_login;
1244     ircd_on_logout = &inspircd_on_logout;
1245     jupe = &inspircd_jupe;
1246     sethost_sts = &inspircd_sethost_sts;
1247     fnc_sts = &inspircd_fnc_sts;
1248     invite_sts = &inspircd_invite_sts;
1249     holdnick_sts = &inspircd_holdnick_sts;
1250    
1251     mode_list = inspircd_mode_list;
1252     ignore_mode_list = inspircd_ignore_mode_list;
1253     status_mode_list = inspircd_status_mode_list;
1254     prefix_mode_list = inspircd_prefix_mode_list;
1255    
1256     ircd = &InspIRCd;
1257    
1258     pcommand_add ("PING", m_ping, 1, MSRC_USER | MSRC_SERVER);
1259     pcommand_add ("PONG", m_pong, 1, MSRC_SERVER);
1260     pcommand_add ("PRIVMSG", m_privmsg, 2, MSRC_USER);
1261     pcommand_add ("NOTICE", m_notice, 2, MSRC_USER | MSRC_SERVER);
1262     pcommand_add ("FJOIN", m_fjoin, 3, MSRC_SERVER);
1263     pcommand_add ("PART", m_part, 1, MSRC_USER);
1264     pcommand_add ("NICK", m_nick, 1, MSRC_USER | MSRC_SERVER);
1265     pcommand_add ("QUIT", m_quit, 1, MSRC_USER);
1266     pcommand_add ("MODE", m_mode, 2, MSRC_USER | MSRC_SERVER);
1267     pcommand_add ("FMODE", m_fmode, 3, MSRC_USER | MSRC_SERVER);
1268     pcommand_add ("SAMODE", m_samode, 2, MSRC_USER);
1269     pcommand_add ("SAJOIN", m_sajoin, 2, MSRC_USER);
1270     pcommand_add ("SAPART", m_sapart, 2, MSRC_USER);
1271     pcommand_add ("SANICK", m_sanick, 2, MSRC_USER);
1272     pcommand_add ("SAQUIT", m_saquit, 1, MSRC_USER);
1273     pcommand_add ("KICK", m_kick, 2, MSRC_USER | MSRC_SERVER);
1274     pcommand_add ("KILL", m_kill, 1, MSRC_USER | MSRC_SERVER);
1275     pcommand_add ("SQUIT", m_squit, 1, MSRC_USER | MSRC_SERVER);
1276     pcommand_add ("RSQUIT", m_rsquit, 1, MSRC_USER);
1277     pcommand_add ("SERVER", m_server, 4, MSRC_UNREG | MSRC_SERVER);
1278     pcommand_add ("STATS", m_stats, 2, MSRC_USER);
1279     pcommand_add ("MOTD", m_motd, 1, MSRC_USER);
1280     pcommand_add ("ADMIN", m_admin, 1, MSRC_USER);
1281     pcommand_add ("FTOPIC", m_ftopic, 4, MSRC_SERVER);
1282     pcommand_add ("JOIN", m_join, 1, MSRC_USER);
1283     pcommand_add ("ERROR", m_error, 1, MSRC_UNREG | MSRC_SERVER);
1284     pcommand_add ("TOPIC", m_topic, 2, MSRC_USER);
1285     pcommand_add ("FHOST", m_fhost, 1, MSRC_USER);
1286     pcommand_add ("IDLE", m_idle, 1, MSRC_USER);
1287     pcommand_add ("AWAY", m_away, 0, MSRC_USER);
1288     pcommand_add ("OPERTYPE", m_opertype, 1, MSRC_USER);
1289     pcommand_add ("METADATA", m_metadata, 3, MSRC_SERVER);
1290     pcommand_add ("CAPAB", m_capab, 1, MSRC_UNREG | MSRC_SERVER);
1291    
1292     hook_add_event ("server_eob");
1293     hook_add_hook ("server_eob", (void (*)(void *)) server_eob);
1294    
1295     m->mflags = MODTYPE_CORE;
1296    
1297     pmodule_loaded = true;
1298     }