]> jfr.im git - irc/evilnet/x3.git/blame - src/proto-p10.c
update memoserv help
[irc/evilnet/x3.git] / src / proto-p10.c
CommitLineData
d76ed9a9
AS
1/* proto-p10.c - IRC protocol output
2 * Copyright 2000-2004 srvx Development Team
3 *
83ff05c3 4 * This file is part of x3.
d76ed9a9
AS
5 *
6 * srvx is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with srvx; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 */
20
3fdd6a74 21#include "nickserv.h"
23475fc6 22#include "chanserv.h"
d76ed9a9
AS
23#include "proto-common.c"
24
25/* Full commands. */
26#define CMD_ACCOUNT "ACCOUNT"
27#define CMD_ADMIN "ADMIN"
28#define CMD_ASLL "ASLL"
29#define CMD_AWAY "AWAY"
30#define CMD_BURST "BURST"
31#define CMD_CLEARMODE "CLEARMODE"
32#define CMD_CLOSE "CLOSE"
33#define CMD_CNOTICE "CNOTICE"
34#define CMD_CONNECT "CONNECT"
35#define CMD_CPRIVMSG "CPRIVMSG"
36#define CMD_CREATE "CREATE"
37#define CMD_DESTRUCT "DESTRUCT"
38#define CMD_DESYNCH "DESYNCH"
39#define CMD_DIE "DIE"
40#define CMD_DNS "DNS"
41#define CMD_EOB "END_OF_BURST"
42#define CMD_EOB_ACK "EOB_ACK"
43#define CMD_ERROR "ERROR"
44#define CMD_FAKEHOST "FAKE"
45#define CMD_GET "GET"
46#define CMD_GLINE "GLINE"
47#define CMD_HASH "HASH"
48#define CMD_HELP "HELP"
49#define CMD_INFO "INFO"
50#define CMD_INVITE "INVITE"
51#define CMD_ISON "ISON"
52#define CMD_JOIN "JOIN"
53#define CMD_JUPE "JUPE"
54#define CMD_KICK "KICK"
55#define CMD_KILL "KILL"
56#define CMD_LINKS "LINKS"
57#define CMD_LIST "LIST"
58#define CMD_LUSERS "LUSERS"
59#define CMD_MAP "MAP"
23475fc6 60#define CMD_MARK "MARK"
d76ed9a9
AS
61#define CMD_MODE "MODE"
62#define CMD_MOTD "MOTD"
63#define CMD_NAMES "NAMES"
64#define CMD_NICK "NICK"
65#define CMD_NOTICE "NOTICE"
66#define CMD_OPER "OPER"
67#define CMD_OPMODE "OPMODE"
68#define CMD_PART "PART"
69#define CMD_PASS "PASS"
70#define CMD_PING "PING"
71#define CMD_PONG "PONG"
72#define CMD_POST "POST"
73#define CMD_PRIVMSG "PRIVMSG"
74#define CMD_PRIVS "PRIVS"
75#define CMD_PROTO "PROTO"
76#define CMD_QUIT "QUIT"
77#define CMD_REHASH "REHASH"
78#define CMD_RESET "RESET"
79#define CMD_RESTART "RESTART"
80#define CMD_RPING "RPING"
81#define CMD_RPONG "RPONG"
82#define CMD_SERVER "SERVER"
83#define CMD_SERVLIST "SERVLIST"
84#define CMD_SERVSET "SERVSET"
85#define CMD_SET "SET"
86#define CMD_SETTIME "SETTIME"
87#define CMD_SILENCE "SILENCE"
88#define CMD_SQUERY "SQUERY"
89#define CMD_SQUIT "SQUIT"
90#define CMD_STATS "STATS"
91#define CMD_SVSNICK "SVSNICK"
92#define CMD_TIME "TIME"
93#define CMD_TOPIC "TOPIC"
94#define CMD_TRACE "TRACE"
95#define CMD_UPING "UPING"
96#define CMD_USER "USER"
97#define CMD_USERHOST "USERHOST"
98#define CMD_USERIP "USERIP"
99#define CMD_VERSION "VERSION"
100#define CMD_WALLCHOPS "WALLCHOPS"
101#define CMD_WALLOPS "WALLOPS"
55342ce8 102#define CMD_WALLHOPS "WALLHOPS"
d76ed9a9
AS
103#define CMD_WALLUSERS "WALLUSERS"
104#define CMD_WALLVOICES "WALLVOICES"
55342ce8 105#define CMD_WALLHOPS "WALLHOPS"
d76ed9a9
AS
106#define CMD_WHO "WHO"
107#define CMD_WHOIS "WHOIS"
108#define CMD_WHOWAS "WHOWAS"
109
110/* Tokenized commands. */
111#define TOK_ACCOUNT "AC"
112#define TOK_ADMIN "AD"
113#define TOK_ASLL "LL"
114#define TOK_AWAY "A"
115#define TOK_BURST "B"
116#define TOK_CLEARMODE "CM"
117#define TOK_CLOSE "CLOSE"
118#define TOK_CNOTICE "CN"
119#define TOK_CONNECT "CO"
120#define TOK_CPRIVMSG "CP"
121#define TOK_CREATE "C"
122#define TOK_DESTRUCT "DE"
123#define TOK_DESYNCH "DS"
124#define TOK_DIE "DIE"
125#define TOK_DNS "DNS"
126#define TOK_EOB "EB"
127#define TOK_EOB_ACK "EA"
128#define TOK_ERROR "Y"
fede8b64 129#define TOK_EXEMPT "EX"
d76ed9a9
AS
130#define TOK_FAKEHOST "FA"
131#define TOK_GET "GET"
132#define TOK_GLINE "GL"
133#define TOK_HASH "HASH"
134#define TOK_HELP "HELP"
135#define TOK_INFO "F"
136#define TOK_INVITE "I"
137#define TOK_ISON "ISON"
138#define TOK_JOIN "J"
139#define TOK_JUPE "JU"
140#define TOK_KICK "K"
141#define TOK_KILL "D"
142#define TOK_LINKS "LI"
143#define TOK_LIST "LIST"
144#define TOK_LUSERS "LU"
145#define TOK_MAP "MAP"
23475fc6 146#define TOK_MARK "MK"
d76ed9a9
AS
147#define TOK_MODE "M"
148#define TOK_MOTD "MO"
149#define TOK_NAMES "E"
150#define TOK_NICK "N"
151#define TOK_NOTICE "O"
152#define TOK_OPER "OPER"
153#define TOK_OPMODE "OM"
154#define TOK_PART "L"
155#define TOK_PASS "PA"
156#define TOK_PING "G"
157#define TOK_PONG "Z"
158#define TOK_POST "POST"
159#define TOK_PRIVMSG "P"
160#define TOK_PRIVS "PRIVS"
161#define TOK_PROTO "PROTO"
162#define TOK_QUIT "Q"
163#define TOK_REHASH "REHASH"
164#define TOK_RESET "RESET"
165#define TOK_RESTART "RESTART"
166#define TOK_RPING "RI"
167#define TOK_RPONG "RO"
168#define TOK_SERVER "S"
169#define TOK_SERVLIST "SERVSET"
170#define TOK_SERVSET "SERVSET"
171#define TOK_SET "SET"
172#define TOK_SETTIME "SE"
173#define TOK_SILENCE "U"
174#define TOK_SQUERY "SQUERY"
175#define TOK_SQUIT "SQ"
176#define TOK_STATS "R"
177#define TOK_SVSNICK "SN"
178#define TOK_TIME "TI"
179#define TOK_TOPIC "T"
180#define TOK_TRACE "TR"
181#define TOK_UPING "UP"
182#define TOK_USER "USER"
183#define TOK_USERHOST "USERHOST"
184#define TOK_USERIP "USERIP"
185#define TOK_VERSION "V"
186#define TOK_WALLCHOPS "WC"
187#define TOK_WALLOPS "WA"
55342ce8 188#define TOK_WALLHOPS "WH"
d76ed9a9
AS
189#define TOK_WALLUSERS "WU"
190#define TOK_WALLVOICES "WV"
55342ce8 191#define TOK_WALLHOPS "WH"
d76ed9a9
AS
192#define TOK_WHO "H"
193#define TOK_WHOIS "W"
194#define TOK_WHOWAS "X"
195
196/* Protocol messages; aliased to full commands or tokens depending
197 on compile-time configuration. ircu prefers tokens WITH THE
198 EXCEPTION OF THE SERVER AND PASS COMMANDS, which cannot be
199 tokenized, because clients' (ie. a linking server) commands are
200 only checked against the full command list.
201*/
202#if defined(ENABLE_TOKENS)
203#define TYPE(NAME) TOK_ ## NAME
204#else /* !ENABLE_TOKENS */
205#define TYPE(NAME) CMD_ ## NAME
206#endif /* ENABLE_TOKENS */
207
208#define P10_ACCOUNT TYPE(ACCOUNT)
209#define P10_ADMIN TYPE(ADMIN)
210#define P10_ASLL TYPE(ASLL)
211#define P10_AWAY TYPE(AWAY)
212#define P10_BURST TYPE(BURST)
213#define P10_CLEARMODE TYPE(CLEARMODE)
214#define P10_CLOSE TYPE(CLOSE)
215#define P10_CNOTICE TYPE(CNOTICE)
216#define P10_CONNECT TYPE(CONNECT)
217#define P10_CPRIVMSG TYPE(CPRIVMSG)
218#define P10_CREATE TYPE(CREATE)
219#define P10_DESTRUCT TYPE(DESTRUCT)
220#define P10_DESYNCH TYPE(DESYNCH)
221#define P10_DIE TYPE(DIE)
222#define P10_DNS TYPE(DNS)
223#define P10_EOB TYPE(EOB)
224#define P10_EOB_ACK TYPE(EOB_ACK)
225#define P10_ERROR TYPE(ERROR)
226#define P10_FAKEHOST TYPE(FAKEHOST)
227#define P10_GET TYPE(GET)
228#define P10_GLINE TYPE(GLINE)
229#define P10_HASH TYPE(HASH)
230#define P10_HELP TYPE(HELP)
231#define P10_INFO TYPE(INFO)
232#define P10_INVITE TYPE(INVITE)
233#define P10_ISON TYPE(ISON)
234#define P10_JOIN TYPE(JOIN)
235#define P10_JUPE TYPE(JUPE)
236#define P10_KICK TYPE(KICK)
237#define P10_KILL TYPE(KILL)
238#define P10_LINKS TYPE(LINKS)
239#define P10_LIST TYPE(LIST)
240#define P10_LUSERS TYPE(LUSERS)
241#define P10_MAP TYPE(MAP)
23475fc6 242#define P10_MARK TYPE(MARK)
d76ed9a9
AS
243#define P10_MODE TYPE(MODE)
244#define P10_MOTD TYPE(MOTD)
245#define P10_NAMES TYPE(NAMES)
246#define P10_NICK TYPE(NICK)
247#define P10_NOTICE TYPE(NOTICE)
248#define P10_OPER TYPE(OPER)
249#define P10_OPMODE TYPE(OPMODE)
250#define P10_PART TYPE(PART)
251#define P10_PASS CMD_PASS
252#define P10_PING TYPE(PING)
253#define P10_PONG TYPE(PONG)
254#define P10_POST TYPE(POST)
255#define P10_PRIVMSG TYPE(PRIVMSG)
256#define P10_PRIVS TYPE(PRIVS)
257#define P10_PROTO TYPE(PROTO)
258#define P10_QUIT TYPE(QUIT)
259#define P10_REHASH TYPE(REHASH)
260#define P10_RESET TYPE(RESET)
261#define P10_RESTART TYPE(RESTART)
262#define P10_RPING TYPE(RPING)
263#define P10_RPONG TYPE(RPONG)
264#define P10_SERVER CMD_SERVER
265#define P10_SERVLIST TYPE(SERVLIST)
266#define P10_SERVSET TYPE(SERVSET)
267#define P10_SET TYPE(SET)
268#define P10_SETTIME TYPE(SETTIME)
269#define P10_SILENCE TYPE(SILENCE)
270#define P10_SQUERY TYPE(SQUERY)
271#define P10_SQUIT TYPE(SQUIT)
272#define P10_STATS TYPE(STATS)
273#define P10_SVSNICK TYPE(SVSNICK)
274#define P10_TIME TYPE(TIME)
275#define P10_TOPIC TYPE(TOPIC)
276#define P10_TRACE TYPE(TRACE)
277#define P10_UPING TYPE(UPING)
278#define P10_USER TYPE(USER)
279#define P10_USERHOST TYPE(USERHOST)
280#define P10_USERIP TYPE(USERIP)
281#define P10_VERSION TYPE(VERSION)
282#define P10_WALLCHOPS TYPE(WALLCHOPS)
283#define P10_WALLOPS TYPE(WALLOPS)
55342ce8 284#define P10_WALLHOPS TYPE(WALLHOPS)
d76ed9a9
AS
285#define P10_WALLUSERS TYPE(WALLUSERS)
286#define P10_WALLVOICES TYPE(WALLVOICES)
287#define P10_WHO TYPE(WHO)
288#define P10_WHOIS TYPE(WHOIS)
289#define P10_WHOWAS TYPE(WHOWAS)
850bf0cf 290#define P10_EXEMPT TYPE(EXEMPT)
d76ed9a9
AS
291
292/* Servers claiming to have a boot or link time before PREHISTORY
293 * trigger errors to the log. We hope no server has been running
294 * constantly since September 1994. :)
295 */
296#define PREHISTORY 780000000
297
298static struct server *servers_num[64*64];
299static privmsg_func_t *privmsg_funcs;
300static unsigned int num_privmsg_funcs;
301static privmsg_func_t *notice_funcs;
302static unsigned int num_notice_funcs;
303static struct dict *unbursted_channels;
304static char *his_servername;
305static char *his_servercomment;
306
307static struct userNode *AddUser(struct server* uplink, const char *nick, const char *ident, const char *hostname, const char *modes, const char *numeric, const char *userinfo, time_t timestamp, const char *realip);
308
309extern int off_channel;
310
311/* Numerics can be XYY, XYYY, or XXYYY; with X's identifying the
312 * server and Y's indentifying the client on that server. */
313struct server*
314GetServerN(const char *numeric)
315{
316 switch (strlen(numeric)) {
317 default:
318 return servers_num[base64toint(numeric, 2)];
319 case 4:
320 case 3:
321 case 1:
322 return servers_num[base64toint(numeric, 1)];
323 case 0:
324 return NULL;
325 }
326}
327
328struct userNode*
329GetUserN(const char *numeric) /* using numeric */
330{
331 struct userNode *un;
332 struct server *s;
333 int n, slen, ulen;
334
335 switch (strlen(numeric)) {
336 default:
337 log_module(MAIN_LOG, LOG_WARNING, "GetUserN(%s): numeric too long!", numeric);
338 return NULL;
339 case 5: slen = 2; ulen = 3; break;
340 case 4: slen = 1; ulen = 3; break;
341 case 3: slen = 1; ulen = 2; break;
342 case 2: case 1: case 0:
343 log_module(MAIN_LOG, LOG_WARNING, "GetUserN(%s): numeric too short!", numeric);
344 return NULL;
345 }
346 if (!(s = servers_num[base64toint(numeric, slen)])) {
347 log_module(MAIN_LOG, LOG_WARNING, "GetUserN(%s): couldn't find server (len=%d)!", numeric, slen);
348 return NULL;
349 }
350 n = base64toint(numeric+slen, ulen) & s->num_mask;
351 if (!(un = s->users[n])) {
352 log_module(MAIN_LOG, LOG_WARNING, "GetUserN(%s) couldn't find user!", numeric);
353 }
354 return un;
355}
356
357static void
358privmsg_user_helper(struct userNode *un, void *data)
359{
360 struct privmsg_desc *pd = data;
361 unsigned int num = un->num_local;
362 if (!pd->is_notice) {
363 if ((num < num_privmsg_funcs) && privmsg_funcs[num]) {
364 privmsg_funcs[num](pd->user, un, pd->text, pd->is_qualified);
365 }
366 } else {
367 if ((num < num_notice_funcs) && notice_funcs[num]) {
368 notice_funcs[num](pd->user, un, pd->text, pd->is_qualified);
369 }
370 }
371}
372
373void
374irc_server(struct server *srv)
375{
376 char extranum[COMBO_NUMERIC_LEN+1];
377
378 inttobase64(extranum, srv->num_mask, (srv->numeric[1] || (srv->num_mask >= 64*64)) ? 3 : 2);
379 if (srv == self) {
380 /* The +s, ignored by Run's ircu, means "service" to Undernet's ircu */
381 putsock(P10_SERVER " %s %d %li %li J10 %s%s +s :%s",
382 srv->name, srv->hops+1, srv->boot, srv->link, srv->numeric, extranum, srv->description);
383 } else {
384 putsock("%s " P10_SERVER " %s %d %li %li %c10 %s%s +s :%s",
385 self->numeric, srv->name, srv->hops+1, srv->boot, srv->link, (srv->self_burst ? 'J' : 'P'), srv->numeric, extranum, srv->description);
386 }
387}
388
389void
390irc_user(struct userNode *user)
391{
392 char b64ip[7];
393 if (!user)
394 return;
395 inttobase64(b64ip, ntohl(user->ip.s_addr), 6);
396 if (user->modes) {
397 int modelen;
398 char modes[32];
399
400 modelen = 0;
401 if (IsOper(user))
402 modes[modelen++] = 'o';
403 if (IsInvisible(user))
404 modes[modelen++] = 'i';
405 if (IsWallOp(user))
406 modes[modelen++] = 'w';
407 if (IsService(user))
408 modes[modelen++] = 'k';
409 if (IsServNotice(user))
410 modes[modelen++] = 's';
411 if (IsDeaf(user))
412 modes[modelen++] = 'd';
413 if (IsGlobal(user))
414 modes[modelen++] = 'g';
182dd032
AS
415 // sethost - reed/apples
416 // if (IsHelperIrcu(user))
417 if (IsSetHost(user))
d76ed9a9 418 modes[modelen++] = 'h';
182dd032
AS
419 if (IsFakeHost(user))
420 modes[modelen++] = 'f';
d76ed9a9
AS
421 if (IsHiddenHost(user))
422 modes[modelen++] = 'x';
423 modes[modelen] = 0;
424
425 /* we don't need to put the + in modes because it's in the format string. */
426 putsock("%s " P10_NICK " %s %d %li %s %s +%s %s %s :%s",
427 user->uplink->numeric, user->nick, user->uplink->hops+1, user->timestamp, user->ident, user->hostname, modes, b64ip, user->numeric, user->info);
428 } else {
429 putsock("%s " P10_NICK " %s %d %li %s %s %s %s :%s",
430 user->uplink->numeric, user->nick, user->uplink->hops+1, user->timestamp, user->ident, user->hostname, b64ip, user->numeric, user->info);
431 }
432}
433
434void
b21e2cfe 435irc_account(struct userNode *user, const char *stamp, time_t timestamp)
d76ed9a9 436{
b21e2cfe 437 putsock("%s " P10_ACCOUNT " %s R %s %lu", self->numeric, user->numeric, stamp, timestamp);
d76ed9a9
AS
438}
439
440void
441irc_fakehost(struct userNode *user, const char *host)
442{
443 putsock("%s " P10_FAKEHOST " %s %s", self->numeric, user->numeric, host);
444}
445
446void
447irc_regnick(UNUSED_ARG(struct userNode *user))
448{
449 /* no operation here */
450}
451
452void
453irc_nick(struct userNode *user, UNUSED_ARG(const char *old_nick))
454{
455 putsock("%s " P10_NICK " %s "FMT_TIME_T, user->numeric, user->nick, now);
456}
457
458void
459irc_fetchtopic(struct userNode *from, const char *to)
460{
461 if (!from || !to)
462 return;
463 putsock("%s " P10_TOPIC " %s", from->numeric, to);
464}
465
466void
467irc_squit(struct server *srv, const char *message, const char *service_message)
468{
469 if (!service_message)
470 service_message = message;
471
472 /* Are we leaving the network? */
473 if (srv == self && cManager.uplink->state == CONNECTED) {
474 unsigned int i;
475
476 /* Quit all clients linked to me. */
477 for (i = 0; i <= self->num_mask; i++) {
478 if (!self->users[i])
479 continue;
480 irc_quit(self->users[i], service_message);
481 }
482 }
483
484 putsock("%s " P10_SQUIT " %s %d :%s", self->numeric, srv->name, 0, message);
485
486 if (srv == self) {
487 /* Force a reconnect to the currently selected server. */
488 cManager.uplink->tries = 0;
489 log_module(MAIN_LOG, LOG_INFO, "Squitting from uplink: %s", message);
490 close_socket();
491 }
492}
493
494void
495irc_wallchops(struct userNode *from, const char *to, const char *message)
496{
497 putsock("%s " P10_WALLCHOPS " %s :%s", from->numeric, to, message);
498}
499
500void
501irc_notice(struct userNode *from, const char *to, const char *message)
502{
503 putsock("%s " P10_NOTICE " %s :%s", from->numeric, to, message);
504}
505
506void
507irc_notice_user(struct userNode *from, struct userNode *to, const char *message)
508{
509 putsock("%s " P10_NOTICE " %s :%s", from->numeric, to->numeric, message);
510}
511
512void
513irc_privmsg(struct userNode *from, const char *to, const char *message)
514{
515 putsock("%s " P10_PRIVMSG " %s :%s", from->numeric, to, message);
516}
517
518void
519irc_eob(void)
520{
521 putsock("%s " P10_EOB, self->numeric);
522}
523
524void
525irc_eob_ack(void)
526{
527 putsock("%s " P10_EOB_ACK, self->numeric);
528}
529
530void
531irc_ping(const char *payload)
532{
533 putsock("%s " P10_PING " :%s", self->numeric, payload);
534}
535
536void
537irc_pong(const char *who, const char *data)
538{
539 putsock("%s " P10_PONG " %s :%s", self->numeric, who, data);
540}
541
542void
543irc_pass(const char *passwd)
544{
545 putsock(P10_PASS " :%s", passwd);
546}
547
548void
549irc_introduce(const char *passwd)
550{
551 void timed_send_ping(void *data);
552
553 self->self_burst = self->burst = 1;
554 irc_pass(passwd);
555 irc_server(self);
556 burst_length = 0;
557 timeq_add(now + ping_freq, timed_send_ping, 0);
558}
559
560void
561irc_gline(struct server *srv, struct gline *gline)
562{
d0cb2fb6
AS
563 putsock("%s " P10_GLINE " %s +%s %ld :<%s> %s",
564 self->numeric, (srv ? srv->numeric : "*"), gline->target, gline->expires-now, gline->issuer, gline->reason);
d76ed9a9
AS
565}
566
567void
568irc_settime(const char *srv_name_mask, time_t new_time)
569{
570 ioset_set_time(new_time);
571 if (!strcmp(srv_name_mask, "*"))
572 srv_name_mask = "";
573 putsock("%s " P10_SETTIME " " FMT_TIME_T " %s", self->numeric, new_time, srv_name_mask);
574}
575
576void
577irc_ungline(const char *mask)
578{
579 putsock("%s " P10_GLINE " * -%s", self->numeric, mask);
580}
581
582static void
583irc_burst(struct chanNode *chan)
584{
585 char burst_line[512];
586 int pos, base_len, len;
587 struct modeNode *mn;
588 struct banNode *bn;
2aef5f4b 589 struct exemptNode *en;
d76ed9a9
AS
590 long last_mode=-1;
591 unsigned int n;
592
593 base_len = sprintf(burst_line, "%s " P10_BURST " %s " FMT_TIME_T " ",
594 self->numeric, chan->name, chan->timestamp);
595 len = irc_make_chanmode(chan, burst_line+base_len);
596 pos = base_len + len;
597 if (len)
598 burst_line[pos++] = ' ';
599
600 /* dump the users */
601 for (n=0; n<chan->members.used; n++) {
602 mn = chan->members.list[n];
603 if (pos > 500) {
604 burst_line[pos-1] = 0; /* -1 to back up over the space or comma */
605 putsock("%s", burst_line);
606 pos = base_len;
607 last_mode = -1;
608 }
609 memcpy(burst_line+pos, mn->user->numeric, strlen(mn->user->numeric));
610 pos += strlen(mn->user->numeric);
611 if (mn->modes && (mn->modes != last_mode)) {
612 last_mode = mn->modes;
613 burst_line[pos++] = ':';
614 if (last_mode & MODE_CHANOP)
615 burst_line[pos++] = 'o';
55342ce8 616 if (last_mode & MODE_HALFOP)
617 burst_line[pos++] = 'h';
d76ed9a9
AS
618 if (last_mode & MODE_VOICE)
619 burst_line[pos++] = 'v';
620 }
621 if ((n+1)<chan->members.used)
622 burst_line[pos++] = ',';
623 }
624 if (chan->banlist.used) {
625 /* dump the bans */
626 if (pos+2+strlen(chan->banlist.list[0]->ban) > 505) {
627 burst_line[pos-1] = 0;
628 putsock("%s", burst_line);
629 pos = base_len;
630 } else {
631 burst_line[pos++] = ' ';
632 }
633
634 burst_line[pos++] = ':';
635 burst_line[pos++] = '%';
636 base_len = pos;
637 for (n=0; n<chan->banlist.used; n++) {
638 bn = chan->banlist.list[n];
639 len = strlen(bn->ban);
640 if (pos+len+1 > 510) {
641 burst_line[pos-1] = 0; /* -1 to back up over the space or comma */
642 putsock("%s", burst_line);
643 pos = base_len;
644 }
645 memcpy(burst_line+pos, bn->ban, len);
646 pos += len;
647 burst_line[pos++] = ' ';
648 }
649 }
2aef5f4b 650 if (chan->exemptlist.used) {
651 /* dump the exempt */
652 if (pos+2+strlen(chan->exemptlist.list[0]->exempt) > 505) {
653 burst_line[pos-1] = 0;
654 putsock("%s", burst_line);
655 pos = base_len;
656 } else {
657 burst_line[pos++] = ' ';
658 }
659
660 burst_line[pos++] = ' ';
661 burst_line[pos++] = '~';
662 burst_line[pos++] = ' ';
663 base_len = pos;
664 for (n=0; n<chan->exemptlist.used; n++) {
665 en = chan->exemptlist.list[n];
666 len = strlen(en->exempt);
667 if (pos+len+1 > 510) {
668 burst_line[pos-1] = 0; /* -1 to back up over the space or comma */
669 putsock("%s", burst_line);
670 pos = base_len;
671 }
672 memcpy(burst_line+pos, en->exempt, len);
673 pos += len;
674 burst_line[pos++] = ' ';
675 }
676 }
d76ed9a9
AS
677 /* print the last line */
678 burst_line[pos] = 0;
679 putsock("%s", burst_line);
680}
681
682void
683irc_quit(struct userNode *user, const char *message)
684{
685 putsock("%s " P10_QUIT " :%s", user->numeric, message);
686}
687
688void
689irc_error(const char *to, const char *message)
690{
691 if (to) {
692 putsock("%s " P10_ERROR " :%s", to, message);
693 } else {
694 putsock(":%s " P10_ERROR " :%s", self->name, message);
695 }
696}
697
698void
699irc_kill(struct userNode *from, struct userNode *target, const char *message)
700{
701 if (from) {
702 putsock("%s " P10_KILL " %s :%s!%s (%s)",
703 from->numeric, target->numeric, self->name, from->nick, message);
704 } else {
705 putsock("%s " P10_KILL " %s :%s (%s)",
706 self->numeric, target->numeric, self->name, message);
707 }
708}
709
710void
711irc_mode(struct userNode *from, struct chanNode *target, const char *modes)
712{
713 putsock("%s " P10_MODE " %s %s "FMT_TIME_T,
714 (from ? from->numeric : self->numeric),
715 target->name, modes, target->timestamp);
716}
717
5a1daaab
AS
718/* Added to allow services to mode users
719 2005 - 8 - 10 by Life4Christ
720*/
721void
722irc_umode(struct userNode *target, const char *modes)
723{
724 putsock("%s " P10_MODE " %s %s ",self->numeric,target->nick, modes);
725}
726
727
d76ed9a9
AS
728void
729irc_invite(struct userNode *from, struct userNode *who, struct chanNode *to)
730{
731 putsock("%s " P10_INVITE " %s %s", from->numeric, who->nick, to->name);
732}
733
734void
735irc_join(struct userNode *who, struct chanNode *what)
736{
737 if (what->members.used == 1) {
738 putsock("%s " P10_CREATE " %s %lu",
739 who->numeric, what->name, what->timestamp);
740 } else {
741 putsock("%s " P10_JOIN " %s %lu", who->numeric, what->name, what->timestamp);
742 }
743}
744
745void
746irc_kick(struct userNode *who, struct userNode *target, struct chanNode *channel, const char *msg)
747{
748 const char *numeric;
749 struct modeNode *mn = GetUserMode(channel, who);
750 numeric = ((mn && (mn->modes & MODE_CHANOP)) || off_channel) ? who->numeric : self->numeric;
751 putsock("%s " P10_KICK " %s %s :%s",
752 numeric, channel->name, target->numeric, msg);
753}
754
755void
756irc_stats(struct userNode *from, struct server *target, char type)
757{
758 putsock("%s " P10_STATS " %c :%s", from->numeric, type, target->numeric);
759}
760
761void
762irc_svsnick(struct userNode *from, struct userNode *target, const char *newnick)
763{
764 putsock("%s " P10_SVSNICK " %s %s "FMT_TIME_T, from->uplink->numeric, target->numeric, newnick, now);
765}
766
767void
768irc_part(struct userNode *who, struct chanNode *what, const char *reason)
769{
770 if (reason) {
771 putsock("%s " P10_PART " %s :%s", who->numeric, what->name, reason);
772 } else {
773 putsock("%s " P10_PART " %s", who->numeric, what->name);
774 }
775}
776
777void
778irc_topic(struct userNode *who, struct chanNode *what, const char *topic)
779{
780 putsock("%s " P10_TOPIC " %s :%s", who->numeric, what->name, topic);
781}
782
783void
784irc_raw(const char *what)
785{
786 putsock("%s", what);
787}
788
789void
790irc_numeric(struct userNode *user, unsigned int num, const char *format, ...)
791{
792 va_list arg_list;
793 char buffer[MAXLEN];
794 va_start(arg_list, format);
795 vsnprintf(buffer, MAXLEN-2, format, arg_list);
796 buffer[MAXLEN-1] = 0;
797 putsock(":%s %03d %s %s", self->name, num, user->nick, buffer);
798}
799
800static void send_burst(void);
801
802static void
803change_nicklen(int new_nicklen)
804{
805 unsigned int nn;
806 char new_nick[NICKLEN+1];
807 struct userNode *user;
808
809 nicklen = new_nicklen;
810 /* fix up any users we have here */
811 for (nn=0; nn<=self->num_mask; nn++) {
812 if (!(user = self->users[nn]))
813 continue;
814 safestrncpy(new_nick, user->nick, sizeof(new_nick));
815 new_nick[nicklen] = 0;
816 NickChange(user, new_nick, 1);
817 }
818}
819
820static CMD_FUNC(cmd_whois)
821{
822 struct userNode *from;
823 struct userNode *who;
824
825 if (argc < 3)
826 return 0;
827 if (!(from = GetUserH(origin))) {
828 log_module(MAIN_LOG, LOG_ERROR, "Could not find WHOIS origin user %s", origin);
829 return 0;
830 }
831 if(!(who = GetUserH(argv[2]))) {
832 irc_numeric(from, ERR_NOSUCHNICK, "%s@%s :No such nick", argv[2], self->name);
833 return 1;
834 }
835 if (IsHiddenHost(who) && !IsOper(from)) {
836 /* Just stay quiet. */
837 return 1;
838 }
839 irc_numeric(from, RPL_WHOISUSER, "%s %s %s * :%s", who->nick, who->ident, who->hostname, who->info);
840 if (his_servername && his_servercomment)
a32da4c7 841 irc_numeric(from, RPL_WHOISSERVER, "%s %s :%s", who->nick, his_servername, his_servercomment);
d76ed9a9 842 else
a32da4c7 843 irc_numeric(from, RPL_WHOISSERVER, "%s %s :%s", who->nick, who->uplink->name, who->uplink->description);
844 if (IsOper(who))
d76ed9a9 845 irc_numeric(from, RPL_WHOISOPERATOR, "%s :is a megalomaniacal power hungry tyrant", who->nick);
d76ed9a9
AS
846 irc_numeric(from, RPL_ENDOFWHOIS, "%s :End of /WHOIS list", who->nick);
847 return 1;
848}
849
850static CMD_FUNC(cmd_server)
851{
852 struct server *srv;
853 const char *str;
854
855 if (argc < 8)
856 return 0;
a32da4c7 857 if (self->uplink) {
d76ed9a9
AS
858 /* another server introduced us */
859 srv = AddServer(GetServerH(origin), argv[1], atoi(argv[2]), atoi(argv[3]), atoi(argv[4]), argv[6], argv[argc-1]);
860 if (!srv)
861 return 0;
862 srv->self_burst = argv[5][0] == 'J';
863 srv->burst = 1;
864 } else {
865 /* this must be our uplink */
866 srv = self->uplink = AddServer(self, argv[1], atoi(argv[2]), atoi(argv[3]), atoi(argv[4]), argv[6], argv[argc-1]);
867 if (!srv)
868 return 0;
869 srv->self_burst = argv[5][0] == 'J';
870 srv->burst = 1;
871 if ((argv[7][0] == '+') && !force_n2k) {
872 log_module(MAIN_LOG, LOG_WARNING, "Got Undernet-style SERVER message but \"force_n2k\" not on.");
873 }
874 send_burst();
875 }
876
877 /* Fix up our timestamps if necessary. */
878 if (srv->boot <= PREHISTORY) {
879 /* Server from the mists of time.. */
880 if (srv->hops == 1) {
881 log_module(MAIN_LOG, LOG_ERROR, "Server %s claims to have booted at time "FMT_TIME_T". This is absurd.", srv->name, srv->boot);
882 }
883 } else if ((str = conf_get_data("server/reliable_clock", RECDB_QSTRING))
884 && enabled_string(str)) {
885 /* If we have a reliable clock, we just keep our current time. */
886 } else {
887 if (srv->boot <= self->boot) {
888 /* The other server is older than us. Accept their timestamp.
889 * Alternately, we are same age, but we accept their time
890 * since we are linking to them. */
891 self->boot = srv->boot;
892 ioset_set_time(srv->link);
893 }
894 }
895 if (srv == self->uplink) {
896 extern time_t burst_begin;
897 burst_begin = now;
898 }
899 return 1;
900}
901
902static CMD_FUNC(cmd_eob)
903{
904 struct server *sender;
905 dict_iterator_t it;
906 unsigned int ii;
907
908 if (!(sender = GetServerH(origin)))
909 return 0;
910 if (sender == self->uplink) {
911 cManager.uplink->state = CONNECTED;
912 for (it = dict_first(unbursted_channels); it; it = iter_next(it))
913 irc_burst(iter_data(it));
914 dict_delete(unbursted_channels);
915 unbursted_channels = NULL;
916 irc_eob();
917 irc_eob_ack();
918 }
919 sender->self_burst = 0;
920 recalc_bursts(sender);
921 for (ii=0; ii<slf_used; ii++)
922 slf_list[ii](sender);
923 return 1;
924}
925
926static CMD_FUNC(cmd_eob_ack)
927{
928 extern time_t burst_begin;
929
930 if (GetServerH(origin) == self->uplink) {
931 burst_length = now - burst_begin;
932 self->self_burst = self->burst = 0;
933 }
934 cManager.uplink->state = CONNECTED;
935 return 1;
936}
937
938static CMD_FUNC(cmd_ping)
939{
940 struct server *srv;
941 struct userNode *un;
942
943 if(argc > 3)
944 irc_pong(argv[2], argv[1]);
945 else if ((srv = GetServerH(origin)))
946 irc_pong(self->name, srv->numeric);
947 else if ((un = GetUserH(origin)))
948 irc_pong(self->name, un->numeric);
949 else
950 irc_pong(self->name, origin);
951
952 timeq_del(0, timed_send_ping, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
953 timeq_del(0, timed_ping_timeout, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
954 timeq_add(now + ping_freq, timed_send_ping, 0);
955 received_ping();
956 return 1;
957}
958
959static CMD_FUNC(cmd_error_nick)
960{
961 /* Go back to original IRC length .. and try to reconnect :/ */
962 change_nicklen(9);
963 irc_squit(self, "Got erroneous nickname, truncating nicks.", NULL);
964 return 1;
965}
966
967struct create_desc {
968 struct userNode *user;
969 time_t when;
970};
971
972static void
973join_helper(struct chanNode *chan, void *data)
974{
975 struct create_desc *cd = data;
976 AddChannelUser(cd->user, chan);
977}
978
979static void
980create_helper(char *name, void *data)
981{
982 struct create_desc *cd = data;
983
984 if (!strcmp(name, "0")) {
985 while (cd->user->channels.used > 0)
986 DelChannelUser(cd->user, cd->user->channels.list[0]->channel, 0, 0);
987 return;
988 }
989
2aef5f4b 990 AddChannelUser(cd->user, AddChannel(name, cd->when, NULL, NULL, NULL));
d76ed9a9
AS
991}
992
993static CMD_FUNC(cmd_create)
994{
995 struct create_desc cd;
996 struct userNode *user;
997
998 if ((argc < 3) || !(user = GetUserH(origin)))
999 return 0;
1000 cd.user = user;
1001 cd.when = atoi(argv[2]);
1002 parse_foreach(argv[1], join_helper, create_helper, NULL, NULL, &cd);
1003 return 1;
1004}
1005
1006static CMD_FUNC(cmd_join)
1007{
1008 struct create_desc cd;
1009
1010 if (!(cd.user = GetUserH(origin)))
1011 return 0;
1012 if (argc < 2)
1013 return 0;
1014 else if (argc < 3)
1015 cd.when = now;
1016 else
1017 cd.when = atoi(argv[2]);
1018 parse_foreach(argv[1], join_helper, create_helper, NULL, NULL, &cd);
1019 return 1;
1020}
1021
1022static CMD_FUNC(cmd_pong)
1023{
1024 if (argc < 3)
1025 return 0;
1026 if (!strcmp(argv[2], self->name)) {
1027 timeq_del(0, timed_send_ping, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
1028 timeq_del(0, timed_ping_timeout, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
1029 timeq_add(now + ping_freq, timed_send_ping, 0);
1030 received_ping();
1031 }
1032 return 1;
1033}
1034
1035static CMD_FUNC(cmd_nick)
1036{
1037 struct userNode *user;
1038 if ((user = GetUserH(origin))) {
1039 /* nick change (since the source is a user numeric) */
1040 if (argc < 2)
1041 return 0;
1042 NickChange(user, argv[1], 1);
1043 } else {
1044 struct server *serv;
1045 char modes[MAXLEN];
1046 /* new nick */
1047 if (argc < 9)
1048 return 0;
1049 serv = GetServerH(origin);
1050 if (argc > 9)
1051 unsplit_string(argv+6, argc-9, modes);
1052 else
1053 strcpy(modes, "+");
1054 AddUser(serv, argv[1], argv[4], argv[5], modes, argv[argc-2], argv[argc-1], atoi(argv[3]), argv[argc-3]);
1055 }
1056 return 1;
1057}
1058
1059static CMD_FUNC(cmd_account)
1060{
1061 struct userNode *user;
d9cd0e9d 1062 struct server *server;
b21e2cfe 1063 struct handle_info *hi;
d76ed9a9 1064
d9cd0e9d 1065 if ((argc < 3) || !origin || !(server = GetServerH(origin)))
d76ed9a9 1066 return 0; /* Origin must be server. */
d9cd0e9d
AS
1067
1068 /* This next line appears to tremple origin.. why? */
d76ed9a9
AS
1069 user = GetUserN(argv[1]);
1070 if (!user)
1071 return 1; /* A QUIT probably passed the ACCOUNT. */
d9cd0e9d
AS
1072
1073 if(!strcmp(argv[2],"C"))
1074 {
b21e2cfe 1075 if((hi = loc_auth(argv[4], argv[5])))
d9cd0e9d
AS
1076 {
1077 /* Return a AC A */
b21e2cfe 1078 putsock("%s " P10_ACCOUNT " %s A %s %lu", self->numeric, server->numeric , argv[3], hi->registered);
d9cd0e9d
AS
1079
1080 }
1081 else
1082 {
1083 /* Return a AC D */
1084 putsock("%s " P10_ACCOUNT " %s D %s", self->numeric, server->numeric , argv[3]);
1085 }
1086 return 1;
1087 }
1088 else if(!strcmp(argv[2],"R"))
1089 call_account_func(user, argv[3]);
1090 else
1091 call_account_func(user, argv[2]); /* For backward compatability */
d76ed9a9
AS
1092 return 1;
1093}
1094
1095static CMD_FUNC(cmd_fakehost)
1096{
1097 struct userNode *user;
1098
1099 if ((argc < 3) || !origin || !GetServerH(origin))
1100 return 0;
1101 if (!(user = GetUserN(argv[1])))
1102 return 1;
1103 assign_fakehost(user, argv[2], 0);
1104 return 1;
1105}
1106
1107static CMD_FUNC(cmd_burst)
1108{
1109 extern int rel_age;
2aef5f4b 1110 char modes[MAXLEN], *members = "";
1111 static char exemptlist[MAXLEN], banlist[MAXLEN];
a32da4c7 1112 unsigned int next = 3, res = 1;
2aef5f4b 1113 int ctype = 0, echeck = 0, bcheck = 0;
d76ed9a9
AS
1114 struct chanNode *cNode;
1115 struct userNode *un;
1116 struct modeNode *mNode;
1117 long mode;
1118 char *user, *end, sep;
1119 time_t in_timestamp;
2aef5f4b 1120 char* parm = NULL;
d76ed9a9
AS
1121
1122 if (argc < 3)
1123 return 0;
1124 modes[0] = 0;
2aef5f4b 1125
1126 exemptlist[0] = 0;
1127 banlist[0] = 0;
1128
d76ed9a9
AS
1129 while (next < argc) {
1130 switch (argv[next][0]) {
1131 case '+': {
1132 const char *pos;
1133 int n_modes;
a32da4c7 1134 for (pos=argv[next], n_modes = 1; *pos; pos++)
d76ed9a9
AS
1135 if ((*pos == 'k') || (*pos == 'l'))
1136 n_modes++;
1137 unsplit_string(argv+next, n_modes, modes);
1138 next += n_modes;
1139 break;
1140 }
2aef5f4b 1141 case '%': {
1142 for(parm = mysep(&argv[next], " "); /* parm = first param */
1143 parm; /* While param is not null */
1144 parm = mysep(&argv[next], " ") /* parm = next param */
1145 )
1146 {
1147 switch (parm[0]) {
1148 case '%': {
1149 ctype = 1;
1150 break;
1151 }
1152 case '~': {
1153 ctype = 2;
1154 break;
1155 }
1156 default: {
1157 break;
1158 }
1159 }
1160 if (ctype == 1) {
1161 if (bcheck == 0) {
1162 /* strip % char off start of very first ban */
23475fc6 1163 if (strlen(parm) > 1) {
1164 strncat(banlist, strtok(parm, "%"), sizeof(banlist) - 1 - strlen(banlist));
1165 strncat(banlist, " ", sizeof(banlist) - 1 - strlen(banlist));
1166 }
2aef5f4b 1167 bcheck = 1;
1168 } else {
1169 strncat(banlist, parm, sizeof(banlist) - 1 - strlen(banlist));
1170 strncat(banlist, " ", sizeof(banlist) - 1 - strlen(banlist));
1171 }
1172 } else if (ctype == 2) {
1173 if (echeck == 0) {
1174 echeck = 1;
1175 } else {
1176 strncat(exemptlist, parm, sizeof(exemptlist) - 1 - strlen(exemptlist));
1177 strncat(exemptlist, " ", sizeof(exemptlist) - 1 - strlen(exemptlist));
1178 }
1179 }
1180 }
1181 next++;
1182 break;
1183 }
a32da4c7 1184 default: members = argv[next++]; break;
2aef5f4b 1185 }
d76ed9a9
AS
1186 }
1187
1188 in_timestamp = atoi(argv[2]);
1189 if ((cNode = dict_find(unbursted_channels, argv[1], NULL))) {
1190 cNode->timestamp = in_timestamp;
1191 dict_remove(unbursted_channels, cNode->name);
1192 irc_burst(cNode);
1193 }
2aef5f4b 1194 cNode = AddChannel(argv[1], in_timestamp, modes, banlist, exemptlist);
1195
a32da4c7 1196 /* Burst channel members in now. */
d76ed9a9
AS
1197 for (user = members, sep = *members, mode = 0; sep; user = end) {
1198 for (end = user + 3; isalnum(*end) || *end == '[' || *end == ']'; end++) ;
1199 sep = *end++; end[-1] = 0;
1200 if (sep == ':') {
1201 mode = 0;
1202 while ((sep = *end++)) {
1203 if (sep == 'o')
1204 mode |= MODE_CHANOP;
55342ce8 1205 else if (sep == 'h')
1206 mode |= MODE_HALFOP;
d76ed9a9
AS
1207 else if (sep == 'v')
1208 mode |= MODE_VOICE;
1117fc5a 1209 else if (isdigit(sep)) {
1210 mode |= MODE_CHANOP;
1211 while (isdigit(*end)) end++;
1212 } else
d76ed9a9
AS
1213 break;
1214 }
1215 if (rel_age < 0)
1216 mode = 0;
1217 }
1218 if (!(un = GetUserN(user))) {
1219 res = 0;
1220 continue;
1221 }
1222 if ((mNode = AddChannelUser(un, cNode)))
1223 mNode->modes = mode;
1224 }
1225
1226 return res;
1227}
1228
1229static CMD_FUNC(cmd_mode)
1230{
1231 struct chanNode *cn;
1232 struct userNode *un;
182dd032
AS
1233 char *sethost; // sethost - reed/apples
1234 int i; // sethost - reed/apples
d76ed9a9
AS
1235
1236 if (argc < 3)
1237 return 0;
1238 if (!IsChannelName(argv[1])) {
1239 un = GetUserH(argv[1]);
1240 if (!un) {
1241 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s whose mode is changing.", argv[1]);
1242 return 0;
1243 }
182dd032
AS
1244 // sethost - reed/apples
1245 if (argc == 3)
d76ed9a9 1246 mod_usermode(un, argv[2]);
182dd032
AS
1247 else {
1248 sethost = malloc(strlen(argv[2]) + 1 + strlen(argv[3]) + 1);
1249 i = 0;
1250 while((sethost[i++] = *argv[2]++));
1251 i--;
1252 sethost[i++] = ' ';
1253 while((sethost[i++] = *argv[3]++));
1254 mod_usermode(un, sethost); // sethost - reed/apples
1255 }
1256
d76ed9a9
AS
1257 return 1;
1258 }
1259
1260 if (!(cn = GetChannel(argv[1]))) {
1261 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose mode is changing.", argv[1]);
1262 return 0;
1263 }
1264 if ((un = GetUserH(origin))) {
1265 struct modeNode *mn;
1266 /* Update idle time for person setting the mode */
1267 if ((mn = GetUserMode(cn, un)))
1268 mn->idle_since = now;
1269 } else {
1270 /* If it came from a server, reset timestamp to re-sync. */
1271 cn->timestamp = atoi(argv[argc-1]);
1272 }
1273
1274 return mod_chanmode(un, cn, argv+2, argc-2, MCP_ALLOW_OVB|MCP_FROM_SERVER|(un ? MC_NOTIFY : 0));
1275}
1276
1277static CMD_FUNC(cmd_opmode)
1278{
1279 struct chanNode *cn;
1280 struct userNode *un;
1281
1282 if (argc < 3)
1283 return 0;
1284
1285 if (!(cn = GetChannel(argv[1]))) {
1286 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose mode is changing.", argv[1]);
1287 return 0;
1288 }
1289 if (!(un = GetUserH(origin))) {
1290 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s requesting OPMODE.", origin);
1291 return 0;
1292 }
1293 if (!IsOper(un)) {
1294 log_module(MAIN_LOG, LOG_ERROR, "Non-privileged user %s using OPMODE.", un->nick);
1295 return 0;
1296 }
1297
1298 return mod_chanmode(un, cn, argv+2, argc-2, MCP_ALLOW_OVB|MCP_FROM_SERVER); /* do NOT announce opmode locally */
1299}
1300
1301static int clear_chanmode(struct chanNode *channel, const char *modes);
1302
1303static CMD_FUNC(cmd_clearmode)
1304{
1305 struct chanNode *cn;
1306 struct userNode *un;
1307
1308 if (argc < 3)
1309 return 0;
1310
1311 if (!(cn = GetChannel(argv[1]))) {
1312 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose mode is changing.", argv[1]);
1313 return 0;
1314 }
1315 if (!(un = GetUserH(origin))) {
1316 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s requesting CLEARMODE.", origin);
1317 return 0;
1318 }
1319 if (!IsOper(un)) {
1320 log_module(MAIN_LOG, LOG_ERROR, "Non-privileged user %s using CLEARMODE.", un->nick);
1321 return 0;
1322 }
1323
1324 return clear_chanmode(cn, argv[2]);
1325}
1326
1327static CMD_FUNC(cmd_topic)
1328{
1329 struct chanNode *cn;
1330 time_t chan_ts, topic_ts;
1331
1332 if (argc < 3)
1333 return 0;
1334 if (!(cn = GetChannel(argv[1]))) {
1335 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose topic is being set", argv[1]);
1336 return 0;
1337 }
1338 if (argc >= 5) {
1339 /* Looks like an Asuka style topic burst. */
1340 chan_ts = atoi(argv[2]);
1341 topic_ts = atoi(argv[3]);
1342 } else {
1343 chan_ts = cn->timestamp;
1344 topic_ts = now;
1345 }
1346 SetChannelTopic(cn, GetUserH(origin), argv[argc-1], 0);
1347 cn->topic_time = topic_ts;
1348 return 1;
1349}
1350
1351static CMD_FUNC(cmd_num_topic)
1352{
1353 struct chanNode *cn;
1354
1355 if (!argv[0])
1356 return 0; /* huh? */
1357 if (argv[2]) {
1358 cn = GetChannel(argv[2]);
1359 if (!cn) {
1360 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s in topic reply", argv[2]);
1361 return 0;
1362 }
1363 } else
1364 return 0;
1365
1366 switch (atoi(argv[0])) {
1367 case 331:
1368 cn->topic_time = 0;
1369 break; /* no topic */
1370 case 332:
1371 if (argc < 4)
1372 return 0;
1373 safestrncpy(cn->topic, unsplit_string(argv+3, argc-3, NULL), sizeof(cn->topic));
1374 break;
1375 case 333:
1376 if (argc < 5)
1377 return 0;
1378 safestrncpy(cn->topic_nick, argv[3], sizeof(cn->topic_nick));
1379 cn->topic_time = atoi(argv[4]);
1380 break;
1381 default:
1382 return 0; /* should never happen */
1383 }
1384 return 1;
1385}
1386
1387static CMD_FUNC(cmd_num_gline)
1388{
1389 if (argc < 6)
1390 return 0;
1391 gline_add(origin, argv[3], atoi(argv[4])-now, argv[5], now, 0);
1392 return 1;
1393}
1394
1395static CMD_FUNC(cmd_quit)
1396{
1397 struct userNode *user;
1398 if (argc < 2)
1399 return 0;
1400 /* Sometimes we get a KILL then a QUIT or the like, so we don't want to
1401 * call DelUser unless we have the user in our grasp. */
1402 if ((user = GetUserH(origin)))
1403 DelUser(user, NULL, false, argv[1]);
1404 return 1;
1405}
1406
1407static CMD_FUNC(cmd_kill)
1408{
1409 struct userNode *user;
1410 if (argc < 2)
1411 return 0;
1412 user = GetUserN(argv[1]);
1413 if (!user) {
1414 /* If we get a KILL for a non-existent user, it could be a
1415 * Ghost response to a KILL we sent out earlier. So we only
1416 * whine if the target is local.
1417 */
1418 if (!strncmp(argv[1], self->numeric, strlen(self->numeric)))
1419 log_module(MAIN_LOG, LOG_ERROR, "Unable to find kill victim %s", argv[1]);
1420 return 0;
1421 }
1422
1423 if (IsLocal(user) && IsService(user)) {
1424 /* TODO: rate limit this so silly things don't happen. */
1425 ReintroduceUser(user);
1426 return 1;
1427 }
1428
1429 DelUser(user, NULL, false, argv[2]);
1430 return 1;
1431}
1432
1433static CMD_FUNC(cmd_part)
1434{
1435 struct userNode *user;
1436
1437 if (argc < 2)
1438 return 0;
1439 user = GetUserH(origin);
1440 if (!user)
1441 return 0;
1442 parse_foreach(argv[1], part_helper, NULL, NULL, NULL, user);
1443 return 1;
1444}
1445
1446static CMD_FUNC(cmd_kick)
1447{
1448 if (argc < 3)
1449 return 0;
1450 ChannelUserKicked(GetUserH(origin), GetUserN(argv[2]), GetChannel(argv[1]));
1451 return 1;
1452}
1453
1454static CMD_FUNC(cmd_squit)
1455{
1456 struct server *server;
1457
1458 if (argc < 4)
1459 return 0;
1460 if (!(server = GetServerH(argv[1])))
1461 return 0;
1462
1463 if (server == self->uplink) {
1464 /* Force a reconnect to the currently selected server. */
1465 cManager.uplink->tries = 0;
1466 log_module(MAIN_LOG, LOG_INFO, "Squitting from uplink: %s", argv[3]);
1467 close_socket();
1468 return 1;
1469 }
1470
1471 DelServer(server, 0, argv[3]);
1472 return 1;
1473}
1474
1475static CMD_FUNC(cmd_privmsg)
1476{
1477 struct privmsg_desc pd;
1478 if (argc != 3)
1479 return 0;
1480 pd.user = GetUserH(origin);
1481 if (!pd.user || (IsGagged(pd.user) && !IsOper(pd.user)))
1482 return 1;
1483 pd.is_notice = 0;
1484 pd.text = argv[2];
1485 parse_foreach(argv[1], privmsg_chan_helper, NULL, privmsg_user_helper, privmsg_invalid, &pd);
1486 return 1;
1487}
1488
1489static CMD_FUNC(cmd_notice)
1490{
1491 struct privmsg_desc pd;
1492 if (argc != 3)
1493 return 0;
1494 pd.user = GetUserH(origin);
1495 if (!pd.user || (IsGagged(pd.user) && !IsOper(pd.user)))
1496 return 1;
1497 pd.is_notice = 1;
1498 pd.text = argv[2];
1499 parse_foreach(argv[1], privmsg_chan_helper, NULL, privmsg_user_helper, privmsg_invalid, &pd);
1500 return 1;
1501}
1502
1503static CMD_FUNC(cmd_away)
1504{
1505 struct userNode *uNode;
1506
1507 uNode = GetUserH(origin);
1508 if (!uNode)
1509 return 1;
1510 if (argc < 2)
1511 uNode->modes &= ~FLAGS_AWAY;
1512 else
1513 uNode->modes |= FLAGS_AWAY;
1514 return 1;
1515}
1516
1517static CMD_FUNC(cmd_gline)
1518{
1519 if (argc < 3)
1520 return 0;
1521 if (argv[2][0] == '+') {
1522 if (argc < 5)
1523 return 0;
1524 gline_add(origin, argv[2]+1, strtoul(argv[3], NULL, 0), argv[argc-1], now, 0);
1525 return 1;
1526 } else if (argv[2][0] == '-') {
1527 gline_remove(argv[2]+1, 0);
1528 return 1;
1529 } else
1530 return 0;
1531}
1532
1533static CMD_FUNC(cmd_svsnick)
1534{
1535 struct userNode *target, *dest;
1536 if ((argc < 4)
1537 || !(target = GetUserN(argv[1]))
1538 || !IsLocal(target)
1539 || (dest = GetUserH(argv[2])))
1540 return 0;
1541 NickChange(target, argv[2], 0);
1542 return 1;
1543}
1544
1545static oper_func_t *of_list;
1546static unsigned int of_size = 0, of_used = 0;
1547
1548void
1549free_user(struct userNode *user)
1550{
1551 free(user->nick);
1552 free(user);
1553}
1554
1555static void
1556parse_cleanup(void)
1557{
1558 unsigned int nn;
1559 free(of_list);
1560 free(privmsg_funcs);
1561 free(notice_funcs);
1562 free(mcf_list);
1563 dict_delete(irc_func_dict);
1564 for (nn=0; nn<dead_users.used; nn++)
1565 free_user(dead_users.list[nn]);
1566 userList_clean(&dead_users);
1567}
1568
1569static void
1570p10_conf_reload(void) {
1571 hidden_host_suffix = conf_get_data("server/hidden_host", RECDB_QSTRING);
1572}
1573
1574static void
1575remove_unbursted_channel(struct chanNode *cNode) {
1576 if (unbursted_channels)
1577 dict_remove(unbursted_channels, cNode->name);
1578}
1579
1580void
1581init_parse(void)
1582{
1583 const char *str, *desc;
1584 int numnick, usermask, max_users;
1585 char numer[COMBO_NUMERIC_LEN+1];
1586
1587 /* read config items */
1588 str = conf_get_data("server/ping_freq", RECDB_QSTRING);
1589 ping_freq = str ? ParseInterval(str) : 120;
1590 str = conf_get_data("server/ping_timeout", RECDB_QSTRING);
1591 ping_timeout = str ? ParseInterval(str) : 30;
1592 str = conf_get_data("server/force_n2k", RECDB_QSTRING);
1593 force_n2k = str ? enabled_string(str) : 1;
1594 str = conf_get_data("server/numeric", RECDB_QSTRING);
1595 if (!str) {
1596 log_module(MAIN_LOG, LOG_ERROR, "No server/numeric entry in config file.");
1597 exit(1);
1598 }
1599 numnick = atoi(str);
1600 str = conf_get_data("server/max_users", RECDB_QSTRING);
1601 max_users = str ? atoi(str) : 4096;
1602 for (usermask = 4; usermask < max_users; usermask <<= 1) ;
1603 usermask--;
1604 if ((numnick < 64) && (usermask < 4096) && !force_n2k)
1605 inttobase64(numer, (numnick << 12) + (usermask & 0x00fff), 3);
1606 else
1607 inttobase64(numer, (numnick << 18) + (usermask & 0x3ffff), 5);
1608
1609 str = conf_get_data("server/his_servername", RECDB_QSTRING);
1610 his_servername = str ? strdup(str) : NULL;
1611 str = conf_get_data("server/his_servercomment", RECDB_QSTRING);
1612 his_servercomment = str ? strdup(str) : NULL;
1613
1614 str = conf_get_data("server/hostname", RECDB_QSTRING);
1615 desc = conf_get_data("server/description", RECDB_QSTRING);
1616 if (!str || !desc) {
1617 log_module(MAIN_LOG, LOG_ERROR, "No server/hostname entry in config file.");
1618 exit(1);
1619 }
1620 self = AddServer(NULL, str, 0, boot_time, now, numer, desc);
1621 conf_register_reload(p10_conf_reload);
1622
1623 irc_func_dict = dict_new();
1624 dict_insert(irc_func_dict, CMD_BURST, cmd_burst);
1625 dict_insert(irc_func_dict, TOK_BURST, cmd_burst);
1626 dict_insert(irc_func_dict, CMD_CREATE, cmd_create);
1627 dict_insert(irc_func_dict, TOK_CREATE, cmd_create);
1628 dict_insert(irc_func_dict, CMD_EOB, cmd_eob);
1629 dict_insert(irc_func_dict, TOK_EOB, cmd_eob);
1630 dict_insert(irc_func_dict, CMD_EOB_ACK, cmd_eob_ack);
1631 dict_insert(irc_func_dict, TOK_EOB_ACK, cmd_eob_ack);
1632 dict_insert(irc_func_dict, CMD_MODE, cmd_mode);
1633 dict_insert(irc_func_dict, TOK_MODE, cmd_mode);
1634 dict_insert(irc_func_dict, CMD_NICK, cmd_nick);
1635 dict_insert(irc_func_dict, TOK_NICK, cmd_nick);
1636 dict_insert(irc_func_dict, CMD_ACCOUNT, cmd_account);
1637 dict_insert(irc_func_dict, TOK_ACCOUNT, cmd_account);
1638 dict_insert(irc_func_dict, CMD_FAKEHOST, cmd_fakehost);
1639 dict_insert(irc_func_dict, TOK_FAKEHOST, cmd_fakehost);
1640 dict_insert(irc_func_dict, CMD_PASS, cmd_pass);
1641 dict_insert(irc_func_dict, TOK_PASS, cmd_pass);
1642 dict_insert(irc_func_dict, CMD_PING, cmd_ping);
1643 dict_insert(irc_func_dict, TOK_PING, cmd_ping);
1644 dict_insert(irc_func_dict, CMD_PRIVMSG, cmd_privmsg);
1645 dict_insert(irc_func_dict, TOK_PRIVMSG, cmd_privmsg);
1646 dict_insert(irc_func_dict, CMD_PONG, cmd_pong);
1647 dict_insert(irc_func_dict, TOK_PONG, cmd_pong);
1648 dict_insert(irc_func_dict, CMD_QUIT, cmd_quit);
1649 dict_insert(irc_func_dict, TOK_QUIT, cmd_quit);
1650 dict_insert(irc_func_dict, CMD_SERVER, cmd_server);
1651 dict_insert(irc_func_dict, TOK_SERVER, cmd_server);
1652 dict_insert(irc_func_dict, CMD_JOIN, cmd_join);
1653 dict_insert(irc_func_dict, TOK_JOIN, cmd_join);
1654 dict_insert(irc_func_dict, CMD_PART, cmd_part);
1655 dict_insert(irc_func_dict, TOK_PART, cmd_part);
1656 dict_insert(irc_func_dict, CMD_ERROR, cmd_error);
1657 dict_insert(irc_func_dict, TOK_ERROR, cmd_error);
1658 dict_insert(irc_func_dict, CMD_TOPIC, cmd_topic);
1659 dict_insert(irc_func_dict, TOK_TOPIC, cmd_topic);
1660 dict_insert(irc_func_dict, CMD_AWAY, cmd_away);
1661 dict_insert(irc_func_dict, TOK_AWAY, cmd_away);
1662 dict_insert(irc_func_dict, CMD_SILENCE, cmd_dummy);
1663 dict_insert(irc_func_dict, TOK_SILENCE, cmd_dummy);
1664 dict_insert(irc_func_dict, CMD_KICK, cmd_kick);
1665 dict_insert(irc_func_dict, TOK_KICK, cmd_kick);
1666 dict_insert(irc_func_dict, CMD_SQUIT, cmd_squit);
1667 dict_insert(irc_func_dict, TOK_SQUIT, cmd_squit);
1668 dict_insert(irc_func_dict, CMD_KILL, cmd_kill);
1669 dict_insert(irc_func_dict, TOK_KILL, cmd_kill);
1670 dict_insert(irc_func_dict, CMD_NOTICE, cmd_notice);
1671 dict_insert(irc_func_dict, TOK_NOTICE, cmd_notice);
1672 dict_insert(irc_func_dict, CMD_STATS, cmd_stats);
1673 dict_insert(irc_func_dict, TOK_STATS, cmd_stats);
1674 dict_insert(irc_func_dict, CMD_SVSNICK, cmd_svsnick);
1675 dict_insert(irc_func_dict, TOK_SVSNICK, cmd_svsnick);
1676 dict_insert(irc_func_dict, CMD_WHOIS, cmd_whois);
1677 dict_insert(irc_func_dict, TOK_WHOIS, cmd_whois);
1678 dict_insert(irc_func_dict, CMD_GLINE, cmd_gline);
1679 dict_insert(irc_func_dict, TOK_GLINE, cmd_gline);
1680 dict_insert(irc_func_dict, CMD_OPMODE, cmd_opmode);
1681 dict_insert(irc_func_dict, TOK_OPMODE, cmd_opmode);
1682 dict_insert(irc_func_dict, CMD_CLEARMODE, cmd_clearmode);
1683 dict_insert(irc_func_dict, TOK_CLEARMODE, cmd_clearmode);
1684 dict_insert(irc_func_dict, CMD_VERSION, cmd_version);
1685 dict_insert(irc_func_dict, TOK_VERSION, cmd_version);
1686 dict_insert(irc_func_dict, CMD_ADMIN, cmd_admin);
1687 dict_insert(irc_func_dict, TOK_ADMIN, cmd_admin);
1688
1689 /* In P10, DESTRUCT doesn't do anything except be broadcast to servers.
1690 * Apparently to obliterate channels from any servers that think they
1691 * exist?
1692 */
1693 dict_insert(irc_func_dict, CMD_DESTRUCT, cmd_dummy);
1694 dict_insert(irc_func_dict, TOK_DESTRUCT, cmd_dummy);
1695 /* Ignore invites */
1696 dict_insert(irc_func_dict, CMD_INVITE, cmd_dummy);
1697 dict_insert(irc_func_dict, TOK_INVITE, cmd_dummy);
1698 /* DESYNCH is just informational, so ignore it */
1699 dict_insert(irc_func_dict, CMD_DESYNCH, cmd_dummy);
1700 dict_insert(irc_func_dict, TOK_DESYNCH, cmd_dummy);
1701 /* Ignore channel operator notices. */
1702 dict_insert(irc_func_dict, CMD_WALLCHOPS, cmd_dummy);
1703 dict_insert(irc_func_dict, TOK_WALLCHOPS, cmd_dummy);
1704 dict_insert(irc_func_dict, CMD_WALLVOICES, cmd_dummy);
1705 dict_insert(irc_func_dict, TOK_WALLVOICES, cmd_dummy);
55342ce8 1706 dict_insert(irc_func_dict, CMD_WALLHOPS, cmd_dummy);
1707 dict_insert(irc_func_dict, TOK_WALLHOPS, cmd_dummy);
d76ed9a9
AS
1708 /* Ignore opers being silly. */
1709 dict_insert(irc_func_dict, CMD_WALLOPS, cmd_dummy);
1710 dict_insert(irc_func_dict, TOK_WALLOPS, cmd_dummy);
55342ce8 1711 dict_insert(irc_func_dict, CMD_WALLHOPS, cmd_dummy);
1712 dict_insert(irc_func_dict, TOK_WALLHOPS, cmd_dummy);
41fadebe 1713 dict_insert(irc_func_dict, TOK_WALLUSERS, cmd_dummy);
850bf0cf
AS
1714 /* Ignore dnsbl exemptions */
1715 dict_insert(irc_func_dict, TOK_EXEMPT, cmd_dummy);
fede8b64
AS
1716 dict_insert(irc_func_dict, TOK_MARK, cmd_dummy);
1717 /* Ignore privs */
1718 dict_insert(irc_func_dict, TOK_PRIVS, cmd_dummy);
a74c9eac 1719 /* Ignore remote luser */
0c835e0f 1720 dict_insert(irc_func_dict, TOK_LUSERS, cmd_dummy);
d76ed9a9
AS
1721 /* We have reliable clock! Always! Wraaa! */
1722 dict_insert(irc_func_dict, CMD_SETTIME, cmd_dummy);
1723 dict_insert(irc_func_dict, TOK_SETTIME, cmd_dummy);
1724 /* handle topics */
1725 dict_insert(irc_func_dict, "331", cmd_num_topic);
1726 dict_insert(irc_func_dict, "332", cmd_num_topic);
1727 dict_insert(irc_func_dict, "333", cmd_num_topic);
1728 dict_insert(irc_func_dict, "345", cmd_dummy); /* blah has been invited to blah */
1729 dict_insert(irc_func_dict, "432", cmd_error_nick); /* Erroneus [sic] nickname */
1730 /* ban list resetting */
1731 /* "stats g" responses */
1732 dict_insert(irc_func_dict, "247", cmd_num_gline);
1733 dict_insert(irc_func_dict, "219", cmd_dummy); /* "End of /STATS report" */
1734 /* other numeric responses we might get */
1735 dict_insert(irc_func_dict, "401", cmd_dummy); /* target left network */
1736 dict_insert(irc_func_dict, "403", cmd_dummy); /* no such channel */
1737 dict_insert(irc_func_dict, "404", cmd_dummy); /* cannot send to channel */
0d16e639 1738 dict_insert(irc_func_dict, "439", cmd_dummy); /* target change too fast */
d76ed9a9
AS
1739 dict_insert(irc_func_dict, "441", cmd_dummy); /* target isn't on that channel */
1740 dict_insert(irc_func_dict, "442", cmd_dummy); /* you aren't on that channel */
1741 dict_insert(irc_func_dict, "443", cmd_dummy); /* is already on channel (after invite?) */
1742 dict_insert(irc_func_dict, "461", cmd_dummy); /* Not enough parameters (after TOPIC w/ 0 args) */
1743 dict_insert(irc_func_dict, "467", cmd_dummy); /* Channel key already set */
1744
1745 num_privmsg_funcs = 16;
1746 privmsg_funcs = malloc(sizeof(privmsg_func_t)*num_privmsg_funcs);
1747 memset(privmsg_funcs, 0, sizeof(privmsg_func_t)*num_privmsg_funcs);
1748
1749 num_notice_funcs = 16;
1750 notice_funcs = malloc(sizeof(privmsg_func_t)*num_notice_funcs);
1751 memset(notice_funcs, 0, sizeof(privmsg_func_t)*num_notice_funcs);
1752
1753 userList_init(&dead_users);
1754 reg_del_channel_func(remove_unbursted_channel);
1755 reg_exit_func(parse_cleanup);
1756}
1757
1758int
1759parse_line(char *line, int recursive)
1760{
1761 char *argv[MAXNUMPARAMS], *origin;
1762 int argc, cmd, res=0;
1763 cmd_func_t *func;
1764
1765 argc = split_line(line, true, MAXNUMPARAMS, argv);
1766 cmd = self->uplink || !argv[0][1] || !argv[0][2];
1767 if (argc > cmd) {
1768 if (cmd) {
1769 if (argv[0][0] == ':') {
1770 origin = argv[0]+1;
1771 } else if (!argv[0][1] || !argv[0][2]) {
1772 struct server *sNode = GetServerN(argv[0]);
1773 origin = sNode ? sNode->name : 0;
1774 } else {
1775 struct userNode *uNode = GetUserN(argv[0]);
1776 origin = uNode ? uNode->nick : 0;
1777 }
1778 } else
1779 origin = 0;
1780 if ((func = dict_find(irc_func_dict, argv[cmd], NULL)))
1781 res = func(origin, argc-cmd, argv+cmd);
1782 }
1783 if (!res) {
1784 log_module(MAIN_LOG, LOG_ERROR, "PARSE ERROR on line: %s", unsplit_string(argv, argc, NULL));
1785 } else if (!recursive) {
1786 unsigned int i;
1787 for (i=0; i<dead_users.used; i++)
1788 free_user(dead_users.list[i]);
1789 dead_users.used = 0;
1790 }
1791 return res;
1792}
1793
1794static void
1795parse_foreach(char *target_list, foreach_chanfunc cf, foreach_nonchan nc, foreach_userfunc uf, foreach_nonuser nu, void *data)
1796{
1797 char *j, old;
1798 do {
1799 j = target_list;
1800 while (*j != 0 && *j != ',')
1801 j++;
1802 old = *j;
1803 *j = 0;
1804 if (IsChannelName(target_list)
1805 || (target_list[0] == '0' && target_list[1] == '\0')) {
1806 struct chanNode *chan = GetChannel(target_list);
1807 if (chan) {
1808 if (cf)
1809 cf(chan, data);
1810 } else {
1811 if (nc)
1812 nc(target_list, data);
1813 }
1814 } else {
1815 struct userNode *user;
1816 struct privmsg_desc *pd = data;
1817
1818 pd->is_qualified = 0;
1819 if (*target_list == '@') {
1820 user = NULL;
1821 } else if (strchr(target_list, '@')) {
1822 struct server *server;
1823
1824 pd->is_qualified = 1;
1825 user = GetUserH(strtok(target_list, "@"));
1826 server = GetServerH(strtok(NULL, "@"));
1827
1828 if (user && (user->uplink != server)) {
1829 /* Don't attempt to index into any arrays
1830 using a user's numeric on another server. */
1831 user = NULL;
1832 }
1833 } else {
1834 user = GetUserN(target_list);
1835 }
1836
1837 if (user) {
1838 if (uf)
1839 uf(user, data);
1840 } else {
1841 if (nu)
1842 nu(target_list, data);
1843 }
1844 }
1845 target_list = j+1;
1846 } while (old == ',');
1847}
1848
1849static int
1850get_local_numeric(void)
1851{
1852 static unsigned int next_numeric = 0;
1853 if (self->clients > self->num_mask)
1854 return -1;
1855 while (self->users[next_numeric])
1856 if (++next_numeric > self->num_mask)
1857 next_numeric = 0;
1858 return next_numeric;
1859}
1860
1861static void
1862make_numeric(struct server *svr, int local_num, char *outbuf)
1863{
1864 int slen, llen;
1865
1866 if (force_n2k || svr->numeric[1]) {
1867 slen = 2;
1868 llen = 3;
1869 } else {
1870 slen = 1;
1871 llen = (local_num < 64*64) ? 2 : 3;
1872 }
1873 strncpy(outbuf, svr->numeric, slen);
1874 inttobase64(outbuf+slen, local_num, llen);
1875 outbuf[slen+llen] = 0;
1876}
1877
1878struct server *
1879AddServer(struct server *uplink, const char *name, int hops, time_t boot, time_t link, const char *numeric, const char *description)
1880{
1881 struct server* sNode;
1882 int slen, mlen;
1883
1884 if ((sNode = GetServerN(numeric))) {
1885 /* This means we're trying to re-add an existant server.
1886 * To be safe, we should forget the previous incarnation.
1887 * (And all its linked servers.)
1888 *
1889 * It usually only happens in replays when the original
1890 * had a ping timeout and the replay didn't (because
1891 * replaying a ping timeout invariably gets things wrong).
1892 */
1893 DelServer(sNode, 0, NULL);
1894 }
1895
1896 switch (strlen(numeric)) {
1897 case 5: slen = 2; mlen = 3; break;
1898 case 4: slen = 1; mlen = 3; break;
1899 case 3: slen = 1; mlen = 2; break;
1900 default:
1901 log_module(MAIN_LOG, LOG_ERROR, "AddServer(\"%s\", \"%s\", ...): Numeric %s has invalid length.", uplink->name, name, numeric);
1902 return NULL;
1903 }
1904
1905 sNode = calloc(1, sizeof(*sNode));
1906 sNode->uplink = uplink;
1907 safestrncpy(sNode->name, name, sizeof(sNode->name));
1908 sNode->num_mask = base64toint(numeric+slen, mlen);
1909 sNode->hops = hops;
1910 sNode->boot = boot;
1911 sNode->link = link;
1912 strncpy(sNode->numeric, numeric, slen);
1913 safestrncpy(sNode->description, description, sizeof(sNode->description));
1914 sNode->users = calloc(sNode->num_mask+1, sizeof(*sNode->users));
1915 serverList_init(&sNode->children);
1916 if (sNode->uplink) {
1917 /* uplink may be NULL if we're just building ourself */
1918 serverList_append(&sNode->uplink->children, sNode);
1919 }
1920 servers_num[base64toint(numeric, slen)] = sNode;
1921 dict_insert(servers, sNode->name, sNode);
1922 return sNode;
1923}
1924
1925void DelServer(struct server* serv, int announce, const char *message)
1926{
1927 unsigned int i;
1928
1929 /* If we receive an ERROR command before the SERVER
1930 * command a NULL server can be passed */
1931 if (!serv)
1932 return;
1933
1934 /* Hrm, what's the right way to SQUIT some other server?
1935 * (This code is only to handle killing juped servers.) */
1936 if (announce && (serv->uplink == self) && (serv != self->uplink))
1937 irc_squit(serv, message, NULL);
1938
1939 /* must recursively remove servers linked to this one first */
1940 for (i=serv->children.used;i>0;)
1941 if (serv->children.list[--i] != self)
1942 DelServer(serv->children.list[i], false, NULL);
1943
1944 /* clean up server's user hash tables */
1945 for (i=0;i<=serv->num_mask;i++)
1946 if (serv->users[i])
1947 DelUser(serv->users[i], NULL, false, "server delinked");
1948
1949 /* delete server */
1950 if (serv->uplink)
1951 serverList_remove(&serv->uplink->children, serv);
1952 if (serv == self->uplink)
1953 self->uplink = NULL;
1954 servers_num[base64toint(serv->numeric, strlen(serv->numeric))] = NULL;
1955 dict_remove(servers, serv->name);
1956 serverList_clean(&serv->children);
1957 free(serv->users);
1958 free(serv);
1959}
1960
1961struct userNode *
a32da4c7 1962AddService(const char *nick, const char *modes, const char *desc, const char *hostname)
d76ed9a9
AS
1963{
1964 char numeric[COMBO_NUMERIC_LEN+1];
1965 int local_num = get_local_numeric();
1966 time_t timestamp = now;
1967 struct userNode *old_user = GetUserH(nick);
1968
1969 if (old_user) {
1970 if (IsLocal(old_user))
1971 return old_user;
1972 timestamp = old_user->timestamp - 1;
1973 }
1974 if (local_num == -1) {
1975 log_module(MAIN_LOG, LOG_ERROR, "Unable to allocate numnick for service %s", nick);
1976 return 0;
1977 }
1978 if (!hostname)
1979 hostname = self->name;
1980 make_numeric(self, local_num, numeric);
180e0971
AS
1981 /* TODO: Make these modes part of the conf file */
1982 return AddUser(self, nick, nick, hostname, modes ? modes : "+oik", numeric, desc, now, "AAAAAA");
d76ed9a9
AS
1983}
1984
1985struct userNode *
1986AddClone(const char *nick, const char *ident, const char *hostname, const char *desc)
1987{
1988 char numeric[COMBO_NUMERIC_LEN+1];
1989 int local_num = get_local_numeric();
1990 time_t timestamp = now;
1991 struct userNode *old_user = GetUserH(nick);
1992
1993 if (old_user) {
1994 if (IsLocal(old_user))
1995 return old_user;
1996 timestamp = old_user->timestamp - 1;
1997 }
1998 if (local_num == -1) {
1999 log_module(MAIN_LOG, LOG_ERROR, "Unable to allocate numnick for clone %s", nick);
2000 return 0;
2001 }
2002 make_numeric(self, local_num, numeric);
2003 return AddUser(self, nick, ident, hostname, "+i", numeric, desc, timestamp, "AAAAAA");
2004}
2005
2006int
2007is_valid_nick(const char *nick) {
ec1a68c8 2008 unsigned int ii;
d76ed9a9
AS
2009 /* IRC has some of The Most Fucked-Up ideas about character sets
2010 * in the world.. */
2011 if (!isalpha(*nick) && !strchr("{|}~[\\]^_`", *nick))
2012 return 0;
ec1a68c8 2013 for (ii = 0; nick[ii]; ++ii)
2014 if (!isalnum(nick[ii]) && !strchr("{|}~[\\]^-_`", nick[ii]))
d76ed9a9
AS
2015 return 0;
2016 if (strlen(nick) > nicklen)
2017 return 0;
2018 return 1;
2019}
2020
2021static struct userNode*
2022AddUser(struct server* uplink, const char *nick, const char *ident, const char *hostname, const char *modes, const char *numeric, const char *userinfo, time_t timestamp, const char *realip)
2023{
2024 struct userNode *oldUser, *uNode;
2025 unsigned int n, ignore_user;
2026
2027 if ((strlen(numeric) < 3) || (strlen(numeric) > 5)) {
2028 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): numeric %s wrong length!", uplink, nick, numeric);
2029 return NULL;
2030 }
2031
2032 if (!uplink) {
2033 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): server for numeric %s doesn't exist!", uplink, nick, numeric);
2034 return NULL;
2035 }
2036
2037 if (uplink != GetServerN(numeric)) {
2038 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): server for numeric %s differs from nominal uplink %s.", uplink, nick, numeric, uplink->name);
2039 return NULL;
2040 }
2041
2042 if (!is_valid_nick(nick)) {
2043 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): invalid nickname detected.", uplink, nick);
2044 return NULL;
2045 }
2046
2047 ignore_user = 0;
2048 if ((oldUser = GetUserH(nick))) {
2049 if (IsLocal(oldUser) && (IsService(oldUser) || IsPersistent(oldUser))) {
2050 /* The service should collide the new user off. */
2051 oldUser->timestamp = timestamp - 1;
2052 irc_user(oldUser);
2053 }
2054 if (oldUser->timestamp > timestamp) {
2055 /* "Old" user is really newer; remove them */
2056 DelUser(oldUser, 0, 1, "Overruled by older nick");
2057 } else {
2058 /* User being added is too new; do not add them to
2059 * clients, but do add them to the server's list, since it
2060 * will send a KILL and QUIT soon. */
2061 ignore_user = 1;
2062 }
2063 }
2064
2065 /* create new usernode and set all values */
2066 uNode = calloc(1, sizeof(*uNode));
2067 uNode->nick = strdup(nick);
2068 safestrncpy(uNode->ident, ident, sizeof(uNode->ident));
2069 safestrncpy(uNode->info, userinfo, sizeof(uNode->info));
2070 safestrncpy(uNode->hostname, hostname, sizeof(uNode->hostname));
2071 safestrncpy(uNode->numeric, numeric, sizeof(uNode->numeric));
2072 uNode->ip.s_addr = htonl(base64toint(realip, 6));
2073 uNode->timestamp = timestamp;
2074 modeList_init(&uNode->channels);
2075 uNode->uplink = uplink;
2076 if (++uNode->uplink->clients > uNode->uplink->max_clients) {
2077 uNode->uplink->max_clients = uNode->uplink->clients;
2078 }
2079 uNode->num_local = base64toint(numeric+strlen(uNode->uplink->numeric), 3) & uNode->uplink->num_mask;
2080 uNode->uplink->users[uNode->num_local] = uNode;
2081 mod_usermode(uNode, modes);
2082 if (ignore_user)
2083 return uNode;
2084
2085 dict_insert(clients, uNode->nick, uNode);
2086 if (dict_size(clients) > max_clients) {
2087 max_clients = dict_size(clients);
2088 max_clients_time = now;
2089 }
2090 if (IsLocal(uNode))
2091 irc_user(uNode);
2092 for (n=0; n<nuf_used; n++)
2093 if (nuf_list[n](uNode))
2094 break;
3fdd6a74 2095
2096 if ((uNode->loc == 1) && (uNode->handle_info))
2097 send_func_list(uNode);
2098
d76ed9a9
AS
2099 return uNode;
2100}
2101
2102/* removes user from it's server's hash table and nick hash table */
2103void
2104DelUser(struct userNode* user, struct userNode *killer, int announce, const char *why)
2105{
2106 unsigned int n;
2107
ec1a68c8 2108 verify(user);
2109
d76ed9a9
AS
2110 /* mark them as dead, in case anybody cares */
2111 user->dead = 1;
2112
ac3bdc8d
AS
2113 /* remove pending adduser commands */
2114 wipe_adduser_pending(NULL, user);
2115
d76ed9a9
AS
2116 /* remove user from all channels */
2117 while (user->channels.used > 0)
2118 DelChannelUser(user, user->channels.list[user->channels.used-1]->channel, false, 0);
2119
2120 /* Call these in reverse order so ChanServ can update presence
2121 information before NickServ nukes the handle_info. */
2122 for (n = duf_used; n > 0; )
2123 duf_list[--n](user, killer, why);
2124
2125 user->uplink->clients--;
2126 user->uplink->users[user->num_local] = NULL;
2127 if (IsOper(user))
2128 userList_remove(&curr_opers, user);
2129 /* remove from global dictionary, but not if after a collide */
2130 if (user == dict_find(clients, user->nick, NULL))
2131 dict_remove(clients, user->nick);
2132
2133 if (IsInvisible(user))
2134 invis_clients--;
2135
2136 if (announce) {
2137 if (IsLocal(user))
2138 irc_quit(user, why);
2139 else
2140 irc_kill(killer, user, why);
2141 }
2142
2143 modeList_clean(&user->channels);
2144 /* We don't free them, in case we try to privmsg them or something
2145 * (like when a stupid oper kills themself). We just put them onto
2146 * a list of clients that get freed after processing each line.
2147 */
2148 if (dead_users.size)
2149 userList_append(&dead_users, user);
2150 else
2151 free_user(user);
2152}
2153
697f4c9a 2154static void call_oper_funcs(struct userNode *user);
2155
d76ed9a9 2156void mod_usermode(struct userNode *user, const char *mode_change) {
d76ed9a9
AS
2157 int add = 1;
2158 const char *word = mode_change;
2159
2160 if (!user || !mode_change)
2161 return;
a32da4c7 2162 while (*word != ' ' && *word) word++;
2163 while (*word == ' ') word++;
d76ed9a9
AS
2164 while (1) {
2165#define do_user_mode(FLAG) do { if (add) user->modes |= FLAG; else user->modes &= ~FLAG; } while (0)
2166 switch (*mode_change++) {
2167 case 0: case ' ': return;
2168 case '+': add = 1; break;
2169 case '-': add = 0; break;
2170 case 'o':
d76ed9a9 2171 if (add) {
ec0120d1
AS
2172 if(!IsOper(user)) { /* Dont re-oper an oper */
2173 userList_append(&curr_opers, user);
2174 call_oper_funcs(user);
2175 }
d76ed9a9
AS
2176 } else {
2177 userList_remove(&curr_opers, user);
2178 }
3d0b24ce 2179 do_user_mode(FLAGS_OPER);
d76ed9a9
AS
2180 break;
2181 case 'O': do_user_mode(FLAGS_LOCOP); break;
2182 case 'i': do_user_mode(FLAGS_INVISIBLE);
2183 if (add)
2184 invis_clients++;
2185 else
2186 invis_clients--;
2187 break;
2188 case 'w': do_user_mode(FLAGS_WALLOP); break;
2189 case 's': do_user_mode(FLAGS_SERVNOTICE); break;
2190 case 'd': do_user_mode(FLAGS_DEAF); break;
2191 case 'k': do_user_mode(FLAGS_SERVICE); break;
2192 case 'g': do_user_mode(FLAGS_GLOBAL); break;
182dd032
AS
2193 // sethost - reed/apples
2194 // case 'h': do_user_mode(FLAGS_HELPER); break;
2195 // I check if there's an 'h' in the first part, and if there,
2196 // then everything after the space becomes their new host.
2197 case 'h': do_user_mode(FLAGS_SETHOST);
2198 if (*word) {
2199 char sethost[MAXLEN];
2200 unsigned int ii;
2201 for (ii=0; (*word != ' ') && (*word != '\0'); )
2202 sethost[ii++] = *word++;
2203 sethost[ii] = 0;
2204 while (*word == ' ')
2205 word++;
2206 safestrncpy(user->sethost, sethost, sizeof(user->sethost));
2207 }
2208 break;
d76ed9a9
AS
2209 case 'x': do_user_mode(FLAGS_HIDDEN_HOST); break;
2210 case 'r':
2211 if (*word) {
2212 char tag[MAXLEN];
2213 unsigned int ii;
2214 for (ii=0; (*word != ' ') && (*word != '\0'); )
2215 tag[ii++] = *word++;
2216 tag[ii] = 0;
2217 while (*word == ' ')
2218 word++;
2219 call_account_func(user, tag);
2220 }
2221 break;
2222 case 'f':
2223 if (*word) {
2224 char host[MAXLEN];
2225 unsigned int ii;
2226 for (ii=0; (*word != ' ') && (*word != '\0'); )
2227 host[ii++] = *word++;
2228 host[ii] = 0;
2229 while (*word == ' ')
2230 word++;
2231 assign_fakehost(user, host, 0);
2232 }
2233 break;
2234 }
2235#undef do_user_mode
2236 }
2237}
2238
2239struct mod_chanmode *
2240mod_chanmode_parse(struct chanNode *channel, char **modes, unsigned int argc, unsigned int flags)
2241{
2242 struct mod_chanmode *change;
2243 unsigned int ii, in_arg, ch_arg, add;
2244
2245 if (argc == 0)
2246 return NULL;
2247 if (!(change = mod_chanmode_alloc(argc - 1)))
2248 return NULL;
2249
2250 for (ii = ch_arg = 0, in_arg = add = 1;
2251 (modes[0][ii] != '\0') && (modes[0][ii] != ' ');
2252 ++ii) {
2253 switch (modes[0][ii]) {
2254 case '+':
2255 add = 1;
2256 break;
2257 case '-':
2258 add = 0;
2259 break;
2260#define do_chan_mode(FLAG) do { if (add) change->modes_set |= (FLAG), change->modes_clear &= ~(FLAG); else change->modes_clear |= (FLAG), change->modes_set &= ~(FLAG); } while(0)
2261 case 'C': do_chan_mode(MODE_NOCTCPS); break;
2262 case 'D': do_chan_mode(MODE_DELAYJOINS); break;
2263 case 'c': do_chan_mode(MODE_NOCOLORS); break;
2264 case 'i': do_chan_mode(MODE_INVITEONLY); break;
2265 case 'm': do_chan_mode(MODE_MODERATED); break;
2266 case 'n': do_chan_mode(MODE_NOPRIVMSGS); break;
2267 case 'p': do_chan_mode(MODE_PRIVATE); break;
2268 case 'r': do_chan_mode(MODE_REGONLY); break;
2269 case 's': do_chan_mode(MODE_SECRET); break;
2270 case 't': do_chan_mode(MODE_TOPICLIMIT); break;
d0cb2fb6
AS
2271 case 'S': do_chan_mode(MODE_STRIPCOLOR); break;
2272 case 'M': do_chan_mode(MODE_MODUNREG); break;
2273 case 'N': do_chan_mode(MODE_NONOTICE); break;
2274 case 'Q': do_chan_mode(MODE_NOQUITMSGS); break;
2275 case 'T': do_chan_mode(MODE_NOAMSG); break;
2276 case 'O': do_chan_mode(MODE_OPERSONLY); break;
c8ca69a0 2277 case 'Z': do_chan_mode(MODE_SSLONLY); break;
88c7cb10 2278 case 'L': do_chan_mode(MODE_HIDEMODE); break;
ec1a68c8 2279 case 'z':
2280 if (!(flags & MCP_REGISTERED)) {
2281 do_chan_mode(MODE_REGISTERED);
2282 } else {
2283 mod_chanmode_free(change);
2284 return NULL;
2285 }
2286 break;
d76ed9a9
AS
2287#undef do_chan_mode
2288 case 'l':
2289 if (add) {
2290 if (in_arg >= argc)
2291 goto error;
2292 change->modes_set |= MODE_LIMIT;
2293 change->new_limit = atoi(modes[in_arg++]);
2294 } else {
2295 change->modes_set &= ~MODE_LIMIT;
2296 change->modes_clear |= MODE_LIMIT;
2297 }
2298 break;
2299 case 'k':
2300 if (add) {
2301 if (in_arg >= argc)
2302 goto error;
2303 change->modes_set |= MODE_KEY;
2304 safestrncpy(change->new_key, modes[in_arg++], sizeof(change->new_key));
2305 } else {
2306 change->modes_clear |= MODE_KEY;
2307 if (!(flags & MCP_KEY_FREE)) {
2308 if (in_arg >= argc)
2309 goto error;
2310 in_arg++;
2311 }
2312 }
2313 break;
2314 case 'b':
2315 if (!(flags & MCP_ALLOW_OVB))
2316 goto error;
2317 if (in_arg >= argc)
2318 goto error;
2319 change->args[ch_arg].mode = MODE_BAN;
2320 if (!add)
2321 change->args[ch_arg].mode |= MODE_REMOVE;
a32da4c7 2322 change->args[ch_arg++].u.hostmask = modes[in_arg++];
d76ed9a9 2323 break;
2aef5f4b 2324 case 'e':
2325 if (!(flags & MCP_ALLOW_OVB))
2326 goto error;
2327 if (in_arg >= argc)
2328 goto error;
2329 change->args[ch_arg].mode = MODE_EXEMPT;
2330 if (!add)
2331 change->args[ch_arg].mode |= MODE_REMOVE;
a32da4c7 2332 change->args[ch_arg++].u.hostmask = modes[in_arg++];
2aef5f4b 2333 break;
55342ce8 2334 case 'o': case 'h': case 'v':
d76ed9a9
AS
2335 {
2336 struct userNode *victim;
2337 if (!(flags & MCP_ALLOW_OVB))
2338 goto error;
2339 if (in_arg >= argc)
2340 goto error;
55342ce8 2341
2342 if (modes[0][ii] == 'o')
2343 change->args[ch_arg].mode = MODE_CHANOP;
2344 else if (modes[0][ii] == 'h')
2345 change->args[ch_arg].mode = MODE_HALFOP;
2346 else if (modes[0][ii] == 'v')
2347 change->args[ch_arg].mode = MODE_VOICE;
2348
d76ed9a9
AS
2349 if (!add)
2350 change->args[ch_arg].mode |= MODE_REMOVE;
2351 if (flags & MCP_FROM_SERVER)
2352 victim = GetUserN(modes[in_arg++]);
2353 else
2354 victim = GetUserH(modes[in_arg++]);
2355 if (!victim)
2356 continue;
a32da4c7 2357 if ((change->args[ch_arg].u.member = GetUserMode(channel, victim)))
d76ed9a9
AS
2358 ch_arg++;
2359 break;
2360 }
2361 default:
2362 if (!(flags & MCP_FROM_SERVER))
2363 goto error;
2364 break;
2365 }
2366 }
2367 change->argc = ch_arg; /* in case any turned out to be ignored */
2368 if (change->modes_set & MODE_SECRET) {
2369 change->modes_set &= ~(MODE_PRIVATE);
2370 change->modes_clear |= MODE_PRIVATE;
2371 } else if (change->modes_set & MODE_PRIVATE) {
2372 change->modes_set &= ~(MODE_SECRET);
2373 change->modes_clear |= MODE_SECRET;
2374 }
2375 return change;
2376 error:
2377 mod_chanmode_free(change);
2378 return NULL;
2379}
2380
2381struct chanmode_buffer {
2382 char modes[MAXLEN];
2383 char args[MAXLEN];
2384 struct chanNode *channel;
2385 struct userNode *actor;
2386 unsigned int modes_used;
2387 unsigned int args_used;
2388 size_t chname_len;
2389 unsigned int is_add : 1;
2390 unsigned int is_chanop : 1;
2391};
2392
2393static void
2394mod_chanmode_append(struct chanmode_buffer *buf, char ch, const char *arg)
2395{
2396 size_t arg_len = strlen(arg);
2397 if (buf->modes_used > (MAXMODEPARAMS) ||
2398 buf->modes_used + buf->args_used + buf->chname_len + arg_len > 450) {
2399 memcpy(buf->modes + buf->modes_used, buf->args, buf->args_used);
2400 buf->modes[buf->modes_used + buf->args_used] = '\0';
2401 irc_mode((buf->is_chanop ? buf->actor : NULL), buf->channel, buf->modes);
2402 buf->modes[0] = buf->is_add ? '+' : '-';
2403 buf->modes_used = 1;
2404 buf->args_used = 0;
2405 }
2406 buf->modes[buf->modes_used++] = ch;
2407 buf->args[buf->args_used++] = ' ';
2408 memcpy(buf->args + buf->args_used, arg, arg_len);
2409 buf->args_used += arg_len;
2410}
2411
2412void
2413mod_chanmode_announce(struct userNode *who, struct chanNode *channel, struct mod_chanmode *change)
2414{
2415 struct chanmode_buffer chbuf;
2416 unsigned int arg;
2417 struct modeNode *mn;
2418 char int_buff[32], mode = '\0';
2419
2420 assert(change->argc <= change->alloc_argc);
2421 memset(&chbuf, 0, sizeof(chbuf));
2422 chbuf.channel = channel;
2423 chbuf.actor = who;
2424 chbuf.chname_len = strlen(channel->name);
2425
2426 mn = GetUserMode(channel, who);
2427 if ((mn && (mn->modes & MODE_CHANOP)) || off_channel)
2428 chbuf.is_chanop = 1;
2429
2430 /* First remove modes */
2431 chbuf.is_add = 0;
2432 if (change->modes_clear) {
2433 if (mode != '-')
2434 chbuf.modes[chbuf.modes_used++] = mode = '-';
2435#define DO_MODE_CHAR(BIT, CHAR) if (change->modes_clear & MODE_##BIT) chbuf.modes[chbuf.modes_used++] = CHAR
2436 DO_MODE_CHAR(PRIVATE, 'p');
2437 DO_MODE_CHAR(SECRET, 's');
2438 DO_MODE_CHAR(MODERATED, 'm');
2439 DO_MODE_CHAR(TOPICLIMIT, 't');
2440 DO_MODE_CHAR(INVITEONLY, 'i');
2441 DO_MODE_CHAR(NOPRIVMSGS, 'n');
2442 DO_MODE_CHAR(LIMIT, 'l');
2443 DO_MODE_CHAR(DELAYJOINS, 'D');
2444 DO_MODE_CHAR(REGONLY, 'r');
2445 DO_MODE_CHAR(NOCOLORS, 'c');
2446 DO_MODE_CHAR(NOCTCPS, 'C');
d0cb2fb6
AS
2447 DO_MODE_CHAR(STRIPCOLOR, 'S');
2448 DO_MODE_CHAR(MODUNREG, 'M');
2449 DO_MODE_CHAR(NONOTICE, 'N');
2450 DO_MODE_CHAR(NOQUITMSGS, 'Q');
2451 DO_MODE_CHAR(NOAMSG, 'T');
2452 DO_MODE_CHAR(OPERSONLY, 'O');
ec1a68c8 2453 DO_MODE_CHAR(REGISTERED, 'z');
c8ca69a0 2454 DO_MODE_CHAR(SSLONLY, 'Z');
88c7cb10 2455 DO_MODE_CHAR(HIDEMODE, 'L');
d76ed9a9
AS
2456#undef DO_MODE_CHAR
2457 if (change->modes_clear & channel->modes & MODE_KEY)
2458 mod_chanmode_append(&chbuf, 'k', channel->key);
2459 }
2460 for (arg = 0; arg < change->argc; ++arg) {
2461 if (!(change->args[arg].mode & MODE_REMOVE))
2462 continue;
2463 if (mode != '-')
2464 chbuf.modes[chbuf.modes_used++] = mode = '-';
2465 switch (change->args[arg].mode & ~MODE_REMOVE) {
2466 case MODE_BAN:
a32da4c7 2467 mod_chanmode_append(&chbuf, 'b', change->args[arg].u.hostmask);
d76ed9a9 2468 break;
2aef5f4b 2469 case MODE_EXEMPT:
a32da4c7 2470 mod_chanmode_append(&chbuf, 'e', change->args[arg].u.hostmask);
2aef5f4b 2471 break;
d76ed9a9
AS
2472 default:
2473 if (change->args[arg].mode & MODE_CHANOP)
a32da4c7 2474 mod_chanmode_append(&chbuf, 'o', change->args[arg].u.member->user->numeric);
55342ce8 2475 if (change->args[arg].mode & MODE_HALFOP)
a32da4c7 2476 mod_chanmode_append(&chbuf, 'h', change->args[arg].u.member->user->numeric);
d76ed9a9 2477 if (change->args[arg].mode & MODE_VOICE)
a32da4c7 2478 mod_chanmode_append(&chbuf, 'v', change->args[arg].u.member->user->numeric);
d76ed9a9
AS
2479 break;
2480 }
2481 }
2482
2483 /* Then set them */
2484 chbuf.is_add = 1;
2485 if (change->modes_set) {
2486 if (mode != '+')
2487 chbuf.modes[chbuf.modes_used++] = mode = '+';
2488#define DO_MODE_CHAR(BIT, CHAR) if (change->modes_set & MODE_##BIT) chbuf.modes[chbuf.modes_used++] = CHAR
2489 DO_MODE_CHAR(PRIVATE, 'p');
2490 DO_MODE_CHAR(SECRET, 's');
2491 DO_MODE_CHAR(MODERATED, 'm');
2492 DO_MODE_CHAR(TOPICLIMIT, 't');
2493 DO_MODE_CHAR(INVITEONLY, 'i');
2494 DO_MODE_CHAR(NOPRIVMSGS, 'n');
2495 DO_MODE_CHAR(DELAYJOINS, 'D');
2496 DO_MODE_CHAR(REGONLY, 'r');
2497 DO_MODE_CHAR(NOCOLORS, 'c');
2498 DO_MODE_CHAR(NOCTCPS, 'C');
d0cb2fb6
AS
2499 DO_MODE_CHAR(STRIPCOLOR, 'S');
2500 DO_MODE_CHAR(MODUNREG, 'M');
2501 DO_MODE_CHAR(NONOTICE, 'N');
2502 DO_MODE_CHAR(NOQUITMSGS, 'Q');
2503 DO_MODE_CHAR(NOAMSG, 'T');
2504 DO_MODE_CHAR(OPERSONLY, 'O');
ec1a68c8 2505 DO_MODE_CHAR(REGISTERED, 'z');
c8ca69a0 2506 DO_MODE_CHAR(SSLONLY, 'Z');
88c7cb10 2507 DO_MODE_CHAR(HIDEMODE, 'L');
d76ed9a9
AS
2508#undef DO_MODE_CHAR
2509 if(change->modes_set & MODE_KEY)
2510 mod_chanmode_append(&chbuf, 'k', change->new_key);
2511 if(change->modes_set & MODE_LIMIT) {
2512 sprintf(int_buff, "%d", change->new_limit);
2513 mod_chanmode_append(&chbuf, 'l', int_buff);
2514 }
2515 }
2516 for (arg = 0; arg < change->argc; ++arg) {
2517 if (change->args[arg].mode & MODE_REMOVE)
2518 continue;
2519 if (mode != '+')
2520 chbuf.modes[chbuf.modes_used++] = mode = '+';
2521 switch (change->args[arg].mode) {
2522 case MODE_BAN:
a32da4c7 2523 mod_chanmode_append(&chbuf, 'b', change->args[arg].u.hostmask);
d76ed9a9 2524 break;
2aef5f4b 2525 case MODE_EXEMPT:
a32da4c7 2526 mod_chanmode_append(&chbuf, 'e', change->args[arg].u.hostmask);
2aef5f4b 2527 break;
d76ed9a9
AS
2528 default:
2529 if (change->args[arg].mode & MODE_CHANOP)
a32da4c7 2530 mod_chanmode_append(&chbuf, 'o', change->args[arg].u.member->user->numeric);
55342ce8 2531 if (change->args[arg].mode & MODE_HALFOP)
a32da4c7 2532 mod_chanmode_append(&chbuf, 'h', change->args[arg].u.member->user->numeric);
d76ed9a9 2533 if (change->args[arg].mode & MODE_VOICE)
a32da4c7 2534 mod_chanmode_append(&chbuf, 'v', change->args[arg].u.member->user->numeric);
d76ed9a9
AS
2535 break;
2536 }
2537 }
2538
2539 /* Flush the buffer and apply changes locally */
2540 if (chbuf.modes_used > 0) {
2541 memcpy(chbuf.modes + chbuf.modes_used, chbuf.args, chbuf.args_used);
2542 chbuf.modes[chbuf.modes_used + chbuf.args_used] = '\0';
2543 irc_mode((chbuf.is_chanop ? chbuf.actor : NULL), chbuf.channel, chbuf.modes);
2544 }
2545 mod_chanmode_apply(who, channel, change);
2546}
2547
2548char *
2549mod_chanmode_format(struct mod_chanmode *change, char *outbuff)
2550{
2551 unsigned int used = 0;
2552 assert(change->argc <= change->alloc_argc);
2553 if (change->modes_clear) {
2554 outbuff[used++] = '-';
2555#define DO_MODE_CHAR(BIT, CHAR) if (change->modes_clear & MODE_##BIT) outbuff[used++] = CHAR
2556 DO_MODE_CHAR(PRIVATE, 'p');
2557 DO_MODE_CHAR(SECRET, 's');
2558 DO_MODE_CHAR(MODERATED, 'm');
2559 DO_MODE_CHAR(TOPICLIMIT, 't');
2560 DO_MODE_CHAR(INVITEONLY, 'i');
2561 DO_MODE_CHAR(NOPRIVMSGS, 'n');
2562 DO_MODE_CHAR(LIMIT, 'l');
2563 DO_MODE_CHAR(KEY, 'k');
2564 DO_MODE_CHAR(DELAYJOINS, 'D');
2565 DO_MODE_CHAR(REGONLY, 'r');
2566 DO_MODE_CHAR(NOCOLORS, 'c');
2567 DO_MODE_CHAR(NOCTCPS, 'C');
d0cb2fb6
AS
2568 DO_MODE_CHAR(STRIPCOLOR, 'S');
2569 DO_MODE_CHAR(MODUNREG, 'M');
2570 DO_MODE_CHAR(NONOTICE, 'N');
2571 DO_MODE_CHAR(NOQUITMSGS, 'Q');
2572 DO_MODE_CHAR(NOAMSG, 'T');
2573 DO_MODE_CHAR(OPERSONLY, 'O');
ec1a68c8 2574 DO_MODE_CHAR(REGISTERED, 'z');
c8ca69a0 2575 DO_MODE_CHAR(SSLONLY, 'Z');
88c7cb10 2576 DO_MODE_CHAR(HIDEMODE, 'L');
d76ed9a9
AS
2577#undef DO_MODE_CHAR
2578 }
2579 if (change->modes_set) {
2580 outbuff[used++] = '+';
2581#define DO_MODE_CHAR(BIT, CHAR) if (change->modes_set & MODE_##BIT) outbuff[used++] = CHAR
2582 DO_MODE_CHAR(PRIVATE, 'p');
2583 DO_MODE_CHAR(SECRET, 's');
2584 DO_MODE_CHAR(MODERATED, 'm');
2585 DO_MODE_CHAR(TOPICLIMIT, 't');
2586 DO_MODE_CHAR(INVITEONLY, 'i');
2587 DO_MODE_CHAR(NOPRIVMSGS, 'n');
2588 DO_MODE_CHAR(DELAYJOINS, 'D');
2589 DO_MODE_CHAR(REGONLY, 'r');
2590 DO_MODE_CHAR(NOCOLORS, 'c');
2591 DO_MODE_CHAR(NOCTCPS, 'C');
d0cb2fb6
AS
2592 DO_MODE_CHAR(STRIPCOLOR, 'S');
2593 DO_MODE_CHAR(MODUNREG, 'M');
2594 DO_MODE_CHAR(NONOTICE, 'N');
2595 DO_MODE_CHAR(NOQUITMSGS, 'Q');
2596 DO_MODE_CHAR(NOAMSG, 'T');
2597 DO_MODE_CHAR(OPERSONLY, 'O');
ec1a68c8 2598 DO_MODE_CHAR(REGISTERED, 'z');
c8ca69a0 2599 DO_MODE_CHAR(SSLONLY, 'Z');
88c7cb10 2600 DO_MODE_CHAR(HIDEMODE, 'L');
d76ed9a9
AS
2601#undef DO_MODE_CHAR
2602 switch (change->modes_set & (MODE_KEY|MODE_LIMIT)) {
2603 case MODE_KEY|MODE_LIMIT:
2604 used += sprintf(outbuff+used, "lk %d %s", change->new_limit, change->new_key);
2605 break;
2606 case MODE_KEY:
2607 used += sprintf(outbuff+used, "k %s", change->new_key);
2608 break;
2609 case MODE_LIMIT:
2610 used += sprintf(outbuff+used, "l %d", change->new_limit);
2611 break;
2612 }
2613 }
2614 outbuff[used] = 0;
2615 return outbuff;
2616}
2617
2618static int
2619clear_chanmode(struct chanNode *channel, const char *modes)
2620{
2621 unsigned int remove;
2622
2623 for (remove = 0; *modes; modes++) {
2624 switch (*modes) {
2625 case 'o': remove |= MODE_CHANOP; break;
55342ce8 2626 case 'h': remove |= MODE_HALFOP; break;
d76ed9a9
AS
2627 case 'v': remove |= MODE_VOICE; break;
2628 case 'p': remove |= MODE_PRIVATE; break;
2629 case 's': remove |= MODE_SECRET; break;
2630 case 'm': remove |= MODE_MODERATED; break;
2631 case 't': remove |= MODE_TOPICLIMIT; break;
2632 case 'i': remove |= MODE_INVITEONLY; break;
2633 case 'n': remove |= MODE_NOPRIVMSGS; break;
2634 case 'k':
2635 remove |= MODE_KEY;
2636 channel->key[0] = '\0';
2637 break;
2638 case 'l':
2639 remove |= MODE_LIMIT;
2640 channel->limit = 0;
2641 break;
2642 case 'b': remove |= MODE_BAN; break;
2aef5f4b 2643 case 'e': remove |= MODE_EXEMPT; break;
d76ed9a9
AS
2644 case 'D': remove |= MODE_DELAYJOINS; break;
2645 case 'r': remove |= MODE_REGONLY; break;
850bf0cf 2646 case 'c': remove |= MODE_NOCOLORS; break;
d76ed9a9 2647 case 'C': remove |= MODE_NOCTCPS; break;
d0cb2fb6
AS
2648 case 'S': remove |= MODE_STRIPCOLOR; break;
2649 case 'M': remove |= MODE_MODUNREG; break;
2650 case 'N': remove |= MODE_NONOTICE; break;
2651 case 'Q': remove |= MODE_NOQUITMSGS; break;
2652 case 'T': remove |= MODE_NOAMSG; break;
2653 case 'O': remove |= MODE_OPERSONLY; break;
ec1a68c8 2654 case 'z': remove |= MODE_REGISTERED; break;
c8ca69a0 2655 case 'Z': remove |= MODE_SSLONLY; break;
88c7cb10 2656 case 'L': remove |= MODE_HIDEMODE; break;
d76ed9a9
AS
2657 }
2658 }
2659
2660 if (!remove)
2661 return 1;
2662
2663 /* Remove simple modes. */
2664 channel->modes &= ~remove;
2665
2666 /* If removing bans, kill 'em all. */
2667 if ((remove & MODE_BAN) && channel->banlist.used) {
2668 unsigned int i;
2669 for (i=0; i<channel->banlist.used; i++)
2670 free(channel->banlist.list[i]);
2671 channel->banlist.used = 0;
2672 }
2673
2aef5f4b 2674 /* If removing exempts, kill 'em all. */
2675 if ((remove & MODE_EXEMPT) && channel->exemptlist.used) {
2676 unsigned int i;
2677 for (i=0; i<channel->exemptlist.used; i++)
2678 free(channel->exemptlist.list[i]);
2679 channel->exemptlist.used = 0;
2680 }
2681
d76ed9a9 2682 /* Remove member modes. */
55342ce8 2683 if ((remove & (MODE_CHANOP | MODE_HALFOP | MODE_VOICE)) && channel->members.used) {
2684 int mask = ~(remove & (MODE_CHANOP | MODE_HALFOP | MODE_VOICE));
d76ed9a9
AS
2685 unsigned int i;
2686
2687 for (i = 0; i < channel->members.used; i++)
2688 channel->members.list[i]->modes &= mask;
2689 }
2690
2691 return 1;
2692}
2693
2694void
2695reg_privmsg_func(struct userNode *user, privmsg_func_t handler)
2696{
2697 unsigned int numeric = user->num_local;
2698 if (numeric >= num_privmsg_funcs) {
2699 int newnum = numeric + 8;
2700 privmsg_funcs = realloc(privmsg_funcs, newnum*sizeof(privmsg_func_t));
2701 memset(privmsg_funcs+num_privmsg_funcs, 0, (newnum-num_privmsg_funcs)*sizeof(privmsg_func_t));
2702 num_privmsg_funcs = newnum;
2703 }
2704 if (privmsg_funcs[numeric])
2705 log_module(MAIN_LOG, LOG_WARNING, "re-registering new privmsg handler for numeric %d", numeric);
2706 privmsg_funcs[numeric] = handler;
2707}
2708
2709void
2710unreg_privmsg_func(struct userNode *user, privmsg_func_t handler)
2711{
2712 unsigned int x;
2713
2714 if (!user || handler)
2715 return; /* this really only works with users */
2716
2717 memset(privmsg_funcs+user->num_local, 0, sizeof(privmsg_func_t));
2718
2719 for (x = user->num_local+1; x < num_privmsg_funcs; x++)
2720 memmove(privmsg_funcs+x-1, privmsg_funcs+x, sizeof(privmsg_func_t));
2721
2722 privmsg_funcs = realloc(privmsg_funcs, num_privmsg_funcs*sizeof(privmsg_func_t));
2723 num_privmsg_funcs--;
2724}
2725
2726
2727void
2728reg_notice_func(struct userNode *user, privmsg_func_t handler)
2729{
2730 unsigned int numeric = user->num_local;
2731 if (numeric >= num_notice_funcs) {
2732 int newnum = numeric + 8;
2733 notice_funcs = realloc(notice_funcs, newnum*sizeof(privmsg_func_t));
2734 memset(notice_funcs+num_notice_funcs, 0, (newnum-num_notice_funcs)*sizeof(privmsg_func_t));
2735 num_notice_funcs = newnum;
2736 }
2737 if (notice_funcs[numeric])
2738 log_module(MAIN_LOG, LOG_WARNING, "re-registering new notice handler for numeric %d", numeric);
2739 notice_funcs[numeric] = handler;
2740}
2741
2742void
2743unreg_notice_func(struct userNode *user, privmsg_func_t handler)
2744{
2745 unsigned int x;
2746
2747 if (!user || handler)
2748 return; /* this really only works with users */
2749
2750 memset(notice_funcs+user->num_local, 0, sizeof(privmsg_func_t));
2751
2752 for (x = user->num_local+1; x < num_notice_funcs; x++)
2753 memmove(notice_funcs+x-1, notice_funcs+x, sizeof(privmsg_func_t));
2754
2755 memset(notice_funcs+user->num_local, 0, sizeof(privmsg_func_t));
2756 notice_funcs = realloc(notice_funcs, num_notice_funcs*sizeof(privmsg_func_t));
2757 num_notice_funcs--;
2758}
2759
2760void
2761reg_oper_func(oper_func_t handler)
2762{
2763 if (of_used == of_size) {
2764 if (of_size) {
2765 of_size <<= 1;
2766 of_list = realloc(of_list, of_size*sizeof(oper_func_t));
2767 } else {
2768 of_size = 8;
2769 of_list = malloc(of_size*sizeof(oper_func_t));
2770 }
2771 }
2772 of_list[of_used++] = handler;
2773}
2774
2775static void
2776call_oper_funcs(struct userNode *user)
2777{
2778 unsigned int n;
2779 if (IsLocal(user))
2780 return;
2781 for (n=0; n<of_used; n++)
2782 of_list[n](user);
2783}
2784
2785static void
2786send_burst(void)
2787{
2788 unsigned int i, hop, max_hop=1;
2789 dict_iterator_t it;
2790
2791 /* burst (juped) servers, closest first (except self, which is sent already) */
2792 for (i=0; i<ArrayLength(servers_num); i++)
2793 if (servers_num[i] && servers_num[i]->hops > max_hop)
2794 max_hop = servers_num[i]->hops;
2795 for (hop=1; hop<=max_hop; hop++) {
2796 for (i=0;i<ArrayLength(servers_num);i++) {
2797 if (servers_num[i]
2798 && (servers_num[i]->hops == hop)
2799 && (servers_num[i] != self->uplink))
2800 irc_server(servers_num[i]);
2801 }
2802 }
2803
2804 /* burst local nicks */
2805 for (i=0; i<=self->num_mask; i++)
2806 if (self->users[i])
2807 irc_user(self->users[i]);
2808
2809 /* build dict of unbursted channel names (just copy existing channels) */
2810 unbursted_channels = dict_new();
2811 for (it = dict_first(channels); it; it = iter_next(it))
2812 dict_insert(unbursted_channels, iter_key(it), iter_data(it));
2813}