]> jfr.im git - irc/evilnet/x3.git/blob - src/proto-p10.c
This is alpha code for LOC account hostmask validation
[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 * x3 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 3 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 "hash.h"
24 #include "helpfile.h"
25 #include "hosthiding.h"
26 #include "proto-common.c"
27 #include "opserv.h"
28
29 /* Full commands. */
30 #define CMD_ACCOUNT "ACCOUNT"
31 #define CMD_ADMIN "ADMIN"
32 #define CMD_ALIST "ALIST"
33 #define CMD_ASLL "ASLL"
34 #define CMD_AWAY "AWAY"
35 #define CMD_BURST "BURST"
36 #define CMD_CLEARMODE "CLEARMODE"
37 #define CMD_CLOSE "CLOSE"
38 #define CMD_CNOTICE "CNOTICE"
39 #define CMD_CONNECT "CONNECT"
40 #define CMD_CPRIVMSG "CPRIVMSG"
41 #define CMD_CREATE "CREATE"
42 #define CMD_DESTRUCT "DESTRUCT"
43 #define CMD_DESYNCH "DESYNCH"
44 #define CMD_DIE "DIE"
45 #define CMD_DNS "DNS"
46 #define CMD_EOB "END_OF_BURST"
47 #define CMD_EOB_ACK "EOB_ACK"
48 #define CMD_ERROR "ERROR"
49 #define CMD_FAKEHOST "FAKE"
50 #define CMD_GET "GET"
51 #define CMD_GLINE "GLINE"
52 #define CMD_HASH "HASH"
53 #define CMD_HELP "HELP"
54 #define CMD_INFO "INFO"
55 #define CMD_INVITE "INVITE"
56 #define CMD_ISON "ISON"
57 #define CMD_JOIN "JOIN"
58 #define CMD_JUPE "JUPE"
59 #define CMD_KICK "KICK"
60 #define CMD_KILL "KILL"
61 #define CMD_LINKS "LINKS"
62 #define CMD_LIST "LIST"
63 #define CMD_LUSERS "LUSERS"
64 #define CMD_MAP "MAP"
65 #define CMD_MARK "MARK"
66 #define CMD_MODE "MODE"
67 #define CMD_MOTD "MOTD"
68 #define CMD_NAMES "NAMES"
69 #define CMD_NICK "NICK"
70 #define CMD_NOTICE "NOTICE"
71 #define CMD_OPER "OPER"
72 #define CMD_OPMODE "OPMODE"
73 #define CMD_PART "PART"
74 #define CMD_PASS "PASS"
75 #define CMD_PING "PING"
76 #define CMD_PONG "PONG"
77 #define CMD_POST "POST"
78 #define CMD_PRIVMSG "PRIVMSG"
79 #define CMD_PRIVS "PRIVS"
80 #define CMD_PROTO "PROTO"
81 #define CMD_QUIT "QUIT"
82 #define CMD_REHASH "REHASH"
83 #define CMD_RESET "RESET"
84 #define CMD_RESTART "RESTART"
85 #define CMD_RPING "RPING"
86 #define CMD_RPONG "RPONG"
87 #define CMD_SERVER "SERVER"
88 #define CMD_SERVLIST "SERVLIST"
89 #define CMD_SERVSET "SERVSET"
90 #define CMD_SET "SET"
91 #define CMD_SETTIME "SETTIME"
92 #define CMD_SHUN "SHUN"
93 #define CMD_SILENCE "SILENCE"
94 #define CMD_SNO "SNO"
95 #define CMD_SQUERY "SQUERY"
96 #define CMD_SQUIT "SQUIT"
97 #define CMD_STATS "STATS"
98 #define CMD_SVSJOIN "SVSJOIN"
99 #define CMD_SVSNICK "SVSNICK"
100 #define CMD_SVSPART "SVSPART"
101 #define CMD_SWHOIS "SWHOIS"
102 #define CMD_TIME "TIME"
103 #define CMD_TOPIC "TOPIC"
104 #define CMD_TRACE "TRACE"
105 #define CMD_UPING "UPING"
106 #define CMD_USER "USER"
107 #define CMD_USERHOST "USERHOST"
108 #define CMD_USERIP "USERIP"
109 #define CMD_VERSION "VERSION"
110 #define CMD_WALLCHOPS "WALLCHOPS"
111 #define CMD_WALLOPS "WALLOPS"
112 #define CMD_WALLHOPS "WALLHOPS"
113 #define CMD_WALLUSERS "WALLUSERS"
114 #define CMD_WALLVOICES "WALLVOICES"
115 #define CMD_WALLHOPS "WALLHOPS"
116 #define CMD_WHO "WHO"
117 #define CMD_WHOIS "WHOIS"
118 #define CMD_WHOWAS "WHOWAS"
119
120 /* Tokenized commands. */
121 #define TOK_ACCOUNT "AC"
122 #define TOK_ADMIN "AD"
123 #define TOK_ALIST "AL"
124 #define TOK_ASLL "LL"
125 #define TOK_AWAY "A"
126 #define TOK_BURST "B"
127 #define TOK_CLEARMODE "CM"
128 #define TOK_CLOSE "CLOSE"
129 #define TOK_CNOTICE "CN"
130 #define TOK_CONNECT "CO"
131 #define TOK_CPRIVMSG "CP"
132 #define TOK_CREATE "C"
133 #define TOK_DESTRUCT "DE"
134 #define TOK_DESYNCH "DS"
135 #define TOK_DIE "DIE"
136 #define TOK_DNS "DNS"
137 #define TOK_EOB "EB"
138 #define TOK_EOB_ACK "EA"
139 #define TOK_ERROR "Y"
140 #define TOK_EXEMPT "EX"
141 #define TOK_FAKEHOST "FA"
142 #define TOK_GET "GET"
143 #define TOK_GLINE "GL"
144 #define TOK_HASH "HASH"
145 #define TOK_HELP "HELP"
146 #define TOK_INFO "F"
147 #define TOK_INVITE "I"
148 #define TOK_ISON "ISON"
149 #define TOK_JOIN "J"
150 #define TOK_JUPE "JU"
151 #define TOK_KICK "K"
152 #define TOK_KILL "D"
153 #define TOK_LINKS "LI"
154 #define TOK_LIST "LIST"
155 #define TOK_LUSERS "LU"
156 #define TOK_MAP "MAP"
157 #define TOK_MARK "MK"
158 #define TOK_MODE "M"
159 #define TOK_MOTD "MO"
160 #define TOK_NAMES "E"
161 #define TOK_NICK "N"
162 #define TOK_NOTICE "O"
163 #define TOK_OPER "OPER"
164 #define TOK_OPMODE "OM"
165 #define TOK_PART "L"
166 #define TOK_PASS "PA"
167 #define TOK_PING "G"
168 #define TOK_PONG "Z"
169 #define TOK_POST "POST"
170 #define TOK_PRIVMSG "P"
171 #define TOK_PRIVS "PRIVS"
172 #define TOK_PROTO "PROTO"
173 #define TOK_QUIT "Q"
174 #define TOK_REHASH "REHASH"
175 #define TOK_RESET "RESET"
176 #define TOK_RESTART "RESTART"
177 #define TOK_RPING "RI"
178 #define TOK_RPONG "RO"
179 #define TOK_SERVER "S"
180 #define TOK_SERVLIST "SERVSET"
181 #define TOK_SERVSET "SERVSET"
182 #define TOK_SET "SET"
183 #define TOK_SETTIME "SE"
184 #define TOK_SHUN "SU"
185 #define TOK_SILENCE "U"
186 #define TOK_SNO "SNO"
187 #define TOK_SQUERY "SQUERY"
188 #define TOK_SQUIT "SQ"
189 #define TOK_STATS "R"
190 #define TOK_SVSJOIN "SJ"
191 #define TOK_SVSNICK "SN"
192 #define TOK_SVSPART "SP"
193 #define TOK_SWHOIS "SW"
194 #define TOK_TIME "TI"
195 #define TOK_TOPIC "T"
196 #define TOK_TRACE "TR"
197 #define TOK_UPING "UP"
198 #define TOK_USER "USER"
199 #define TOK_USERHOST "USERHOST"
200 #define TOK_USERIP "USERIP"
201 #define TOK_VERSION "V"
202 #define TOK_WALLCHOPS "WC"
203 #define TOK_WALLOPS "WA"
204 #define TOK_WALLHOPS "WH"
205 #define TOK_WALLUSERS "WU"
206 #define TOK_WALLVOICES "WV"
207 #define TOK_WALLHOPS "WH"
208 #define TOK_WHO "H"
209 #define TOK_WHOIS "W"
210 #define TOK_WHOWAS "X"
211
212 /* Protocol messages; aliased to full commands or tokens depending
213 on compile-time configuration. ircu prefers tokens WITH THE
214 EXCEPTION OF THE SERVER AND PASS COMMANDS, which cannot be
215 tokenized, because clients' (ie. a linking server) commands are
216 only checked against the full command list.
217 */
218 #if defined(ENABLE_TOKENS)
219 #define TYPE(NAME) TOK_ ## NAME
220 #else /* !ENABLE_TOKENS */
221 #define TYPE(NAME) CMD_ ## NAME
222 #endif /* ENABLE_TOKENS */
223
224 #define P10_ACCOUNT TYPE(ACCOUNT)
225 #define P10_ADMIN TYPE(ADMIN)
226 #define P10_ASLL TYPE(ASLL)
227 #define P10_AWAY TYPE(AWAY)
228 #define P10_BURST TYPE(BURST)
229 #define P10_CLEARMODE TYPE(CLEARMODE)
230 #define P10_CLOSE TYPE(CLOSE)
231 #define P10_CNOTICE TYPE(CNOTICE)
232 #define P10_CONNECT TYPE(CONNECT)
233 #define P10_CPRIVMSG TYPE(CPRIVMSG)
234 #define P10_CREATE TYPE(CREATE)
235 #define P10_DESTRUCT TYPE(DESTRUCT)
236 #define P10_DESYNCH TYPE(DESYNCH)
237 #define P10_DIE TYPE(DIE)
238 #define P10_DNS TYPE(DNS)
239 #define P10_EOB TYPE(EOB)
240 #define P10_EOB_ACK TYPE(EOB_ACK)
241 #define P10_ERROR TYPE(ERROR)
242 #define P10_FAKEHOST TYPE(FAKEHOST)
243 #define P10_GET TYPE(GET)
244 #define P10_GLINE TYPE(GLINE)
245 #define P10_HASH TYPE(HASH)
246 #define P10_HELP TYPE(HELP)
247 #define P10_INFO TYPE(INFO)
248 #define P10_INVITE TYPE(INVITE)
249 #define P10_ISON TYPE(ISON)
250 #define P10_JOIN TYPE(JOIN)
251 #define P10_JUPE TYPE(JUPE)
252 #define P10_KICK TYPE(KICK)
253 #define P10_KILL TYPE(KILL)
254 #define P10_LINKS TYPE(LINKS)
255 #define P10_LIST TYPE(LIST)
256 #define P10_LUSERS TYPE(LUSERS)
257 #define P10_MAP TYPE(MAP)
258 #define P10_MARK TYPE(MARK)
259 #define P10_MODE TYPE(MODE)
260 #define P10_MOTD TYPE(MOTD)
261 #define P10_NAMES TYPE(NAMES)
262 #define P10_NICK TYPE(NICK)
263 #define P10_NOTICE TYPE(NOTICE)
264 #define P10_OPER TYPE(OPER)
265 #define P10_OPMODE TYPE(OPMODE)
266 #define P10_PART TYPE(PART)
267 #define P10_PASS CMD_PASS
268 #define P10_PING TYPE(PING)
269 #define P10_PONG TYPE(PONG)
270 #define P10_POST TYPE(POST)
271 #define P10_PRIVMSG TYPE(PRIVMSG)
272 #define P10_PRIVS TYPE(PRIVS)
273 #define P10_PROTO TYPE(PROTO)
274 #define P10_QUIT TYPE(QUIT)
275 #define P10_REHASH TYPE(REHASH)
276 #define P10_RESET TYPE(RESET)
277 #define P10_RESTART TYPE(RESTART)
278 #define P10_RPING TYPE(RPING)
279 #define P10_RPONG TYPE(RPONG)
280 #define P10_SERVER CMD_SERVER
281 #define P10_SERVLIST TYPE(SERVLIST)
282 #define P10_SERVSET TYPE(SERVSET)
283 #define P10_SET TYPE(SET)
284 #define P10_SETTIME TYPE(SETTIME)
285 #define P10_SHUN TYPE(SHUN)
286 #define P10_SILENCE TYPE(SILENCE)
287 #define P10_SNO TYPE(SNO)
288 #define P10_SQUERY TYPE(SQUERY)
289 #define P10_SQUIT TYPE(SQUIT)
290 #define P10_STATS TYPE(STATS)
291 #define P10_SVSJOIN TYPE(SVSJOIN)
292 #define P10_SVSNICK TYPE(SVSNICK)
293 #define P10_SVSPART TYPE(SVSPART)
294 #define P10_SWHOIS TYPE(SWHOIS)
295 #define P10_TIME TYPE(TIME)
296 #define P10_TOPIC TYPE(TOPIC)
297 #define P10_TRACE TYPE(TRACE)
298 #define P10_UPING TYPE(UPING)
299 #define P10_USER TYPE(USER)
300 #define P10_USERHOST TYPE(USERHOST)
301 #define P10_USERIP TYPE(USERIP)
302 #define P10_VERSION TYPE(VERSION)
303 #define P10_WALLCHOPS TYPE(WALLCHOPS)
304 #define P10_WALLOPS TYPE(WALLOPS)
305 #define P10_WALLHOPS TYPE(WALLHOPS)
306 #define P10_WALLUSERS TYPE(WALLUSERS)
307 #define P10_WALLVOICES TYPE(WALLVOICES)
308 #define P10_WHO TYPE(WHO)
309 #define P10_WHOIS TYPE(WHOIS)
310 #define P10_WHOWAS TYPE(WHOWAS)
311 #define P10_EXEMPT TYPE(EXEMPT)
312
313 /* Servers claiming to have a boot or link time before PREHISTORY
314 * trigger errors to the log. We hope no server has been running
315 * constantly since September 1994. :)
316 */
317 #define PREHISTORY 780000000
318
319 #define MODELEN 40 + KEYLEN
320
321 static struct server *servers_num[64*64];
322 static privmsg_func_t *privmsg_funcs;
323 static unsigned int num_privmsg_funcs;
324 static privmsg_func_t *notice_funcs;
325 static unsigned int num_notice_funcs;
326 static struct dict *unbursted_channels;
327 static char *his_servername;
328 static char *his_servercomment;
329 static int extended_accounts;
330
331 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);
332
333 extern int off_channel;
334 extern int DefConLevel;
335 extern int DefConTimeOut;
336 extern char *DefConChanModes;
337
338 static int parse_oplevel(char *str);
339
340 char privbuf[512] = "";
341
342 /* Numerics can be XYY, XYYY, or XXYYY; with X's identifying the
343 * server and Y's indentifying the client on that server. */
344 struct server*
345 GetServerN(const char *numeric)
346 {
347 switch (strlen(numeric)) {
348 default:
349 return servers_num[base64toint(numeric, 2)];
350 case 4:
351 case 3:
352 case 1:
353 return servers_num[base64toint(numeric, 1)];
354 case 0:
355 return NULL;
356 }
357 }
358
359 struct userNode*
360 GetUserN(const char *numeric) /* using numeric */
361 {
362 struct userNode *un;
363 struct server *s;
364 int n, slen, ulen;
365
366 switch (strlen(numeric)) {
367 default:
368 log_module(MAIN_LOG, LOG_WARNING, "GetUserN(%s): numeric too long!", numeric);
369 return NULL;
370 case 5: slen = 2; ulen = 3; break;
371 case 4: slen = 1; ulen = 3; break;
372 case 3: slen = 1; ulen = 2; break;
373 case 2: case 1: case 0:
374 log_module(MAIN_LOG, LOG_WARNING, "GetUserN(%s): numeric too short!", numeric);
375 return NULL;
376 }
377 if (!(s = servers_num[base64toint(numeric, slen)])) {
378 log_module(MAIN_LOG, LOG_WARNING, "GetUserN(%s): couldn't find server (len=%d)!", numeric, slen);
379 return NULL;
380 }
381 n = base64toint(numeric+slen, ulen) & s->num_mask;
382 if (!(un = s->users[n])) {
383 log_module(MAIN_LOG, LOG_WARNING, "GetUserN(%s) couldn't find user!", numeric);
384 }
385 return un;
386 }
387
388 extern struct userNode *opserv;
389 static void
390 check_ctcp(struct userNode *user, struct userNode *bot, char *text, UNUSED_ARG(int server_qualified))
391 {
392 char *cmd;
393 /* if its a version reply, do an alert check (only alerts with version=something) */
394 if(bot == opserv) {
395 if(text[0] == '\001') {
396 text++;
397 cmd = mysep(&text, " ");
398 if(!irccasecmp(cmd, "VERSION")) {
399 char *version = mysep(&text, "\n");
400 if(!version)
401 version = "";
402 /* opserv_debug("Opserv got CTCP VERSION Notice from %s: %s", user->nick, version); */
403 /* TODO: setup a ctcp_funcs thing to handle this and other CTCPS properly */
404 user->version_reply = strdup(version);
405 /* TODO: put this in the db */
406 if(match_ircglob(version, "WebTV;*"))
407 user->no_notice = true; /* webbies cant see notices */
408 }
409 }
410 }
411 }
412
413
414 static void
415 privmsg_user_helper(struct userNode *un, void *data)
416 {
417 struct privmsg_desc *pd = data;
418 unsigned int num = un->num_local;
419 if (!pd->is_notice) {
420 if ((num < num_privmsg_funcs) && privmsg_funcs[num]) {
421 privmsg_funcs[num](pd->user, un, pd->text, pd->is_qualified);
422 }
423 } else {
424 if ((num < num_notice_funcs) && notice_funcs[num]) {
425 check_ctcp(pd->user, un, pd->text, pd->is_qualified);
426 notice_funcs[num](pd->user, un, pd->text, pd->is_qualified);
427 }
428 }
429 }
430
431 /* equiv to user doing /connect server port target */
432 void irc_connect(struct userNode *user, char *server, unsigned int port, struct server *target)
433 {
434 putsock("%s " P10_CONNECT " %s %d %s", user->numeric, server, port, target->numeric);
435 }
436
437 void
438 irc_squit_route(struct server *srv, const char *message, ...)
439 {
440 va_list arg_list;
441 char buffer[MAXLEN];
442 va_start(arg_list, message);
443 vsnprintf(buffer, MAXLEN-2, message, arg_list);
444 buffer[MAXLEN-1] = 0;
445
446 /* When would we squit ourselves exactly?? -Rubin */
447 if(srv == self && cManager.uplink->state == CONNECTED ) {
448 unsigned int i;
449
450 /* Quit all clients linked to me. */
451 for(i = 0; i <= self->num_mask; i++) {
452 if(!self->users[i])
453 continue;
454 irc_quit(self->users[i], buffer);
455 }
456 }
457
458 putsock("%s " P10_SQUIT " %s %d :%s", self->numeric, srv->name, 0, buffer);
459
460 if(srv == self) {
461 /* Force a reconnect to the currently selected server. */
462 cManager.uplink->tries = 0;
463 log_module(MAIN_LOG, LOG_INFO, "Squitting from uplink: %s", buffer);
464 close_socket();
465 }
466 }
467
468 void
469 irc_server(struct server *srv)
470 {
471 char extranum[COMBO_NUMERIC_LEN+1];
472
473 inttobase64(extranum, srv->num_mask, (srv->numeric[1] || (srv->num_mask >= 64*64)) ? 3 : 2);
474 if (srv == self) {
475 putsock(P10_SERVER " %s %d %li %li J10 %s%s +s6 :%s",
476 srv->name, srv->hops+1, srv->boot, srv->link, srv->numeric, extranum, srv->description);
477 } else {
478 putsock("%s " P10_SERVER " %s %d %li %li %c10 %s%s +s6 :%s",
479 self->numeric, srv->name, srv->hops+1, srv->boot, srv->link, (srv->self_burst ? 'J' : 'P'), srv->numeric, extranum, srv->description);
480 }
481 }
482
483 static void
484 irc_p10_pton(irc_in_addr_t *ip, const char *input)
485 {
486 if (strlen(input) == 6) {
487 unsigned int value;
488 memset(ip, 0, 6 * sizeof(ip->in6[0]));
489 value = base64toint(input, 6);
490 if (value)
491 ip->in6[5] = htons(65535);
492 ip->in6[6] = htons(value >> 16);
493 ip->in6[7] = htons(value & 65535);
494 } else {
495 unsigned int pos = 0;
496 do {
497 if (*input == '_') {
498 unsigned int left;
499 for (left = (25 - strlen(input)) / 3; left; left--)
500 ip->in6[pos++] = 0;
501 input++;
502 } else {
503 ip->in6[pos++] = ntohs(base64toint(input, 3));
504 input += 3;
505 }
506 } while (pos < 8);
507 }
508 }
509
510 static void
511 irc_p10_ntop(char *output, const irc_in_addr_t *ip)
512 {
513 if (!irc_in_addr_is_valid(*ip)) {
514 strcpy(output, "AAAAAA");
515 } else if (irc_in_addr_is_ipv4(*ip)) {
516 unsigned int in4;
517 in4 = (ntohs(ip->in6[6]) << 16) | ntohs(ip->in6[7]);
518 inttobase64(output, in4, 6);
519 output[6] = '\0';
520 } else if (irc_in_addr_is_ipv6(*ip)) {
521 unsigned int max_start, max_zeros, curr_zeros, zero, ii;
522 /* Can start by printing out the leading non-zero parts. */
523 for (ii = 0; (ip->in6[ii]) && (ii < 8); ++ii) {
524 inttobase64(output, ntohs(ip->in6[ii]), 3);
525 output += 3;
526 }
527 /* Find the longest run of zeros. */
528 for (max_start = zero = ii, max_zeros = curr_zeros = 0; ii < 8; ++ii) {
529 if (!ip->in6[ii])
530 curr_zeros++;
531 else if (curr_zeros > max_zeros) {
532 max_start = ii - curr_zeros;
533 max_zeros = curr_zeros;
534 curr_zeros = 0;
535 }
536 }
537 if (curr_zeros > max_zeros) {
538 max_start = ii - curr_zeros;
539 max_zeros = curr_zeros;
540 curr_zeros = 0;
541 }
542 /* Print the rest of the address */
543 for (ii = zero; ii < 8; ) {
544 if ((ii == max_start) && max_zeros) {
545 *output++ = '_';
546 ii += max_zeros;
547 } else {
548 inttobase64(output, ntohs(ip->in6[ii]), 3);
549 output += 3;
550 }
551 }
552 *output = '\0';
553 } else {
554 strcpy(output, "???");
555 }
556 }
557
558 void
559 irc_user(struct userNode *user)
560 {
561 char b64ip[25];
562 if (!user)
563 return;
564 irc_p10_ntop(b64ip, &user->ip);
565 if (user->modes) {
566 int modelen;
567 char modes[32];
568
569 modelen = 0;
570 if (IsOper(user))
571 modes[modelen++] = 'o';
572 if (IsInvisible(user))
573 modes[modelen++] = 'i';
574 if (IsWallOp(user))
575 modes[modelen++] = 'w';
576 if (IsService(user))
577 modes[modelen++] = 'k';
578 if (IsServNotice(user))
579 modes[modelen++] = 's';
580 if (IsDeaf(user))
581 modes[modelen++] = 'd';
582 if (IsGlobal(user))
583 modes[modelen++] = 'g';
584 // sethost - reed/apples
585 // if (IsHelperIrcu(user))
586 if (IsSetHost(user))
587 modes[modelen++] = 'h';
588 if (IsFakeHost(user))
589 modes[modelen++] = 'f';
590 if (IsHiddenHost(user))
591 modes[modelen++] = 'x';
592 modes[modelen] = 0;
593
594 /* we don't need to put the + in modes because it's in the format string. */
595 putsock("%s " P10_NICK " %s %d %li %s %s +%s %s %s :%s",
596 user->uplink->numeric, user->nick, user->uplink->hops+1, user->timestamp, user->ident, user->hostname, modes, b64ip, user->numeric, user->info);
597 } else {
598 putsock("%s " P10_NICK " %s %d %li %s %s %s %s :%s",
599 user->uplink->numeric, user->nick, user->uplink->hops+1, user->timestamp, user->ident, user->hostname, b64ip, user->numeric, user->info);
600 }
601 }
602
603 void
604 irc_rename(struct userNode *user, const char *new_handle)
605 {
606 if(extended_accounts)
607 putsock("%s " P10_ACCOUNT " %s M %s", self->numeric, user->numeric, new_handle);
608 }
609
610 void
611 irc_delete(struct userNode *user)
612 {
613 if(extended_accounts)
614 putsock("%s " P10_ACCOUNT " %s U", self->numeric, user->numeric);
615 }
616
617 void
618 irc_account(struct userNode *user, const char *stamp, time_t timestamp)
619 {
620 if(extended_accounts)
621 putsock("%s " P10_ACCOUNT " %s R %s %lu", self->numeric, user->numeric, stamp, timestamp);
622 else
623 putsock("%s " P10_ACCOUNT " %s %s %lu", self->numeric, user->numeric, stamp, timestamp);
624 }
625
626 void
627 irc_fakehost(struct userNode *user, const char *host)
628 {
629 putsock("%s " P10_FAKEHOST " %s %s", self->numeric, user->numeric, host);
630 }
631
632 void
633 irc_regnick(UNUSED_ARG(struct userNode *user))
634 {
635 /* no operation here */
636 }
637
638 void
639 irc_nick(struct userNode *user, UNUSED_ARG(const char *old_nick))
640 {
641 putsock("%s " P10_NICK " %s "FMT_TIME_T, user->numeric, user->nick, now);
642 }
643
644 void
645 irc_fetchtopic(struct userNode *from, const char *to)
646 {
647 if (!from || !to)
648 return;
649 putsock("%s " P10_TOPIC " %s", from->numeric, to);
650 }
651
652 void
653 irc_squit(struct server *srv, const char *message, const char *service_message)
654 {
655 if (!service_message)
656 service_message = message;
657
658 /* Are we leaving the network? */
659 if (srv == self && cManager.uplink->state == CONNECTED) {
660 unsigned int i;
661
662 /* Quit all clients linked to me. */
663 for (i = 0; i <= self->num_mask; i++) {
664 if (!self->users[i])
665 continue;
666 irc_quit(self->users[i], service_message);
667 }
668 }
669
670 putsock("%s " P10_SQUIT " %s %d :%s", self->numeric, srv->name, 0, message);
671
672 if (srv == self) {
673 /* Force a reconnect to the currently selected server. */
674 cManager.uplink->tries = 0;
675 log_module(MAIN_LOG, LOG_INFO, "Squitting from uplink: %s", message);
676 close_socket();
677 }
678 }
679
680 void
681 irc_wallchops(struct userNode *from, const char *to, const char *message)
682 {
683 putsock("%s " P10_WALLCHOPS " %s :%s", from->numeric, to, message);
684 }
685
686 void
687 irc_wallops(const char *format, ...)
688 {
689 va_list arg_list;
690 char buffer[MAXLEN];
691 va_start(arg_list, format);
692 vsnprintf(buffer, MAXLEN-2, format, arg_list);
693 buffer[MAXLEN-1] = 0;
694 putsock("%s " P10_WALLOPS " :%s", self->numeric, buffer);
695 }
696
697
698 void
699 irc_notice(struct userNode *from, const char *to, const char *message)
700 {
701 putsock("%s " P10_NOTICE " %s :%s", from->numeric, to, message);
702 }
703
704 void
705 irc_notice_user(struct userNode *from, struct userNode *to, const char *message)
706 {
707 putsock("%s " P10_NOTICE " %s :%s", from->numeric, to->numeric, message);
708 }
709
710 void
711 irc_privmsg(struct userNode *from, const char *to, const char *message)
712 {
713 putsock("%s " P10_PRIVMSG " %s :%s", from->numeric, to, message);
714 }
715
716 void
717 irc_privmsg_user(struct userNode *from, struct userNode *to, const char *message)
718 {
719 putsock("%s " P10_PRIVMSG " %s :%s", from->numeric, to->numeric, message);
720 }
721
722 void
723 irc_version_user(struct userNode *from, struct userNode *to)
724 {
725 irc_privmsg_user(from, to, "\001VERSION\001");
726 }
727
728 void
729 irc_eob(void)
730 {
731 putsock("%s " P10_EOB, self->numeric);
732 }
733
734 void
735 irc_eob_ack(void)
736 {
737 putsock("%s " P10_EOB_ACK, self->numeric);
738
739 char *nick;
740 const char *str;
741 str = conf_get_data("services/opserv/nick", RECDB_QSTRING);
742 nick = strdup(str);
743
744 if (nick && (DefConLevel < 5)) {
745 DefConProcess(GetUserH(nick));
746
747 if (DefConTimeOut > 0)
748 timeq_add(now + DefConTimeOut, defcon_timeout, NULL);
749 }
750 }
751
752 void
753 irc_rpong(const char *from1, const char *from2, const char *pingtime, const char *clientinfo)
754 {
755 putsock("%s " P10_RPONG " %s %s %s :%s", self->numeric, from1, from2, pingtime, clientinfo);
756 }
757
758 void
759 irc_ping(const char *payload)
760 {
761 putsock("%s " P10_PING " :%s", self->numeric, payload);
762 }
763
764 void
765 irc_pong(const char *who, const char *data)
766 {
767 putsock("%s " P10_PONG " %s :%s", self->numeric, who, data);
768 }
769
770 void
771 irc_pong_asll(const char *who, const char *orig_ts)
772 {
773 char *delim;
774 struct timeval orig;
775 struct timeval now;
776 int diff;
777
778 orig.tv_sec = strtoul(orig_ts, &delim, 10);
779 orig.tv_usec = (*delim == '.') ? strtoul(delim + 1, NULL, 10) : 0;
780 gettimeofday(&now, NULL);
781 diff = (now.tv_sec - orig.tv_sec) * 1000 + (now.tv_usec - orig.tv_usec) / 1000;
782 putsock("%s " P10_PONG " %s %s %d " FMT_TIME_T ".%06u", self->numeric, who, orig_ts, diff, now.tv_sec, (unsigned)now.tv_usec);
783 }
784
785 void
786 irc_pass(const char *passwd)
787 {
788 putsock(P10_PASS " :%s", passwd);
789 }
790
791 void
792 irc_introduce(const char *passwd)
793 {
794 void timed_send_ping(void *data);
795
796 self->self_burst = self->burst = 1;
797 irc_pass(passwd);
798 irc_server(self);
799 burst_length = 0;
800 timeq_add(now + ping_freq, timed_send_ping, 0);
801 }
802
803 void
804 irc_gline(struct server *srv, struct gline *gline, int silent)
805 {
806 putsock("%s " P10_GLINE " %s +%s %ld :%s<%s> %s",
807 self->numeric, (srv ? srv->numeric : "*"), gline->target, gline->expires-now, silent ? "AUTO " : "", gline->issuer, gline->reason);
808 }
809
810 void
811 irc_shun(struct server *srv, struct shun *shun)
812 {
813 putsock("%s " P10_SHUN " %s +%s %ld :<%s> %s",
814 self->numeric, (srv ? srv->numeric : "*"), shun->target, shun->expires-now, shun->issuer, shun->reason);
815 }
816
817 void
818 irc_settime(const char *srv_name_mask, time_t new_time)
819 {
820 ioset_set_time(new_time);
821 if (!strcmp(srv_name_mask, "*"))
822 srv_name_mask = "";
823 putsock("%s " P10_SETTIME " " FMT_TIME_T " %s", self->numeric, new_time, srv_name_mask);
824 }
825
826 void
827 irc_ungline(const char *mask)
828 {
829 putsock("%s " P10_GLINE " * -%s", self->numeric, mask);
830 }
831
832 void
833 irc_unshun(const char *mask)
834 {
835 putsock("%s " P10_SHUN " * -%s", self->numeric, mask);
836 }
837
838 static void
839 irc_burst(struct chanNode *chan)
840 {
841 char burst_line[512];
842 int pos, base_len, len;
843 struct modeNode *mn;
844 struct banNode *bn;
845 struct exemptNode *en;
846 long last_mode=-1;
847 unsigned int n;
848
849 base_len = sprintf(burst_line, "%s " P10_BURST " %s " FMT_TIME_T " ",
850 self->numeric, chan->name, chan->timestamp);
851 len = irc_make_chanmode(chan, burst_line+base_len);
852 pos = base_len + len;
853 if (len)
854 burst_line[pos++] = ' ';
855
856 if(chan->members.used < 1)
857 return; /* dont burst empty channels (created by discrims) */
858 /* dump the users */
859 for (n=0; n<chan->members.used; n++) {
860 mn = chan->members.list[n];
861 if (pos > 500) {
862 burst_line[pos-1] = 0; /* -1 to back up over the space or comma */
863 putsock("%s", burst_line);
864 pos = base_len;
865 last_mode = -1;
866 }
867
868 memcpy(burst_line+pos, mn->user->numeric, strlen(mn->user->numeric));
869 pos += strlen(mn->user->numeric);
870 if (mn->modes && (mn->modes != last_mode)) {
871 last_mode = mn->modes;
872 burst_line[pos++] = ':';
873 if (last_mode & MODE_CHANOP)
874 burst_line[pos++] = 'o';
875 if (last_mode & MODE_HALFOP)
876 burst_line[pos++] = 'h';
877 if (last_mode & MODE_VOICE)
878 burst_line[pos++] = 'v';
879 }
880 if ((n+1)<chan->members.used)
881 burst_line[pos++] = ',';
882 }
883 if (chan->banlist.used) {
884 /* dump the bans */
885 if (pos+2+strlen(chan->banlist.list[0]->ban) > 505) {
886 burst_line[pos-1] = 0;
887 putsock("%s", burst_line);
888 pos = base_len;
889 } else {
890 burst_line[pos++] = ' ';
891 }
892
893 burst_line[pos++] = ':';
894 burst_line[pos++] = '%';
895 base_len = pos;
896 for (n=0; n<chan->banlist.used; n++) {
897 bn = chan->banlist.list[n];
898 len = strlen(bn->ban);
899 if (pos+len+1 > 510) {
900 burst_line[pos-1] = 0; /* -1 to back up over the space or comma */
901 putsock("%s", burst_line);
902 pos = base_len;
903 }
904 memcpy(burst_line+pos, bn->ban, len);
905 pos += len;
906 burst_line[pos++] = ' ';
907 }
908 }
909 if (chan->exemptlist.used) {
910 /* dump the exempt */
911 if (pos+2+strlen(chan->exemptlist.list[0]->exempt) > 505) {
912 burst_line[pos-1] = 0;
913 putsock("%s", burst_line);
914 pos = base_len;
915 } else {
916 burst_line[pos++] = ' ';
917 }
918
919 burst_line[pos++] = ' ';
920 burst_line[pos++] = '~';
921 burst_line[pos++] = ' ';
922 base_len = pos;
923 for (n=0; n<chan->exemptlist.used; n++) {
924 en = chan->exemptlist.list[n];
925 len = strlen(en->exempt);
926 if (pos+len+1 > 510) {
927 burst_line[pos-1] = 0; /* -1 to back up over the space or comma */
928 putsock("%s", burst_line);
929 pos = base_len;
930 }
931 memcpy(burst_line+pos, en->exempt, len);
932 pos += len;
933 burst_line[pos++] = ' ';
934 }
935 }
936 /* print the last line */
937 burst_line[pos] = 0;
938 putsock("%s", burst_line);
939 }
940
941 void
942 irc_quit(struct userNode *user, const char *message)
943 {
944 putsock("%s " P10_QUIT " :%s", user->numeric, message);
945 }
946
947 void
948 irc_error(const char *to, const char *message)
949 {
950 if (to) {
951 putsock("%s " P10_ERROR " :%s", to, message);
952 } else {
953 putsock(":%s " P10_ERROR " :%s", self->name, message);
954 }
955 }
956
957 void
958 irc_kill(struct userNode *from, struct userNode *target, const char *message)
959 {
960 if (from) {
961 putsock("%s " P10_KILL " %s :%s!%s (%s)",
962 from->numeric, target->numeric, self->name, from->nick, message);
963 } else {
964 putsock("%s " P10_KILL " %s :%s (%s)",
965 self->numeric, target->numeric, self->name, message);
966 }
967 }
968
969 void
970 irc_mode(struct userNode *from, struct chanNode *target, const char *modes)
971 {
972 call_channel_mode_funcs(from, target, (char **)&modes, 0);
973 putsock("%s " P10_MODE " %s %s "FMT_TIME_T,
974 (from ? from->numeric : self->numeric),
975 target->name, modes, target->timestamp);
976 }
977
978 /* Added to allow services to mode users
979 2005 - 8 - 10 by Life4Christ
980 */
981 void
982 irc_umode(struct userNode *target, const char *modes)
983 {
984 putsock("%s " P10_MODE " %s %s ",self->numeric,target->nick, modes);
985 }
986
987
988 void
989 irc_invite(struct userNode *from, struct userNode *who, struct chanNode *to)
990 {
991 putsock("%s " P10_INVITE " %s %s", from->numeric, who->nick, to->name);
992 }
993
994 void
995 irc_silence(struct userNode *who, const char *mask, int add)
996 {
997 putsock("%s " P10_SILENCE " %s %s%s", self->numeric, who->numeric, add ? "+" : "-", mask);
998 }
999
1000 void
1001 irc_join(struct userNode *who, struct chanNode *what)
1002 {
1003 if (what->members.used == 1) {
1004 putsock("%s " P10_CREATE " %s %lu",
1005 who->numeric, what->name, what->timestamp);
1006 } else {
1007 putsock("%s " P10_JOIN " %s %lu", who->numeric, what->name, what->timestamp);
1008 }
1009 }
1010
1011 void
1012 irc_svsjoin(struct userNode *from, struct userNode *who, struct chanNode *to)
1013 {
1014 putsock("%s " P10_SVSJOIN " %s %s "FMT_TIME_T, from->uplink->numeric, who->numeric, to->name, now);
1015 }
1016
1017 void
1018 irc_svspart(struct userNode *from, struct userNode *who, struct chanNode *to)
1019 {
1020 putsock("%s " P10_SVSPART " %s %s", from->uplink->numeric, who->numeric, to->name);
1021 }
1022
1023 void
1024 irc_kick(struct userNode *who, struct userNode *target, struct chanNode *channel, const char *msg)
1025 {
1026 const char *numeric;
1027 struct modeNode *mn = GetUserMode(channel, who);
1028 numeric = ((mn && (mn->modes & MODE_CHANOP)) || off_channel) ? who->numeric : self->numeric;
1029 putsock("%s " P10_KICK " %s %s :%s",
1030 numeric, channel->name, target->numeric, msg);
1031 }
1032
1033 void
1034 irc_stats(struct userNode *from, struct server *target, char type)
1035 {
1036 putsock("%s " P10_STATS " %c :%s", from->numeric, type, target->numeric);
1037 }
1038
1039 void
1040 irc_svsnick(struct userNode *from, struct userNode *target, const char *newnick)
1041 {
1042 putsock("%s " P10_SVSNICK " %s %s "FMT_TIME_T, from->uplink->numeric, target->numeric, newnick, now);
1043 }
1044
1045 void
1046 irc_swhois(struct userNode *from, struct userNode *target, const char *message)
1047 {
1048 putsock("%s " P10_SWHOIS " %s %s%s", from->uplink->numeric, target->numeric, message ? ":" : "",
1049 message ? message : "");
1050
1051 }
1052
1053 void
1054 irc_part(struct userNode *who, struct chanNode *what, const char *reason)
1055 {
1056 if (reason) {
1057 putsock("%s " P10_PART " %s :%s", who->numeric, what->name, reason);
1058 } else {
1059 putsock("%s " P10_PART " %s", who->numeric, what->name);
1060 }
1061 }
1062
1063 void
1064 irc_topic(struct userNode *service, struct userNode *who, struct chanNode *what, const char *topic)
1065 {
1066
1067 int type = 4;
1068 int host_in_topic = 0;
1069 const char *hstr, *tstr;
1070 char *host, *hostmask;
1071 char shost[MAXLEN];
1072 char sident[MAXLEN];
1073
1074 tstr = conf_get_data("server/type", RECDB_QSTRING);
1075 hstr = conf_get_data("server/host_in_topic", RECDB_QSTRING);
1076 if(tstr)
1077 type = atoi(tstr);
1078 else
1079 type = 4;/* default to 040 style topics */
1080
1081 if (hstr) {
1082 if (IsFakeHost(who))
1083 safestrncpy(shost, who->fakehost, sizeof(shost));
1084 else if (IsSetHost(who)) {
1085 hostmask = strdup(who->sethost);
1086 if ((host = (strrchr(hostmask, '@'))))
1087 *host++ = '\0';
1088 else
1089 host = hostmask;
1090
1091 safestrncpy(sident, hostmask, sizeof(shost));
1092 safestrncpy(shost, host, sizeof(shost));
1093 } else
1094 safestrncpy(shost, who->hostname, sizeof(shost));
1095
1096 host_in_topic = atoi(hstr);
1097 }
1098
1099 if (type >= 5) {
1100 putsock("%s " P10_TOPIC " %s %s%s%s%s%s " FMT_TIME_T " " FMT_TIME_T " :%s", service->numeric, what->name,
1101 who->nick, host_in_topic ? "!" : "", host_in_topic ? (IsSetHost(who) ? sident : who->ident) : "",
1102 host_in_topic ? "@" : "", host_in_topic ? shost : "", what->timestamp, now, topic);
1103 } else {
1104 who = service;
1105 putsock("%s " P10_TOPIC " %s :%s", who->numeric, what->name, topic);
1106 }
1107 }
1108
1109 void
1110 irc_raw(const char *what)
1111 {
1112 putsock("%s", what);
1113 }
1114
1115 void
1116 irc_numeric(struct userNode *user, unsigned int num, const char *format, ...)
1117 {
1118 va_list arg_list;
1119 char buffer[MAXLEN];
1120 va_start(arg_list, format);
1121 vsnprintf(buffer, MAXLEN-2, format, arg_list);
1122 buffer[MAXLEN-1] = 0;
1123 putsock(":%s %03d %s %s", self->name, num, user->nick, buffer);
1124 }
1125
1126 void
1127 irc_mark(struct userNode *user, char *mark)
1128 {
1129 char *host = user->hostname;
1130
1131 /* TODO: Allow mark overwrite. If they are marked, and their fakehost is oldmark.hostname, update it to newmark.hostname so mark can be called multiple times. Probably requires ircd modification also */
1132 if(user->mark)
1133 return;
1134
1135 /* if the mark will put us over the host length, clip some off the left hand side
1136 * to make room...
1137 */
1138 if(strlen(host) + 1 + strlen(mark) > HOSTLEN)
1139 host += 1 + ( (strlen(host) + 1 + strlen(mark)) - HOSTLEN );
1140 putsock("%s " CMD_MARK " %s DNSBL +m %s.%s", self->numeric, user->nick, mark, host);
1141 putsock("%s " CMD_MARK " %s DNSBL_DATA %s", self->numeric, user->nick, mark);
1142
1143 /* Save it in the user */
1144 user->mark = strdup(mark);
1145
1146 /* If they are not otherwise marked, mark their host with fakehost */
1147 if(!IsFakeHost(user) && !IsSetHost(user) && !(IsHiddenHost(user) && user->handle_info) )
1148 {
1149 struct modeNode *mn = NULL;
1150 char fakehost[HOSTLEN];
1151 unsigned int count = 0;
1152 unsigned int n = 0;
1153
1154 putsock("%s " CMD_FAKEHOST " %s %s.%s", self->numeric, user->numeric, mark, host);
1155 putsock("%s " CMD_MODE " %s +x", self->numeric, user->nick);
1156
1157 snprintf(fakehost, sizeof(fakehost), "%s.%s", mark, host);
1158 safestrncpy(user->fakehost, fakehost, sizeof(user->fakehost));
1159
1160 for (n=count=0; n<user->channels.used; n++) {
1161 mn = user->channels.list[n];
1162 if (strlen(mn->channel->name) >= 1) /* Sanity */
1163 check_bans(user, mn->channel->name);
1164 }
1165 }
1166 }
1167
1168 static void send_burst(void);
1169
1170 static void
1171 change_nicklen(int new_nicklen)
1172 {
1173 unsigned int nn;
1174 char new_nick[NICKLEN+1];
1175 struct userNode *user;
1176
1177 nicklen = new_nicklen;
1178 /* fix up any users we have here */
1179 for (nn=0; nn<=self->num_mask; nn++) {
1180 if (!(user = self->users[nn]))
1181 continue;
1182 safestrncpy(new_nick, user->nick, sizeof(new_nick));
1183 new_nick[nicklen] = 0;
1184 NickChange(user, new_nick, 1);
1185 }
1186 }
1187
1188 static CMD_FUNC(cmd_whois)
1189 {
1190 struct userNode *from;
1191 struct userNode *who;
1192
1193 if (argc < 3)
1194 return 0;
1195 if (!(from = GetUserH(origin))) {
1196 log_module(MAIN_LOG, LOG_ERROR, "Could not find WHOIS origin user %s", origin);
1197 return 0;
1198 }
1199 if(!(who = GetUserH(argv[2]))) {
1200 irc_numeric(from, ERR_NOSUCHNICK, "%s@%s :No such nick", argv[2], self->name);
1201 return 1;
1202 }
1203 if (IsHiddenHost(who) && !IsOper(from)) {
1204 /* Just stay quiet. */
1205 return 1;
1206 }
1207 irc_numeric(from, RPL_WHOISUSER, "%s %s %s * :%s", who->nick, who->ident, who->hostname, who->info);
1208 if (his_servername && his_servercomment)
1209 irc_numeric(from, RPL_WHOISSERVER, "%s %s :%s", who->nick, his_servername, his_servercomment);
1210 else
1211 irc_numeric(from, RPL_WHOISSERVER, "%s %s :%s", who->nick, who->uplink->name, who->uplink->description);
1212 if (IsOper(who))
1213 irc_numeric(from, RPL_WHOISOPERATOR, "%s :is a megalomaniacal power hungry tyrant", who->nick);
1214 irc_numeric(from, RPL_ENDOFWHOIS, "%s :End of /WHOIS list", who->nick);
1215 return 1;
1216 }
1217
1218 static CMD_FUNC(cmd_server)
1219 {
1220 struct server *srv;
1221 const char *str;
1222
1223 if (argc < 8)
1224 return 0;
1225 if (self->uplink) {
1226 /* another server introduced us */
1227 srv = AddServer(GetServerH(origin), argv[1], atoi(argv[2]), atoi(argv[3]), atoi(argv[4]), argv[6], argv[argc-1]);
1228 if (!srv)
1229 return 0;
1230 srv->self_burst = argv[5][0] == 'J';
1231 srv->burst = 1;
1232 } else {
1233 /* this must be our uplink */
1234 srv = self->uplink = AddServer(self, argv[1], atoi(argv[2]), atoi(argv[3]), atoi(argv[4]), argv[6], argv[argc-1]);
1235 if (!srv)
1236 return 0;
1237 srv->self_burst = argv[5][0] == 'J';
1238 srv->burst = 1;
1239 if ((argv[7][0] == '+') && !force_n2k) {
1240 log_module(MAIN_LOG, LOG_WARNING, "Got Undernet-style SERVER message but \"force_n2k\" not on.");
1241 }
1242 send_burst();
1243 }
1244
1245 /* Fix up our timestamps if necessary. */
1246 if (srv->boot <= PREHISTORY) {
1247 /* Server from the mists of time.. */
1248 if (srv->hops == 1) {
1249 log_module(MAIN_LOG, LOG_ERROR, "Server %s claims to have booted at time "FMT_TIME_T". This is absurd.", srv->name, srv->boot);
1250 }
1251 } else if ((str = conf_get_data("server/reliable_clock", RECDB_QSTRING))
1252 && enabled_string(str)) {
1253 /* If we have a reliable clock, we just keep our current time. */
1254 } else {
1255 if (srv->boot <= self->boot) {
1256 /* The other server is older than us. Accept their timestamp.
1257 * Alternately, we are same age, but we accept their time
1258 * since we are linking to them. */
1259 self->boot = srv->boot;
1260 ioset_set_time(srv->link);
1261 }
1262 }
1263 if (srv == self->uplink) {
1264 extern time_t burst_begin;
1265 burst_begin = now;
1266 }
1267 return 1;
1268 }
1269
1270
1271 static CMD_FUNC(cmd_eob)
1272 {
1273 struct server *sender;
1274 dict_iterator_t it;
1275 unsigned int ii;
1276
1277 if (!(sender = GetServerH(origin)))
1278 return 0;
1279 if (sender == self->uplink) {
1280 cManager.uplink->state = CONNECTED;
1281 for (it = dict_first(unbursted_channels); it; it = iter_next(it))
1282 irc_burst(iter_data(it));
1283 dict_delete(unbursted_channels);
1284 unbursted_channels = NULL;
1285 irc_eob();
1286 irc_eob_ack();
1287
1288 /* now that we know who our uplink is,
1289 * we can center the routing map and activate auto-routing.
1290 */
1291 //activate_routing(NULL, NULL, NULL);
1292 routing_init();
1293 }
1294 sender->self_burst = 0;
1295 recalc_bursts(sender);
1296 for (ii=0; ii<slf_used; ii++)
1297 slf_list[ii](sender);
1298 /* let auto-routing figure out if we were
1299 * wating on this server to link a child to it */
1300 /* DONT call this if uplink is _US_ */
1301 if(sender->uplink != self)
1302 routing_handle_connect(sender->name, sender->uplink->name);
1303 return 1;
1304 }
1305
1306 static CMD_FUNC(cmd_eob_ack)
1307 {
1308 extern time_t burst_begin;
1309
1310 if (GetServerH(origin) == self->uplink) {
1311 burst_length = now - burst_begin;
1312 self->self_burst = self->burst = 0;
1313 }
1314 cManager.uplink->state = CONNECTED;
1315 return 1;
1316 }
1317
1318 static CMD_FUNC(cmd_rping)
1319 {
1320 struct server *dest;
1321
1322 if (!(dest = GetServerN(argv[1])))
1323 return 0;
1324
1325 if (dest == self)
1326 irc_rpong(argv[2], argv[3], argv[4], argv[5]);
1327
1328 return 1;
1329 }
1330
1331 static CMD_FUNC(cmd_ping)
1332 {
1333 struct server *srv;
1334 struct userNode *un;
1335
1336 if (argc > 3)
1337 irc_pong_asll(argv[2], argv[3]);
1338 else if ((srv = GetServerH(origin)))
1339 irc_pong(self->name, srv->numeric);
1340 else if ((un = GetUserH(origin)))
1341 irc_pong(self->name, un->numeric);
1342 else
1343 irc_pong(self->name, origin);
1344
1345 timeq_del(0, timed_send_ping, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
1346 timeq_del(0, timed_ping_timeout, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
1347 timeq_add(now + ping_freq, timed_send_ping, 0);
1348 received_ping();
1349 return 1;
1350 }
1351
1352 static CMD_FUNC(cmd_error_nick)
1353 {
1354 /* Go back to original IRC length .. and try to reconnect :/ */
1355 change_nicklen(9);
1356 irc_squit(self, "Got erroneous nickname, truncating nicks.", NULL);
1357 return 1;
1358 }
1359
1360 struct create_desc {
1361 struct userNode *user;
1362 time_t when;
1363 };
1364
1365 static void
1366 join_helper(struct chanNode *chan, void *data)
1367 {
1368 struct create_desc *cd = data;
1369 AddChannelUser(cd->user, chan);
1370 }
1371
1372 static void
1373 create_helper(char *name, void *data)
1374 {
1375 struct create_desc *cd = data;
1376
1377 if (!strcmp(name, "0")) {
1378 while (cd->user->channels.used > 0)
1379 DelChannelUser(cd->user, cd->user->channels.list[0]->channel, 0, 0);
1380 return;
1381 }
1382
1383 AddChannelUser(cd->user, AddChannel(name, cd->when, NULL, NULL, NULL));
1384 }
1385
1386 static CMD_FUNC(cmd_create)
1387 {
1388 struct create_desc cd;
1389 struct userNode *user;
1390
1391 if ((argc < 3) || !(user = GetUserH(origin)))
1392 return 0;
1393 cd.user = user;
1394 cd.when = atoi(argv[2]);
1395 parse_foreach(argv[1], join_helper, create_helper, NULL, NULL, &cd);
1396 return 1;
1397 }
1398
1399 static CMD_FUNC(cmd_join)
1400 {
1401 struct create_desc cd;
1402
1403 if (!(cd.user = GetUserH(origin)))
1404 return 0;
1405 if (argc < 2)
1406 return 0;
1407 else if (argc < 3)
1408 cd.when = now;
1409 else
1410 cd.when = atoi(argv[2]);
1411 parse_foreach(argv[1], join_helper, create_helper, NULL, NULL, &cd);
1412 return 1;
1413 }
1414
1415 static CMD_FUNC(cmd_svsjoin)
1416 {
1417 struct create_desc cd;
1418
1419 if (!(cd.user = GetUserH(argv[1])))
1420 return 0;
1421 if (argc < 3)
1422 return 0;
1423 else if (argc < 4)
1424 cd.when = now;
1425 else
1426 cd.when = atoi(argv[3]);
1427 parse_foreach(argv[2], join_helper, create_helper, NULL, NULL, &cd);
1428 return 1;
1429 }
1430
1431 static CMD_FUNC(cmd_pong)
1432 {
1433 if (argc < 3)
1434 return 0;
1435 if (!strcmp(argv[2], self->name)) {
1436 timeq_del(0, timed_send_ping, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
1437 timeq_del(0, timed_ping_timeout, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
1438 timeq_add(now + ping_freq, timed_send_ping, 0);
1439 received_ping();
1440 }
1441 return 1;
1442 }
1443
1444 static CMD_FUNC(cmd_nick)
1445 {
1446 struct userNode *user;
1447 if ((user = GetUserH(origin))) {
1448 /* nick change (since the source is a user numeric) */
1449 if (argc < 2)
1450 return 0;
1451 NickChange(user, argv[1], 1);
1452 } else {
1453 struct server *serv;
1454 struct userNode *nuser;
1455 char modes[MAXLEN];
1456 /* new nick */
1457 if (argc < 9)
1458 return 0;
1459 serv = GetServerH(origin);
1460 if (argc > 9)
1461 unsplit_string(argv+6, argc-9, modes);
1462 else
1463 strcpy(modes, "+");
1464 nuser = AddUser(serv, argv[1], argv[4], argv[5], modes, argv[argc-2], argv[argc-1], atoi(argv[3]), argv[argc-3]);
1465 }
1466 return 1;
1467 }
1468
1469 static CMD_FUNC(cmd_account)
1470 {
1471 struct userNode *user;
1472 struct server *server;
1473 struct handle_info *hi;
1474
1475 if ((argc < 3) || !origin || !(server = GetServerH(origin)))
1476 return 0; /* Origin must be server. */
1477
1478 /* This next line appears to tremple origin.. why? */
1479 user = GetUserN(argv[1]);
1480 if (!user)
1481 return 1; /* A QUIT probably passed the ACCOUNT. */
1482
1483 if(!extended_accounts) /* any need for this function without? */
1484 return 1;
1485
1486 if(!strcmp(argv[2],"C"))
1487 {
1488 if((hi = loc_auth(argv[4], argv[5], NULL)))
1489 {
1490 /* Return a AC A */
1491 putsock("%s " P10_ACCOUNT " %s A %s %lu", self->numeric, server->numeric , argv[3], hi->registered);
1492
1493 }
1494 else
1495 {
1496 /* Return a AC D */
1497 putsock("%s " P10_ACCOUNT " %s D %s", self->numeric, server->numeric , argv[3]);
1498 }
1499 return 1;
1500 }
1501 else if(!strcmp(argv[2],"H")) /* New enhanced (host) version of C */
1502 {
1503 if((hi = loc_auth(argv[5], argv[6], argv[4] )))
1504 {
1505 /* Return a AC A */
1506 putsock("%s " P10_ACCOUNT " %s A %s %lu", self->numeric, server->numeric , argv[3], hi->registered);
1507
1508 }
1509 else
1510 {
1511 /* Return a AC D */
1512 putsock("%s " P10_ACCOUNT " %s D %s", self->numeric, server->numeric , argv[3]);
1513 }
1514 return 1;
1515 }
1516 else if(!strcmp(argv[2],"R"))
1517 call_account_func(user, argv[3]);
1518 else
1519 call_account_func(user, argv[2]); /* For backward compatability */
1520 return 1;
1521 }
1522
1523 static CMD_FUNC(cmd_fakehost)
1524 {
1525 struct userNode *user;
1526
1527 if ((argc < 3) || !origin || !GetServerH(origin))
1528 return 0;
1529 if (!(user = GetUserN(argv[1])))
1530 return 1;
1531 assign_fakehost(user, argv[2], 0);
1532 return 1;
1533 }
1534
1535 static struct {
1536 char *name;
1537 unsigned int priv;
1538 } privtab[] = {
1539 #define P(priv) { #priv, PRIV_ ## priv }
1540 P(CHAN_LIMIT), P(MODE_LCHAN), P(WALK_LCHAN), P(DEOP_LCHAN),
1541 P(SHOW_INVIS), P(SHOW_ALL_INVIS), P(UNLIMIT_QUERY), P(KILL),
1542 P(LOCAL_KILL), P(REHASH), P(RESTART), P(DIE),
1543 P(GLINE), P(LOCAL_GLINE), P(JUPE), P(LOCAL_JUPE),
1544 P(OPMODE), P(LOCAL_OPMODE), P(SET), P(WHOX),
1545 P(BADCHAN), P(LOCAL_BADCHAN), P(SEE_CHAN), P(PROPAGATE),
1546 P(DISPLAY), P(SEE_OPERS), P(WIDE_GLINE), P(FORCE_OPMODE),
1547 P(FORCE_LOCAL_OPMODE), P(REMOTEREHASH), P(CHECK), P(SEE_SECRET_CHAN),
1548 P(SHUN), P(LOCAL_SHUN), P(WIDE_SHUN),
1549 #undef P
1550 { 0, 0 }
1551 };
1552
1553 char *client_report_privs(struct userNode *client)
1554 {
1555 int i;
1556
1557 privbuf[0] = '\0';
1558 for (i = 0; privtab[i].name; i++) {
1559 if (HasPriv(client, privtab[i].priv)) {
1560 strcat(privbuf, privtab[i].name);
1561 strcat(privbuf, " ");
1562 }
1563 }
1564
1565 privbuf[strlen(privbuf)] = 0;
1566
1567 return privbuf;
1568 }
1569
1570 int client_modify_priv_by_name(struct userNode *who, char *priv, int what) {
1571 int i = 0;
1572 assert(0 != priv);
1573 assert(0 != who);
1574
1575 for (i = 0; privtab[i].name; i++) {
1576 if (0 == strcmp(privtab[i].name, priv)) {
1577 if (what == PRIV_ADD)
1578 GrantPriv(who, privtab[i].priv);
1579 else if (what == PRIV_DEL) {
1580 RevokePriv(who, privtab[i].priv);
1581 }
1582 }
1583 }
1584 return 0;
1585 }
1586
1587 int check_priv(char *priv)
1588 {
1589 int i;
1590
1591 for (i = 0; privtab[i].name; i++) {
1592 if (0 == strcmp(privtab[i].name, priv)) {
1593 return 1;
1594 }
1595 }
1596 return 0;
1597 }
1598
1599 void
1600 irc_privs(struct userNode *target, char *flag, int add)
1601 {
1602 client_modify_priv_by_name(target, flag, add);
1603 putsock("%s " P10_PRIVS " %s %s%s", self->numeric, target->numeric, (add == PRIV_ADD) ? "+" : "-", flag);
1604 }
1605
1606 static CMD_FUNC(cmd_privs)
1607 {
1608 char *tstr = NULL;
1609 int type = 0;
1610
1611 tstr = conf_get_data("server/type", RECDB_QSTRING);
1612 if(tstr)
1613 type = atoi(tstr);
1614
1615 if (type < 6)
1616 return 1; /* silently ignore */
1617
1618 struct userNode *user = argc > 1 ? GetUserN(argv[1]) : NULL;
1619 char buf[512] = "";
1620 int what = PRIV_ADD;
1621 char *p = 0;
1622 char *tmp;
1623 unsigned int i;
1624
1625 if (argc < 3)
1626 return 0;
1627
1628 if (!user)
1629 return 0;
1630
1631 for (i=1; i<argc; i++) {
1632 strcat(buf, argv[i]);
1633 strcat(buf, " ");
1634 }
1635
1636 for (i = 2; i < argc; i++) {
1637 if (*argv[i] == '+') { what = PRIV_ADD; argv[i]++; }
1638 if (*argv[i] == '-') { what = PRIV_DEL; argv[i]++; }
1639 for (tmp = x3_strtok(&p, argv[i], ","); tmp;
1640 tmp = x3_strtok(&p, NULL, ",")) {
1641 client_modify_priv_by_name(user, tmp, what);
1642 }
1643 }
1644 return 1;
1645 }
1646
1647 static CMD_FUNC(cmd_burst)
1648 {
1649 extern int rel_age;
1650 char modes[MAXLEN], *members = "";
1651 static char exemptlist[MAXLEN], banlist[MAXLEN];
1652 unsigned int next = 3, res = 1;
1653 int ctype = 0, echeck = 0, bcheck = 0;
1654 struct chanData *cData;
1655 struct chanNode *cNode;
1656 struct userNode *un;
1657 struct modeNode *mNode;
1658 long mode;
1659 int oplevel = -1;
1660 char *user, *end, sep;
1661 time_t in_timestamp;
1662 char* parm = NULL;
1663
1664 if (argc < 3)
1665 return 0;
1666 modes[0] = 0;
1667
1668 exemptlist[0] = 0;
1669 banlist[0] = 0;
1670
1671 while (next < argc) {
1672 switch (argv[next][0]) {
1673 case '+': {
1674 const char *pos;
1675 int n_modes;
1676 for (pos=argv[next], n_modes = 1; *pos; pos++)
1677 if ((*pos == 'k') || (*pos == 'l') || (*pos == 'A')
1678 || (*pos == 'U'))
1679 n_modes++;
1680 unsplit_string(argv+next, n_modes, modes);
1681 next += n_modes;
1682 break;
1683 }
1684 case '%': {
1685 for(parm = mysep(&argv[next], " "); /* parm = first param */
1686 parm; /* While param is not null */
1687 parm = mysep(&argv[next], " ") /* parm = next param */
1688 )
1689 {
1690 switch (parm[0]) {
1691 case '%': {
1692 ctype = 1;
1693 break;
1694 }
1695 case '~': {
1696 ctype = 2;
1697 break;
1698 }
1699 default: {
1700 break;
1701 }
1702 }
1703 if (ctype == 1) {
1704 if (bcheck == 0) {
1705 /* strip % char off start of very first ban */
1706 if (strlen(parm) > 1) {
1707 strncat(banlist, strtok(parm, "%"), sizeof(banlist) - 1 - strlen(banlist));
1708 strncat(banlist, " ", sizeof(banlist) - 1 - strlen(banlist));
1709 }
1710 bcheck = 1;
1711 } else {
1712 strncat(banlist, parm, sizeof(banlist) - 1 - strlen(banlist));
1713 strncat(banlist, " ", sizeof(banlist) - 1 - strlen(banlist));
1714 }
1715 } else if (ctype == 2) {
1716 if (echeck == 0) {
1717 echeck = 1;
1718 } else {
1719 strncat(exemptlist, parm, sizeof(exemptlist) - 1 - strlen(exemptlist));
1720 strncat(exemptlist, " ", sizeof(exemptlist) - 1 - strlen(exemptlist));
1721 }
1722 }
1723 }
1724 next++;
1725 break;
1726 }
1727 default: members = argv[next++]; break;
1728 }
1729 }
1730
1731 in_timestamp = atoi(argv[2]);
1732 if ((cNode = dict_find(unbursted_channels, argv[1], NULL))) {
1733 cNode->timestamp = in_timestamp;
1734 dict_remove(unbursted_channels, cNode->name);
1735 irc_burst(cNode);
1736 }
1737 cNode = AddChannel(argv[1], in_timestamp, modes, banlist, exemptlist);
1738 cData = cNode->channel_info;
1739
1740 if (!cData) {
1741 if (cNode->modes & MODE_REGISTERED) {
1742 irc_join(opserv, cNode);
1743 irc_mode(opserv, cNode, "-z");
1744 irc_part(opserv, cNode, "");
1745 }
1746 }
1747
1748 /* Burst channel members in now. */
1749 for (user = members, sep = *members, mode = 0; sep; user = end) {
1750 for (end = user + 3; isalnum(*end) || *end == '[' || *end == ']'; end++) ;
1751 sep = *end++; end[-1] = 0;
1752 if (sep == ':') {
1753 mode = 0;
1754 while ((sep = *end++)) {
1755 if (sep == 'o') {
1756 mode |= MODE_CHANOP;
1757 oplevel = -1;
1758 } else if (sep == 'h') {
1759 mode |= MODE_HALFOP;
1760 oplevel = -1;
1761 } else if (sep == 'v') {
1762 mode |= MODE_VOICE;
1763 oplevel = -1;
1764 } else if (isdigit(sep)) {
1765 mode |= MODE_CHANOP;
1766 if (oplevel >= 0)
1767 oplevel += parse_oplevel(end);
1768 else
1769 oplevel = parse_oplevel(end);
1770 while (isdigit(*end)) end++;
1771 } else
1772 break;
1773 }
1774 if (rel_age < 0)
1775 mode = 0;
1776 }
1777 if (!(un = GetUserN(user))) {
1778 res = 0;
1779 continue;
1780 }
1781 if ((mNode = AddChannelUser(un, cNode))) {
1782 mNode->modes = mode;
1783 mNode->oplevel = oplevel;
1784 }
1785 }
1786
1787 return res;
1788 }
1789
1790 /* TODO:
1791 * This is a stub that doesn't actually do anything. It should be completed
1792 * so that bans on *!*@markname.* match users as it does in nefarious
1793 */
1794 static CMD_FUNC(cmd_mark)
1795 {
1796 struct userNode *target;
1797 /*
1798 * log_module(MAIN_LOG, LOG_ERROR, "DEBUG: mark, user %s, type %s, arg %s", argv[1], argv[2], argv[3]);
1799 */
1800
1801 if(argc < 4)
1802 return 0;
1803 if(!strcasecmp(argv[2], "DNSBL")) {
1804 /* DNSBL <modes> */
1805 return 1;
1806 }
1807 else if(!strcasecmp(argv[2], "DNSBL_DATA")) {
1808 /* DNSBL_DATA name */
1809 target = GetUserH(argv[1]);
1810 if(!target) {
1811 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s whose dnsbl mark is changing.", argv[1]);
1812 return 0;
1813 }
1814 target->mark = strdup(argv[3]);
1815 return 1;
1816
1817 }
1818 else if(!strcasecmp(argv[2], "CVERSION")) {
1819 /* DNSBL_DATA name */
1820 target = GetUserH(argv[1]);
1821 if(!target) {
1822 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s whose version mark is changing.", argv[1]);
1823 return 0;
1824 }
1825
1826 char *version = unsplit_string(argv + 3, argc - 3, NULL);
1827 if(!version)
1828 version = "";
1829
1830 target->version_reply = strdup(version);
1831
1832 if(match_ircglob(version, "WebTV;*"))
1833 target->no_notice = true; /* webbies cant see notices */
1834
1835 return 1;
1836 }
1837 /* unknown type of mark */
1838 return 1;
1839 }
1840
1841 static CMD_FUNC(cmd_mode)
1842 {
1843 struct chanNode *cn;
1844 struct userNode *un;
1845 char *sethost; // sethost - reed/apples
1846 int i; // sethost - reed/apples
1847
1848 if (argc < 3)
1849 return 0;
1850 if (!IsChannelName(argv[1])) {
1851 un = GetUserH(argv[1]);
1852 if (!un) {
1853 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s whose mode is changing.", argv[1]);
1854 return 0;
1855 }
1856 // sethost - reed/apples
1857 if (argc == 3)
1858 mod_usermode(un, argv[2]);
1859 else {
1860 sethost = malloc(strlen(argv[2]) + 1 + strlen(argv[3]) + 1);
1861 i = 0;
1862 while((sethost[i++] = *argv[2]++));
1863 i--;
1864 sethost[i++] = ' ';
1865 while((sethost[i++] = *argv[3]++));
1866 mod_usermode(un, sethost); // sethost - reed/apples
1867 }
1868
1869 return 1;
1870 }
1871
1872 if (!(cn = GetChannel(argv[1]))) {
1873 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose mode is changing.", argv[1]);
1874 return 0;
1875 }
1876 if ((un = GetUserH(origin))) {
1877 struct modeNode *mn;
1878 /* Update idle time for person setting the mode */
1879 if ((mn = GetUserMode(cn, un)))
1880 mn->idle_since = now;
1881 } else {
1882 /* If it came from a server, reset timestamp to re-sync. */
1883 cn->timestamp = atoi(argv[argc-1]);
1884 }
1885
1886 if (checkDefCon(DEFCON_NO_MODE_CHANGE) && !IsOper(un)) {
1887 const char *str;
1888 str = conf_get_data("services/opserv/nick", RECDB_QSTRING);
1889 if (str) {
1890 char modes[MODELEN];
1891 struct userNode *opserv = GetUserH(str);
1892
1893 send_message_type(4, un, opserv, "Channel modes cannot be changed due to DefCon level %d in effect, please try again soon", DefConLevel);
1894 irc_make_chanmode(cn, modes);
1895 irc_mode(opserv, cn, modes);
1896 irc_mode(opserv, cn, DefConChanModes);
1897 }
1898 return 1;
1899 }
1900
1901
1902 return mod_chanmode(un, cn, argv+2, argc-2, MCP_ALLOW_OVB|MCP_FROM_SERVER|(un ? MC_NOTIFY : 0));
1903 }
1904
1905 static CMD_FUNC(cmd_opmode)
1906 {
1907 struct chanNode *cn;
1908 struct userNode *un;
1909
1910 if (argc < 3)
1911 return 0;
1912
1913 if (!(cn = GetChannel(argv[1]))) {
1914 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose mode is changing.", argv[1]);
1915 return 0;
1916 }
1917 if (!(un = GetUserH(origin))) {
1918 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s requesting OPMODE.", origin);
1919 return 0;
1920 }
1921 if (!IsOper(un)) {
1922 log_module(MAIN_LOG, LOG_ERROR, "Non-privileged user %s using OPMODE.", un->nick);
1923 return 0;
1924 }
1925
1926 return mod_chanmode(un, cn, argv+2, argc-2, MCP_ALLOW_OVB|MCP_FROM_SERVER); /* do NOT announce opmode locally */
1927 }
1928
1929 static int clear_chanmode(struct chanNode *channel, const char *modes);
1930
1931 static CMD_FUNC(cmd_clearmode)
1932 {
1933 struct chanNode *cn;
1934 struct userNode *un;
1935
1936 if (argc < 3)
1937 return 0;
1938
1939 if (!(cn = GetChannel(argv[1]))) {
1940 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose mode is changing.", argv[1]);
1941 return 0;
1942 }
1943 if (!(un = GetUserH(origin))) {
1944 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s requesting CLEARMODE.", origin);
1945 return 0;
1946 }
1947 if (!IsOper(un)) {
1948 log_module(MAIN_LOG, LOG_ERROR, "Non-privileged user %s using CLEARMODE.", un->nick);
1949 return 0;
1950 }
1951
1952 return clear_chanmode(cn, argv[2]);
1953 }
1954
1955 static CMD_FUNC(cmd_topic)
1956 {
1957 struct chanNode *cn;
1958 time_t chan_ts, topic_ts;
1959 struct userNode *user;
1960
1961 if (argc < 3)
1962 return 0;
1963 if (!(cn = GetChannel(argv[1]))) {
1964 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose topic is being set", argv[1]);
1965 return 0;
1966 }
1967
1968
1969 if (argc == 5) { /* Asuka / Topic Bursting IRCu's */
1970 user = GetUserH(origin);
1971 chan_ts = atoi(argv[2]);
1972 topic_ts = atoi(argv[3]);
1973 } else if (argc >= 6) { /* Nefarious 0.5.0 */
1974 user = GetUserH(strtok(argv[2], "!"));
1975 chan_ts = atoi(argv[3]);
1976 topic_ts = atoi(argv[4]);
1977 } else { /* Regular IRCu (No Topic Bursting)*/
1978 user = GetUserH(origin);
1979 chan_ts = cn->timestamp;
1980 topic_ts = now;
1981 }
1982
1983 SetChannelTopic(cn, user, user, argv[argc-1], 0);
1984 cn->topic_time = topic_ts;
1985 return 1;
1986 }
1987
1988 static CMD_FUNC(cmd_num_topic)
1989 {
1990 struct chanNode *cn;
1991
1992 if (!argv[0])
1993 return 0; /* huh? */
1994 if (argv[2]) {
1995 cn = GetChannel(argv[2]);
1996 if (!cn) {
1997 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s in topic reply", argv[2]);
1998 return 0;
1999 }
2000 } else
2001 return 0;
2002
2003 switch (atoi(argv[0])) {
2004 case 331:
2005 cn->topic_time = 0;
2006 break; /* no topic */
2007 case 332:
2008 if (argc < 4)
2009 return 0;
2010 safestrncpy(cn->topic, unsplit_string(argv+3, argc-3, NULL), sizeof(cn->topic));
2011 break;
2012 case 333:
2013 if (argc < 5)
2014 return 0;
2015 safestrncpy(cn->topic_nick, argv[3], sizeof(cn->topic_nick));
2016 cn->topic_time = atoi(argv[4]);
2017 break;
2018 default:
2019 return 0; /* should never happen */
2020 }
2021 return 1;
2022 }
2023
2024 static CMD_FUNC(cmd_num_gline)
2025 {
2026 if (argc < 6)
2027 return 0;
2028 gline_add(origin, argv[3], atoi(argv[4])-now, argv[5], now, 0, 0);
2029 return 1;
2030 }
2031
2032 static CMD_FUNC(cmd_num_shun)
2033 {
2034 if (argc < 6)
2035 return 0;
2036 shun_add(origin, argv[3], atoi(argv[4])-now, argv[5], now, 0);
2037 return 1;
2038 }
2039
2040 static CMD_FUNC(cmd_quit)
2041 {
2042 struct userNode *user;
2043 if (argc < 2)
2044 return 0;
2045 /* Sometimes we get a KILL then a QUIT or the like, so we don't want to
2046 * call DelUser unless we have the user in our grasp. */
2047 if ((user = GetUserH(origin)))
2048 DelUser(user, NULL, false, argv[1]);
2049 return 1;
2050 }
2051
2052 static CMD_FUNC(cmd_kill)
2053 {
2054 struct userNode *user;
2055 if (argc < 2)
2056 return 0;
2057 user = GetUserN(argv[1]);
2058 if (!user) {
2059 /* If we get a KILL for a non-existent user, it could be a
2060 * Ghost response to a KILL we sent out earlier. So we only
2061 * whine if the target is local.
2062 */
2063 if (!strncmp(argv[1], self->numeric, strlen(self->numeric)))
2064 log_module(MAIN_LOG, LOG_ERROR, "Unable to find kill victim %s", argv[1]);
2065 return 0;
2066 }
2067
2068 if (IsLocal(user) && IsService(user)) {
2069 /* TODO: rate limit this so silly things don't happen. */
2070 ReintroduceUser(user);
2071 return 1;
2072 }
2073
2074 DelUser(user, NULL, false, argv[2]);
2075 return 1;
2076 }
2077
2078 static CMD_FUNC(cmd_part)
2079 {
2080 struct userNode *user;
2081
2082 if (argc < 2)
2083 return 0;
2084 user = GetUserH(origin);
2085 if (!user)
2086 return 0;
2087 parse_foreach(argv[1], part_helper, NULL, NULL, NULL, user);
2088 return 1;
2089 }
2090
2091 static CMD_FUNC(cmd_svspart)
2092 {
2093 struct userNode *user;
2094
2095 if (argc < 3)
2096 return 0;
2097 user = GetUserN(argv[1]);
2098 if (!user)
2099 return 0;
2100 parse_foreach(argv[2], part_helper, NULL, NULL, NULL, user);
2101 return 1;
2102 }
2103
2104 static CMD_FUNC(cmd_silence)
2105 {
2106 struct userNode *user;
2107 char *mask;
2108 char *new_mask;
2109 unsigned int i;
2110
2111 if (argc < 2)
2112 return 0;
2113
2114 user = GetUserN(argv[1]);
2115
2116 /* Sanity, go nuts if this happens */
2117 if (!user)
2118 return 0;
2119
2120 /* We can safely ignore this if a user adding a silence is not
2121 * ignored. However this brings up a TODO. If a user logs in and
2122 * they have silences on the IRCd then we need to set them here
2123 * somehow
2124 */
2125 if (!user->handle_info)
2126 return 1;
2127
2128 mask = argv[2];
2129
2130 if (*mask == '-') {
2131 for (i=0; i<user->handle_info->ignores->used; i++) {
2132 if (!irccasecmp(mask+1, user->handle_info->ignores->list[i]))
2133 user->handle_info->ignores->list[i] = user->handle_info->ignores->list[--user->handle_info->ignores->used];
2134 }
2135 } else {
2136 for (i=0; i<user->handle_info->ignores->used; i++) {
2137 if (!strcmp(mask+1, user->handle_info->ignores->list[i]))
2138 return 1; /* Already on the users NickServ ignore list, safely ignore */
2139 }
2140
2141 new_mask = strdup(mask+1);
2142 string_list_append(user->handle_info->ignores, new_mask);
2143 }
2144 return 1;
2145 }
2146
2147 static CMD_FUNC(cmd_kick)
2148 {
2149 if (argc < 3)
2150 return 0;
2151 ChannelUserKicked(GetUserH(origin), GetUserN(argv[2]), GetChannel(argv[1]));
2152 return 1;
2153 }
2154
2155 static CMD_FUNC(cmd_squit)
2156 {
2157 struct server *server;
2158 char *uplink;
2159
2160 if (argc < 4)
2161 return 0;
2162 if (!(server = GetServerH(argv[1])))
2163 return 0;
2164
2165 if (server == self->uplink) {
2166 /* Force a reconnect to the currently selected server. */
2167 cManager.uplink->tries = 0;
2168 log_module(MAIN_LOG, LOG_INFO, "Squitting from uplink: %s", argv[3]);
2169 close_socket();
2170 return 1;
2171 }
2172
2173 uplink = strdup(server->uplink->name);
2174 DelServer(server, 0, argv[3]);
2175 /* if its a pingout and pingout connecting is enabled
2176 or its a read error and readerror connecting is enabled
2177 or were doing a "N" and i need to connect that server somewhere */
2178 routing_handle_squit(argv[1], uplink, argv[3]);
2179 free(uplink);
2180 return 1;
2181 }
2182
2183 static CMD_FUNC(cmd_privmsg)
2184 {
2185 struct privmsg_desc pd;
2186 if (argc != 3)
2187 return 0;
2188 pd.user = GetUserH(origin);
2189 if (!pd.user || (IsGagged(pd.user) && !IsOper(pd.user)))
2190 return 1;
2191
2192 if (checkDefCon(DEFCON_OPER_ONLY) && !IsOper(pd.user)) {
2193 const char *str;
2194 str = conf_get_data("services/opserv/nick", RECDB_QSTRING);
2195 if (str)
2196 send_message_type(4, pd.user, GetUserH(str), "Services are currently not available, please try again soon");
2197 return 1;
2198 }
2199
2200 if (checkDefCon(DEFCON_SILENT_OPER_ONLY) && !IsOper(pd.user))
2201 return 1; /* Silently Ignore */
2202
2203 pd.is_notice = 0;
2204 pd.text = argv[2];
2205 parse_foreach(argv[1], privmsg_chan_helper, NULL, privmsg_user_helper, privmsg_invalid, &pd);
2206
2207 return 1;
2208 }
2209
2210 static CMD_FUNC(cmd_notice)
2211 {
2212 struct privmsg_desc pd;
2213 struct server *srv;
2214 int nuser = 0;
2215
2216 if (argc != 3)
2217 return 0;
2218
2219 pd.user = GetUserH(origin);
2220 if(!pd.user)
2221 nuser = 1;
2222 if (!pd.user || (IsGagged(pd.user) && !IsOper(pd.user))) {
2223 }
2224 else {
2225 pd.is_notice = 1;
2226 pd.text = argv[2];
2227 parse_foreach(argv[1], privmsg_chan_helper, NULL, privmsg_user_helper, privmsg_invalid, &pd);
2228 }
2229
2230 srv = GetServerH(origin);
2231 if(srv) {
2232 char *sargv[MAXNUMPARAMS];
2233 int sargc;
2234
2235 sargc = split_line(argv[2], true, MAXNUMPARAMS, sargv);
2236
2237 if(!strcasecmp(sargv[0], "Connect:")) {
2238 /* :Connect: Host shoe.loxxin.net not listed in ircd.conf */
2239 if(!strcasecmp(sargv[3], "not") && !strcasecmp(sargv[4], "listed")) {
2240 routing_handle_connect_failure(srv, sargv[2], unsplit_string(sargv+3, sargc-3, NULL));
2241 }
2242 }
2243 else if(!strcasecmp(sargv[0], "Link")) {
2244 /* :Link with mephisto.etheria.cx cancelled: Server mephisto.etheria.cx[216.46.33.71]
2245 *
2246 * :Link with laptop.afternet.org cancelled: Connection refused
2247 */
2248 if(!strcasecmp(sargv[3], "cancelled:")) {
2249 routing_handle_connect_failure(srv, sargv[2], unsplit_string(sargv+4, sargc-4, NULL));
2250 }
2251 }
2252 }
2253 return 1;
2254 }
2255
2256 static CMD_FUNC(cmd_away)
2257 {
2258 struct userNode *uNode;
2259
2260 uNode = GetUserH(origin);
2261 if (!uNode)
2262 return 1;
2263 if (argc < 2)
2264 uNode->modes &= ~FLAGS_AWAY;
2265 else
2266 uNode->modes |= FLAGS_AWAY;
2267 return 1;
2268 }
2269
2270 static CMD_FUNC(cmd_gline)
2271 {
2272 if (argc < 3)
2273 return 0;
2274 if (argv[2][0] == '+') {
2275 if (argc < 5)
2276 return 0;
2277 gline_add(origin, argv[2]+1, strtoul(argv[3], NULL, 0), argv[argc-1], now, 0, 0);
2278 return 1;
2279 } else if (argv[2][0] == '-') {
2280 gline_remove(argv[2]+1, 0);
2281 return 1;
2282 } else
2283 return 0;
2284 }
2285
2286 static CMD_FUNC(cmd_shun)
2287 {
2288 if (argc < 3)
2289 return 0;
2290 if (argv[2][0] == '+') {
2291 if (argc < 5)
2292 return 0;
2293 shun_add(origin, argv[2]+1, strtoul(argv[3], NULL, 0), argv[argc-1], now, 0);
2294 return 1;
2295 } else if (argv[2][0] == '-') {
2296 shun_remove(argv[2]+1, 0);
2297 return 1;
2298 } else
2299 return 0;
2300 }
2301
2302 static CMD_FUNC(cmd_svsnick)
2303 {
2304 struct userNode *target, *dest;
2305 if ((argc < 4)
2306 || !(target = GetUserN(argv[1]))
2307 || !IsLocal(target)
2308 || (dest = GetUserH(argv[2])))
2309 return 0;
2310 NickChange(target, argv[2], 0);
2311 return 1;
2312 }
2313
2314 static oper_func_t *of_list;
2315 static unsigned int of_size = 0, of_used = 0;
2316
2317 void
2318 free_user(struct userNode *user)
2319 {
2320 free(user->nick);
2321 free(user);
2322 }
2323
2324 static void
2325 parse_cleanup(void)
2326 {
2327 unsigned int nn;
2328 free(of_list);
2329 free(privmsg_funcs);
2330 free(notice_funcs);
2331 free(mcf_list);
2332 dict_delete(irc_func_dict);
2333 for (nn=0; nn<dead_users.used; nn++)
2334 free_user(dead_users.list[nn]);
2335 userList_clean(&dead_users);
2336 }
2337
2338 static void
2339 p10_conf_reload(void) {
2340 hidden_host_suffix = conf_get_data("server/hidden_host", RECDB_QSTRING);
2341 }
2342
2343 static void
2344 remove_unbursted_channel(struct chanNode *cNode) {
2345 if (unbursted_channels)
2346 dict_remove(unbursted_channels, cNode->name);
2347 }
2348
2349 void
2350 init_parse(void)
2351 {
2352 const char *str, *desc;
2353 int numnick, usermask, max_users;
2354 char numer[COMBO_NUMERIC_LEN+1];
2355
2356 /* read config items */
2357 str = conf_get_data("server/extended_accounts", RECDB_QSTRING);
2358 extended_accounts = str ? enabled_string(str) : 1;
2359 str = conf_get_data("server/ping_freq", RECDB_QSTRING);
2360 ping_freq = str ? ParseInterval(str) : 120;
2361 str = conf_get_data("server/ping_timeout", RECDB_QSTRING);
2362 ping_timeout = str ? ParseInterval(str) : 30;
2363 str = conf_get_data("server/force_n2k", RECDB_QSTRING);
2364 force_n2k = str ? enabled_string(str) : 1;
2365 str = conf_get_data("server/numeric", RECDB_QSTRING);
2366 if (!str) {
2367 log_module(MAIN_LOG, LOG_ERROR, "No server/numeric entry in config file.");
2368 exit(1);
2369 }
2370 numnick = atoi(str);
2371 str = conf_get_data("server/max_users", RECDB_QSTRING);
2372 max_users = str ? atoi(str) : 4096;
2373 for (usermask = 4; usermask < max_users; usermask <<= 1) ;
2374 usermask--;
2375 if ((numnick < 64) && (usermask < 4096) && !force_n2k)
2376 inttobase64(numer, (numnick << 12) + (usermask & 0x00fff), 3);
2377 else
2378 inttobase64(numer, (numnick << 18) + (usermask & 0x3ffff), 5);
2379
2380 str = conf_get_data("server/his_servername", RECDB_QSTRING);
2381 his_servername = str ? strdup(str) : NULL;
2382 str = conf_get_data("server/his_servercomment", RECDB_QSTRING);
2383 his_servercomment = str ? strdup(str) : NULL;
2384
2385 str = conf_get_data("server/hostname", RECDB_QSTRING);
2386 desc = conf_get_data("server/description", RECDB_QSTRING);
2387 if (!str || !desc) {
2388 log_module(MAIN_LOG, LOG_ERROR, "No server/hostname entry in config file.");
2389 exit(1);
2390 }
2391 self = AddServer(NULL, str, 0, boot_time, now, numer, desc);
2392 conf_register_reload(p10_conf_reload);
2393
2394 irc_func_dict = dict_new();
2395 dict_insert(irc_func_dict, CMD_BURST, cmd_burst);
2396 dict_insert(irc_func_dict, TOK_BURST, cmd_burst);
2397 dict_insert(irc_func_dict, CMD_CREATE, cmd_create);
2398 dict_insert(irc_func_dict, TOK_CREATE, cmd_create);
2399 dict_insert(irc_func_dict, CMD_EOB, cmd_eob);
2400 dict_insert(irc_func_dict, TOK_EOB, cmd_eob);
2401 dict_insert(irc_func_dict, CMD_EOB_ACK, cmd_eob_ack);
2402 dict_insert(irc_func_dict, TOK_EOB_ACK, cmd_eob_ack);
2403 dict_insert(irc_func_dict, CMD_MODE, cmd_mode);
2404 dict_insert(irc_func_dict, TOK_MODE, cmd_mode);
2405 dict_insert(irc_func_dict, CMD_MARK, cmd_mark);
2406 dict_insert(irc_func_dict, TOK_MARK, cmd_mark);
2407 dict_insert(irc_func_dict, CMD_NICK, cmd_nick);
2408 dict_insert(irc_func_dict, TOK_NICK, cmd_nick);
2409 dict_insert(irc_func_dict, CMD_ACCOUNT, cmd_account);
2410 dict_insert(irc_func_dict, TOK_ACCOUNT, cmd_account);
2411 dict_insert(irc_func_dict, CMD_FAKEHOST, cmd_fakehost);
2412 dict_insert(irc_func_dict, TOK_FAKEHOST, cmd_fakehost);
2413 dict_insert(irc_func_dict, CMD_PASS, cmd_pass);
2414 dict_insert(irc_func_dict, TOK_PASS, cmd_pass);
2415 dict_insert(irc_func_dict, CMD_PING, cmd_ping);
2416 dict_insert(irc_func_dict, TOK_PING, cmd_ping);
2417 dict_insert(irc_func_dict, CMD_PRIVMSG, cmd_privmsg);
2418 dict_insert(irc_func_dict, TOK_PRIVMSG, cmd_privmsg);
2419 dict_insert(irc_func_dict, CMD_PONG, cmd_pong);
2420 dict_insert(irc_func_dict, TOK_PONG, cmd_pong);
2421 dict_insert(irc_func_dict, CMD_QUIT, cmd_quit);
2422 dict_insert(irc_func_dict, TOK_QUIT, cmd_quit);
2423 dict_insert(irc_func_dict, CMD_SERVER, cmd_server);
2424 dict_insert(irc_func_dict, TOK_SERVER, cmd_server);
2425 dict_insert(irc_func_dict, CMD_JOIN, cmd_join);
2426 dict_insert(irc_func_dict, TOK_JOIN, cmd_join);
2427 dict_insert(irc_func_dict, CMD_PART, cmd_part);
2428 dict_insert(irc_func_dict, TOK_PART, cmd_part);
2429 dict_insert(irc_func_dict, CMD_ERROR, cmd_error);
2430 dict_insert(irc_func_dict, TOK_ERROR, cmd_error);
2431 dict_insert(irc_func_dict, CMD_TOPIC, cmd_topic);
2432 dict_insert(irc_func_dict, TOK_TOPIC, cmd_topic);
2433 dict_insert(irc_func_dict, CMD_AWAY, cmd_away);
2434 dict_insert(irc_func_dict, TOK_AWAY, cmd_away);
2435 dict_insert(irc_func_dict, CMD_SILENCE, cmd_silence);
2436 dict_insert(irc_func_dict, TOK_SILENCE, cmd_silence);
2437 dict_insert(irc_func_dict, CMD_KICK, cmd_kick);
2438 dict_insert(irc_func_dict, TOK_KICK, cmd_kick);
2439 dict_insert(irc_func_dict, CMD_SQUIT, cmd_squit);
2440 dict_insert(irc_func_dict, TOK_SQUIT, cmd_squit);
2441 dict_insert(irc_func_dict, CMD_KILL, cmd_kill);
2442 dict_insert(irc_func_dict, TOK_KILL, cmd_kill);
2443 dict_insert(irc_func_dict, CMD_NOTICE, cmd_notice);
2444 dict_insert(irc_func_dict, TOK_NOTICE, cmd_notice);
2445 dict_insert(irc_func_dict, CMD_STATS, cmd_stats);
2446 dict_insert(irc_func_dict, TOK_STATS, cmd_stats);
2447 dict_insert(irc_func_dict, CMD_SVSJOIN, cmd_svsjoin);
2448 dict_insert(irc_func_dict, TOK_SVSJOIN, cmd_svsjoin);
2449 dict_insert(irc_func_dict, CMD_SVSNICK, cmd_svsnick);
2450 dict_insert(irc_func_dict, TOK_SVSNICK, cmd_svsnick);
2451 dict_insert(irc_func_dict, CMD_SVSPART, cmd_svspart);
2452 dict_insert(irc_func_dict, TOK_SVSPART, cmd_svspart);
2453 dict_insert(irc_func_dict, CMD_SWHOIS, cmd_dummy);
2454 dict_insert(irc_func_dict, TOK_SWHOIS, cmd_dummy);
2455 dict_insert(irc_func_dict, CMD_WHOIS, cmd_whois);
2456 dict_insert(irc_func_dict, TOK_WHOIS, cmd_whois);
2457 dict_insert(irc_func_dict, CMD_GLINE, cmd_gline);
2458 dict_insert(irc_func_dict, TOK_GLINE, cmd_gline);
2459 dict_insert(irc_func_dict, CMD_SHUN, cmd_shun);
2460 dict_insert(irc_func_dict, TOK_SHUN, cmd_shun);
2461 dict_insert(irc_func_dict, CMD_OPMODE, cmd_opmode);
2462 dict_insert(irc_func_dict, TOK_OPMODE, cmd_opmode);
2463 dict_insert(irc_func_dict, CMD_CLEARMODE, cmd_clearmode);
2464 dict_insert(irc_func_dict, TOK_CLEARMODE, cmd_clearmode);
2465 dict_insert(irc_func_dict, CMD_VERSION, cmd_version);
2466 dict_insert(irc_func_dict, TOK_VERSION, cmd_version);
2467 dict_insert(irc_func_dict, CMD_ADMIN, cmd_admin);
2468 dict_insert(irc_func_dict, TOK_ADMIN, cmd_admin);
2469 dict_insert(irc_func_dict, CMD_SNO, cmd_dummy);
2470 dict_insert(irc_func_dict, TOK_SNO, cmd_dummy);
2471
2472 dict_insert(irc_func_dict, CMD_RPING, cmd_rping);
2473 dict_insert(irc_func_dict, TOK_RPING, cmd_rping);
2474 dict_insert(irc_func_dict, CMD_RPONG, cmd_dummy);
2475 dict_insert(irc_func_dict, TOK_RPONG, cmd_dummy);
2476
2477 /* In P10, DESTRUCT doesn't do anything except be broadcast to servers.
2478 * Apparently to obliterate channels from any servers that think they
2479 * exist?
2480 */
2481 dict_insert(irc_func_dict, CMD_DESTRUCT, cmd_dummy);
2482 dict_insert(irc_func_dict, TOK_DESTRUCT, cmd_dummy);
2483 /* Ignore invites */
2484 dict_insert(irc_func_dict, CMD_INVITE, cmd_dummy);
2485 dict_insert(irc_func_dict, TOK_INVITE, cmd_dummy);
2486 /* DESYNCH is just informational, so ignore it */
2487 dict_insert(irc_func_dict, CMD_DESYNCH, cmd_dummy);
2488 dict_insert(irc_func_dict, TOK_DESYNCH, cmd_dummy);
2489 /* Ignore channel operator notices. */
2490 dict_insert(irc_func_dict, CMD_WALLCHOPS, cmd_dummy);
2491 dict_insert(irc_func_dict, TOK_WALLCHOPS, cmd_dummy);
2492 dict_insert(irc_func_dict, CMD_WALLVOICES, cmd_dummy);
2493 dict_insert(irc_func_dict, TOK_WALLVOICES, cmd_dummy);
2494 dict_insert(irc_func_dict, CMD_WALLHOPS, cmd_dummy);
2495 dict_insert(irc_func_dict, TOK_WALLHOPS, cmd_dummy);
2496 /* Ignore opers being silly. */
2497 dict_insert(irc_func_dict, CMD_WALLOPS, cmd_dummy);
2498 dict_insert(irc_func_dict, TOK_WALLOPS, cmd_dummy);
2499 dict_insert(irc_func_dict, CMD_WALLHOPS, cmd_dummy);
2500 dict_insert(irc_func_dict, TOK_WALLHOPS, cmd_dummy);
2501 dict_insert(irc_func_dict, TOK_WALLUSERS, cmd_dummy);
2502 /* Ignore dnsbl exemptions */
2503 dict_insert(irc_func_dict, TOK_EXEMPT, cmd_dummy);
2504 dict_insert(irc_func_dict, CMD_PRIVS, cmd_privs);
2505 dict_insert(irc_func_dict, TOK_PRIVS, cmd_privs);
2506 /* ignore ALIST for now */
2507 dict_insert(irc_func_dict, TOK_ALIST, cmd_dummy);
2508 dict_insert(irc_func_dict, CMD_ALIST, cmd_dummy);
2509 /* Ignore remote luser */
2510 dict_insert(irc_func_dict, TOK_LUSERS, cmd_dummy);
2511 /* We have reliable clock! Always! Wraaa! */
2512 dict_insert(irc_func_dict, CMD_SETTIME, cmd_dummy);
2513 dict_insert(irc_func_dict, TOK_SETTIME, cmd_dummy);
2514
2515 /* ignore /trace and /motd commands targetted at us */
2516 dict_insert(irc_func_dict, TOK_TRACE, cmd_dummy);
2517 dict_insert(irc_func_dict, TOK_MOTD, cmd_dummy);
2518
2519 /* handle topics */
2520 dict_insert(irc_func_dict, "331", cmd_num_topic);
2521 dict_insert(irc_func_dict, "332", cmd_num_topic);
2522 dict_insert(irc_func_dict, "333", cmd_num_topic);
2523 dict_insert(irc_func_dict, "345", cmd_dummy); /* blah has been invited to blah */
2524 dict_insert(irc_func_dict, "432", cmd_error_nick); /* Erroneus [sic] nickname */
2525 /* ban list resetting */
2526 /* "stats g" responses */
2527 dict_insert(irc_func_dict, "247", cmd_num_gline);
2528 dict_insert(irc_func_dict, "542", cmd_num_shun);
2529 dict_insert(irc_func_dict, "219", cmd_dummy); /* "End of /STATS report" */
2530 /* other numeric responses we might get */
2531 dict_insert(irc_func_dict, "401", cmd_dummy); /* target left network */
2532 dict_insert(irc_func_dict, "403", cmd_dummy); /* no such channel */
2533 dict_insert(irc_func_dict, "404", cmd_dummy); /* cannot send to channel */
2534 dict_insert(irc_func_dict, "439", cmd_dummy); /* target change too fast */
2535 dict_insert(irc_func_dict, "441", cmd_dummy); /* target isn't on that channel */
2536 dict_insert(irc_func_dict, "442", cmd_dummy); /* you aren't on that channel */
2537 dict_insert(irc_func_dict, "443", cmd_dummy); /* is already on channel (after invite?) */
2538 dict_insert(irc_func_dict, "461", cmd_dummy); /* Not enough parameters (after TOPIC w/ 0 args) */
2539 dict_insert(irc_func_dict, "467", cmd_dummy); /* Channel key already set */
2540
2541 num_privmsg_funcs = 16;
2542 privmsg_funcs = malloc(sizeof(privmsg_func_t)*num_privmsg_funcs);
2543 memset(privmsg_funcs, 0, sizeof(privmsg_func_t)*num_privmsg_funcs);
2544
2545 num_notice_funcs = 16;
2546 notice_funcs = malloc(sizeof(privmsg_func_t)*num_notice_funcs);
2547 memset(notice_funcs, 0, sizeof(privmsg_func_t)*num_notice_funcs);
2548
2549 userList_init(&dead_users);
2550 reg_del_channel_func(remove_unbursted_channel);
2551 reg_exit_func(parse_cleanup);
2552 // reg_notice_func(opserv, check_ctcp);
2553 }
2554
2555 int
2556 parse_line(char *line, int recursive)
2557 {
2558 char *argv[MAXNUMPARAMS], *origin;
2559 int argc, cmd, res=0;
2560 cmd_func_t *func;
2561
2562 argc = split_line(line, true, MAXNUMPARAMS, argv);
2563 cmd = self->uplink || !argv[0][1] || !argv[0][2];
2564 if (argc > cmd) {
2565 if (cmd) {
2566 if (argv[0][0] == ':') {
2567 origin = argv[0]+1;
2568 } else if (!argv[0][1] || !argv[0][2]) {
2569 struct server *sNode = GetServerN(argv[0]);
2570 origin = sNode ? sNode->name : 0;
2571 } else {
2572 struct userNode *uNode = GetUserN(argv[0]);
2573 origin = uNode ? uNode->nick : 0;
2574 }
2575 } else
2576 origin = 0;
2577 if ((func = dict_find(irc_func_dict, argv[cmd], NULL)))
2578 res = func(origin, argc-cmd, argv+cmd);
2579 }
2580 if (!res) {
2581 log_module(MAIN_LOG, LOG_ERROR, "PARSE ERROR on line: %s", unsplit_string(argv, argc, NULL));
2582 } else if (!recursive) {
2583 unsigned int i;
2584 for (i=0; i<dead_users.used; i++)
2585 free_user(dead_users.list[i]);
2586 dead_users.used = 0;
2587 }
2588 return res;
2589 }
2590
2591 static void
2592 parse_foreach(char *target_list, foreach_chanfunc cf, foreach_nonchan nc, foreach_userfunc uf, foreach_nonuser nu, void *data)
2593 {
2594 char *j, old;
2595
2596 do {
2597 j = target_list;
2598 while (*j != 0 && *j != ',')
2599 j++;
2600 old = *j;
2601 *j = 0;
2602
2603 if (IsChannelName(target_list)
2604 || (target_list[0] == '0' && target_list[1] == '\0')) {
2605 struct chanNode *chan = GetChannel(target_list);
2606
2607 if (chan) {
2608 if (cf)
2609 cf(chan, data);
2610 } else {
2611 if (nc)
2612 nc(target_list, data);
2613 }
2614 } else {
2615 struct userNode *user;
2616 struct privmsg_desc *pd = data;
2617
2618 pd->is_qualified = 0;
2619 if (*target_list == '@') {
2620 user = NULL;
2621 } else if (strchr(target_list, '@')) {
2622 struct server *server;
2623
2624 pd->is_qualified = 1;
2625 user = GetUserH(strtok(target_list, "@"));
2626 server = GetServerH(strtok(NULL, "@"));
2627
2628 if (user && (user->uplink != server)) {
2629 /* Don't attempt to index into any arrays
2630 using a user's numeric on another server. */
2631 user = NULL;
2632 }
2633 } else {
2634 user = GetUserN(target_list);
2635 }
2636
2637 if (user) {
2638 if (uf)
2639 uf(user, data);
2640 } else {
2641 if (nu)
2642 nu(target_list, data);
2643 }
2644 }
2645 target_list = j+1;
2646 } while (old == ',');
2647 }
2648
2649 static int
2650 get_local_numeric(void)
2651 {
2652 static unsigned int next_numeric = 0;
2653 if (self->clients > self->num_mask)
2654 return -1;
2655 while (self->users[next_numeric])
2656 if (++next_numeric > self->num_mask)
2657 next_numeric = 0;
2658 return next_numeric;
2659 }
2660
2661 static void
2662 make_numeric(struct server *svr, int local_num, char *outbuf)
2663 {
2664 int slen, llen;
2665
2666 if (force_n2k || svr->numeric[1]) {
2667 slen = 2;
2668 llen = 3;
2669 } else {
2670 slen = 1;
2671 llen = (local_num < 64*64) ? 2 : 3;
2672 }
2673 strncpy(outbuf, svr->numeric, slen);
2674 inttobase64(outbuf+slen, local_num, llen);
2675 outbuf[slen+llen] = 0;
2676 }
2677
2678 struct server *
2679 AddServer(struct server *uplink, const char *name, int hops, time_t boot, time_t link, const char *numeric, const char *description)
2680 {
2681 struct server* sNode;
2682 int slen, mlen;
2683
2684 if ((sNode = GetServerN(numeric))) {
2685 /* This means we're trying to re-add an existant server.
2686 * To be safe, we should forget the previous incarnation.
2687 * (And all its linked servers.)
2688 *
2689 * It usually only happens in replays when the original
2690 * had a ping timeout and the replay didn't (because
2691 * replaying a ping timeout invariably gets things wrong).
2692 */
2693 DelServer(sNode, 0, NULL);
2694 }
2695
2696 switch (strlen(numeric)) {
2697 case 5: slen = 2; mlen = 3; break;
2698 case 4: slen = 1; mlen = 3; break;
2699 case 3: slen = 1; mlen = 2; break;
2700 default:
2701 log_module(MAIN_LOG, LOG_ERROR, "AddServer(\"%s\", \"%s\", ...): Numeric %s has invalid length.", uplink->name, name, numeric);
2702 return NULL;
2703 }
2704
2705 sNode = calloc(1, sizeof(*sNode));
2706 sNode->uplink = uplink;
2707 safestrncpy(sNode->name, name, sizeof(sNode->name));
2708 sNode->num_mask = base64toint(numeric+slen, mlen);
2709 sNode->hops = hops;
2710 sNode->boot = boot;
2711 sNode->link = link;
2712 strncpy(sNode->numeric, numeric, slen);
2713 safestrncpy(sNode->description, description, sizeof(sNode->description));
2714 sNode->users = calloc(sNode->num_mask+1, sizeof(*sNode->users));
2715 serverList_init(&sNode->children);
2716 if (sNode->uplink) {
2717 /* uplink may be NULL if we're just building ourself */
2718 serverList_append(&sNode->uplink->children, sNode);
2719 }
2720 servers_num[base64toint(numeric, slen)] = sNode;
2721 dict_insert(servers, sNode->name, sNode);
2722 return sNode;
2723 }
2724
2725 void DelServer(struct server* serv, int announce, const char *message)
2726 {
2727 unsigned int i;
2728
2729 /* If we receive an ERROR command before the SERVER
2730 * command a NULL server can be passed */
2731 if (!serv)
2732 return;
2733
2734 /* Hrm, what's the right way to SQUIT some other server?
2735 * (This code is only to handle killing juped servers.) */
2736 if (announce && (serv->uplink == self) && (serv != self->uplink))
2737 irc_squit(serv, message, NULL);
2738
2739 /* must recursively remove servers linked to this one first */
2740 for (i=serv->children.used;i>0;)
2741 if (serv->children.list[--i] != self)
2742 DelServer(serv->children.list[i], false, NULL);
2743
2744 /* clean up server's user hash tables */
2745 for (i=0;i<=serv->num_mask;i++)
2746 if (serv->users[i])
2747 DelUser(serv->users[i], NULL, false, "server delinked");
2748
2749 /* delete server */
2750 if (serv->uplink)
2751 serverList_remove(&serv->uplink->children, serv);
2752 if (serv == self->uplink)
2753 self->uplink = NULL;
2754 servers_num[base64toint(serv->numeric, strlen(serv->numeric))] = NULL;
2755 dict_remove(servers, serv->name);
2756 serverList_clean(&serv->children);
2757 free(serv->users);
2758 free(serv);
2759 }
2760
2761 struct userNode *
2762 AddService(const char *nick, const char *modes, const char *desc, const char *hostname)
2763 {
2764 char numeric[COMBO_NUMERIC_LEN+1];
2765 int local_num = get_local_numeric();
2766 time_t timestamp = now;
2767 struct userNode *old_user = GetUserH(nick);
2768
2769 if (old_user) {
2770 if (IsLocal(old_user))
2771 return old_user;
2772 timestamp = old_user->timestamp - 1;
2773 }
2774 if (local_num == -1) {
2775 log_module(MAIN_LOG, LOG_ERROR, "Unable to allocate numnick for service %s", nick);
2776 return 0;
2777 }
2778 if (!hostname)
2779 hostname = self->name;
2780 make_numeric(self, local_num, numeric);
2781 /* TODO: Make these modes part of the conf file */
2782 return AddUser(self, nick, nick, hostname, modes ? modes : "+oik", numeric, desc, now, "AAAAAA");
2783 }
2784
2785 struct userNode *
2786 AddClone(const char *nick, const char *ident, const char *hostname, const char *desc)
2787 {
2788 char numeric[COMBO_NUMERIC_LEN+1];
2789 int local_num = get_local_numeric();
2790 time_t timestamp = now;
2791 struct userNode *old_user = GetUserH(nick);
2792
2793 if (old_user) {
2794 if (IsLocal(old_user))
2795 return old_user;
2796 timestamp = old_user->timestamp - 1;
2797 }
2798 if (local_num == -1) {
2799 log_module(MAIN_LOG, LOG_ERROR, "Unable to allocate numnick for clone %s", nick);
2800 return 0;
2801 }
2802 make_numeric(self, local_num, numeric);
2803 return AddUser(self, nick, ident, hostname, "+i", numeric, desc, timestamp, "AAAAAA");
2804 }
2805
2806 int
2807 is_valid_nick(const char *nick) {
2808 unsigned int ii;
2809 /* IRC has some of The Most Fucked-Up ideas about character sets
2810 * in the world.. */
2811 if (!isalpha(*nick) && !strchr("{|}~[\\]^_`", *nick))
2812 return 0;
2813 for (ii = 0; nick[ii]; ++ii)
2814 if (!isalnum(nick[ii]) && !strchr("{|}~[\\]^-_`", nick[ii]))
2815 return 0;
2816 if (strlen(nick) > nicklen)
2817 return 0;
2818 return 1;
2819 }
2820
2821 static struct userNode*
2822 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)
2823 {
2824 struct userNode *oldUser, *uNode;
2825 unsigned int n, ignore_user;
2826 char *tstr;
2827 int type;
2828
2829 if ((strlen(numeric) < 3) || (strlen(numeric) > 5)) {
2830 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): numeric %s wrong length!", uplink, nick, numeric);
2831 return NULL;
2832 }
2833
2834 if (!uplink) {
2835 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): server for numeric %s doesn't exist!", uplink, nick, numeric);
2836 return NULL;
2837 }
2838
2839 if (uplink != GetServerN(numeric)) {
2840 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): server for numeric %s differs from nominal uplink %s.", uplink, nick, numeric, uplink->name);
2841 return NULL;
2842 }
2843
2844 if (!is_valid_nick(nick)) {
2845 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): invalid nickname detected.", uplink, nick);
2846 return NULL;
2847 }
2848
2849 ignore_user = 0;
2850 if ((oldUser = GetUserH(nick))) {
2851 if (IsLocal(oldUser) && (IsService(oldUser) || IsPersistent(oldUser))) {
2852 /* The service should collide the new user off. */
2853 oldUser->timestamp = timestamp - 1;
2854 irc_user(oldUser);
2855 }
2856 if (oldUser->timestamp > timestamp) {
2857 /* "Old" user is really newer; remove them */
2858 DelUser(oldUser, 0, 1, "Overruled by older nick");
2859 } else {
2860 /* User being added is too new; do not add them to
2861 * clients, but do add them to the server's list, since it
2862 * will send a KILL and QUIT soon. */
2863 ignore_user = 1;
2864 }
2865 }
2866
2867 /* create new usernode and set all values */
2868 uNode = calloc(1, sizeof(*uNode));
2869 uNode->nick = strdup(nick);
2870 safestrncpy(uNode->ident, ident, sizeof(uNode->ident));
2871 safestrncpy(uNode->info, userinfo, sizeof(uNode->info));
2872 safestrncpy(uNode->hostname, hostname, sizeof(uNode->hostname));
2873 safestrncpy(uNode->numeric, numeric, sizeof(uNode->numeric));
2874 irc_p10_pton(&uNode->ip, realip);
2875
2876 tstr = conf_get_data("server/type", RECDB_QSTRING);
2877 type = atoi(tstr);
2878 if (type > 6) {
2879 if (irc_in_addr_is_ipv4(uNode->ip)) {
2880 make_virtip((char*)irc_ntoa(&uNode->ip), (char*)irc_ntoa(&uNode->ip), uNode->cryptip);
2881 make_virthost((char*)irc_ntoa(&uNode->ip), uNode->hostname, uNode->crypthost);
2882 } else if (irc_in_addr_is_ipv6(uNode->ip)) {
2883 make_ipv6virthost((char*)irc_ntoa(&uNode->ip), uNode->hostname, uNode->crypthost);
2884 }
2885 }
2886
2887 if (!uNode->crypthost && uNode->cryptip)
2888 snprintf(uNode->crypthost, sizeof(uNode->crypthost), "%s", strdup(uNode->cryptip));
2889
2890 uNode->timestamp = timestamp;
2891 modeList_init(&uNode->channels);
2892 uNode->uplink = uplink;
2893 if (++uNode->uplink->clients > uNode->uplink->max_clients) {
2894 uNode->uplink->max_clients = uNode->uplink->clients;
2895 }
2896 uNode->num_local = base64toint(numeric+strlen(uNode->uplink->numeric), 3) & uNode->uplink->num_mask;
2897 uNode->uplink->users[uNode->num_local] = uNode;
2898 mod_usermode(uNode, modes);
2899
2900 set_geoip_info(uNode);
2901
2902 if (ignore_user)
2903 return uNode;
2904
2905 dict_insert(clients, uNode->nick, uNode);
2906 if (dict_size(clients) > max_clients) {
2907 max_clients = dict_size(clients);
2908 max_clients_time = now;
2909 }
2910 if (IsLocal(uNode))
2911 irc_user(uNode);
2912 for (n=0; n<nuf_used; n++)
2913 if (nuf_list[n](uNode))
2914 break;
2915
2916 if ((uNode->loc == 1) && (uNode->handle_info))
2917 send_func_list(uNode);
2918
2919 return uNode;
2920 }
2921
2922 /* removes user from it's server's hash table and nick hash table */
2923 void
2924 DelUser(struct userNode* user, struct userNode *killer, int announce, const char *why)
2925 {
2926 unsigned int n;
2927
2928 verify(user);
2929
2930 /* mark them as dead, in case anybody cares */
2931 user->dead = 1;
2932
2933 /* remove pending adduser commands */
2934 wipe_adduser_pending(NULL, user);
2935
2936 /* remove user from all channels */
2937 while (user->channels.used > 0)
2938 DelChannelUser(user, user->channels.list[user->channels.used-1]->channel, false, 0);
2939
2940 /* Call these in reverse order so ChanServ can update presence
2941 information before NickServ nukes the handle_info. */
2942 for (n = duf_used; n > 0; )
2943 duf_list[--n](user, killer, why);
2944
2945 user->uplink->clients--;
2946 user->uplink->users[user->num_local] = NULL;
2947 if (IsOper(user))
2948 userList_remove(&curr_opers, user);
2949 /* remove from global dictionary, but not if after a collide */
2950 if (user == dict_find(clients, user->nick, NULL))
2951 dict_remove(clients, user->nick);
2952
2953 if (IsInvisible(user))
2954 invis_clients--;
2955
2956 if (announce) {
2957 if (IsLocal(user))
2958 irc_quit(user, why);
2959 else
2960 irc_kill(killer, user, why);
2961 }
2962
2963 modeList_clean(&user->channels);
2964
2965 /* Clean up version data */
2966 if(user->version_reply) {
2967 free(user->version_reply);
2968 user->version_reply = NULL;
2969 }
2970
2971 /* clean up mark */
2972 if(user->mark) {
2973 free(user->mark);
2974 user->mark = NULL;
2975 }
2976
2977 /* clean up geoip data if any */
2978 if(user->country_code) free(user->country_code);
2979 if(user->city) free(user->city);
2980 if(user->region) free(user->region);
2981 if(user->postal_code) free(user->postal_code);
2982
2983 /* We don't free them, in case we try to privmsg them or something
2984 * (like when a stupid oper kills themself). We just put them onto
2985 * a list of clients that get freed after processing each line.
2986 */
2987 if (dead_users.size)
2988 userList_append(&dead_users, user);
2989 else
2990 free_user(user);
2991 }
2992
2993 static void call_oper_funcs(struct userNode *user);
2994
2995 void mod_usermode(struct userNode *user, const char *mode_change) {
2996 int add = 1;
2997 const char *word = mode_change;
2998
2999 if (!user || !mode_change)
3000 return;
3001
3002 call_user_mode_funcs(user, mode_change);
3003
3004 while (*word != ' ' && *word) word++;
3005 while (*word == ' ') word++;
3006 while (1) {
3007 #define do_user_mode(FLAG) do { if (add) user->modes |= FLAG; else user->modes &= ~FLAG; } while (0)
3008 switch (*mode_change++) {
3009 case 0: case ' ': return;
3010 case '+': add = 1; break;
3011 case '-': add = 0; break;
3012 case 'o':
3013 if (add) {
3014 if(!IsOper(user)) { /* Dont re-oper an oper */
3015 userList_append(&curr_opers, user);
3016 call_oper_funcs(user);
3017 }
3018 } else {
3019 userList_remove(&curr_opers, user);
3020 }
3021 do_user_mode(FLAGS_OPER);
3022 break;
3023 case 'O': do_user_mode(FLAGS_LOCOP); break;
3024 case 'i': do_user_mode(FLAGS_INVISIBLE);
3025 if (add)
3026 invis_clients++;
3027 else
3028 invis_clients--;
3029 break;
3030 case 'w': do_user_mode(FLAGS_WALLOP); break;
3031 case 's': do_user_mode(FLAGS_SERVNOTICE); break;
3032 case 'd': do_user_mode(FLAGS_DEAF); break;
3033 case 'k': do_user_mode(FLAGS_SERVICE); break;
3034 case 'g': do_user_mode(FLAGS_GLOBAL); break;
3035 // sethost - reed/apples
3036 // case 'h': do_user_mode(FLAGS_HELPER); break;
3037 // I check if there's an 'h' in the first part, and if there,
3038 // then everything after the space becomes their new host.
3039 case 'C': do_user_mode(FLAGS_CLOAKHOST);
3040 if (*word) {
3041 char cloakhost[MAXLEN];
3042 unsigned int ii;
3043 for (ii=0; (*word != ' ') && (*word != '\0'); )
3044 cloakhost[ii++] = *word++;
3045 cloakhost[ii] = 0;
3046 while (*word == ' ')
3047 word++;
3048 safestrncpy(user->crypthost, cloakhost, sizeof(user->crypthost));
3049 }
3050 break;
3051 case 'c': do_user_mode(FLAGS_CLOAKIP);
3052 if (*word) {
3053 char cloakip[MAXLEN];
3054 unsigned int ii;
3055 for (ii=0; (*word != ' ') && (*word != '\0'); )
3056 cloakip[ii++] = *word++;
3057 cloakip[ii] = 0;
3058 while (*word == ' ')
3059 word++;
3060 safestrncpy(user->cryptip, cloakip, sizeof(user->cryptip));
3061 }
3062 break;
3063 case 'h': do_user_mode(FLAGS_SETHOST);
3064 if (*word) {
3065 char sethost[MAXLEN];
3066 unsigned int ii;
3067 for (ii=0; (*word != ' ') && (*word != '\0'); )
3068 sethost[ii++] = *word++;
3069 sethost[ii] = 0;
3070 while (*word == ' ')
3071 word++;
3072 safestrncpy(user->sethost, sethost, sizeof(user->sethost));
3073 }
3074 break;
3075 case 'x': do_user_mode(FLAGS_HIDDEN_HOST); break;
3076 case 'r':
3077 if (*word) {
3078 char tag[MAXLEN];
3079 unsigned int ii;
3080 for (ii=0; (*word != ' ') && (*word != '\0'); )
3081 tag[ii++] = *word++;
3082 tag[ii] = 0;
3083 while (*word == ' ')
3084 word++;
3085 call_account_func(user, tag);
3086 }
3087 break;
3088 case 'f':
3089 if (*word) {
3090 char host[MAXLEN];
3091 unsigned int ii;
3092 for (ii=0; (*word != ' ') && (*word != '\0'); )
3093 host[ii++] = *word++;
3094 host[ii] = 0;
3095 while (*word == ' ')
3096 word++;
3097 assign_fakehost(user, host, 0);
3098 }
3099 break;
3100 }
3101 #undef do_user_mode
3102 }
3103 }
3104
3105 struct mod_chanmode *
3106 mod_chanmode_parse(struct chanNode *channel, char **modes, unsigned int argc, unsigned int flags, short base_oplevel)
3107 {
3108 struct mod_chanmode *change;
3109 unsigned int ii, in_arg, ch_arg, add;
3110
3111 if (argc == 0)
3112 return NULL;
3113 if (!(change = mod_chanmode_alloc(argc - 1)))
3114 return NULL;
3115
3116 for (ii = ch_arg = 0, in_arg = add = 1;
3117 (modes[0][ii] != '\0') && (modes[0][ii] != ' ');
3118 ++ii) {
3119 switch (modes[0][ii]) {
3120 case '+':
3121 add = 1;
3122 break;
3123 case '-':
3124 add = 0;
3125 break;
3126 #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)
3127 case 'C': do_chan_mode(MODE_NOCTCPS); break;
3128 case 'D': do_chan_mode(MODE_DELAYJOINS); break;
3129 case 'c': do_chan_mode(MODE_NOCOLORS); break;
3130 case 'i': do_chan_mode(MODE_INVITEONLY); break;
3131 case 'm': do_chan_mode(MODE_MODERATED); break;
3132 case 'n': do_chan_mode(MODE_NOPRIVMSGS); break;
3133 case 'p': do_chan_mode(MODE_PRIVATE); break;
3134 case 'r': do_chan_mode(MODE_REGONLY); break;
3135 case 's': do_chan_mode(MODE_SECRET); break;
3136 case 't': do_chan_mode(MODE_TOPICLIMIT); break;
3137 case 'S': do_chan_mode(MODE_STRIPCOLOR); break;
3138 case 'M': do_chan_mode(MODE_MODUNREG); break;
3139 case 'N': do_chan_mode(MODE_NONOTICE); break;
3140 case 'Q': do_chan_mode(MODE_NOQUITMSGS); break;
3141 case 'T': do_chan_mode(MODE_NOAMSG); break;
3142 case 'O': do_chan_mode(MODE_OPERSONLY); break;
3143 case 'Z': do_chan_mode(MODE_SSLONLY); break;
3144 case 'L': do_chan_mode(MODE_HIDEMODE); break;
3145 case 'z':
3146 if (!(flags & MCP_REGISTERED)) {
3147 do_chan_mode(MODE_REGISTERED);
3148 } else {
3149 mod_chanmode_free(change);
3150 return NULL;
3151 }
3152 break;
3153 #undef do_chan_mode
3154 case 'l':
3155 if (add) {
3156 if (in_arg >= argc)
3157 goto error;
3158 change->modes_set |= MODE_LIMIT;
3159 change->new_limit = atoi(modes[in_arg++]);
3160 } else {
3161 change->modes_set &= ~MODE_LIMIT;
3162 change->modes_clear |= MODE_LIMIT;
3163 }
3164 break;
3165 case 'k':
3166 if (add) {
3167 if (in_arg >= argc)
3168 goto error;
3169 change->modes_set |= MODE_KEY;
3170 safestrncpy(change->new_key, modes[in_arg++], sizeof(change->new_key));
3171 } else {
3172 change->modes_clear |= MODE_KEY;
3173 if (!(flags & MCP_KEY_FREE)) {
3174 if (in_arg >= argc)
3175 goto error;
3176 in_arg++;
3177 }
3178 }
3179 break;
3180 case 'U':
3181 if (add)
3182 {
3183 if (in_arg >= argc)
3184 goto error;
3185 change->modes_set |= MODE_UPASS;
3186 safestrncpy(change->new_upass, modes[in_arg++], sizeof(change->new_upass));
3187 } else {
3188 change->modes_clear |= MODE_UPASS;
3189 if (!(flags & MCP_UPASS_FREE)) {
3190 if (in_arg >= argc)
3191 goto error;
3192 in_arg++;
3193 }
3194 }
3195 break;
3196 case 'A':
3197 if (add) {
3198 if (in_arg >= argc)
3199 goto error;
3200 change->modes_set |= MODE_APASS;
3201 safestrncpy(change->new_apass, modes[in_arg++], sizeof(change->new_apass));
3202 } else {
3203 change->modes_clear |= MODE_APASS;
3204 if (!(flags & MCP_APASS_FREE)) {
3205 if (in_arg >= argc)
3206 goto error;
3207 in_arg++;
3208 }
3209 }
3210 break;
3211 case 'b':
3212 if (!(flags & MCP_ALLOW_OVB))
3213 goto error;
3214 if (in_arg >= argc)
3215 goto error;
3216 change->args[ch_arg].mode = MODE_BAN;
3217 if (!add)
3218 change->args[ch_arg].mode |= MODE_REMOVE;
3219 change->args[ch_arg++].u.hostmask = modes[in_arg++];
3220 break;
3221 case 'e':
3222 if (!(flags & MCP_ALLOW_OVB))
3223 goto error;
3224 if (in_arg >= argc)
3225 goto error;
3226 change->args[ch_arg].mode = MODE_EXEMPT;
3227 if (!add)
3228 change->args[ch_arg].mode |= MODE_REMOVE;
3229 change->args[ch_arg++].u.hostmask = modes[in_arg++];
3230 break;
3231 case 'o': case 'h': case 'v':
3232 {
3233 struct userNode *victim;
3234 char *oplevel_str;
3235 int oplevel;
3236
3237 if (in_arg >= argc)
3238 goto error;
3239 oplevel_str = strchr(modes[in_arg], ':');
3240 if (oplevel_str)
3241 {
3242 /* XXYYY M #channel +o XXYYY:<oplevel> */
3243 *oplevel_str++ = '\0';
3244 oplevel = parse_oplevel(oplevel_str);
3245 if (oplevel <= base_oplevel && !(flags & MCP_FROM_SERVER))
3246 oplevel = base_oplevel + 1;
3247 }
3248 else if (channel->modes & MODE_UPASS)
3249 oplevel = base_oplevel + 1;
3250 else
3251 oplevel = -1;
3252
3253 /* Check that oplevel is within bounds. */
3254 if (oplevel > MAXOPLEVEL)
3255 oplevel = MAXOPLEVEL;
3256
3257 if (!(flags & MCP_ALLOW_OVB))
3258 goto error;
3259 if (in_arg >= argc)
3260 goto error;
3261
3262 if (modes[0][ii] == 'o')
3263 change->args[ch_arg].mode = MODE_CHANOP;
3264 else if (modes[0][ii] == 'h')
3265 change->args[ch_arg].mode = MODE_HALFOP;
3266 else if (modes[0][ii] == 'v')
3267 change->args[ch_arg].mode = MODE_VOICE;
3268
3269 if (!add)
3270 change->args[ch_arg].mode |= MODE_REMOVE;
3271 if (flags & MCP_FROM_SERVER)
3272 victim = GetUserN(modes[in_arg++]);
3273 else
3274 victim = GetUserH(modes[in_arg++]);
3275 if (!victim)
3276 continue;
3277 if ((change->args[ch_arg].u.member = GetUserMode(channel, victim)))
3278 {
3279 /* Apply the oplevel change */
3280 change->args[ch_arg].u.member->oplevel = oplevel;
3281 ch_arg++;
3282 }
3283 break;
3284 }
3285 default:
3286 if (!(flags & MCP_FROM_SERVER))
3287 goto error;
3288 break;
3289 }
3290 }
3291 change->argc = ch_arg; /* in case any turned out to be ignored */
3292 if (change->modes_set & MODE_SECRET) {
3293 change->modes_set &= ~(MODE_PRIVATE);
3294 change->modes_clear |= MODE_PRIVATE;
3295 } else if (change->modes_set & MODE_PRIVATE) {
3296 change->modes_set &= ~(MODE_SECRET);
3297 change->modes_clear |= MODE_SECRET;
3298 }
3299 return change;
3300 error:
3301 mod_chanmode_free(change);
3302 return NULL;
3303 }
3304
3305 struct chanmode_buffer {
3306 char modes[MAXLEN];
3307 char args[MAXLEN];
3308 struct chanNode *channel;
3309 struct userNode *actor;
3310 unsigned int modes_used;
3311 unsigned int args_used;
3312 size_t chname_len;
3313 unsigned int is_add : 1;
3314 unsigned int is_chanop : 1;
3315 };
3316
3317 static void
3318 mod_chanmode_append(struct chanmode_buffer *buf, char ch, const char *arg)
3319 {
3320 size_t arg_len = strlen(arg);
3321 if (buf->modes_used > (MAXMODEPARAMS) ||
3322 buf->modes_used + buf->args_used + buf->chname_len + arg_len > 450) {
3323 memcpy(buf->modes + buf->modes_used, buf->args, buf->args_used);
3324 buf->modes[buf->modes_used + buf->args_used] = '\0';
3325 irc_mode((buf->is_chanop ? buf->actor : NULL), buf->channel, buf->modes);
3326 buf->modes[0] = buf->is_add ? '+' : '-';
3327 buf->modes_used = 1;
3328 buf->args_used = 0;
3329 }
3330 buf->modes[buf->modes_used++] = ch;
3331 buf->args[buf->args_used++] = ' ';
3332 memcpy(buf->args + buf->args_used, arg, arg_len);
3333 buf->args_used += arg_len;
3334 }
3335
3336 void
3337 mod_chanmode_announce(struct userNode *who, struct chanNode *channel, struct mod_chanmode *change)
3338 {
3339 struct chanmode_buffer chbuf;
3340 unsigned int arg;
3341 struct modeNode *mn;
3342 char int_buff[32], mode = '\0';
3343
3344 assert(change->argc <= change->alloc_argc);
3345 memset(&chbuf, 0, sizeof(chbuf));
3346 chbuf.channel = channel;
3347 chbuf.actor = who;
3348 chbuf.chname_len = strlen(channel->name);
3349
3350 mn = GetUserMode(channel, who);
3351 if ((mn && (mn->modes & MODE_CHANOP)) || off_channel)
3352 chbuf.is_chanop = 1;
3353
3354 /* First remove modes */
3355 chbuf.is_add = 0;
3356 if (change->modes_clear) {
3357 if (mode != '-')
3358 chbuf.modes[chbuf.modes_used++] = mode = '-';
3359 #define DO_MODE_CHAR(BIT, CHAR) if (change->modes_clear & MODE_##BIT) chbuf.modes[chbuf.modes_used++] = CHAR
3360 DO_MODE_CHAR(PRIVATE, 'p');
3361 DO_MODE_CHAR(SECRET, 's');
3362 DO_MODE_CHAR(MODERATED, 'm');
3363 DO_MODE_CHAR(TOPICLIMIT, 't');
3364 DO_MODE_CHAR(INVITEONLY, 'i');
3365 DO_MODE_CHAR(NOPRIVMSGS, 'n');
3366 DO_MODE_CHAR(LIMIT, 'l');
3367 DO_MODE_CHAR(DELAYJOINS, 'D');
3368 DO_MODE_CHAR(REGONLY, 'r');
3369 DO_MODE_CHAR(NOCOLORS, 'c');
3370 DO_MODE_CHAR(NOCTCPS, 'C');
3371 DO_MODE_CHAR(STRIPCOLOR, 'S');
3372 DO_MODE_CHAR(MODUNREG, 'M');
3373 DO_MODE_CHAR(NONOTICE, 'N');
3374 DO_MODE_CHAR(NOQUITMSGS, 'Q');
3375 DO_MODE_CHAR(NOAMSG, 'T');
3376 DO_MODE_CHAR(OPERSONLY, 'O');
3377 DO_MODE_CHAR(REGISTERED, 'z');
3378 DO_MODE_CHAR(SSLONLY, 'Z');
3379 DO_MODE_CHAR(HIDEMODE, 'L');
3380 #undef DO_MODE_CHAR
3381 if (change->modes_clear & channel->modes & MODE_KEY)
3382 mod_chanmode_append(&chbuf, 'k', channel->key);
3383 if (change->modes_clear & channel->modes & MODE_UPASS)
3384 mod_chanmode_append(&chbuf, 'U', channel->upass);
3385 if (change->modes_clear * channel->modes & MODE_APASS)
3386 mod_chanmode_append(&chbuf, 'A', channel->apass);
3387 }
3388 for (arg = 0; arg < change->argc; ++arg) {
3389 if (!(change->args[arg].mode & MODE_REMOVE))
3390 continue;
3391 if (mode != '-')
3392 chbuf.modes[chbuf.modes_used++] = mode = '-';
3393 switch (change->args[arg].mode & ~MODE_REMOVE) {
3394 case MODE_BAN:
3395 mod_chanmode_append(&chbuf, 'b', change->args[arg].u.hostmask);
3396 break;
3397 case MODE_EXEMPT:
3398 mod_chanmode_append(&chbuf, 'e', change->args[arg].u.hostmask);
3399 break;
3400 default:
3401 if (change->args[arg].mode & MODE_CHANOP)
3402 mod_chanmode_append(&chbuf, 'o', change->args[arg].u.member->user->numeric);
3403 if (change->args[arg].mode & MODE_HALFOP)
3404 mod_chanmode_append(&chbuf, 'h', change->args[arg].u.member->user->numeric);
3405 if (change->args[arg].mode & MODE_VOICE)
3406 mod_chanmode_append(&chbuf, 'v', change->args[arg].u.member->user->numeric);
3407 break;
3408 }
3409 }
3410
3411 /* Then set them */
3412 chbuf.is_add = 1;
3413 if (change->modes_set) {
3414 if (mode != '+')
3415 chbuf.modes[chbuf.modes_used++] = mode = '+';
3416 #define DO_MODE_CHAR(BIT, CHAR) if (change->modes_set & MODE_##BIT) chbuf.modes[chbuf.modes_used++] = CHAR
3417 DO_MODE_CHAR(PRIVATE, 'p');
3418 DO_MODE_CHAR(SECRET, 's');
3419 DO_MODE_CHAR(MODERATED, 'm');
3420 DO_MODE_CHAR(TOPICLIMIT, 't');
3421 DO_MODE_CHAR(INVITEONLY, 'i');
3422 DO_MODE_CHAR(NOPRIVMSGS, 'n');
3423 DO_MODE_CHAR(DELAYJOINS, 'D');
3424 DO_MODE_CHAR(REGONLY, 'r');
3425 DO_MODE_CHAR(NOCOLORS, 'c');
3426 DO_MODE_CHAR(NOCTCPS, 'C');
3427 DO_MODE_CHAR(STRIPCOLOR, 'S');
3428 DO_MODE_CHAR(MODUNREG, 'M');
3429 DO_MODE_CHAR(NONOTICE, 'N');
3430 DO_MODE_CHAR(NOQUITMSGS, 'Q');
3431 DO_MODE_CHAR(NOAMSG, 'T');
3432 DO_MODE_CHAR(OPERSONLY, 'O');
3433 DO_MODE_CHAR(REGISTERED, 'z');
3434 DO_MODE_CHAR(SSLONLY, 'Z');
3435 DO_MODE_CHAR(HIDEMODE, 'L');
3436 #undef DO_MODE_CHAR
3437 if(change->modes_set & MODE_KEY)
3438 mod_chanmode_append(&chbuf, 'k', change->new_key);
3439 if (change->modes_set & MODE_UPASS)
3440 mod_chanmode_append(&chbuf, 'U', change->new_upass);
3441 if (change->modes_set & MODE_APASS)
3442 mod_chanmode_append(&chbuf, 'A', change->new_apass);
3443 if(change->modes_set & MODE_LIMIT) {
3444 sprintf(int_buff, "%d", change->new_limit);
3445 mod_chanmode_append(&chbuf, 'l', int_buff);
3446 }
3447 }
3448 for (arg = 0; arg < change->argc; ++arg) {
3449 if (change->args[arg].mode & MODE_REMOVE)
3450 continue;
3451 if (mode != '+')
3452 chbuf.modes[chbuf.modes_used++] = mode = '+';
3453 switch (change->args[arg].mode) {
3454 case MODE_BAN:
3455 mod_chanmode_append(&chbuf, 'b', change->args[arg].u.hostmask);
3456 break;
3457 case MODE_EXEMPT:
3458 mod_chanmode_append(&chbuf, 'e', change->args[arg].u.hostmask);
3459 break;
3460 default:
3461 if (change->args[arg].mode & MODE_CHANOP)
3462 mod_chanmode_append(&chbuf, 'o', change->args[arg].u.member->user->numeric);
3463 if (change->args[arg].mode & MODE_HALFOP)
3464 mod_chanmode_append(&chbuf, 'h', change->args[arg].u.member->user->numeric);
3465 if (change->args[arg].mode & MODE_VOICE)
3466 mod_chanmode_append(&chbuf, 'v', change->args[arg].u.member->user->numeric);
3467 break;
3468 }
3469 }
3470
3471 /* Flush the buffer and apply changes locally */
3472 if (chbuf.modes_used > 0) {
3473 memcpy(chbuf.modes + chbuf.modes_used, chbuf.args, chbuf.args_used);
3474 chbuf.modes[chbuf.modes_used + chbuf.args_used] = '\0';
3475 irc_mode((chbuf.is_chanop ? chbuf.actor : NULL), chbuf.channel, chbuf.modes);
3476 }
3477 mod_chanmode_apply(who, channel, change);
3478 }
3479
3480 char *
3481 mod_chanmode_format(struct mod_chanmode *change, char *outbuff)
3482 {
3483 unsigned int used = 0;
3484 assert(change->argc <= change->alloc_argc);
3485 if (change->modes_clear) {
3486 outbuff[used++] = '-';
3487 #define DO_MODE_CHAR(BIT, CHAR) if (change->modes_clear & MODE_##BIT) outbuff[used++] = CHAR
3488 DO_MODE_CHAR(PRIVATE, 'p');
3489 DO_MODE_CHAR(SECRET, 's');
3490 DO_MODE_CHAR(MODERATED, 'm');
3491 DO_MODE_CHAR(TOPICLIMIT, 't');
3492 DO_MODE_CHAR(INVITEONLY, 'i');
3493 DO_MODE_CHAR(NOPRIVMSGS, 'n');
3494 DO_MODE_CHAR(LIMIT, 'l');
3495 DO_MODE_CHAR(KEY, 'k');
3496 DO_MODE_CHAR(UPASS, 'U');
3497 DO_MODE_CHAR(APASS, 'A');
3498 DO_MODE_CHAR(DELAYJOINS, 'D');
3499 DO_MODE_CHAR(REGONLY, 'r');
3500 DO_MODE_CHAR(NOCOLORS, 'c');
3501 DO_MODE_CHAR(NOCTCPS, 'C');
3502 DO_MODE_CHAR(STRIPCOLOR, 'S');
3503 DO_MODE_CHAR(MODUNREG, 'M');
3504 DO_MODE_CHAR(NONOTICE, 'N');
3505 DO_MODE_CHAR(NOQUITMSGS, 'Q');
3506 DO_MODE_CHAR(NOAMSG, 'T');
3507 DO_MODE_CHAR(OPERSONLY, 'O');
3508 DO_MODE_CHAR(REGISTERED, 'z');
3509 DO_MODE_CHAR(SSLONLY, 'Z');
3510 DO_MODE_CHAR(HIDEMODE, 'L');
3511 #undef DO_MODE_CHAR
3512 }
3513 if (change->modes_set) {
3514 outbuff[used++] = '+';
3515 #define DO_MODE_CHAR(BIT, CHAR) if (change->modes_set & MODE_##BIT) outbuff[used++] = CHAR
3516 DO_MODE_CHAR(PRIVATE, 'p');
3517 DO_MODE_CHAR(SECRET, 's');
3518 DO_MODE_CHAR(MODERATED, 'm');
3519 DO_MODE_CHAR(TOPICLIMIT, 't');
3520 DO_MODE_CHAR(INVITEONLY, 'i');
3521 DO_MODE_CHAR(NOPRIVMSGS, 'n');
3522 DO_MODE_CHAR(DELAYJOINS, 'D');
3523 DO_MODE_CHAR(REGONLY, 'r');
3524 DO_MODE_CHAR(NOCOLORS, 'c');
3525 DO_MODE_CHAR(NOCTCPS, 'C');
3526 DO_MODE_CHAR(STRIPCOLOR, 'S');
3527 DO_MODE_CHAR(MODUNREG, 'M');
3528 DO_MODE_CHAR(NONOTICE, 'N');
3529 DO_MODE_CHAR(NOQUITMSGS, 'Q');
3530 DO_MODE_CHAR(NOAMSG, 'T');
3531 DO_MODE_CHAR(OPERSONLY, 'O');
3532 DO_MODE_CHAR(REGISTERED, 'z');
3533 DO_MODE_CHAR(SSLONLY, 'Z');
3534 DO_MODE_CHAR(HIDEMODE, 'L');
3535 #undef DO_MODE_CHAR
3536 switch (change->modes_set & (MODE_KEY|MODE_LIMIT|MODE_APASS|MODE_UPASS)) {
3537 /* Doing this implementation has been a pain in the arse, I hope I didn't forget a possible combination */
3538 case MODE_KEY|MODE_LIMIT|MODE_APASS|MODE_UPASS:
3539 used += sprintf(outbuff+used, "lkAU %d %s %s %s", change->new_limit, change->new_key,change->new_apass, change->new_upass);
3540 break;
3541
3542 case MODE_KEY|MODE_LIMIT|MODE_APASS:
3543 used += sprintf(outbuff+used, "lkA %d %s %s", change->new_limit, change->new_key, change->new_apass);
3544 break;
3545 case MODE_KEY|MODE_LIMIT|MODE_UPASS:
3546 used += sprintf(outbuff+used, "lkU %d %s %s", change->new_limit, change->new_key, change->new_upass);
3547 break;
3548 case MODE_KEY|MODE_APASS|MODE_UPASS:
3549 used += sprintf(outbuff+used, "kAU %s %s %s", change->new_key, change->new_apass, change->new_upass);
3550 break;
3551
3552 case MODE_KEY|MODE_APASS:
3553 used += sprintf(outbuff+used, "kA %s %s", change->new_key, change->new_apass);
3554 break;
3555 case MODE_KEY|MODE_UPASS:
3556 used += sprintf(outbuff+used, "kU %s %s", change->new_key, change->new_upass);
3557 break;
3558 case MODE_KEY|MODE_LIMIT:
3559 used += sprintf(outbuff+used, "lk %d %s", change->new_limit, change->new_key);
3560 break;
3561
3562 case MODE_LIMIT|MODE_UPASS:
3563 used += sprintf(outbuff+used, "lU %d %s", change->new_limit, change->new_upass);
3564 break;
3565 case MODE_LIMIT|MODE_APASS:
3566 used += sprintf(outbuff+used, "lA %d %s", change->new_limit, change->new_apass);
3567 break;
3568 case MODE_APASS|MODE_UPASS:
3569 used += sprintf(outbuff+used, "AU %s %s", change->new_apass, change->new_upass);
3570
3571 case MODE_LIMIT|MODE_APASS|MODE_UPASS:
3572 used += sprintf(outbuff+used, "lAU %d %s %s", change->new_limit, change->new_apass, change->new_upass);
3573 break;
3574
3575 case MODE_APASS:
3576 used += sprintf(outbuff+used, "A %s", change->new_apass);
3577 break;
3578 case MODE_UPASS:
3579 used += sprintf(outbuff+used, "U %s", change->new_upass);
3580 break;
3581 case MODE_KEY:
3582 used += sprintf(outbuff+used, "k %s", change->new_key);
3583 break;
3584 case MODE_LIMIT:
3585 used += sprintf(outbuff+used, "l %d", change->new_limit);
3586 break;
3587 }
3588 }
3589 outbuff[used] = 0;
3590 return outbuff;
3591 }
3592
3593 static int
3594 clear_chanmode(struct chanNode *channel, const char *modes)
3595 {
3596 unsigned int remove;
3597
3598 for (remove = 0; *modes; modes++) {
3599 switch (*modes) {
3600 case 'o': remove |= MODE_CHANOP; break;
3601 case 'h': remove |= MODE_HALFOP; break;
3602 case 'v': remove |= MODE_VOICE; break;
3603 case 'p': remove |= MODE_PRIVATE; break;
3604 case 's': remove |= MODE_SECRET; break;
3605 case 'm': remove |= MODE_MODERATED; break;
3606 case 't': remove |= MODE_TOPICLIMIT; break;
3607 case 'i': remove |= MODE_INVITEONLY; break;
3608 case 'n': remove |= MODE_NOPRIVMSGS; break;
3609 case 'k':
3610 remove |= MODE_KEY;
3611 channel->key[0] = '\0';
3612 break;
3613 case 'A':
3614 remove |= MODE_APASS;
3615 channel->apass[0] = '\0';
3616 break;
3617 case 'U':
3618 remove |= MODE_UPASS;
3619 channel->upass[0] = '\0';
3620 break;
3621 case 'l':
3622 remove |= MODE_LIMIT;
3623 channel->limit = 0;
3624 break;
3625 case 'b': remove |= MODE_BAN; break;
3626 case 'e': remove |= MODE_EXEMPT; break;
3627 case 'D': remove |= MODE_DELAYJOINS; break;
3628 case 'r': remove |= MODE_REGONLY; break;
3629 case 'c': remove |= MODE_NOCOLORS; break;
3630 case 'C': remove |= MODE_NOCTCPS; break;
3631 case 'S': remove |= MODE_STRIPCOLOR; break;
3632 case 'M': remove |= MODE_MODUNREG; break;
3633 case 'N': remove |= MODE_NONOTICE; break;
3634 case 'Q': remove |= MODE_NOQUITMSGS; break;
3635 case 'T': remove |= MODE_NOAMSG; break;
3636 case 'O': remove |= MODE_OPERSONLY; break;
3637 case 'z': remove |= MODE_REGISTERED; break;
3638 case 'Z': remove |= MODE_SSLONLY; break;
3639 case 'L': remove |= MODE_HIDEMODE; break;
3640 }
3641 }
3642
3643 if (!remove)
3644 return 1;
3645
3646 /* Remove simple modes. */
3647 channel->modes &= ~remove;
3648
3649 /* If removing bans, kill 'em all. */
3650 if ((remove & MODE_BAN) && channel->banlist.used) {
3651 unsigned int i;
3652 for (i=0; i<channel->banlist.used; i++)
3653 free(channel->banlist.list[i]);
3654 channel->banlist.used = 0;
3655 }
3656
3657 /* If removing exempts, kill 'em all. */
3658 if ((remove & MODE_EXEMPT) && channel->exemptlist.used) {
3659 unsigned int i;
3660 for (i=0; i<channel->exemptlist.used; i++)
3661 free(channel->exemptlist.list[i]);
3662 channel->exemptlist.used = 0;
3663 }
3664
3665 /* Remove member modes. */
3666 if ((remove & (MODE_CHANOP | MODE_HALFOP | MODE_VOICE)) && channel->members.used) {
3667 int mask = ~(remove & (MODE_CHANOP | MODE_HALFOP | MODE_VOICE));
3668 unsigned int i;
3669
3670 for (i = 0; i < channel->members.used; i++)
3671 channel->members.list[i]->modes &= mask;
3672 }
3673
3674 return 1;
3675 }
3676
3677 void
3678 reg_privmsg_func(struct userNode *user, privmsg_func_t handler)
3679 {
3680 unsigned int numeric = user->num_local;
3681 if (numeric >= num_privmsg_funcs) {
3682 int newnum = numeric + 8;
3683 privmsg_funcs = realloc(privmsg_funcs, newnum*sizeof(privmsg_func_t));
3684 memset(privmsg_funcs+num_privmsg_funcs, 0, (newnum-num_privmsg_funcs)*sizeof(privmsg_func_t));
3685 num_privmsg_funcs = newnum;
3686 }
3687 if (privmsg_funcs[numeric])
3688 log_module(MAIN_LOG, LOG_WARNING, "re-registering new privmsg handler for numeric %d", numeric);
3689 privmsg_funcs[numeric] = handler;
3690 }
3691
3692 void
3693 unreg_privmsg_func(struct userNode *user, privmsg_func_t handler)
3694 {
3695 unsigned int x;
3696
3697 if (!user || handler)
3698 return; /* this really only works with users */
3699
3700 memset(privmsg_funcs+user->num_local, 0, sizeof(privmsg_func_t));
3701
3702 for (x = user->num_local+1; x < num_privmsg_funcs; x++)
3703 memmove(privmsg_funcs+x-1, privmsg_funcs+x, sizeof(privmsg_func_t));
3704
3705 privmsg_funcs = realloc(privmsg_funcs, num_privmsg_funcs*sizeof(privmsg_func_t));
3706 num_privmsg_funcs--;
3707 }
3708
3709
3710 void
3711 reg_notice_func(struct userNode *user, privmsg_func_t handler)
3712 {
3713 unsigned int numeric = user->num_local;
3714 if (numeric >= num_notice_funcs) {
3715 int newnum = numeric + 8;
3716 notice_funcs = realloc(notice_funcs, newnum*sizeof(privmsg_func_t));
3717 memset(notice_funcs+num_notice_funcs, 0, (newnum-num_notice_funcs)*sizeof(privmsg_func_t));
3718 num_notice_funcs = newnum;
3719 }
3720 if (notice_funcs[numeric])
3721 log_module(MAIN_LOG, LOG_WARNING, "re-registering new notice handler for numeric %d", numeric);
3722 notice_funcs[numeric] = handler;
3723 }
3724
3725 void
3726 unreg_notice_func(struct userNode *user, privmsg_func_t handler)
3727 {
3728 unsigned int x;
3729
3730 if (!user || handler)
3731 return; /* this really only works with users */
3732
3733 memset(notice_funcs+user->num_local, 0, sizeof(privmsg_func_t));
3734
3735 for (x = user->num_local+1; x < num_notice_funcs; x++)
3736 memmove(notice_funcs+x-1, notice_funcs+x, sizeof(privmsg_func_t));
3737
3738 memset(notice_funcs+user->num_local, 0, sizeof(privmsg_func_t));
3739 notice_funcs = realloc(notice_funcs, num_notice_funcs*sizeof(privmsg_func_t));
3740 num_notice_funcs--;
3741 }
3742
3743 void
3744 reg_oper_func(oper_func_t handler)
3745 {
3746 if (of_used == of_size) {
3747 if (of_size) {
3748 of_size <<= 1;
3749 of_list = realloc(of_list, of_size*sizeof(oper_func_t));
3750 } else {
3751 of_size = 8;
3752 of_list = malloc(of_size*sizeof(oper_func_t));
3753 }
3754 }
3755 of_list[of_used++] = handler;
3756 }
3757
3758 static void
3759 call_oper_funcs(struct userNode *user)
3760 {
3761 unsigned int n;
3762 if (IsLocal(user))
3763 return;
3764 for (n=0; n<of_used; n++)
3765 of_list[n](user);
3766 }
3767
3768 static void
3769 send_burst(void)
3770 {
3771 unsigned int i, hop, max_hop=1;
3772 dict_iterator_t it;
3773
3774 /* burst (juped) servers, closest first (except self, which is sent already) */
3775 for (i=0; i<ArrayLength(servers_num); i++)
3776 if (servers_num[i] && servers_num[i]->hops > max_hop)
3777 max_hop = servers_num[i]->hops;
3778 for (hop=1; hop<=max_hop; hop++) {
3779 for (i=0;i<ArrayLength(servers_num);i++) {
3780 if (servers_num[i]
3781 && (servers_num[i]->hops == hop)
3782 && (servers_num[i] != self->uplink))
3783 irc_server(servers_num[i]);
3784 }
3785 }
3786
3787 /* burst local nicks */
3788 for (i=0; i<=self->num_mask; i++)
3789 if (self->users[i])
3790 irc_user(self->users[i]);
3791
3792 /* build dict of unbursted channel names (just copy existing channels) */
3793 unbursted_channels = dict_new();
3794 for (it = dict_first(channels); it; it = iter_next(it))
3795 dict_insert(unbursted_channels, iter_key(it), iter_data(it));
3796 }
3797
3798 /*
3799 * Oplevel parsing
3800 */
3801 static int
3802 parse_oplevel(char *str)
3803 {
3804 int oplevel = 0;
3805 while (isdigit(*str))
3806 oplevel = oplevel * 10 + *str++ - '0';
3807 return oplevel;
3808 }
3809