]> jfr.im git - irc/evilnet/x3.git/blame - src/proto-p10.c
bug fix
[irc/evilnet/x3.git] / src / proto-p10.c
CommitLineData
d76ed9a9 1/* proto-p10.c - IRC protocol output
2 * Copyright 2000-2004 srvx Development Team
3 *
83ff05c3 4 * This file is part of x3.
d76ed9a9 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 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 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 102#define CMD_WALLUSERS "WALLUSERS"
103#define CMD_WALLVOICES "WALLVOICES"
55342ce8 104#define CMD_WALLHOPS "WALLHOPS"
d76ed9a9 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 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 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 188#define TOK_WALLUSERS "WU"
189#define TOK_WALLVOICES "WV"
55342ce8 190#define TOK_WALLHOPS "WH"
d76ed9a9 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 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 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 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 414 // sethost - reed/apples
415 // if (IsHelperIrcu(user))
416 if (IsSetHost(user))
d76ed9a9 417 modes[modelen++] = 'h';
182dd032 418 if (IsFakeHost(user))
419 modes[modelen++] = 'f';
d76ed9a9 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 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 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 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 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 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 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 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 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 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 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 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 1066
1067 /* This next line appears to tremple origin.. why? */
d76ed9a9 1068 user = GetUserN(argv[1]);
1069 if (!user)
1070 return 1; /* A QUIT probably passed the ACCOUNT. */
d9cd0e9d 1071
1072 if(!strcmp(argv[2],"C"))
1073 {
b21e2cfe 1074 if((hi = loc_auth(argv[4], argv[5])))
d9cd0e9d 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 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 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 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 1120
1121 if (argc < 3)
1122 return 0;
1123 modes[0] = 0;
2aef5f4b 1124
1125 exemptlist[0] = 0;
1126 banlist[0] = 0;
1127
d76ed9a9 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 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 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 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 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 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 1232 char *sethost; // sethost - reed/apples
1233 int i; // sethost - reed/apples
d76ed9a9 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 1243 // sethost - reed/apples
1244 if (argc == 3)
d76ed9a9 1245 mod_usermode(un, argv[2]);
182dd032 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 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 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 1713 /* Ignore dnsbl exemptions */
1714 dict_insert(irc_func_dict, TOK_EXEMPT, cmd_dummy);
fede8b64 1715 dict_insert(irc_func_dict, TOK_MARK, cmd_dummy);
1716 /* Ignore privs */
1717 dict_insert(irc_func_dict, TOK_PRIVS, cmd_dummy);
a74c9eac 1718 /* Ignore remote luser */
0c835e0f 1719 dict_insert(irc_func_dict, TOK_LUSERS, cmd_dummy);
d76ed9a9 1720 /* We have reliable clock! Always! Wraaa! */
1721 dict_insert(irc_func_dict, CMD_SETTIME, cmd_dummy);
1722 dict_insert(irc_func_dict, TOK_SETTIME, cmd_dummy);
1723 /* handle topics */
1724 dict_insert(irc_func_dict, "331", cmd_num_topic);
1725 dict_insert(irc_func_dict, "332", cmd_num_topic);
1726 dict_insert(irc_func_dict, "333", cmd_num_topic);
1727 dict_insert(irc_func_dict, "345", cmd_dummy); /* blah has been invited to blah */
1728 dict_insert(irc_func_dict, "432", cmd_error_nick); /* Erroneus [sic] nickname */
1729 /* ban list resetting */
1730 /* "stats g" responses */
1731 dict_insert(irc_func_dict, "247", cmd_num_gline);
1732 dict_insert(irc_func_dict, "219", cmd_dummy); /* "End of /STATS report" */
1733 /* other numeric responses we might get */
1734 dict_insert(irc_func_dict, "401", cmd_dummy); /* target left network */
1735 dict_insert(irc_func_dict, "403", cmd_dummy); /* no such channel */
1736 dict_insert(irc_func_dict, "404", cmd_dummy); /* cannot send to channel */
0d16e639 1737 dict_insert(irc_func_dict, "439", cmd_dummy); /* target change too fast */
d76ed9a9 1738 dict_insert(irc_func_dict, "441", cmd_dummy); /* target isn't on that channel */
1739 dict_insert(irc_func_dict, "442", cmd_dummy); /* you aren't on that channel */
1740 dict_insert(irc_func_dict, "443", cmd_dummy); /* is already on channel (after invite?) */
1741 dict_insert(irc_func_dict, "461", cmd_dummy); /* Not enough parameters (after TOPIC w/ 0 args) */
1742 dict_insert(irc_func_dict, "467", cmd_dummy); /* Channel key already set */
1743
1744 num_privmsg_funcs = 16;
1745 privmsg_funcs = malloc(sizeof(privmsg_func_t)*num_privmsg_funcs);
1746 memset(privmsg_funcs, 0, sizeof(privmsg_func_t)*num_privmsg_funcs);
1747
1748 num_notice_funcs = 16;
1749 notice_funcs = malloc(sizeof(privmsg_func_t)*num_notice_funcs);
1750 memset(notice_funcs, 0, sizeof(privmsg_func_t)*num_notice_funcs);
1751
1752 userList_init(&dead_users);
1753 reg_del_channel_func(remove_unbursted_channel);
1754 reg_exit_func(parse_cleanup);
1755}
1756
1757int
1758parse_line(char *line, int recursive)
1759{
1760 char *argv[MAXNUMPARAMS], *origin;
1761 int argc, cmd, res=0;
1762 cmd_func_t *func;
1763
1764 argc = split_line(line, true, MAXNUMPARAMS, argv);
1765 cmd = self->uplink || !argv[0][1] || !argv[0][2];
1766 if (argc > cmd) {
1767 if (cmd) {
1768 if (argv[0][0] == ':') {
1769 origin = argv[0]+1;
1770 } else if (!argv[0][1] || !argv[0][2]) {
1771 struct server *sNode = GetServerN(argv[0]);
1772 origin = sNode ? sNode->name : 0;
1773 } else {
1774 struct userNode *uNode = GetUserN(argv[0]);
1775 origin = uNode ? uNode->nick : 0;
1776 }
1777 } else
1778 origin = 0;
1779 if ((func = dict_find(irc_func_dict, argv[cmd], NULL)))
1780 res = func(origin, argc-cmd, argv+cmd);
1781 }
1782 if (!res) {
1783 log_module(MAIN_LOG, LOG_ERROR, "PARSE ERROR on line: %s", unsplit_string(argv, argc, NULL));
1784 } else if (!recursive) {
1785 unsigned int i;
1786 for (i=0; i<dead_users.used; i++)
1787 free_user(dead_users.list[i]);
1788 dead_users.used = 0;
1789 }
1790 return res;
1791}
1792
1793static void
1794parse_foreach(char *target_list, foreach_chanfunc cf, foreach_nonchan nc, foreach_userfunc uf, foreach_nonuser nu, void *data)
1795{
1796 char *j, old;
1797 do {
1798 j = target_list;
1799 while (*j != 0 && *j != ',')
1800 j++;
1801 old = *j;
1802 *j = 0;
1803 if (IsChannelName(target_list)
1804 || (target_list[0] == '0' && target_list[1] == '\0')) {
1805 struct chanNode *chan = GetChannel(target_list);
1806 if (chan) {
1807 if (cf)
1808 cf(chan, data);
1809 } else {
1810 if (nc)
1811 nc(target_list, data);
1812 }
1813 } else {
1814 struct userNode *user;
1815 struct privmsg_desc *pd = data;
1816
1817 pd->is_qualified = 0;
1818 if (*target_list == '@') {
1819 user = NULL;
1820 } else if (strchr(target_list, '@')) {
1821 struct server *server;
1822
1823 pd->is_qualified = 1;
1824 user = GetUserH(strtok(target_list, "@"));
1825 server = GetServerH(strtok(NULL, "@"));
1826
1827 if (user && (user->uplink != server)) {
1828 /* Don't attempt to index into any arrays
1829 using a user's numeric on another server. */
1830 user = NULL;
1831 }
1832 } else {
1833 user = GetUserN(target_list);
1834 }
1835
1836 if (user) {
1837 if (uf)
1838 uf(user, data);
1839 } else {
1840 if (nu)
1841 nu(target_list, data);
1842 }
1843 }
1844 target_list = j+1;
1845 } while (old == ',');
1846}
1847
1848static int
1849get_local_numeric(void)
1850{
1851 static unsigned int next_numeric = 0;
1852 if (self->clients > self->num_mask)
1853 return -1;
1854 while (self->users[next_numeric])
1855 if (++next_numeric > self->num_mask)
1856 next_numeric = 0;
1857 return next_numeric;
1858}
1859
1860static void
1861make_numeric(struct server *svr, int local_num, char *outbuf)
1862{
1863 int slen, llen;
1864
1865 if (force_n2k || svr->numeric[1]) {
1866 slen = 2;
1867 llen = 3;
1868 } else {
1869 slen = 1;
1870 llen = (local_num < 64*64) ? 2 : 3;
1871 }
1872 strncpy(outbuf, svr->numeric, slen);
1873 inttobase64(outbuf+slen, local_num, llen);
1874 outbuf[slen+llen] = 0;
1875}
1876
1877struct server *
1878AddServer(struct server *uplink, const char *name, int hops, time_t boot, time_t link, const char *numeric, const char *description)
1879{
1880 struct server* sNode;
1881 int slen, mlen;
1882
1883 if ((sNode = GetServerN(numeric))) {
1884 /* This means we're trying to re-add an existant server.
1885 * To be safe, we should forget the previous incarnation.
1886 * (And all its linked servers.)
1887 *
1888 * It usually only happens in replays when the original
1889 * had a ping timeout and the replay didn't (because
1890 * replaying a ping timeout invariably gets things wrong).
1891 */
1892 DelServer(sNode, 0, NULL);
1893 }
1894
1895 switch (strlen(numeric)) {
1896 case 5: slen = 2; mlen = 3; break;
1897 case 4: slen = 1; mlen = 3; break;
1898 case 3: slen = 1; mlen = 2; break;
1899 default:
1900 log_module(MAIN_LOG, LOG_ERROR, "AddServer(\"%s\", \"%s\", ...): Numeric %s has invalid length.", uplink->name, name, numeric);
1901 return NULL;
1902 }
1903
1904 sNode = calloc(1, sizeof(*sNode));
1905 sNode->uplink = uplink;
1906 safestrncpy(sNode->name, name, sizeof(sNode->name));
1907 sNode->num_mask = base64toint(numeric+slen, mlen);
1908 sNode->hops = hops;
1909 sNode->boot = boot;
1910 sNode->link = link;
1911 strncpy(sNode->numeric, numeric, slen);
1912 safestrncpy(sNode->description, description, sizeof(sNode->description));
1913 sNode->users = calloc(sNode->num_mask+1, sizeof(*sNode->users));
1914 serverList_init(&sNode->children);
1915 if (sNode->uplink) {
1916 /* uplink may be NULL if we're just building ourself */
1917 serverList_append(&sNode->uplink->children, sNode);
1918 }
1919 servers_num[base64toint(numeric, slen)] = sNode;
1920 dict_insert(servers, sNode->name, sNode);
1921 return sNode;
1922}
1923
1924void DelServer(struct server* serv, int announce, const char *message)
1925{
1926 unsigned int i;
1927
1928 /* If we receive an ERROR command before the SERVER
1929 * command a NULL server can be passed */
1930 if (!serv)
1931 return;
1932
1933 /* Hrm, what's the right way to SQUIT some other server?
1934 * (This code is only to handle killing juped servers.) */
1935 if (announce && (serv->uplink == self) && (serv != self->uplink))
1936 irc_squit(serv, message, NULL);
1937
1938 /* must recursively remove servers linked to this one first */
1939 for (i=serv->children.used;i>0;)
1940 if (serv->children.list[--i] != self)
1941 DelServer(serv->children.list[i], false, NULL);
1942
1943 /* clean up server's user hash tables */
1944 for (i=0;i<=serv->num_mask;i++)
1945 if (serv->users[i])
1946 DelUser(serv->users[i], NULL, false, "server delinked");
1947
1948 /* delete server */
1949 if (serv->uplink)
1950 serverList_remove(&serv->uplink->children, serv);
1951 if (serv == self->uplink)
1952 self->uplink = NULL;
1953 servers_num[base64toint(serv->numeric, strlen(serv->numeric))] = NULL;
1954 dict_remove(servers, serv->name);
1955 serverList_clean(&serv->children);
1956 free(serv->users);
1957 free(serv);
1958}
1959
1960struct userNode *
a32da4c7 1961AddService(const char *nick, const char *modes, const char *desc, const char *hostname)
d76ed9a9 1962{
1963 char numeric[COMBO_NUMERIC_LEN+1];
1964 int local_num = get_local_numeric();
1965 time_t timestamp = now;
1966 struct userNode *old_user = GetUserH(nick);
1967
1968 if (old_user) {
1969 if (IsLocal(old_user))
1970 return old_user;
1971 timestamp = old_user->timestamp - 1;
1972 }
1973 if (local_num == -1) {
1974 log_module(MAIN_LOG, LOG_ERROR, "Unable to allocate numnick for service %s", nick);
1975 return 0;
1976 }
1977 if (!hostname)
1978 hostname = self->name;
1979 make_numeric(self, local_num, numeric);
180e0971 1980 /* TODO: Make these modes part of the conf file */
1981 return AddUser(self, nick, nick, hostname, modes ? modes : "+oik", numeric, desc, now, "AAAAAA");
d76ed9a9 1982}
1983
1984struct userNode *
1985AddClone(const char *nick, const char *ident, const char *hostname, const char *desc)
1986{
1987 char numeric[COMBO_NUMERIC_LEN+1];
1988 int local_num = get_local_numeric();
1989 time_t timestamp = now;
1990 struct userNode *old_user = GetUserH(nick);
1991
1992 if (old_user) {
1993 if (IsLocal(old_user))
1994 return old_user;
1995 timestamp = old_user->timestamp - 1;
1996 }
1997 if (local_num == -1) {
1998 log_module(MAIN_LOG, LOG_ERROR, "Unable to allocate numnick for clone %s", nick);
1999 return 0;
2000 }
2001 make_numeric(self, local_num, numeric);
2002 return AddUser(self, nick, ident, hostname, "+i", numeric, desc, timestamp, "AAAAAA");
2003}
2004
2005int
2006is_valid_nick(const char *nick) {
ec1a68c8 2007 unsigned int ii;
d76ed9a9 2008 /* IRC has some of The Most Fucked-Up ideas about character sets
2009 * in the world.. */
2010 if (!isalpha(*nick) && !strchr("{|}~[\\]^_`", *nick))
2011 return 0;
ec1a68c8 2012 for (ii = 0; nick[ii]; ++ii)
2013 if (!isalnum(nick[ii]) && !strchr("{|}~[\\]^-_`", nick[ii]))
d76ed9a9 2014 return 0;
2015 if (strlen(nick) > nicklen)
2016 return 0;
2017 return 1;
2018}
2019
2020static struct userNode*
2021AddUser(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)
2022{
2023 struct userNode *oldUser, *uNode;
2024 unsigned int n, ignore_user;
2025
2026 if ((strlen(numeric) < 3) || (strlen(numeric) > 5)) {
2027 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): numeric %s wrong length!", uplink, nick, numeric);
2028 return NULL;
2029 }
2030
2031 if (!uplink) {
2032 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): server for numeric %s doesn't exist!", uplink, nick, numeric);
2033 return NULL;
2034 }
2035
2036 if (uplink != GetServerN(numeric)) {
2037 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): server for numeric %s differs from nominal uplink %s.", uplink, nick, numeric, uplink->name);
2038 return NULL;
2039 }
2040
2041 if (!is_valid_nick(nick)) {
2042 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): invalid nickname detected.", uplink, nick);
2043 return NULL;
2044 }
2045
2046 ignore_user = 0;
2047 if ((oldUser = GetUserH(nick))) {
2048 if (IsLocal(oldUser) && (IsService(oldUser) || IsPersistent(oldUser))) {
2049 /* The service should collide the new user off. */
2050 oldUser->timestamp = timestamp - 1;
2051 irc_user(oldUser);
2052 }
2053 if (oldUser->timestamp > timestamp) {
2054 /* "Old" user is really newer; remove them */
2055 DelUser(oldUser, 0, 1, "Overruled by older nick");
2056 } else {
2057 /* User being added is too new; do not add them to
2058 * clients, but do add them to the server's list, since it
2059 * will send a KILL and QUIT soon. */
2060 ignore_user = 1;
2061 }
2062 }
2063
2064 /* create new usernode and set all values */
2065 uNode = calloc(1, sizeof(*uNode));
2066 uNode->nick = strdup(nick);
2067 safestrncpy(uNode->ident, ident, sizeof(uNode->ident));
2068 safestrncpy(uNode->info, userinfo, sizeof(uNode->info));
2069 safestrncpy(uNode->hostname, hostname, sizeof(uNode->hostname));
2070 safestrncpy(uNode->numeric, numeric, sizeof(uNode->numeric));
2071 uNode->ip.s_addr = htonl(base64toint(realip, 6));
2072 uNode->timestamp = timestamp;
2073 modeList_init(&uNode->channels);
2074 uNode->uplink = uplink;
2075 if (++uNode->uplink->clients > uNode->uplink->max_clients) {
2076 uNode->uplink->max_clients = uNode->uplink->clients;
2077 }
2078 uNode->num_local = base64toint(numeric+strlen(uNode->uplink->numeric), 3) & uNode->uplink->num_mask;
2079 uNode->uplink->users[uNode->num_local] = uNode;
2080 mod_usermode(uNode, modes);
2081 if (ignore_user)
2082 return uNode;
2083
2084 dict_insert(clients, uNode->nick, uNode);
2085 if (dict_size(clients) > max_clients) {
2086 max_clients = dict_size(clients);
2087 max_clients_time = now;
2088 }
2089 if (IsLocal(uNode))
2090 irc_user(uNode);
2091 for (n=0; n<nuf_used; n++)
2092 if (nuf_list[n](uNode))
2093 break;
2094 return uNode;
2095}
2096
2097/* removes user from it's server's hash table and nick hash table */
2098void
2099DelUser(struct userNode* user, struct userNode *killer, int announce, const char *why)
2100{
2101 unsigned int n;
2102
ec1a68c8 2103 verify(user);
2104
d76ed9a9 2105 /* mark them as dead, in case anybody cares */
2106 user->dead = 1;
2107
ac3bdc8d 2108 /* remove pending adduser commands */
2109 wipe_adduser_pending(NULL, user);
2110
d76ed9a9 2111 /* remove user from all channels */
2112 while (user->channels.used > 0)
2113 DelChannelUser(user, user->channels.list[user->channels.used-1]->channel, false, 0);
2114
2115 /* Call these in reverse order so ChanServ can update presence
2116 information before NickServ nukes the handle_info. */
2117 for (n = duf_used; n > 0; )
2118 duf_list[--n](user, killer, why);
2119
2120 user->uplink->clients--;
2121 user->uplink->users[user->num_local] = NULL;
2122 if (IsOper(user))
2123 userList_remove(&curr_opers, user);
2124 /* remove from global dictionary, but not if after a collide */
2125 if (user == dict_find(clients, user->nick, NULL))
2126 dict_remove(clients, user->nick);
2127
2128 if (IsInvisible(user))
2129 invis_clients--;
2130
2131 if (announce) {
2132 if (IsLocal(user))
2133 irc_quit(user, why);
2134 else
2135 irc_kill(killer, user, why);
2136 }
2137
2138 modeList_clean(&user->channels);
2139 /* We don't free them, in case we try to privmsg them or something
2140 * (like when a stupid oper kills themself). We just put them onto
2141 * a list of clients that get freed after processing each line.
2142 */
2143 if (dead_users.size)
2144 userList_append(&dead_users, user);
2145 else
2146 free_user(user);
2147}
2148
697f4c9a 2149static void call_oper_funcs(struct userNode *user);
2150
d76ed9a9 2151void mod_usermode(struct userNode *user, const char *mode_change) {
d76ed9a9 2152 int add = 1;
2153 const char *word = mode_change;
2154
2155 if (!user || !mode_change)
2156 return;
a32da4c7 2157 while (*word != ' ' && *word) word++;
2158 while (*word == ' ') word++;
d76ed9a9 2159 while (1) {
2160#define do_user_mode(FLAG) do { if (add) user->modes |= FLAG; else user->modes &= ~FLAG; } while (0)
2161 switch (*mode_change++) {
2162 case 0: case ' ': return;
2163 case '+': add = 1; break;
2164 case '-': add = 0; break;
2165 case 'o':
d76ed9a9 2166 if (add) {
ec0120d1 2167 if(!IsOper(user)) { /* Dont re-oper an oper */
2168 userList_append(&curr_opers, user);
2169 call_oper_funcs(user);
2170 }
d76ed9a9 2171 } else {
2172 userList_remove(&curr_opers, user);
2173 }
3d0b24ce 2174 do_user_mode(FLAGS_OPER);
d76ed9a9 2175 break;
2176 case 'O': do_user_mode(FLAGS_LOCOP); break;
2177 case 'i': do_user_mode(FLAGS_INVISIBLE);
2178 if (add)
2179 invis_clients++;
2180 else
2181 invis_clients--;
2182 break;
2183 case 'w': do_user_mode(FLAGS_WALLOP); break;
2184 case 's': do_user_mode(FLAGS_SERVNOTICE); break;
2185 case 'd': do_user_mode(FLAGS_DEAF); break;
2186 case 'k': do_user_mode(FLAGS_SERVICE); break;
2187 case 'g': do_user_mode(FLAGS_GLOBAL); break;
182dd032 2188 // sethost - reed/apples
2189 // case 'h': do_user_mode(FLAGS_HELPER); break;
2190 // I check if there's an 'h' in the first part, and if there,
2191 // then everything after the space becomes their new host.
2192 case 'h': do_user_mode(FLAGS_SETHOST);
2193 if (*word) {
2194 char sethost[MAXLEN];
2195 unsigned int ii;
2196 for (ii=0; (*word != ' ') && (*word != '\0'); )
2197 sethost[ii++] = *word++;
2198 sethost[ii] = 0;
2199 while (*word == ' ')
2200 word++;
2201 safestrncpy(user->sethost, sethost, sizeof(user->sethost));
2202 }
2203 break;
d76ed9a9 2204 case 'x': do_user_mode(FLAGS_HIDDEN_HOST); break;
2205 case 'r':
2206 if (*word) {
2207 char tag[MAXLEN];
2208 unsigned int ii;
2209 for (ii=0; (*word != ' ') && (*word != '\0'); )
2210 tag[ii++] = *word++;
2211 tag[ii] = 0;
2212 while (*word == ' ')
2213 word++;
2214 call_account_func(user, tag);
2215 }
2216 break;
2217 case 'f':
2218 if (*word) {
2219 char host[MAXLEN];
2220 unsigned int ii;
2221 for (ii=0; (*word != ' ') && (*word != '\0'); )
2222 host[ii++] = *word++;
2223 host[ii] = 0;
2224 while (*word == ' ')
2225 word++;
2226 assign_fakehost(user, host, 0);
2227 }
2228 break;
2229 }
2230#undef do_user_mode
2231 }
2232}
2233
2234struct mod_chanmode *
2235mod_chanmode_parse(struct chanNode *channel, char **modes, unsigned int argc, unsigned int flags)
2236{
2237 struct mod_chanmode *change;
2238 unsigned int ii, in_arg, ch_arg, add;
2239
2240 if (argc == 0)
2241 return NULL;
2242 if (!(change = mod_chanmode_alloc(argc - 1)))
2243 return NULL;
2244
2245 for (ii = ch_arg = 0, in_arg = add = 1;
2246 (modes[0][ii] != '\0') && (modes[0][ii] != ' ');
2247 ++ii) {
2248 switch (modes[0][ii]) {
2249 case '+':
2250 add = 1;
2251 break;
2252 case '-':
2253 add = 0;
2254 break;
2255#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)
2256 case 'C': do_chan_mode(MODE_NOCTCPS); break;
2257 case 'D': do_chan_mode(MODE_DELAYJOINS); break;
2258 case 'c': do_chan_mode(MODE_NOCOLORS); break;
2259 case 'i': do_chan_mode(MODE_INVITEONLY); break;
2260 case 'm': do_chan_mode(MODE_MODERATED); break;
2261 case 'n': do_chan_mode(MODE_NOPRIVMSGS); break;
2262 case 'p': do_chan_mode(MODE_PRIVATE); break;
2263 case 'r': do_chan_mode(MODE_REGONLY); break;
2264 case 's': do_chan_mode(MODE_SECRET); break;
2265 case 't': do_chan_mode(MODE_TOPICLIMIT); break;
d0cb2fb6 2266 case 'S': do_chan_mode(MODE_STRIPCOLOR); break;
2267 case 'M': do_chan_mode(MODE_MODUNREG); break;
2268 case 'N': do_chan_mode(MODE_NONOTICE); break;
2269 case 'Q': do_chan_mode(MODE_NOQUITMSGS); break;
2270 case 'T': do_chan_mode(MODE_NOAMSG); break;
2271 case 'O': do_chan_mode(MODE_OPERSONLY); break;
c8ca69a0 2272 case 'Z': do_chan_mode(MODE_SSLONLY); break;
88c7cb10 2273 case 'L': do_chan_mode(MODE_HIDEMODE); break;
ec1a68c8 2274 case 'z':
2275 if (!(flags & MCP_REGISTERED)) {
2276 do_chan_mode(MODE_REGISTERED);
2277 } else {
2278 mod_chanmode_free(change);
2279 return NULL;
2280 }
2281 break;
d76ed9a9 2282#undef do_chan_mode
2283 case 'l':
2284 if (add) {
2285 if (in_arg >= argc)
2286 goto error;
2287 change->modes_set |= MODE_LIMIT;
2288 change->new_limit = atoi(modes[in_arg++]);
2289 } else {
2290 change->modes_set &= ~MODE_LIMIT;
2291 change->modes_clear |= MODE_LIMIT;
2292 }
2293 break;
2294 case 'k':
2295 if (add) {
2296 if (in_arg >= argc)
2297 goto error;
2298 change->modes_set |= MODE_KEY;
2299 safestrncpy(change->new_key, modes[in_arg++], sizeof(change->new_key));
2300 } else {
2301 change->modes_clear |= MODE_KEY;
2302 if (!(flags & MCP_KEY_FREE)) {
2303 if (in_arg >= argc)
2304 goto error;
2305 in_arg++;
2306 }
2307 }
2308 break;
2309 case 'b':
2310 if (!(flags & MCP_ALLOW_OVB))
2311 goto error;
2312 if (in_arg >= argc)
2313 goto error;
2314 change->args[ch_arg].mode = MODE_BAN;
2315 if (!add)
2316 change->args[ch_arg].mode |= MODE_REMOVE;
a32da4c7 2317 change->args[ch_arg++].u.hostmask = modes[in_arg++];
d76ed9a9 2318 break;
2aef5f4b 2319 case 'e':
2320 if (!(flags & MCP_ALLOW_OVB))
2321 goto error;
2322 if (in_arg >= argc)
2323 goto error;
2324 change->args[ch_arg].mode = MODE_EXEMPT;
2325 if (!add)
2326 change->args[ch_arg].mode |= MODE_REMOVE;
a32da4c7 2327 change->args[ch_arg++].u.hostmask = modes[in_arg++];
2aef5f4b 2328 break;
55342ce8 2329 case 'o': case 'h': case 'v':
d76ed9a9 2330 {
2331 struct userNode *victim;
2332 if (!(flags & MCP_ALLOW_OVB))
2333 goto error;
2334 if (in_arg >= argc)
2335 goto error;
55342ce8 2336
2337 if (modes[0][ii] == 'o')
2338 change->args[ch_arg].mode = MODE_CHANOP;
2339 else if (modes[0][ii] == 'h')
2340 change->args[ch_arg].mode = MODE_HALFOP;
2341 else if (modes[0][ii] == 'v')
2342 change->args[ch_arg].mode = MODE_VOICE;
2343
d76ed9a9 2344 if (!add)
2345 change->args[ch_arg].mode |= MODE_REMOVE;
2346 if (flags & MCP_FROM_SERVER)
2347 victim = GetUserN(modes[in_arg++]);
2348 else
2349 victim = GetUserH(modes[in_arg++]);
2350 if (!victim)
2351 continue;
a32da4c7 2352 if ((change->args[ch_arg].u.member = GetUserMode(channel, victim)))
d76ed9a9 2353 ch_arg++;
2354 break;
2355 }
2356 default:
2357 if (!(flags & MCP_FROM_SERVER))
2358 goto error;
2359 break;
2360 }
2361 }
2362 change->argc = ch_arg; /* in case any turned out to be ignored */
2363 if (change->modes_set & MODE_SECRET) {
2364 change->modes_set &= ~(MODE_PRIVATE);
2365 change->modes_clear |= MODE_PRIVATE;
2366 } else if (change->modes_set & MODE_PRIVATE) {
2367 change->modes_set &= ~(MODE_SECRET);
2368 change->modes_clear |= MODE_SECRET;
2369 }
2370 return change;
2371 error:
2372 mod_chanmode_free(change);
2373 return NULL;
2374}
2375
2376struct chanmode_buffer {
2377 char modes[MAXLEN];
2378 char args[MAXLEN];
2379 struct chanNode *channel;
2380 struct userNode *actor;
2381 unsigned int modes_used;
2382 unsigned int args_used;
2383 size_t chname_len;
2384 unsigned int is_add : 1;
2385 unsigned int is_chanop : 1;
2386};
2387
2388static void
2389mod_chanmode_append(struct chanmode_buffer *buf, char ch, const char *arg)
2390{
2391 size_t arg_len = strlen(arg);
2392 if (buf->modes_used > (MAXMODEPARAMS) ||
2393 buf->modes_used + buf->args_used + buf->chname_len + arg_len > 450) {
2394 memcpy(buf->modes + buf->modes_used, buf->args, buf->args_used);
2395 buf->modes[buf->modes_used + buf->args_used] = '\0';
2396 irc_mode((buf->is_chanop ? buf->actor : NULL), buf->channel, buf->modes);
2397 buf->modes[0] = buf->is_add ? '+' : '-';
2398 buf->modes_used = 1;
2399 buf->args_used = 0;
2400 }
2401 buf->modes[buf->modes_used++] = ch;
2402 buf->args[buf->args_used++] = ' ';
2403 memcpy(buf->args + buf->args_used, arg, arg_len);
2404 buf->args_used += arg_len;
2405}
2406
2407void
2408mod_chanmode_announce(struct userNode *who, struct chanNode *channel, struct mod_chanmode *change)
2409{
2410 struct chanmode_buffer chbuf;
2411 unsigned int arg;
2412 struct modeNode *mn;
2413 char int_buff[32], mode = '\0';
2414
2415 assert(change->argc <= change->alloc_argc);
2416 memset(&chbuf, 0, sizeof(chbuf));
2417 chbuf.channel = channel;
2418 chbuf.actor = who;
2419 chbuf.chname_len = strlen(channel->name);
2420
2421 mn = GetUserMode(channel, who);
2422 if ((mn && (mn->modes & MODE_CHANOP)) || off_channel)
2423 chbuf.is_chanop = 1;
2424
2425 /* First remove modes */
2426 chbuf.is_add = 0;
2427 if (change->modes_clear) {
2428 if (mode != '-')
2429 chbuf.modes[chbuf.modes_used++] = mode = '-';
2430#define DO_MODE_CHAR(BIT, CHAR) if (change->modes_clear & MODE_##BIT) chbuf.modes[chbuf.modes_used++] = CHAR
2431 DO_MODE_CHAR(PRIVATE, 'p');
2432 DO_MODE_CHAR(SECRET, 's');
2433 DO_MODE_CHAR(MODERATED, 'm');
2434 DO_MODE_CHAR(TOPICLIMIT, 't');
2435 DO_MODE_CHAR(INVITEONLY, 'i');
2436 DO_MODE_CHAR(NOPRIVMSGS, 'n');
2437 DO_MODE_CHAR(LIMIT, 'l');
2438 DO_MODE_CHAR(DELAYJOINS, 'D');
2439 DO_MODE_CHAR(REGONLY, 'r');
2440 DO_MODE_CHAR(NOCOLORS, 'c');
2441 DO_MODE_CHAR(NOCTCPS, 'C');
d0cb2fb6 2442 DO_MODE_CHAR(STRIPCOLOR, 'S');
2443 DO_MODE_CHAR(MODUNREG, 'M');
2444 DO_MODE_CHAR(NONOTICE, 'N');
2445 DO_MODE_CHAR(NOQUITMSGS, 'Q');
2446 DO_MODE_CHAR(NOAMSG, 'T');
2447 DO_MODE_CHAR(OPERSONLY, 'O');
ec1a68c8 2448 DO_MODE_CHAR(REGISTERED, 'z');
c8ca69a0 2449 DO_MODE_CHAR(SSLONLY, 'Z');
88c7cb10 2450 DO_MODE_CHAR(HIDEMODE, 'L');
d76ed9a9 2451#undef DO_MODE_CHAR
2452 if (change->modes_clear & channel->modes & MODE_KEY)
2453 mod_chanmode_append(&chbuf, 'k', channel->key);
2454 }
2455 for (arg = 0; arg < change->argc; ++arg) {
2456 if (!(change->args[arg].mode & MODE_REMOVE))
2457 continue;
2458 if (mode != '-')
2459 chbuf.modes[chbuf.modes_used++] = mode = '-';
2460 switch (change->args[arg].mode & ~MODE_REMOVE) {
2461 case MODE_BAN:
a32da4c7 2462 mod_chanmode_append(&chbuf, 'b', change->args[arg].u.hostmask);
d76ed9a9 2463 break;
2aef5f4b 2464 case MODE_EXEMPT:
a32da4c7 2465 mod_chanmode_append(&chbuf, 'e', change->args[arg].u.hostmask);
2aef5f4b 2466 break;
d76ed9a9 2467 default:
2468 if (change->args[arg].mode & MODE_CHANOP)
a32da4c7 2469 mod_chanmode_append(&chbuf, 'o', change->args[arg].u.member->user->numeric);
55342ce8 2470 if (change->args[arg].mode & MODE_HALFOP)
a32da4c7 2471 mod_chanmode_append(&chbuf, 'h', change->args[arg].u.member->user->numeric);
d76ed9a9 2472 if (change->args[arg].mode & MODE_VOICE)
a32da4c7 2473 mod_chanmode_append(&chbuf, 'v', change->args[arg].u.member->user->numeric);
d76ed9a9 2474 break;
2475 }
2476 }
2477
2478 /* Then set them */
2479 chbuf.is_add = 1;
2480 if (change->modes_set) {
2481 if (mode != '+')
2482 chbuf.modes[chbuf.modes_used++] = mode = '+';
2483#define DO_MODE_CHAR(BIT, CHAR) if (change->modes_set & MODE_##BIT) chbuf.modes[chbuf.modes_used++] = CHAR
2484 DO_MODE_CHAR(PRIVATE, 'p');
2485 DO_MODE_CHAR(SECRET, 's');
2486 DO_MODE_CHAR(MODERATED, 'm');
2487 DO_MODE_CHAR(TOPICLIMIT, 't');
2488 DO_MODE_CHAR(INVITEONLY, 'i');
2489 DO_MODE_CHAR(NOPRIVMSGS, 'n');
2490 DO_MODE_CHAR(DELAYJOINS, 'D');
2491 DO_MODE_CHAR(REGONLY, 'r');
2492 DO_MODE_CHAR(NOCOLORS, 'c');
2493 DO_MODE_CHAR(NOCTCPS, 'C');
d0cb2fb6 2494 DO_MODE_CHAR(STRIPCOLOR, 'S');
2495 DO_MODE_CHAR(MODUNREG, 'M');
2496 DO_MODE_CHAR(NONOTICE, 'N');
2497 DO_MODE_CHAR(NOQUITMSGS, 'Q');
2498 DO_MODE_CHAR(NOAMSG, 'T');
2499 DO_MODE_CHAR(OPERSONLY, 'O');
ec1a68c8 2500 DO_MODE_CHAR(REGISTERED, 'z');
c8ca69a0 2501 DO_MODE_CHAR(SSLONLY, 'Z');
88c7cb10 2502 DO_MODE_CHAR(HIDEMODE, 'L');
d76ed9a9 2503#undef DO_MODE_CHAR
2504 if(change->modes_set & MODE_KEY)
2505 mod_chanmode_append(&chbuf, 'k', change->new_key);
2506 if(change->modes_set & MODE_LIMIT) {
2507 sprintf(int_buff, "%d", change->new_limit);
2508 mod_chanmode_append(&chbuf, 'l', int_buff);
2509 }
2510 }
2511 for (arg = 0; arg < change->argc; ++arg) {
2512 if (change->args[arg].mode & MODE_REMOVE)
2513 continue;
2514 if (mode != '+')
2515 chbuf.modes[chbuf.modes_used++] = mode = '+';
2516 switch (change->args[arg].mode) {
2517 case MODE_BAN:
a32da4c7 2518 mod_chanmode_append(&chbuf, 'b', change->args[arg].u.hostmask);
d76ed9a9 2519 break;
2aef5f4b 2520 case MODE_EXEMPT:
a32da4c7 2521 mod_chanmode_append(&chbuf, 'e', change->args[arg].u.hostmask);
2aef5f4b 2522 break;
d76ed9a9 2523 default:
2524 if (change->args[arg].mode & MODE_CHANOP)
a32da4c7 2525 mod_chanmode_append(&chbuf, 'o', change->args[arg].u.member->user->numeric);
55342ce8 2526 if (change->args[arg].mode & MODE_HALFOP)
a32da4c7 2527 mod_chanmode_append(&chbuf, 'h', change->args[arg].u.member->user->numeric);
d76ed9a9 2528 if (change->args[arg].mode & MODE_VOICE)
a32da4c7 2529 mod_chanmode_append(&chbuf, 'v', change->args[arg].u.member->user->numeric);
d76ed9a9 2530 break;
2531 }
2532 }
2533
2534 /* Flush the buffer and apply changes locally */
2535 if (chbuf.modes_used > 0) {
2536 memcpy(chbuf.modes + chbuf.modes_used, chbuf.args, chbuf.args_used);
2537 chbuf.modes[chbuf.modes_used + chbuf.args_used] = '\0';
2538 irc_mode((chbuf.is_chanop ? chbuf.actor : NULL), chbuf.channel, chbuf.modes);
2539 }
2540 mod_chanmode_apply(who, channel, change);
2541}
2542
2543char *
2544mod_chanmode_format(struct mod_chanmode *change, char *outbuff)
2545{
2546 unsigned int used = 0;
2547 assert(change->argc <= change->alloc_argc);
2548 if (change->modes_clear) {
2549 outbuff[used++] = '-';
2550#define DO_MODE_CHAR(BIT, CHAR) if (change->modes_clear & MODE_##BIT) outbuff[used++] = CHAR
2551 DO_MODE_CHAR(PRIVATE, 'p');
2552 DO_MODE_CHAR(SECRET, 's');
2553 DO_MODE_CHAR(MODERATED, 'm');
2554 DO_MODE_CHAR(TOPICLIMIT, 't');
2555 DO_MODE_CHAR(INVITEONLY, 'i');
2556 DO_MODE_CHAR(NOPRIVMSGS, 'n');
2557 DO_MODE_CHAR(LIMIT, 'l');
2558 DO_MODE_CHAR(KEY, 'k');
2559 DO_MODE_CHAR(DELAYJOINS, 'D');
2560 DO_MODE_CHAR(REGONLY, 'r');
2561 DO_MODE_CHAR(NOCOLORS, 'c');
2562 DO_MODE_CHAR(NOCTCPS, 'C');
d0cb2fb6 2563 DO_MODE_CHAR(STRIPCOLOR, 'S');
2564 DO_MODE_CHAR(MODUNREG, 'M');
2565 DO_MODE_CHAR(NONOTICE, 'N');
2566 DO_MODE_CHAR(NOQUITMSGS, 'Q');
2567 DO_MODE_CHAR(NOAMSG, 'T');
2568 DO_MODE_CHAR(OPERSONLY, 'O');
ec1a68c8 2569 DO_MODE_CHAR(REGISTERED, 'z');
c8ca69a0 2570 DO_MODE_CHAR(SSLONLY, 'Z');
88c7cb10 2571 DO_MODE_CHAR(HIDEMODE, 'L');
d76ed9a9 2572#undef DO_MODE_CHAR
2573 }
2574 if (change->modes_set) {
2575 outbuff[used++] = '+';
2576#define DO_MODE_CHAR(BIT, CHAR) if (change->modes_set & MODE_##BIT) outbuff[used++] = CHAR
2577 DO_MODE_CHAR(PRIVATE, 'p');
2578 DO_MODE_CHAR(SECRET, 's');
2579 DO_MODE_CHAR(MODERATED, 'm');
2580 DO_MODE_CHAR(TOPICLIMIT, 't');
2581 DO_MODE_CHAR(INVITEONLY, 'i');
2582 DO_MODE_CHAR(NOPRIVMSGS, 'n');
2583 DO_MODE_CHAR(DELAYJOINS, 'D');
2584 DO_MODE_CHAR(REGONLY, 'r');
2585 DO_MODE_CHAR(NOCOLORS, 'c');
2586 DO_MODE_CHAR(NOCTCPS, 'C');
d0cb2fb6 2587 DO_MODE_CHAR(STRIPCOLOR, 'S');
2588 DO_MODE_CHAR(MODUNREG, 'M');
2589 DO_MODE_CHAR(NONOTICE, 'N');
2590 DO_MODE_CHAR(NOQUITMSGS, 'Q');
2591 DO_MODE_CHAR(NOAMSG, 'T');
2592 DO_MODE_CHAR(OPERSONLY, 'O');
ec1a68c8 2593 DO_MODE_CHAR(REGISTERED, 'z');
c8ca69a0 2594 DO_MODE_CHAR(SSLONLY, 'Z');
88c7cb10 2595 DO_MODE_CHAR(HIDEMODE, 'L');
d76ed9a9 2596#undef DO_MODE_CHAR
2597 switch (change->modes_set & (MODE_KEY|MODE_LIMIT)) {
2598 case MODE_KEY|MODE_LIMIT:
2599 used += sprintf(outbuff+used, "lk %d %s", change->new_limit, change->new_key);
2600 break;
2601 case MODE_KEY:
2602 used += sprintf(outbuff+used, "k %s", change->new_key);
2603 break;
2604 case MODE_LIMIT:
2605 used += sprintf(outbuff+used, "l %d", change->new_limit);
2606 break;
2607 }
2608 }
2609 outbuff[used] = 0;
2610 return outbuff;
2611}
2612
2613static int
2614clear_chanmode(struct chanNode *channel, const char *modes)
2615{
2616 unsigned int remove;
2617
2618 for (remove = 0; *modes; modes++) {
2619 switch (*modes) {
2620 case 'o': remove |= MODE_CHANOP; break;
55342ce8 2621 case 'h': remove |= MODE_HALFOP; break;
d76ed9a9 2622 case 'v': remove |= MODE_VOICE; break;
2623 case 'p': remove |= MODE_PRIVATE; break;
2624 case 's': remove |= MODE_SECRET; break;
2625 case 'm': remove |= MODE_MODERATED; break;
2626 case 't': remove |= MODE_TOPICLIMIT; break;
2627 case 'i': remove |= MODE_INVITEONLY; break;
2628 case 'n': remove |= MODE_NOPRIVMSGS; break;
2629 case 'k':
2630 remove |= MODE_KEY;
2631 channel->key[0] = '\0';
2632 break;
2633 case 'l':
2634 remove |= MODE_LIMIT;
2635 channel->limit = 0;
2636 break;
2637 case 'b': remove |= MODE_BAN; break;
2aef5f4b 2638 case 'e': remove |= MODE_EXEMPT; break;
d76ed9a9 2639 case 'D': remove |= MODE_DELAYJOINS; break;
2640 case 'r': remove |= MODE_REGONLY; break;
850bf0cf 2641 case 'c': remove |= MODE_NOCOLORS; break;
d76ed9a9 2642 case 'C': remove |= MODE_NOCTCPS; break;
d0cb2fb6 2643 case 'S': remove |= MODE_STRIPCOLOR; break;
2644 case 'M': remove |= MODE_MODUNREG; break;
2645 case 'N': remove |= MODE_NONOTICE; break;
2646 case 'Q': remove |= MODE_NOQUITMSGS; break;
2647 case 'T': remove |= MODE_NOAMSG; break;
2648 case 'O': remove |= MODE_OPERSONLY; break;
ec1a68c8 2649 case 'z': remove |= MODE_REGISTERED; break;
c8ca69a0 2650 case 'Z': remove |= MODE_SSLONLY; break;
88c7cb10 2651 case 'L': remove |= MODE_HIDEMODE; break;
d76ed9a9 2652 }
2653 }
2654
2655 if (!remove)
2656 return 1;
2657
2658 /* Remove simple modes. */
2659 channel->modes &= ~remove;
2660
2661 /* If removing bans, kill 'em all. */
2662 if ((remove & MODE_BAN) && channel->banlist.used) {
2663 unsigned int i;
2664 for (i=0; i<channel->banlist.used; i++)
2665 free(channel->banlist.list[i]);
2666 channel->banlist.used = 0;
2667 }
2668
2aef5f4b 2669 /* If removing exempts, kill 'em all. */
2670 if ((remove & MODE_EXEMPT) && channel->exemptlist.used) {
2671 unsigned int i;
2672 for (i=0; i<channel->exemptlist.used; i++)
2673 free(channel->exemptlist.list[i]);
2674 channel->exemptlist.used = 0;
2675 }
2676
d76ed9a9 2677 /* Remove member modes. */
55342ce8 2678 if ((remove & (MODE_CHANOP | MODE_HALFOP | MODE_VOICE)) && channel->members.used) {
2679 int mask = ~(remove & (MODE_CHANOP | MODE_HALFOP | MODE_VOICE));
d76ed9a9 2680 unsigned int i;
2681
2682 for (i = 0; i < channel->members.used; i++)
2683 channel->members.list[i]->modes &= mask;
2684 }
2685
2686 return 1;
2687}
2688
2689void
2690reg_privmsg_func(struct userNode *user, privmsg_func_t handler)
2691{
2692 unsigned int numeric = user->num_local;
2693 if (numeric >= num_privmsg_funcs) {
2694 int newnum = numeric + 8;
2695 privmsg_funcs = realloc(privmsg_funcs, newnum*sizeof(privmsg_func_t));
2696 memset(privmsg_funcs+num_privmsg_funcs, 0, (newnum-num_privmsg_funcs)*sizeof(privmsg_func_t));
2697 num_privmsg_funcs = newnum;
2698 }
2699 if (privmsg_funcs[numeric])
2700 log_module(MAIN_LOG, LOG_WARNING, "re-registering new privmsg handler for numeric %d", numeric);
2701 privmsg_funcs[numeric] = handler;
2702}
2703
2704void
2705unreg_privmsg_func(struct userNode *user, privmsg_func_t handler)
2706{
2707 unsigned int x;
2708
2709 if (!user || handler)
2710 return; /* this really only works with users */
2711
2712 memset(privmsg_funcs+user->num_local, 0, sizeof(privmsg_func_t));
2713
2714 for (x = user->num_local+1; x < num_privmsg_funcs; x++)
2715 memmove(privmsg_funcs+x-1, privmsg_funcs+x, sizeof(privmsg_func_t));
2716
2717 privmsg_funcs = realloc(privmsg_funcs, num_privmsg_funcs*sizeof(privmsg_func_t));
2718 num_privmsg_funcs--;
2719}
2720
2721
2722void
2723reg_notice_func(struct userNode *user, privmsg_func_t handler)
2724{
2725 unsigned int numeric = user->num_local;
2726 if (numeric >= num_notice_funcs) {
2727 int newnum = numeric + 8;
2728 notice_funcs = realloc(notice_funcs, newnum*sizeof(privmsg_func_t));
2729 memset(notice_funcs+num_notice_funcs, 0, (newnum-num_notice_funcs)*sizeof(privmsg_func_t));
2730 num_notice_funcs = newnum;
2731 }
2732 if (notice_funcs[numeric])
2733 log_module(MAIN_LOG, LOG_WARNING, "re-registering new notice handler for numeric %d", numeric);
2734 notice_funcs[numeric] = handler;
2735}
2736
2737void
2738unreg_notice_func(struct userNode *user, privmsg_func_t handler)
2739{
2740 unsigned int x;
2741
2742 if (!user || handler)
2743 return; /* this really only works with users */
2744
2745 memset(notice_funcs+user->num_local, 0, sizeof(privmsg_func_t));
2746
2747 for (x = user->num_local+1; x < num_notice_funcs; x++)
2748 memmove(notice_funcs+x-1, notice_funcs+x, sizeof(privmsg_func_t));
2749
2750 memset(notice_funcs+user->num_local, 0, sizeof(privmsg_func_t));
2751 notice_funcs = realloc(notice_funcs, num_notice_funcs*sizeof(privmsg_func_t));
2752 num_notice_funcs--;
2753}
2754
2755void
2756reg_oper_func(oper_func_t handler)
2757{
2758 if (of_used == of_size) {
2759 if (of_size) {
2760 of_size <<= 1;
2761 of_list = realloc(of_list, of_size*sizeof(oper_func_t));
2762 } else {
2763 of_size = 8;
2764 of_list = malloc(of_size*sizeof(oper_func_t));
2765 }
2766 }
2767 of_list[of_used++] = handler;
2768}
2769
2770static void
2771call_oper_funcs(struct userNode *user)
2772{
2773 unsigned int n;
2774 if (IsLocal(user))
2775 return;
2776 for (n=0; n<of_used; n++)
2777 of_list[n](user);
2778}
2779
2780static void
2781send_burst(void)
2782{
2783 unsigned int i, hop, max_hop=1;
2784 dict_iterator_t it;
2785
2786 /* burst (juped) servers, closest first (except self, which is sent already) */
2787 for (i=0; i<ArrayLength(servers_num); i++)
2788 if (servers_num[i] && servers_num[i]->hops > max_hop)
2789 max_hop = servers_num[i]->hops;
2790 for (hop=1; hop<=max_hop; hop++) {
2791 for (i=0;i<ArrayLength(servers_num);i++) {
2792 if (servers_num[i]
2793 && (servers_num[i]->hops == hop)
2794 && (servers_num[i] != self->uplink))
2795 irc_server(servers_num[i]);
2796 }
2797 }
2798
2799 /* burst local nicks */
2800 for (i=0; i<=self->num_mask; i++)
2801 if (self->users[i])
2802 irc_user(self->users[i]);
2803
2804 /* build dict of unbursted channel names (just copy existing channels) */
2805 unbursted_channels = dict_new();
2806 for (it = dict_first(channels); it; it = iter_next(it))
2807 dict_insert(unbursted_channels, iter_key(it), iter_data(it));
2808}