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