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