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