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