ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/ermyth/modules/protocol/nefarious.C
Revision: 1.6
Committed: Sun Sep 9 20:05:52 2007 UTC (19 years 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 William Pitcock, et al.
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 P10 ircd's.
6     * Some sources used: Run's documentation, beware's description,
7     * raw data sent by nefarious.
8     *
9 pippijn 1.6 * $Id: nefarious.C,v 1.5 2007-09-05 11:23:15 pippijn Exp $
10 pippijn 1.1 */
11    
12     #include "atheme.h"
13 pippijn 1.6 #include "servers.h"
14 pippijn 1.1 #include <account/myuser.h>
15     #include "uplink.h"
16     #include "pmodule.h"
17     #include "protocol/nefarious.h"
18    
19 pippijn 1.6 static char const rcsid[] = "$Id: nefarious.C,v 1.5 2007-09-05 11:23:15 pippijn Exp $";
20 pippijn 1.1
21     /* *INDENT-OFF* */
22    
23 pippijn 1.4 namespace protocol
24 pippijn 1.1 {
25 pippijn 1.4 static ircd_t Nefarious = {
26     "Nefarious IRCU 0.4.0 or later", /* IRCd name */
27     "$", /* TLD Prefix, used by Global. */
28     true, /* Whether or not we use IRCNet/TS6 UID */
29     false, /* Whether or not we use RCOMMAND */
30     false, /* Whether or not we support channel owners. */
31     false, /* Whether or not we support channel protection. */
32     true, /* Whether or not we support halfops. */
33     true, /* Whether or not we use P10 */
34     true, /* Whether or not we use vhosts. */
35     CMODE_PERM|CMODE_OPERONLY|CMODE_ADMONLY, /* Oper-only cmodes */
36     0, /* Integer flag for owner channel flag. */
37     0, /* Integer flag for protect channel flag. */
38     CMODE_HALFOP, /* Integer flag for halfops. */
39     "+", /* Mode we set for owner. */
40     "+", /* Mode we set for protect. */
41     "+", /* Mode we set for halfops. */
42     PROTOCOL_NEFARIOUS, /* Protocol type */
43     CMODE_PERM, /* Permanent cmodes */
44     "be", /* Ban-like cmodes */
45     'e', /* Except mchar */
46     'e', /* Invex mchar (+e also exempts from +i in Nefarious) */
47     IRCD_CIDR_BANS /* Flags */
48     };
49    
50     struct nefarious_handler : handler
51     {
52     nefarious_handler ();
53     virtual ~nefarious_handler ();
54    
55     virtual unsigned int server_login (void);
56     virtual void introduce_nick (user_t *u);
57     virtual void invite_sts (user_t *source, user_t *target, channel_t *channel);
58     virtual void quit_sts (user_t *u, char const * const reason);
59     virtual void wallops_sts (char const * const text);
60     virtual void join_sts (channel_t *c, user_t *u, bool isnew, char const * const modes);
61     virtual void kick (char const * const from, char const * const channel, char const * const to, char const * const reason);
62     virtual void privmsg (char const * const from, char const * const target, char const * const fmt, ...);
63     virtual void notice_user_sts (user_t *from, user_t *target, char const * const text);
64     virtual void notice_global_sts (user_t *from, char const * const mask, char const * const text);
65     virtual void notice_channel_sts (user_t *from, channel_t *target, char const * const text);
66     virtual void wallchops (user_t *sender, channel_t *channel, char const * const message);
67     virtual void numeric_sts (char const * const from, int numeric, char const * const target, char const * const fmt, ...);
68     virtual void skill (char const * const from, char const * const nick, char const * const fmt, ...);
69     virtual void part_sts (channel_t *c, user_t *u);
70     virtual void kline_sts (char const * const server, char const * const user, char const * const host, long duration, char const * const reason);
71     virtual void unkline_sts (char const * const server, char const * const user, char const * const host);
72     virtual void topic_sts (channel_t *c, char const * const setter, time_t ts, time_t prevts, char const * const topic);
73     virtual void mode_sts (char const * const sender, channel_t *target, char const * const modes);
74     virtual void ping_sts (void);
75     virtual void ircd_on_login (char const * const origin, char const * const user, char const * const wantedhost);
76     virtual bool ircd_on_logout (char const * const origin, char const * const user, char const * const wantedhost);
77     virtual void jupe (char const * const server, char const * const reason);
78     };
79    
80     static cmode_t nefarious_mode_list[] = {
81     { 'a', CMODE_ADMONLY },
82     { 'i', CMODE_INVITE },
83     { 'm', CMODE_MOD },
84     { 'n', CMODE_NOEXT },
85     { 'p', CMODE_PRIV },
86     { 'r', CMODE_REGONLY },
87     { 's', CMODE_SEC },
88     { 't', CMODE_TOPIC },
89     { 'z', CMODE_PERM },
90     { 'c', CMODE_NOCOLOR },
91     { 'C', CMODE_NOCTCP },
92     { 'D', CMODE_DELAYED },
93     { 'Q', CMODE_NOQUIT },
94     { 'N', CMODE_NONOTICE },
95     { 'M', CMODE_SOFTMOD },
96     { 'C', CMODE_NOCTCP },
97     { 'S', CMODE_STRIP },
98     { 'T', CMODE_NOAMSG },
99     { 'O', CMODE_OPERONLY },
100     { 'L', CMODE_SOFTPRIV },
101     { 'Z', CMODE_SSLONLY },
102     { '\0', 0 }
103     };
104    
105 pippijn 1.5 static extmode_t nefarious_ignore_mode_list[] = {
106 pippijn 1.4 { '\0', 0 }
107     };
108    
109     static cmode_t nefarious_status_mode_list[] = {
110     { 'o', CMODE_OP },
111     { 'h', CMODE_HALFOP },
112     { 'v', CMODE_VOICE },
113     { '\0', 0 }
114     };
115    
116     static cmode_t nefarious_prefix_mode_list[] = {
117     { '@', CMODE_OP },
118     { '%', CMODE_HALFOP },
119     { '+', CMODE_VOICE },
120     { '\0', 0 }
121     };
122    
123     static void check_hidehost(user_t *u);
124    
125     /* *INDENT-ON* */
126    
127     /* login to our uplink */
128     unsigned int
129     nefarious_handler::server_login (void)
130     {
131     int ret;
132    
133     ret = sts ("PASS :%s", curr_uplink->pass);
134     if (ret == 1)
135     return 1;
136    
137     me.bursting = true;
138    
139     /* SERVER irc.undernet.org 1 933022556 947908144 J10 AA]]] :[127.0.0.1] A Undernet Server */
140     sts ("SERVER %s 1 %ld %ld J10 %s]]] +s :%s", me.name, me.start, NOW, me.numeric, me.desc);
141    
142     services_init ();
143    
144     sts ("%s EB", me.numeric);
145    
146     return 0;
147     }
148    
149     /* introduce a client */
150     void
151     nefarious_handler::introduce_nick (user_t *u)
152     {
153     sts ("%s N %s 1 %ld %s %s +%s%sk ]]]]]] %s :%s", me.numeric, u->nick, u->ts, u->user, u->host, "io", chansvs.fantasy ? "" : "d", u->uid, u->gecos);
154     }
155    
156     /* invite a user to a channel */
157     void
158     nefarious_handler::invite_sts (user_t *sender, user_t *target, channel_t *channel)
159     {
160     /* target is a nick, weird eh? -- jilles */
161     sts ("%s I %s %s", sender->uid, target->nick, channel->name);
162     }
163    
164     void
165     nefarious_handler::quit_sts (user_t *u, char const * const reason)
166     {
167     if (!me.connected)
168     return;
169    
170     sts ("%s Q :%s", u->uid, reason);
171     }
172    
173     /* WALLOPS wrapper */
174     void
175     nefarious_handler::wallops_sts (char const * const text)
176     {
177     sts ("%s WA :%s", me.numeric, text);
178     }
179    
180     /* join a channel */
181     void
182     nefarious_handler::join_sts (channel_t *c, user_t *u, bool isnew, char const * const modes)
183     {
184     /* If the channel doesn't exist, we need to create it. */
185     if (isnew)
186     {
187     sts ("%s C %s %ld", u->uid, c->name, c->ts);
188     if (modes[0] && modes[1])
189     sts ("%s M %s %s", u->uid, c->name, modes);
190     }
191     else
192     {
193     sts ("%s J %s %ld", u->uid, c->name, c->ts);
194     sts ("%s M %s +o %s", me.numeric, c->name, u->uid);
195     }
196     }
197    
198     /* kicks a user from a channel */
199     void
200     nefarious_handler::kick (char const * const from, char const * const channel, char const * const to, char const * const reason)
201     {
202     channel_t *chan = channel_find (channel);
203     user_t *fptr = user_find_named (from);
204     user_t *user = user_find_named (to);
205    
206     if (!chan || !user || !fptr)
207     return;
208    
209     sts ("%s K %s %s :%s", fptr->uid, channel, user->uid, reason);
210    
211     chanuser_delete (chan, user);
212     }
213    
214     /* PRIVMSG wrapper */
215     void
216     nefarious_handler::privmsg (char const * const from, char const * const target, char const * const fmt, ...)
217     {
218     va_list ap;
219     user_t *u = user_find_named (from);
220     char buf[BUFSIZE];
221    
222     if (!u)
223     return;
224    
225     va_start (ap, fmt);
226     vsnprintf (buf, BUFSIZE, fmt, ap);
227     va_end (ap);
228    
229     sts ("%s P %s :%s", u->uid, target, buf);
230     }
231    
232     /* NOTICE wrapper */
233     void
234     nefarious_handler::notice_user_sts (user_t *from, user_t *target, char const * const text)
235     {
236     sts ("%s O %s :%s", from ? from->uid : me.numeric, target->uid, text);
237     }
238    
239     void
240     nefarious_handler::notice_global_sts (user_t *from, char const * const mask, char const * const text)
241     {
242     node_t *n;
243     tld_t *tld;
244    
245     if (!strcmp (mask, "*"))
246     {
247     LIST_FOREACH (n, tldlist.head)
248     {
249     tld = static_cast<tld_t *> (n->data);
250     sts ("%s O %s*%s :%s", from ? from->uid : me.numeric, ircd->tldprefix, tld->name, text);
251     }
252     }
253     else
254     sts ("%s O %s%s :%s", from ? from->uid : me.numeric, ircd->tldprefix, mask, text);
255     }
256    
257     void
258     nefarious_handler::notice_channel_sts (user_t *from, channel_t *target, char const * const text)
259     {
260     sts ("%s O %s :%s", from ? from->uid : me.numeric, target->name, text);
261     }
262    
263     void
264     nefarious_handler::wallchops (user_t *sender, channel_t *channel, char const * const message)
265     {
266     sts ("%s WC %s :%s", sender->uid, channel->name, message);
267     }
268    
269     void
270     nefarious_handler::numeric_sts (char const * const from, int numeric, char const * const target, char const * const fmt, ...)
271     {
272     va_list ap;
273     char buf[BUFSIZE];
274     user_t *source_p, *target_p;
275    
276     source_p = user_find_named (from);
277     target_p = user_find_named (target);
278    
279     if (!target_p)
280     return;
281    
282     va_start (ap, fmt);
283     vsnprintf (buf, BUFSIZE, fmt, ap);
284     va_end (ap);
285    
286     sts ("%s %d %s %s", source_p ? source_p->uid : me.numeric, numeric, target_p->uid, buf);
287     }
288    
289     /* KILL wrapper */
290     void
291     nefarious_handler::skill (char const * const from, char const * const nick, char const * const fmt, ...)
292     {
293     va_list ap;
294     char buf[BUFSIZE];
295     user_t *fptr = user_find_named (from);
296     user_t *tptr = user_find_named (nick);
297    
298     if (!tptr)
299     return;
300    
301     va_start (ap, fmt);
302     vsnprintf (buf, BUFSIZE, fmt, ap);
303     va_end (ap);
304    
305     sts ("%s D %s :%s!%s!%s (%s)", fptr ? fptr->uid : me.numeric, tptr->uid, from, from, from, buf);
306     }
307    
308     /* PART wrapper */
309     void
310     nefarious_handler::part_sts (channel_t *c, user_t *u)
311     {
312     sts ("%s L %s", u->uid, c->name);
313     }
314    
315     /* server-to-server KLINE wrapper */
316     void
317     nefarious_handler::kline_sts (char const * const server, char const * const user, char const * const host, long duration, char const * const reason)
318     {
319     if (!me.connected)
320     return;
321    
322     /* hold permanent akills for four weeks -- jilles */
323     sts ("%s GL * +%s@%s %ld :%s", me.numeric, user, host, duration > 0 ? duration : 2419200, reason);
324     }
325    
326     /* server-to-server UNKLINE wrapper */
327     void
328     nefarious_handler::unkline_sts (char const * const server, char const * const user, char const * const host)
329     {
330     if (!me.connected)
331     return;
332    
333     sts ("%s GL * -%s@%s", me.numeric, user, host);
334     }
335    
336     /* topic wrapper */
337     void
338     nefarious_handler::topic_sts (channel_t *c, char const * const setter, time_t ts, time_t prevts, char const * const topic)
339     {
340     if (!me.connected || !c)
341     return;
342    
343     /* for nefarious, set topicsetter iff we can set the proper topicTS */
344     if (ts > prevts || prevts == 0)
345     sts ("%s T %s %s %ld %ld :%s", chansvs.me->me->uid, c->name, setter, c->ts, ts, topic);
346     else
347 pippijn 1.1 {
348 pippijn 1.4 ts = NOW;
349     if (ts < prevts)
350     ts = prevts + 1;
351     sts ("%s T %s %ld %ld :%s", chansvs.me->me->uid, c->name, c->ts, NOW, topic);
352     c->topicts = ts;
353 pippijn 1.1 }
354 pippijn 1.4 }
355    
356     /* mode wrapper */
357     void
358     nefarious_handler::mode_sts (char const * const sender, channel_t *target, char const * const modes)
359     {
360     user_t *fptr = user_find_named (sender);
361    
362     if (!fptr)
363     return;
364    
365     sts ("%s M %s %s", fptr->uid, target->name, modes);
366     }
367    
368     /* ping wrapper */
369     void
370     nefarious_handler::ping_sts (void)
371     {
372     if (!me.connected)
373 pippijn 1.1 return;
374 pippijn 1.4
375     sts ("%s G !%ld %s %ld", me.numeric, NOW, me.name, NOW);
376     }
377    
378     /* protocol-specific stuff to do on login */
379     void
380     nefarious_handler::ircd_on_login (char const * const origin, char const * const user, char const * const wantedhost)
381     {
382     user_t *u = user_find_named (origin);
383    
384     if (!u)
385     return;
386    
387     sts ("%s AC %s R %s", me.numeric, u->uid, u->myuser->name);
388     check_hidehost (u);
389     }
390    
391     /* P10 does not support logout, so kill the user
392     * we can't keep track of which logins are stale and which aren't -- jilles
393     * Except we can in Nefarious --nenolod
394 pippijn 1.1 */
395 pippijn 1.4 bool
396     nefarious_handler::ircd_on_logout (char const * const origin, char const * const user, char const * const wantedhost)
397     {
398     user_t *u = user_find_named (origin);
399    
400     if (!me.connected)
401     return false;
402    
403     if (!u)
404     return false;
405    
406     sts ("%s AC %s U", me.numeric, u->uid);
407     if (u->flags & UF_HIDEHOSTREQ && me.hidehostsuffix != NULL && !strcmp (u->vhost + strlen (u->vhost) - strlen (me.hidehostsuffix), me.hidehostsuffix))
408     {
409     slog (LG_DEBUG, "nefarious_ircd_on_logout(): removing +x vhost for %s: %s -> %s", u->nick, u->vhost, u->host);
410     strlcpy (u->vhost, u->host, sizeof u->vhost);
411 pippijn 1.1 }
412 pippijn 1.4
413     return false;
414     }
415    
416     void
417     nefarious_handler::jupe (char const * const server, char const * const reason)
418     {
419     server_t *s;
420    
421     if (!me.connected)
422     return;
423    
424     /* hold it for a day (arbitrary) -- jilles */
425     /* get rid of local deactivation too */
426     s = server_find (server);
427     if (s != NULL && s->uplink != NULL)
428     sts ("%s JU %s +%s %d %ld :%s", me.numeric, s->uplink->sid, server, 86400, NOW, reason);
429     sts ("%s JU * +%s %d %ld :%s", me.numeric, server, 86400, NOW, reason);
430     }
431    
432     static void
433     m_topic (sourceinfo_t *si, int parc, char *parv[])
434     {
435     channel_t *c = channel_find (parv[0]);
436     char *source;
437     time_t ts = 0;
438    
439     if (!c)
440 pippijn 1.1 return;
441 pippijn 1.4
442     if (si->s != NULL)
443     source = si->s->name;
444     else
445     source = si->su->nick;
446    
447     if (parc > 2)
448     ts = atoi (parv[parc - 2]);
449     if (ts == 0)
450     ts = NOW;
451     else if (c->topic != NULL && ts < c->topicts)
452 pippijn 1.1 return;
453 pippijn 1.4 handle_topic_from (si, c, parc > 4 ? parv[parc - 4] : source, ts, parv[parc - 1]);
454     }
455    
456     /* AB G !1119920789.573932 services.atheme.org 1119920789.573932 */
457     static void
458     m_ping (sourceinfo_t *si, int parc, char *parv[])
459     {
460     /* reply to PING's */
461     sts ("%s Z %s %s %s", me.numeric, parv[0], parv[1], parv[2]);
462     }
463    
464     static void
465     m_pong (sourceinfo_t *si, int parc, char *parv[])
466     {
467     me.uplinkpong = NOW;
468    
469     /* -> :test.projectxero.net PONG test.projectxero.net :shrike.malkier.net */
470     if (me.bursting)
471     {
472     #ifdef HAVE_GETTIMEOFDAY
473     e_time (burstime, &burstime);
474    
475     slog (LG_INFO, "m_pong(): finished synching with uplink (%d %s)", (tv2ms (&burstime) > 1000) ? (tv2ms (&burstime) / 1000) : tv2ms (&burstime), (tv2ms (&burstime) > 1000) ? "s" : "ms");
476    
477     wallops ("Finished synching to network in %d %s.", (tv2ms (&burstime) > 1000) ? (tv2ms (&burstime) / 1000) : tv2ms (&burstime), (tv2ms (&burstime) > 1000) ? "s" : "ms");
478     #else
479     slog (LG_INFO, "m_pong(): finished synching with uplink");
480     wallops ("Finished synching to network.");
481     #endif
482    
483     me.bursting = false;
484     }
485     }
486    
487     static void
488     m_privmsg (sourceinfo_t *si, int parc, char *parv[])
489     {
490     if (parc != 2)
491 pippijn 1.1 return;
492 pippijn 1.4
493     handle_message (si, parv[0], false, parv[1]);
494     }
495    
496     static void
497     m_notice (sourceinfo_t *si, int parc, char *parv[])
498     {
499     if (parc != 2)
500 pippijn 1.1 return;
501 pippijn 1.4
502     handle_message (si, parv[0], true, parv[1]);
503     }
504    
505     static void
506     m_create (sourceinfo_t *si, int parc, char *parv[])
507     {
508     char buf[BUFSIZE];
509     int chanc;
510     char *chanv[256];
511     int i;
512    
513     chanc = sjtoken (parv[0], ',', chanv);
514    
515     for (i = 0; i < chanc; i++)
516     {
517     channel_t *c = channel_add (chanv[i], atoi (parv[1]), si->su->server);
518    
519     /* Tell the core to check mode locks now,
520     * otherwise it may only happen after the next
521     * mode change.
522     * P10 does not allow any redundant modes
523     * so this will not look ugly. -- jilles */
524     channel_mode (NULL, c, "+");
525    
526     buf[0] = '@';
527     buf[1] = '\0';
528    
529     strlcat (buf, si->su->uid, BUFSIZE);
530    
531     chanuser_add (c, buf);
532     }
533     }
534    
535     static void
536     m_join (sourceinfo_t *si, int parc, char *parv[])
537     {
538     int chanc;
539     char *chanv[256];
540     int i;
541     node_t *n, *tn;
542     chanuser_t *cu;
543    
544     /* JOIN 0 is really a part from all channels */
545     if (!strcmp (parv[0], "0"))
546     {
547     LIST_FOREACH_SAFE (n, tn, si->su->channels.head)
548     {
549     cu = (chanuser_t *) n->data;
550     chanuser_delete (cu->chan, si->su);
551     }
552     return;
553     }
554     if (parc < 2)
555 pippijn 1.1 return;
556 pippijn 1.4
557     chanc = sjtoken (parv[0], ',', chanv);
558    
559     for (i = 0; i < chanc; i++)
560     {
561     channel_t *c = channel_find (chanv[i]);
562    
563     if (!c)
564     {
565     c = channel_add (chanv[i], atoi (parv[1]), si->su->server);
566     channel_mode (NULL, c, "+");
567     }
568    
569     chanuser_add (c, si->su->uid);
570     }
571     }
572    
573     static void
574     m_burst (sourceinfo_t *si, int parc, char *parv[])
575     {
576     channel_t *c;
577     unsigned int modec;
578     char *modev[16];
579     unsigned int userc;
580     char *userv[256];
581     unsigned int i;
582     int j;
583     char prefix[16];
584     char newnick[16 + NICKLEN];
585     char *p;
586     time_t ts;
587     int bantype;
588    
589     /* S BURST <channel> <ts> [parameters]
590     * parameters can be:
591     * +<simple mode>
592     * %<bans separated with spaces>
593     * <nicks>
594     */
595     ts = atoi (parv[1]);
596    
597     c = channel_find (parv[0]);
598    
599     if (c == NULL)
600     {
601     slog (LG_DEBUG, "m_burst(): new channel: %s", parv[0]);
602     c = channel_add (parv[0], ts, si->s);
603     }
604     else if (ts < c->ts)
605     {
606     chanuser_t *cu;
607     node_t *n;
608    
609     clear_simple_modes (c);
610     chanban_clear (c);
611     handle_topic_from (si, c, "", 0, "");
612     LIST_FOREACH (n, c->members.head)
613     {
614     cu = (chanuser_t *) n->data;
615     if (cu->user->server == me.me)
616     {
617     /* it's a service, reop */
618     sts ("%s M %s +o %s", me.numeric, c->name, CLIENT_NAME (cu->user));
619     cu->modes = CMODE_OP;
620     }
621     else
622     cu->modes = 0;
623     }
624    
625     slog (LG_DEBUG, "m_burst(): TS changed for %s (%ld -> %ld)", c->name, c->ts, ts);
626     c->ts = ts;
627     c->callback.tschange (c);
628     }
629     if (parc < 3 || parv[2][0] != '+')
630     {
631     /* Tell the core to check mode locks now,
632     * otherwise it may only happen after the next
633     * mode change. -- jilles */
634     channel_mode (NULL, c, "+");
635     }
636    
637     bantype = 'b';
638     j = 2;
639     while (j < parc)
640     {
641     if (parv[j][0] == '+')
642     {
643     modec = 0;
644     modev[modec++] = parv[j++];
645     if (strchr (modev[0], 'k') && j < parc)
646     modev[modec++] = parv[j++];
647     if (strchr (modev[0], 'l') && j < parc)
648     modev[modec++] = parv[j++];
649     channel_mode (NULL, c, modec, modev);
650     }
651     else if (parv[j][0] == '%')
652     {
653     userc = sjtoken (parv[j++] + 1, ' ', userv);
654     for (i = 0; i < userc; i++)
655     if (!strcmp (userv[i], "~"))
656     /* A ban "~" means exceptions are
657     * following */
658     bantype = 'e';
659     else
660     chanban_add (c, userv[i], bantype);
661     }
662     else
663     {
664     userc = sjtoken (parv[j++], ',', userv);
665    
666     prefix[0] = '\0';
667     for (i = 0; i < userc; i++)
668     {
669     p = strchr (userv[i], ':');
670     if (p != NULL)
671     {
672     *p = '\0';
673     prefix[0] = '\0';
674     prefix[1] = '\0';
675     prefix[2] = '\0';
676     p++;
677     while (*p)
678     {
679     if (*p == 'o')
680     prefix[prefix[0] ? 1 : 0] = '@';
681     else if (*p == 'h')
682     prefix[prefix[0] ? 1 : 0] = '%';
683     else if (*p == 'v')
684     prefix[prefix[0] ? 1 : 0] = '+';
685     p++;
686     }
687     }
688     strlcpy (newnick, prefix, sizeof newnick);
689     strlcat (newnick, userv[i], sizeof newnick);
690     chanuser_add (c, newnick);
691     }
692     }
693     }
694    
695     if (c->nummembers == 0 && !(c->modes & ircd->perm_mode))
696     channel_delete (c);
697     }
698    
699     static void
700     m_part (sourceinfo_t *si, int parc, char *parv[])
701     {
702     int chanc;
703     char *chanv[256];
704     int i;
705    
706     chanc = sjtoken (parv[0], ',', chanv);
707     for (i = 0; i < chanc; i++)
708     {
709     slog (LG_DEBUG, "m_part(): user left channel: %s -> %s", si->su->nick, chanv[i]);
710    
711     chanuser_delete (channel_find (chanv[i]), si->su);
712     }
713     }
714    
715     static void
716     m_nick (sourceinfo_t *si, int parc, char *parv[])
717     {
718     user_t *u;
719     struct in_addr ip;
720     char ipstring[64];
721     char *p;
722     int i;
723    
724     /* got the right number of args for an introduction? */
725     if (parc >= 8)
726     {
727     /* -> AB N jilles 1 1137687480 jilles jaguar.test +oiwgrx jilles B]AAAB ABAAE :Jilles Tjoelker */
728     /* -> AB N test4 1 1137690148 jilles jaguar.test +iw B]AAAB ABAAG :Jilles Tjoelker */
729     slog (LG_DEBUG, "m_nick(): new user on `%s': %s", si->s->name, parv[0]);
730    
731     ipstring[0] = '\0';
732     if (strlen (parv[parc - 3]) == 6)
733     {
734     ip.s_addr = ntohl (base64touint (parv[parc - 3]));
735     if (!inet_ntop (AF_INET, &ip, ipstring, sizeof ipstring))
736     ipstring[0] = '\0';
737     }
738     u = user_add (parv[0], parv[3], parv[4], NULL, ipstring, parv[parc - 2], parv[parc - 1], si->s, atoi (parv[2]));
739    
740     if (parv[5][0] == '+')
741     {
742     user_mode (u, parv[5]);
743     i = 1;
744     if (strchr (parv[5], 'r'))
745     {
746     handle_burstlogin (u, parv[5 + i]);
747     /* killed to force logout? */
748     if (user_find (parv[parc - 2]) == NULL)
749     return;
750     i++;
751     }
752     if (strchr (parv[5], 'h'))
753     {
754     p = strchr (parv[5 + i], '@');
755     if (p == NULL)
756     strlcpy (u->vhost, parv[5 + i], sizeof u->vhost);
757     else
758     {
759     strlcpy (u->vhost, p + 1, sizeof u->vhost);
760     strlcpy (u->user, parv[5 + i], sizeof u->user);
761     p = strchr (u->user, '@');
762     if (p != NULL)
763     *p = '\0';
764     }
765     i++;
766     }
767     if (strchr (parv[5], 'x'))
768     {
769     u->flags |= UF_HIDEHOSTREQ;
770     /* this must be after setting the account name */
771     check_hidehost (u);
772     }
773     }
774    
775     handle_nickchange (u);
776     }
777     /* if it's only 2 then it's a nickname change */
778     else if (parc == 2)
779     {
780     if (!si->su)
781     {
782     slog (LG_DEBUG, "m_nick(): server trying to change nick: %s", si->s != NULL ? si->s->name : "<none>");
783     return;
784     }
785    
786     slog (LG_DEBUG, "m_nick(): nickname change from `%s': %s", si->su->nick, parv[0]);
787    
788     user_changenick (si->su, parv[0], atoi (parv[1]));
789    
790     handle_nickchange (si->su);
791     }
792     else
793     {
794     slog (LG_DEBUG, "m_nick(): got NICK with wrong (%d) number of params", parc);
795    
796     for (i = 0; i < parc; i++)
797     slog (LG_DEBUG, "m_nick(): parv[%d] = %s", i, parv[i]);
798     }
799     }
800    
801     static void
802     m_quit (sourceinfo_t *si, int parc, char *parv[])
803     {
804     slog (LG_DEBUG, "m_quit(): user leaving: %s", si->su->nick);
805    
806     /* user_delete() takes care of removing channels and so forth */
807     user_delete (si->su);
808     }
809    
810     static void
811     m_mode (sourceinfo_t *si, int parc, char *parv[])
812     {
813     user_t *u;
814     char *p;
815    
816     if (*parv[0] == '#')
817     channel_mode (NULL, channel_find (parv[0]), parc - 1, &parv[1]);
818     else
819     {
820     /* Yes this is a nick and not a UID -- jilles */
821     u = user_find_named (parv[0]);
822     if (u == NULL)
823     {
824     slog (LG_DEBUG, "m_mode(): user mode for unknown user %s", parv[0]);
825     return;
826     }
827     user_mode (u, parv[1]);
828     if (strchr (parv[1], 'x'))
829     {
830     u->flags |= UF_HIDEHOSTREQ;
831     check_hidehost (u);
832     }
833     if (strchr (parv[1], 'h'))
834     {
835     if (parc > 2)
836     {
837     /* assume +h */
838     p = strchr (parv[2], '@');
839     if (p == NULL)
840     strlcpy (u->vhost, parv[2], sizeof u->vhost);
841     else
842     {
843     strlcpy (u->vhost, p + 1, sizeof u->vhost);
844     strlcpy (u->user, parv[2], sizeof u->user);
845     p = strchr (u->user, '@');
846     if (p != NULL)
847     *p = '\0';
848     }
849     slog (LG_DEBUG, "m_mode(): user %s setting vhost %s@%s", u->nick, u->user, u->vhost);
850     }
851     else
852     {
853     /* must be -h */
854     /* XXX we don't know the original ident */
855     slog (LG_DEBUG, "m_mode(): user %s turning off vhost", u->nick);
856     strlcpy (u->vhost, u->host, sizeof u->vhost);
857     /* revert to +x vhost if applicable */
858     check_hidehost (u);
859     }
860     }
861     }
862     }
863    
864     static void
865     m_clearmode (sourceinfo_t *si, int parc, char *parv[])
866     {
867     channel_t *chan;
868     char *p, c;
869     node_t *n, *tn;
870     chanuser_t *cu;
871     int i;
872    
873     /* -> ABAAA CM # b */
874     /* Note: this is an IRCop command, do not enforce mode locks. */
875     chan = channel_find (parv[0]);
876     if (chan == NULL)
877     {
878     slog (LG_DEBUG, "m_clearmode(): unknown channel %s", parv[0]);
879     return;
880     }
881     p = parv[1];
882     while ((c = *p++))
883     {
884     if (c == 'b')
885     {
886     LIST_FOREACH_SAFE (n, tn, chan->bans.head)
887     {
888     if (((chanban_t *) n->data)->type == 'b')
889     chanban_delete (static_cast<chanban_t *> (n->data));
890     }
891     }
892     else if (c == 'e')
893     {
894     LIST_FOREACH_SAFE (n, tn, chan->bans.head)
895     {
896     if (((chanban_t *) n->data)->type == 'e')
897     chanban_delete (static_cast<chanban_t *> (n->data));
898     }
899     }
900     else if (c == 'k')
901     {
902     if (chan->key)
903     sfree (chan->key);
904     chan->key = NULL;
905     }
906     else if (c == 'l')
907     chan->limit = 0;
908     else if (c == 'o')
909     {
910     LIST_FOREACH (n, chan->members.head)
911     {
912     cu = (chanuser_t *) n->data;
913     if (cu->user->server == me.me)
914     {
915     /* it's a service, reop */
916     sts ("%s M %s +o %s", me.numeric, chan->name, cu->user->uid);
917     }
918     else
919     cu->modes &= ~CMODE_OP;
920     }
921     }
922     else if (c == 'h')
923     {
924     LIST_FOREACH (n, chan->members.head)
925     {
926     cu = (chanuser_t *) n->data;
927     cu->modes &= ~CMODE_HALFOP;
928     }
929     }
930     else if (c == 'v')
931     {
932     LIST_FOREACH (n, chan->members.head)
933     {
934     cu = (chanuser_t *) n->data;
935     cu->modes &= ~CMODE_VOICE;
936     }
937     }
938     else
939     for (i = 0; mode_list[i].mode != '\0'; i++)
940     {
941     if (c == mode_list[i].mode)
942     chan->modes &= ~mode_list[i].value;
943     }
944     }
945     }
946    
947     static void
948     m_kick (sourceinfo_t *si, int parc, char *parv[])
949     {
950     user_t *u = user_find (parv[1]);
951     channel_t *c = channel_find (parv[0]);
952    
953     /* -> :rakaur KICK #shrike rintaun :test */
954     slog (LG_DEBUG, "m_kick(): user was kicked: %s -> %s", parv[1], parv[0]);
955    
956     if (!u)
957     {
958     slog (LG_DEBUG, "m_kick(): got kick for nonexistant user %s", parv[1]);
959     return;
960     }
961    
962     if (!c)
963     {
964     slog (LG_DEBUG, "m_kick(): got kick in nonexistant channel: %s", parv[0]);
965     return;
966     }
967    
968     if (!chanuser_find (c, u))
969     {
970     slog (LG_DEBUG, "m_kick(): got kick for %s not in %s", u->nick, c->name);
971     return;
972     }
973    
974     chanuser_delete (c, u);
975    
976     /* if they kicked us, let's rejoin */
977     if (is_internal_client (u))
978     {
979     slog (LG_DEBUG, "m_kick(): %s got kicked from %s; rejoining", u->nick, parv[0]);
980     join (parv[0], u->nick);
981     }
982     }
983    
984     static void
985     m_kill (sourceinfo_t *si, int parc, char *parv[])
986     {
987     handle_kill (si, parv[0], parc > 1 ? parv[1] : "<No reason given>");
988     }
989    
990     static void
991     m_squit (sourceinfo_t *si, int parc, char *parv[])
992     {
993     slog (LG_DEBUG, "m_squit(): server leaving: %s from %s", parv[0], parv[1]);
994     server_delete (parv[0]);
995     }
996    
997     /* SERVER ircu.devel.atheme.org 1 1119902586 1119908830 J10 ABAP] + :lets lol */
998     static void
999     m_server (sourceinfo_t *si, int parc, char *parv[])
1000     {
1001     server_t *s;
1002    
1003     /* We dont care about the max connections. */
1004     parv[5][2] = '\0';
1005    
1006     slog (LG_DEBUG, "m_server(): new server: %s, id %s, %s", parv[0], parv[5], parv[4][0] == 'P' ? "eob" : "bursting");
1007     s = handle_server (si, parv[0], parv[5], atoi (parv[1]), parv[7]);
1008    
1009     /* SF_EOB may only be set when we have all users on the server.
1010     * so store the fact that they are EOB in another flag.
1011     * handle_eob() will set SF_EOB when the uplink has finished bursting.
1012     * -- jilles */
1013     if (s != NULL && parv[4][0] == 'P')
1014     s->flags |= SF_EOB2;
1015     }
1016    
1017     static void
1018     m_stats (sourceinfo_t *si, int parc, char *parv[])
1019     {
1020     handle_stats (si->su, parv[0][0]);
1021     }
1022    
1023     static void
1024     m_admin (sourceinfo_t *si, int parc, char *parv[])
1025     {
1026     handle_admin (si->su);
1027     }
1028    
1029     static void
1030     m_version (sourceinfo_t *si, int parc, char *parv[])
1031     {
1032     handle_version (si->su);
1033     }
1034    
1035     static void
1036     m_info (sourceinfo_t *si, int parc, char *parv[])
1037     {
1038     handle_info (si->su);
1039     }
1040    
1041     static void
1042     m_motd (sourceinfo_t *si, int parc, char *parv[])
1043     {
1044     handle_motd (si->su);
1045     }
1046    
1047     static void
1048     m_whois (sourceinfo_t *si, int parc, char *parv[])
1049     {
1050     handle_whois (si->su, parv[1]);
1051     }
1052    
1053     static void
1054     m_trace (sourceinfo_t *si, int parc, char *parv[])
1055     {
1056     handle_trace (si->su, parv[0], parc >= 2 ? parv[1] : NULL);
1057     }
1058    
1059     static void
1060     m_away (sourceinfo_t *si, int parc, char *parv[])
1061     {
1062     handle_away (si->su, parc >= 1 ? parv[0] : NULL);
1063     }
1064    
1065     static void
1066     m_pass (sourceinfo_t *si, int parc, char *parv[])
1067     {
1068     if (strcmp (curr_uplink->pass, parv[0]))
1069     {
1070     slog (LG_INFO, "m_pass(): password mismatch from uplink; aborting");
1071     runflags |= RF_SHUTDOWN;
1072     }
1073     }
1074    
1075     static void
1076     m_error (sourceinfo_t *si, int parc, char *parv[])
1077     {
1078     slog (LG_INFO, "m_error(): error from server: %s", parv[0]);
1079     }
1080    
1081     static void
1082     m_eos (sourceinfo_t *si, int parc, char *parv[])
1083     {
1084     handle_eob (si->s);
1085    
1086     /* acknowledge a local END_OF_BURST */
1087     if (si->s->uplink == me.me)
1088     sts ("%s EA", me.numeric);
1089     }
1090    
1091     static void
1092     check_hidehost (user_t *u)
1093     {
1094     static bool warned = false;
1095    
1096     /* do they qualify? */
1097     if (!(u->flags & UF_HIDEHOSTREQ) || u->myuser == NULL || (u->myuser->flags & MU_WAITAUTH))
1098 pippijn 1.1 return;
1099 pippijn 1.4 /* don't use this if they have some other kind of vhost */
1100     if (strcmp (u->host, u->vhost))
1101     {
1102     slog (LG_DEBUG, "check_hidehost(): +x overruled by other vhost for %s", u->nick);
1103     return;
1104     }
1105     if (me.hidehostsuffix == NULL)
1106     {
1107     if (!warned)
1108     {
1109     wallops ("Misconfiguration: serverinfo::hidehostsuffix not set");
1110     warned = true;
1111     }
1112     return;
1113     }
1114     snprintf (u->vhost, sizeof u->vhost, "%s.%s", u->myuser->name, me.hidehostsuffix);
1115     slog (LG_DEBUG, "check_hidehost(): %s -> %s", u->nick, u->vhost);
1116     }
1117    
1118     static pcommand_t const pcommands[] = {
1119     { "G", m_ping, 1, MSRC_USER | MSRC_SERVER },
1120     { "Z", m_pong, 1, MSRC_SERVER },
1121     { "P", m_privmsg, 2, MSRC_USER },
1122     { "O", m_notice, 2, MSRC_USER | MSRC_SERVER },
1123     { "NOTICE", m_notice, 2, MSRC_UNREG },
1124     { "C", m_create, 1, MSRC_USER },
1125     { "J", m_join, 1, MSRC_USER },
1126     { "EB", m_eos, 0, MSRC_SERVER },
1127     { "B", m_burst, 2, MSRC_SERVER },
1128     { "L", m_part, 1, MSRC_USER },
1129     { "N", m_nick, 2, MSRC_USER | MSRC_SERVER },
1130     { "Q", m_quit, 1, MSRC_USER },
1131     { "M", m_mode, 2, MSRC_USER | MSRC_SERVER },
1132     { "OM", m_mode, 2, MSRC_USER }, /* OPMODE, treat as MODE */
1133     { "CM", m_clearmode, 2, MSRC_USER },
1134     { "K", m_kick, 2, MSRC_USER | MSRC_SERVER },
1135     { "D", m_kill, 1, MSRC_USER | MSRC_SERVER },
1136     { "SQ", m_squit, 1, MSRC_USER | MSRC_SERVER },
1137     { "S", m_server, 8, MSRC_SERVER },
1138     { "SERVER", m_server, 8, MSRC_UNREG },
1139     { "R", m_stats, 2, MSRC_USER },
1140     { "AD", m_admin, 1, MSRC_USER },
1141     { "V", m_version, 1, MSRC_USER },
1142     { "F", m_info, 1, MSRC_USER },
1143     { "W", m_whois, 2, MSRC_USER },
1144     { "TR", m_trace, 1, MSRC_USER },
1145     { "A", m_away, 0, MSRC_USER },
1146     { "PASS", m_pass, 1, MSRC_UNREG },
1147     { "Y", m_error, 1, MSRC_UNREG | MSRC_SERVER },
1148     { "ERROR", m_error, 1, MSRC_UNREG | MSRC_SERVER },
1149     { "T", m_topic, 2, MSRC_USER | MSRC_SERVER },
1150     { "MO", m_motd, 1, MSRC_USER },
1151     };
1152    
1153     nefarious_handler::nefarious_handler ()
1154     {
1155     parse = &p10_parse;
1156    
1157     mode_list = nefarious_mode_list;
1158     ignore_mode_list = nefarious_ignore_mode_list;
1159     status_mode_list = nefarious_status_mode_list;
1160     prefix_mode_list = nefarious_prefix_mode_list;
1161    
1162     ircd = &Nefarious;
1163    
1164     pcommand_add (&pcommands);
1165     }
1166    
1167     nefarious_handler::~nefarious_handler ()
1168     {
1169     parse = NULL;
1170    
1171     mode_list = NULL;
1172     ignore_mode_list = NULL;
1173     status_mode_list = NULL;
1174     prefix_mode_list = NULL;
1175    
1176     ircd = NULL;
1177    
1178     pcommand_delete (&pcommands);
1179     }
1180     } // namespace protocol
1181    
1182     #define FACREG_TYPE protocol::nefarious_handler
1183     #define FACREG_TYPE_NAME "nefarious"
1184     #define FACREG_INTERFACE_TYPE protocol::handler
1185     #include <ermyth/factory_reg.h>