]>
Commit | Line | Data |
---|---|---|
1 | /* Functions for manipulating local users on channels */ | |
2 | ||
3 | #include "localuser.h" | |
4 | #include "localuserchannel.h" | |
5 | #include "../nick/nick.h" | |
6 | #include "../channel/channel.h" | |
7 | #include "../irc/irc.h" | |
8 | #include "../lib/version.h" | |
9 | #include "../lib/sstring.h" | |
10 | ||
11 | #include <stdarg.h> | |
12 | #include <stdio.h> | |
13 | #include <assert.h> | |
14 | #include <string.h> | |
15 | #include <stdint.h> | |
16 | ||
17 | MODULE_VERSION(""); | |
18 | ||
19 | typedef struct pendingkick { | |
20 | nick *source, *target; | |
21 | channel *chan; | |
22 | sstring *reason; | |
23 | struct pendingkick *next; | |
24 | } pendingkick; | |
25 | ||
26 | pendingkick *pendingkicklist; | |
27 | ||
28 | int handlechannelmsgcmd(void *source, int cargc, char **cargv); | |
29 | int handlechannelnoticecmd(void *source, int cargc, char **cargv); | |
30 | int handleinvitecmd(void *source, int cargc, char **cargv); | |
31 | void clearpendingkicks(int hooknum, void *arg); | |
32 | void checkpendingkicknicks(int hooknum, void *arg); | |
33 | void checkpendingkickchannels(int hooknum, void *arg); | |
34 | void _localkickuser(nick *np, channel *cp, nick *target, const char *message); | |
35 | void luc_handlekick(int hooknum, void *arg); | |
36 | ||
37 | void _init() { | |
38 | pendingkicklist=NULL; | |
39 | registerserverhandler("P",&handlechannelmsgcmd,2); | |
40 | registerserverhandler("O",&handlechannelnoticecmd,2); | |
41 | registerserverhandler("I",&handleinvitecmd,2); | |
42 | registerhook(HOOK_CHANNEL_KICK, luc_handlekick); | |
43 | registerhook(HOOK_NICK_LOSTNICK, checkpendingkicknicks); | |
44 | registerhook(HOOK_CHANNEL_LOSTCHANNEL, checkpendingkickchannels); | |
45 | registerhook(HOOK_CORE_ENDOFHOOKSQUEUE,&clearpendingkicks); | |
46 | } | |
47 | ||
48 | void _fini() { | |
49 | pendingkick *pk; | |
50 | ||
51 | deregisterserverhandler("P",&handlechannelmsgcmd); | |
52 | deregisterserverhandler("O",&handlechannelnoticecmd); | |
53 | deregisterserverhandler("I",&handleinvitecmd); | |
54 | deregisterhook(HOOK_CHANNEL_KICK, luc_handlekick); | |
55 | deregisterhook(HOOK_NICK_LOSTNICK, checkpendingkicknicks); | |
56 | deregisterhook(HOOK_CHANNEL_LOSTCHANNEL, checkpendingkickchannels); | |
57 | deregisterhook(HOOK_CORE_ENDOFHOOKSQUEUE,&clearpendingkicks); | |
58 | ||
59 | for (pk=pendingkicklist;pk;pk=pendingkicklist) { | |
60 | pendingkicklist = pk->next; | |
61 | freesstring(pk->reason); | |
62 | free(pk); | |
63 | } | |
64 | } | |
65 | ||
66 | void luc_handlekick(int hooknum, void *arg) { | |
67 | void **args=arg; | |
68 | nick *target=args[1]; | |
69 | void *myargs[3]; | |
70 | ||
71 | if (homeserver(target->numeric)!=mylongnum) | |
72 | return; | |
73 | ||
74 | if (umhandlers[target->numeric & MAXLOCALUSER]) { | |
75 | myargs[0]=args[2]; | |
76 | myargs[1]=args[0]; | |
77 | myargs[2]=args[3]; | |
78 | ||
79 | (umhandlers[target->numeric & MAXLOCALUSER])(target, LU_KICKED, myargs); | |
80 | } | |
81 | } | |
82 | ||
83 | /* invites look something like: | |
84 | * XXyyy I TargetNick :#channel | |
85 | */ | |
86 | ||
87 | int handleinvitecmd(void *source, int cargc, char **cargv) { | |
88 | void *nargs[2]; | |
89 | nick *sender; | |
90 | channel *cp; | |
91 | nick *target; | |
92 | ||
93 | if (cargc<2) { | |
94 | return CMD_OK; | |
95 | } | |
96 | ||
97 | if (!(sender=getnickbynumericstr(source))) { | |
98 | Error("localuserchannel",ERR_WARNING,"Got invite from unknown numeric %s.",(char *)source); | |
99 | return CMD_OK; | |
100 | } | |
101 | ||
102 | if (!(target=getnickbynick(cargv[0]))) { | |
103 | Error("localuserchannel",ERR_WARNING,"Got invite for unknown local user %s.",cargv[0]); | |
104 | return CMD_OK; | |
105 | } | |
106 | ||
107 | if (!(cp=findchannel(cargv[1]))) { | |
108 | Error("localuserchannel",ERR_WARNING,"Got invite for non-existent channel %s.",cargv[1]); | |
109 | return CMD_OK; | |
110 | } | |
111 | ||
112 | if (homeserver(target->numeric) != mylongnum) { | |
113 | Error("localuserchannel",ERR_WARNING,"Got invite for non-local user %s.",target->nick); | |
114 | return CMD_OK; | |
115 | } | |
116 | ||
117 | /* This is a valid race condition.. */ | |
118 | if (getnumerichandlefromchanhash(cp->users, target->numeric)) { | |
119 | Error("localuserchannel",ERR_DEBUG,"Got invite for user %s already on %s.",target->nick, cp->index->name->content); | |
120 | return CMD_OK; | |
121 | } | |
122 | ||
123 | nargs[0]=(void *)sender; | |
124 | nargs[1]=(void *)cp; | |
125 | ||
126 | if (umhandlers[target->numeric&MAXLOCALUSER]) { | |
127 | (umhandlers[target->numeric&MAXLOCALUSER])(target, LU_INVITE, nargs); | |
128 | } | |
129 | ||
130 | return CMD_OK; | |
131 | } | |
132 | ||
133 | /* PRIVMSG/NOTICE to channel handling is identical up to the point where the hook is called. */ | |
134 | static int handlechannelmsgornotice(void *source, int cargc, char **cargv, int isnotice) { | |
135 | void *nargs[3]; | |
136 | nick *sender; | |
137 | channel *target; | |
138 | nick *np; | |
139 | unsigned long numeric; | |
140 | int i; | |
141 | int found=0; | |
142 | ||
143 | if (cargc<2) { | |
144 | return CMD_OK; | |
145 | } | |
146 | ||
147 | if (cargv[0][0]!='#' && cargv[0][0]!='+') { | |
148 | /* Not a channel message */ | |
149 | return CMD_OK; | |
150 | } | |
151 | ||
152 | if ((sender=getnickbynumericstr((char *)source))==NULL) { | |
153 | Error("localuserchannel",ERR_DEBUG,"PRIVMSG/NOTICE from non existant user %s",(char *)source); | |
154 | return CMD_OK; | |
155 | } | |
156 | ||
157 | if ((target=findchannel(cargv[0]))==NULL) { | |
158 | Error("localuserchannel",ERR_DEBUG,"PRIVMSG/NOTICE to non existant channel %s",cargv[0]); | |
159 | return CMD_OK; | |
160 | } | |
161 | ||
162 | /* OK, we have a valid channel the message was sent to. Let's look to see | |
163 | * if we have any local users on there. Set up the arguments first as they are | |
164 | * always going to be the same. */ | |
165 | ||
166 | nargs[0]=(void *)sender; | |
167 | nargs[1]=(void *)target; | |
168 | nargs[2]=(void *)cargv[1]; | |
169 | ||
170 | for (found=0,i=0;i<target->users->hashsize;i++) { | |
171 | numeric=target->users->content[i]; | |
172 | if (numeric!=nouser && homeserver(numeric&CU_NUMERICMASK)==mylongnum) { | |
173 | /* OK, it's one of our users.. we need to deal with it */ | |
174 | if (found && ((getnickbynumericstr((char *)source)==NULL) || ((findchannel(cargv[0]))==NULL) || !(getnumerichandlefromchanhash(target->users, sender->numeric)))) { | |
175 | Error("localuserchannel", ERR_INFO, "Nick or channel lost, or user no longer on channel in LU_CHANMSG"); | |
176 | break; | |
177 | } | |
178 | found++; | |
179 | if (umhandlers[numeric&MAXLOCALUSER]) { | |
180 | if ((np=getnickbynumeric(numeric))==NULL) { | |
181 | Error("localuserchannel",ERR_ERROR,"PRIVMSG/NOTICE to channel user who doesn't exist (?) on %s",cargv[0]); | |
182 | continue; | |
183 | } | |
184 | if (!IsDeaf(np)) { | |
185 | (umhandlers[numeric&MAXLOCALUSER])(np,(isnotice?LU_CHANNOTICE:LU_CHANMSG),nargs); | |
186 | } else { | |
187 | found--; | |
188 | } | |
189 | } | |
190 | } | |
191 | } | |
192 | ||
193 | if (!found) { | |
194 | Error("localuserchannel",ERR_DEBUG,"Couldn't find any local targets for PRIVMSG/NOTICE to %s",cargv[0]); | |
195 | } | |
196 | ||
197 | return CMD_OK; | |
198 | } | |
199 | ||
200 | /* Wrapper functions to call the above code */ | |
201 | int handlechannelmsgcmd(void *source, int cargc, char **cargv) { | |
202 | return handlechannelmsgornotice(source, cargc, cargv, 0); | |
203 | } | |
204 | ||
205 | int handlechannelnoticecmd(void *source, int cargc, char **cargv) { | |
206 | return handlechannelmsgornotice(source, cargc, cargv, 1); | |
207 | } | |
208 | ||
209 | /* Burst onto channel. This replaces the timestamp and modes | |
210 | * with the provided ones. Keys and limits use the provided ones | |
211 | * if needed. nick * is optional, but joins the channel opped if | |
212 | * provided. | |
213 | * | |
214 | * Due to the way ircu works, this only works if the provided timestamp is | |
215 | * older than the one currently on the channel. If the timestamps are | |
216 | * equal, the modes are ignored, but the user (if any) is still allowed to | |
217 | * join with op. If the provided timestamp is newer than the exsting one we | |
218 | * just do a join instead - if you try to replace an old timestamp with a | |
219 | * newer one ircu will just laugh at you (and you will be desynced). | |
220 | */ | |
221 | int localburstontochannel(channel *cp, nick *np, time_t timestamp, flag_t modes, unsigned int limit, char *key) { | |
222 | unsigned int i; | |
223 | char extramodebuf[512]; | |
224 | char nickbuf[512]; | |
225 | ||
226 | if (cp==NULL) | |
227 | return 1; | |
228 | ||
229 | if (timestamp > cp->timestamp) { | |
230 | return localjoinchannel(np, cp); | |
231 | } | |
232 | ||
233 | if (timestamp < cp->timestamp) { | |
234 | cp->timestamp=timestamp; | |
235 | cp->flags=modes; | |
236 | ||
237 | /* deal with key - if we need one use the provided one if set, otherwise | |
238 | * the existing one, but if there is no existing one clear +k */ | |
239 | if (IsKey(cp)) { | |
240 | /* Sanitise the provided key - this might invalidate it.. */ | |
241 | if (key) | |
242 | clean_key(key); | |
243 | ||
244 | if (key && *key) { | |
245 | /* Free old key, if any */ | |
246 | if (cp->key) | |
247 | freesstring(cp->key); | |
248 | ||
249 | cp->key=getsstring(key,KEYLEN); | |
250 | } else { | |
251 | if (!cp->key) | |
252 | ClearKey(cp); | |
253 | } | |
254 | } else { | |
255 | /* Not +k - free the existing key, if any */ | |
256 | freesstring(cp->key); | |
257 | cp->key=NULL; | |
258 | } | |
259 | ||
260 | if (IsLimit(cp)) { | |
261 | if (limit) { | |
262 | cp->limit=limit; | |
263 | } else { | |
264 | if (!cp->limit) | |
265 | ClearLimit(cp); | |
266 | } | |
267 | } else { | |
268 | if (cp->limit) | |
269 | cp->limit=0; | |
270 | } | |
271 | ||
272 | /* We also need to blow away all other op/voice and bans on the | |
273 | * channel. This is the same code we use when someone else does | |
274 | * it to us. */ | |
275 | clearallbans(cp); | |
276 | for (i=0;i<cp->users->hashsize;i++) { | |
277 | if (cp->users->content[i]!=nouser) { | |
278 | cp->users->content[i]&=CU_NUMERICMASK; | |
279 | } | |
280 | } | |
281 | } | |
282 | ||
283 | /* Actually add the nick to the channel. Make sure it's a local nick and actually exists first. */ | |
284 | if (np && (homeserver(np->numeric) == mylongnum) && | |
285 | !(getnumerichandlefromchanhash(cp->users,np->numeric))) { | |
286 | addnicktochannel(cp,(np->numeric)|CUMODE_OP); | |
287 | } else { | |
288 | np=NULL; /* If we're not adding it here, don't send it later in the burst msg either */ | |
289 | } | |
290 | ||
291 | if (connected) { | |
292 | /* actual burst message */ | |
293 | if (np) { | |
294 | sprintf(nickbuf," %s:o", longtonumeric(np->numeric,5)); | |
295 | } else { | |
296 | nickbuf[0]='\0'; | |
297 | } | |
298 | ||
299 | if (IsLimit(cp)) { | |
300 | sprintf(extramodebuf," %d",cp->limit); | |
301 | } else { | |
302 | extramodebuf[0]='\0'; | |
303 | } | |
304 | ||
305 | /* XX B #channel <timestamp> +modes <limit> <key> <user> */ | |
306 | irc_send("%s B %s %lu %s%s%s%s%s", | |
307 | mynumeric->content,cp->index->name->content,cp->timestamp, | |
308 | printflags(cp->flags,cmodeflags),extramodebuf, | |
309 | IsKey(cp)?" ":"",IsKey(cp)?cp->key->content:"", nickbuf); | |
310 | } | |
311 | ||
312 | /* Tell the world something happened... */ | |
313 | triggerhook(HOOK_CHANNEL_BURST,cp); | |
314 | ||
315 | return 0; | |
316 | } | |
317 | ||
318 | int localjoinchannel(nick *np, channel *cp) { | |
319 | void *harg[2]; | |
320 | ||
321 | if (cp==NULL || np==NULL) { | |
322 | return 1; | |
323 | } | |
324 | ||
325 | /* Check that the user _is_ a local one.. */ | |
326 | if (homeserver(np->numeric)!=mylongnum) { | |
327 | return 1; | |
328 | } | |
329 | ||
330 | /* Check that the user isn't on the channel already */ | |
331 | if ((getnumerichandlefromchanhash(cp->users,np->numeric))!=NULL) { | |
332 | return 1; | |
333 | } | |
334 | ||
335 | /* OK, join the channel.. */ | |
336 | addnicktochannel(cp,np->numeric); | |
337 | ||
338 | /* Trigger the event */ | |
339 | harg[0]=cp; | |
340 | harg[1]=np; | |
341 | ||
342 | if (connected) { | |
343 | irc_send("%s J %s %lu",longtonumeric(np->numeric,5),cp->index->name->content,cp->timestamp); | |
344 | } | |
345 | ||
346 | triggerhook(HOOK_CHANNEL_JOIN, harg); | |
347 | ||
348 | return 0; | |
349 | } | |
350 | ||
351 | int localpartchannel(nick *np, channel *cp, char *reason) { | |
352 | void *harg[3]; | |
353 | ||
354 | /* Check pointers are valid.. */ | |
355 | if (cp==NULL || np==NULL) { | |
356 | Error("localuserchannel",ERR_WARNING,"Trying to part NULL channel or NULL nick (cp=%p,np=%p)",cp,np); | |
357 | return 1; | |
358 | } | |
359 | ||
360 | /* And that user is local.. */ | |
361 | if (homeserver(np->numeric)!=mylongnum) { | |
362 | Error("localuserchannel",ERR_WARNING,"Trying to part remote user %s",np->nick); | |
363 | return 1; | |
364 | } | |
365 | ||
366 | /* Check that user is on channel */ | |
367 | if (getnumerichandlefromchanhash(cp->users,np->numeric)==NULL) { | |
368 | Error("localuserchannel",ERR_WARNING,"Trying to part user %s from channel %s it is not on",np->nick,cp->index->name->content); | |
369 | return 1; | |
370 | } | |
371 | ||
372 | if (connected) { | |
373 | if (reason != NULL) | |
374 | irc_send("%s L %s :%s",longtonumeric(np->numeric,5),cp->index->name->content, reason); | |
375 | else | |
376 | irc_send("%s L %s",longtonumeric(np->numeric,5),cp->index->name->content); | |
377 | } | |
378 | ||
379 | harg[0]=cp; | |
380 | harg[1]=np; | |
381 | harg[2]=NULL; | |
382 | triggerhook(HOOK_CHANNEL_PART,harg); | |
383 | ||
384 | /* Now leave the channel */ | |
385 | delnickfromchannel(cp,np->numeric,1); | |
386 | ||
387 | return 0; | |
388 | } | |
389 | ||
390 | int localcreatechannel(nick *np, char *channame) { | |
391 | channel *cp; | |
392 | ||
393 | /* Check that the user _is_ a local one.. */ | |
394 | if (homeserver(np->numeric)!=mylongnum) { | |
395 | return 1; | |
396 | } | |
397 | ||
398 | if ((cp=findchannel(channame))!=NULL) { | |
399 | /* Already exists */ | |
400 | return 1; | |
401 | } | |
402 | ||
403 | cp=createchannel(channame); | |
404 | cp->timestamp=getnettime(); | |
405 | ||
406 | /* Add the local user to the channel, preopped */ | |
407 | addnicktochannel(cp,(np->numeric)|CUMODE_OP); | |
408 | ||
409 | if (connected) { | |
410 | irc_send("%s C %s %ld",longtonumeric(np->numeric,5),cp->index->name->content,cp->timestamp); | |
411 | } | |
412 | ||
413 | triggerhook(HOOK_CHANNEL_NEWCHANNEL,(void *)cp); | |
414 | return 0; | |
415 | } | |
416 | ||
417 | int localgetops(nick *np, channel *cp) { | |
418 | unsigned long *lp; | |
419 | ||
420 | /* Check that the user _is_ a local one.. */ | |
421 | if (homeserver(np->numeric)!=mylongnum) { | |
422 | return 1; | |
423 | } | |
424 | ||
425 | if ((lp=getnumerichandlefromchanhash(cp->users,np->numeric))==NULL) { | |
426 | return 1; | |
427 | } | |
428 | ||
429 | if (*lp & CUMODE_OP) { | |
430 | /* already opped */ | |
431 | return 1; | |
432 | } | |
433 | ||
434 | /* Op the user */ | |
435 | (*lp)|=CUMODE_OP; | |
436 | ||
437 | if (connected) { | |
438 | irc_send("%s M %s +o %s",mynumeric->content,cp->index->name->content,longtonumeric(np->numeric,5)); | |
439 | } | |
440 | ||
441 | return 0; | |
442 | } | |
443 | ||
444 | int localgetvoice(nick *np, channel *cp) { | |
445 | unsigned long *lp; | |
446 | ||
447 | /* Check that the user _is_ a local one.. */ | |
448 | if (homeserver(np->numeric)!=mylongnum) { | |
449 | return 1; | |
450 | } | |
451 | ||
452 | if ((lp=getnumerichandlefromchanhash(cp->users,np->numeric))==NULL) { | |
453 | return 1; | |
454 | } | |
455 | ||
456 | if (*lp & CUMODE_VOICE) { | |
457 | /* already opped */ | |
458 | return 1; | |
459 | } | |
460 | ||
461 | /* Voice the user */ | |
462 | (*lp)|=CUMODE_VOICE; | |
463 | ||
464 | if (connected) { | |
465 | irc_send("%s M %s +v %s",mynumeric->content,cp->index->name->content,longtonumeric(np->numeric,5)); | |
466 | } | |
467 | ||
468 | return 0; | |
469 | } | |
470 | ||
471 | /* | |
472 | * localsetmodeinit: | |
473 | * Initialises the modechanges structure. | |
474 | */ | |
475 | ||
476 | void localsetmodeinit (modechanges *changes, channel *cp, nick *np) { | |
477 | changes->cp=cp; | |
478 | changes->source=np; | |
479 | changes->changecount=0; | |
480 | changes->addflags=0; | |
481 | changes->delflags=0; | |
482 | } | |
483 | ||
484 | /* | |
485 | * localdosetmode_ban: | |
486 | * Set or clear a ban on the channel | |
487 | */ | |
488 | ||
489 | void localdosetmode_ban (modechanges *changes, const char *ban, short dir) { | |
490 | sstring *bansstr=getsstring(ban,HOSTLEN+NICKLEN+USERLEN+5); | |
491 | ||
492 | /* If we're told to clear a ban that isn't here, do nothing. */ | |
493 | if (dir==MCB_DEL && !clearban(changes->cp, bansstr->content, 1)) | |
494 | return; | |
495 | ||
496 | /* Similarly if someone is trying to add a completely overlapped ban, do | |
497 | * nothing */ | |
498 | if (dir==MCB_ADD && !setban(changes->cp, bansstr->content)) | |
499 | return; | |
500 | ||
501 | if (changes->changecount >= MAXMODEARGS) | |
502 | localsetmodeflush(changes, 0); | |
503 | ||
504 | changes->changes[changes->changecount].str=bansstr; | |
505 | changes->changes[changes->changecount].flag='b'; | |
506 | changes->changes[changes->changecount++].dir=dir; | |
507 | ||
508 | if (dir==MCB_ADD) { | |
509 | setban(changes->cp, bansstr->content); | |
510 | } | |
511 | } | |
512 | ||
513 | /* | |
514 | * localdosetmode_key: | |
515 | * Set or clear a key on the channel | |
516 | */ | |
517 | ||
518 | void localdosetmode_key (modechanges *changes, char *key, short dir) { | |
519 | int i,j; | |
520 | sstring *keysstr; | |
521 | ||
522 | /* Check we have space in the buffer */ | |
523 | if (changes->changecount >= MAXMODEARGS) | |
524 | localsetmodeflush(changes,0); | |
525 | ||
526 | if (dir==MCB_ADD) { | |
527 | /* Sanitise the key. If this nullifies it then we give up */ | |
528 | clean_key(key); | |
529 | if (!*key) | |
530 | return; | |
531 | ||
532 | /* Get a copy of the key for use later */ | |
533 | keysstr=getsstring(key, KEYLEN); | |
534 | ||
535 | /* Check there isn't a key set/clear in the pipeline already.. */ | |
536 | for (i=0;i<changes->changecount;i++) { | |
537 | if (changes->changes[i].flag=='k') { | |
538 | /* There's a change already.. */ | |
539 | if (changes->changes[i].dir==MCB_ADD) { | |
540 | /* Already an add key change. Here, we just replace the key | |
541 | * we were going to add with this one. Note that we need to | |
542 | * free the current cp->key, and the changes.str */ | |
543 | freesstring(changes->changes[i].str); | |
544 | changes->changes[i].str=keysstr; | |
545 | freesstring(changes->cp->key); | |
546 | changes->cp->key=getsstring(key, KEYLEN); | |
547 | /* That's it, we're done */ | |
548 | freesstring(keysstr); | |
549 | return; | |
550 | } else { | |
551 | /* There was a command to delete key.. we need to flush | |
552 | * this out then add the new key (-k+k isn't valid). | |
553 | * Make sure this gets flushed, then drop through to common code */ | |
554 | localsetmodeflush(changes, 1); | |
555 | } | |
556 | } | |
557 | } | |
558 | ||
559 | /* We got here, so there's no change pending already .. check for key on chan */ | |
560 | if (IsKey(changes->cp)) { | |
561 | if (sstringcompare(changes->cp->key, keysstr)) { | |
562 | /* Key is set and different. Need to put -k in and flush changes now */ | |
563 | changes->changes[changes->changecount].str=changes->cp->key; /* implicit free */ | |
564 | changes->changes[changes->changecount].dir=MCB_DEL; | |
565 | changes->changes[changes->changecount++].flag='k'; | |
566 | localsetmodeflush(changes, 1); /* Note that this will free the sstring on the channel */ | |
567 | } else { | |
568 | /* Key is set and the same: do nothing. */ | |
569 | freesstring(keysstr); /* Don't need this after all */ | |
570 | return; | |
571 | } | |
572 | } | |
573 | ||
574 | /* If we get here, there's no key on the channel right now and nothing in the buffer to | |
575 | * add or remove one */ | |
576 | SetKey(changes->cp); | |
577 | changes->cp->key=getsstring(key, KEYLEN); | |
578 | ||
579 | changes->changes[changes->changecount].str=keysstr; | |
580 | changes->changes[changes->changecount].dir=MCB_ADD; | |
581 | changes->changes[changes->changecount++].flag='k'; | |
582 | } else { | |
583 | /* We're removing a key.. */ | |
584 | /* Only bother if key is set atm */ | |
585 | if (IsKey(changes->cp)) { | |
586 | ClearKey(changes->cp); | |
587 | for(i=0;i<changes->changecount;i++) { | |
588 | if (changes->changes[i].flag=='k') { | |
589 | /* We were already doing something with a key.. | |
590 | * it MUST be adding one */ | |
591 | assert(changes->changes[i].dir==MCB_ADD); | |
592 | /* Just forget the earlier change */ | |
593 | freesstring(changes->changes[i].str); | |
594 | changes->changecount--; | |
595 | for (j=i;j<changes->changecount;j++) { | |
596 | changes->changes[j]=changes->changes[j+1]; | |
597 | } | |
598 | /* Explicitly free key on chan */ | |
599 | freesstring(changes->cp->key); | |
600 | changes->cp->key=NULL; | |
601 | return; | |
602 | } | |
603 | } | |
604 | ||
605 | /* We didn't hit a key change, so put a remove command in */ | |
606 | changes->changes[changes->changecount].str=changes->cp->key; /* implicit free */ | |
607 | changes->cp->key=NULL; | |
608 | changes->changes[changes->changecount].dir=MCB_DEL; | |
609 | changes->changes[changes->changecount++].flag='k'; | |
610 | } | |
611 | } | |
612 | } | |
613 | ||
614 | void localdosetmode_limit (modechanges *changes, unsigned int limit, short dir) { | |
615 | int i; | |
616 | char buf[20]; | |
617 | ||
618 | if (dir==MCB_DEL) { | |
619 | localdosetmode_simple(changes, 0, CHANMODE_LIMIT); | |
620 | return; | |
621 | } | |
622 | ||
623 | /* Kill redundant changes */ | |
624 | if ((IsLimit(changes->cp) && changes->cp->limit==limit) || !limit) | |
625 | return; | |
626 | ||
627 | SetLimit(changes->cp); | |
628 | changes->cp->limit=limit; | |
629 | changes->delflags &= ~CHANMODE_LIMIT; | |
630 | ||
631 | /* Check for existing limit add */ | |
632 | for (i=0;i<changes->changecount;i++) { | |
633 | if (changes->changes[i].flag=='l') { | |
634 | /* must be add */ | |
635 | freesstring(changes->changes[i].str); | |
636 | sprintf(buf,"%u",limit); | |
637 | changes->changes[i].str=getsstring(buf,20); | |
638 | return; | |
639 | } | |
640 | } | |
641 | ||
642 | /* None, add new one. Note that we can do +l even if a limit is already set */ | |
643 | if (changes->changecount >= MAXMODEARGS) | |
644 | localsetmodeflush(changes,0); | |
645 | ||
646 | changes->changes[changes->changecount].flag='l'; | |
647 | sprintf(buf,"%u",limit); | |
648 | changes->changes[changes->changecount].str=getsstring(buf,20); | |
649 | changes->changes[changes->changecount++].dir=MCB_ADD; | |
650 | } | |
651 | ||
652 | void localdosetmode_simple (modechanges *changes, flag_t addmodes, flag_t delmodes) { | |
653 | int i,j; | |
654 | ||
655 | /* We can't add a mode we're deleting, a key, limit, or mode that's already set */ | |
656 | addmodes &= ~(delmodes | CHANMODE_KEY | CHANMODE_LIMIT | changes->cp->flags); | |
657 | ||
658 | /* We can't delete a key or a mode that's not set */ | |
659 | delmodes &= (~(CHANMODE_KEY) & changes->cp->flags); | |
660 | ||
661 | /* If we're doing +p, do -s as well, and vice versa */ | |
662 | /* Also disallow +ps */ | |
663 | if (addmodes & CHANMODE_SECRET) { | |
664 | addmodes &= ~(CHANMODE_PRIVATE); | |
665 | delmodes |= (CHANMODE_PRIVATE & changes->cp->flags); | |
666 | } | |
667 | ||
668 | if (addmodes & CHANMODE_PRIVATE) { | |
669 | delmodes |= (CHANMODE_SECRET & changes->cp->flags); | |
670 | } | |
671 | ||
672 | /* Fold changes into channel */ | |
673 | changes->cp->flags |= addmodes; | |
674 | changes->cp->flags &= ~delmodes; | |
675 | ||
676 | if (delmodes & CHANMODE_LIMIT) { | |
677 | /* Check for +l in the parametered changes */ | |
678 | for (i=0;i<changes->changecount;i++) { | |
679 | if (changes->changes[i].flag=='l') { | |
680 | freesstring(changes->changes[i].str); | |
681 | changes->changecount--; | |
682 | for (j=0;j<changes->changecount;j++) { | |
683 | changes->changes[j]=changes->changes[j+1]; | |
684 | } | |
685 | } | |
686 | break; | |
687 | } | |
688 | changes->cp->limit=0; | |
689 | } | |
690 | ||
691 | /* And into the changes buffer */ | |
692 | changes->addflags &= ~delmodes; | |
693 | changes->addflags |= addmodes; | |
694 | ||
695 | changes->delflags &= ~addmodes; | |
696 | changes->delflags |= delmodes; | |
697 | } | |
698 | ||
699 | /* | |
700 | * localdosetmode: | |
701 | * Applies a mode change. | |
702 | */ | |
703 | ||
704 | void localdosetmode_nick (modechanges *changes, nick *target, short modes) { | |
705 | unsigned long *lp; | |
706 | ||
707 | if ((lp=getnumerichandlefromchanhash(changes->cp->users,target->numeric))==NULL) { | |
708 | /* Target isn't on channel, abort */ | |
709 | return; | |
710 | } | |
711 | ||
712 | if ((modes & MC_DEOP) && (*lp & CUMODE_OP)) { | |
713 | (*lp) &= ~CUMODE_OP; | |
714 | if (changes->changecount >= MAXMODEARGS) | |
715 | localsetmodeflush(changes, 0); | |
716 | changes->changes[changes->changecount].str=getsstring(longtonumeric(target->numeric,5),5); | |
717 | changes->changes[changes->changecount].dir=MCB_DEL; | |
718 | changes->changes[changes->changecount++].flag='o'; | |
719 | } | |
720 | ||
721 | if ((modes & MC_DEVOICE) && (*lp & CUMODE_VOICE)) { | |
722 | (*lp) &= ~CUMODE_VOICE; | |
723 | if (changes->changecount >= MAXMODEARGS) | |
724 | localsetmodeflush(changes, 0); | |
725 | changes->changes[changes->changecount].str=getsstring(longtonumeric(target->numeric,5),5); | |
726 | changes->changes[changes->changecount].dir=MCB_DEL; | |
727 | changes->changes[changes->changecount++].flag='v'; | |
728 | } | |
729 | ||
730 | if ((modes & MC_OP) && !(modes & MC_DEOP) && !(*lp & CUMODE_OP)) { | |
731 | (*lp) |= CUMODE_OP; | |
732 | if (changes->changecount >= MAXMODEARGS) | |
733 | localsetmodeflush(changes, 0); | |
734 | changes->changes[changes->changecount].str=getsstring(longtonumeric(target->numeric,5),5); | |
735 | changes->changes[changes->changecount].dir=MCB_ADD; | |
736 | changes->changes[changes->changecount++].flag='o'; | |
737 | } | |
738 | ||
739 | if ((modes & MC_VOICE) && !(modes & MC_DEVOICE) && !(*lp & CUMODE_VOICE)) { | |
740 | (*lp) |= CUMODE_VOICE; | |
741 | if (changes->changecount >= MAXMODEARGS) | |
742 | localsetmodeflush(changes, 0); | |
743 | changes->changes[changes->changecount].str=getsstring(longtonumeric(target->numeric,5),5); | |
744 | changes->changes[changes->changecount].dir=MCB_ADD; | |
745 | changes->changes[changes->changecount++].flag='v'; | |
746 | } | |
747 | ||
748 | } | |
749 | ||
750 | /* | |
751 | * localsetmodeflush: | |
752 | * Sends out mode changes to the network. | |
753 | */ | |
754 | ||
755 | void localsetmodeflush (modechanges *changes, int flushall) { | |
756 | int i,j=0; | |
757 | unsigned long *lp; | |
758 | char source[6]; | |
759 | ||
760 | char addmodes[26]; | |
761 | int ampos=0; | |
762 | char remmodes[26]; | |
763 | int rmpos=0; | |
764 | ||
765 | char addargs[500]; | |
766 | int aapos=0; | |
767 | char remargs[500]; | |
768 | int rapos=0; | |
769 | ||
770 | strcpy(addmodes, printflags_noprefix(changes->addflags, cmodeflags)); | |
771 | ampos=strlen(addmodes); | |
772 | ||
773 | strcpy(remmodes, printflags_noprefix(changes->delflags, cmodeflags)); | |
774 | rmpos=strlen(remmodes); | |
775 | ||
776 | changes->addflags=changes->delflags=0; | |
777 | ||
778 | for (i=0;i<changes->changecount;i++) { | |
779 | /* Don't overflow the string, kinda nasty to work out.. format is: */ | |
780 | /* AAAA M #chan +add-rem (addstr) (remstr) */ | |
781 | if ((changes->cp->index->name->length + aapos + rapos + | |
782 | ampos + rmpos + changes->changes[i].str->length + 20) > BUFSIZE) | |
783 | break; | |
784 | ||
785 | switch (changes->changes[i].dir) { | |
786 | case MCB_ADD: | |
787 | addmodes[ampos++]=changes->changes[i].flag; | |
788 | aapos+=sprintf(addargs+aapos, "%s ", changes->changes[i].str->content); | |
789 | break; | |
790 | ||
791 | case MCB_DEL: | |
792 | remmodes[rmpos++]=changes->changes[i].flag; | |
793 | rapos+=sprintf(remargs+rapos, "%s ", changes->changes[i].str->content); | |
794 | break; | |
795 | } | |
796 | freesstring(changes->changes[i].str); | |
797 | } | |
798 | ||
799 | if (i<changes->changecount) { | |
800 | for (j=i;j<changes->changecount;j++) | |
801 | changes->changes[j-i]=changes->changes[j]; | |
802 | } | |
803 | ||
804 | changes->changecount -= i; | |
805 | ||
806 | if ((ampos+rmpos)==0) { | |
807 | /* No changes */ | |
808 | return; | |
809 | } | |
810 | ||
811 | addmodes[ampos]='\0'; | |
812 | remmodes[rmpos]='\0'; | |
813 | addargs[aapos]='\0'; | |
814 | remargs[rapos]='\0'; | |
815 | ||
816 | if (changes->source==NULL || | |
817 | (lp=getnumerichandlefromchanhash(changes->cp->users,changes->source->numeric))==NULL) { | |
818 | /* User isn't on channel, hack mode */ | |
819 | strcpy(source,mynumeric->content); | |
820 | } else { | |
821 | /* Check the user is local */ | |
822 | if (homeserver(changes->source->numeric)!=mylongnum) { | |
823 | return; | |
824 | } | |
825 | if ((*lp&CUMODE_OP)==0) { | |
826 | localgetops(changes->source,changes->cp); | |
827 | } | |
828 | strcpy(source,longtonumeric(changes->source->numeric,5)); | |
829 | } | |
830 | ||
831 | if (connected) { | |
832 | irc_send("%s M %s %s%s%s%s %s%s",source,changes->cp->index->name->content, | |
833 | rmpos ? "-" : "", remmodes, | |
834 | ampos ? "+" : "", addmodes, remargs, addargs); | |
835 | } | |
836 | ||
837 | /* If we have to flush everything out but didn't finish, go round again */ | |
838 | if (changes->changecount && flushall) | |
839 | localsetmodeflush(changes, 1); | |
840 | } | |
841 | ||
842 | /* | |
843 | * localsetmodes: | |
844 | * Sets modes for the user on the channel. This is now a stub routine that | |
845 | * uses the new functions. | |
846 | */ | |
847 | ||
848 | int localsetmodes(nick *np, channel *cp, nick *target, short modes) { | |
849 | modechanges changes; | |
850 | ||
851 | localsetmodeinit (&changes, cp, np); | |
852 | localdosetmode_nick (&changes, target, modes); | |
853 | localsetmodeflush (&changes, 1); | |
854 | ||
855 | return 0; | |
856 | } | |
857 | ||
858 | /* This function just sends the actual mode change, | |
859 | * it assumes that the actual channel modes have been changed appropriately. | |
860 | * This is unfortunately inconsistent with the rest of the localuser API.. | |
861 | */ | |
862 | ||
863 | void localusermodechange(nick *np, channel *cp, char *modes) { | |
864 | char source[10]; | |
865 | unsigned long *lp; | |
866 | ||
867 | if (np==NULL || (lp=getnumerichandlefromchanhash(cp->users,np->numeric))==NULL) { | |
868 | /* User isn't on channel, hack mode */ | |
869 | strcpy(source,mynumeric->content); | |
870 | } else { | |
871 | /* Check the user is local */ | |
872 | if (homeserver(np->numeric)!=mylongnum) { | |
873 | return; | |
874 | } | |
875 | if ((*lp&CUMODE_OP)==0) { | |
876 | localgetops(np,cp); | |
877 | } | |
878 | strcpy(source,longtonumeric(np->numeric,5)); | |
879 | } | |
880 | ||
881 | if (connected) { | |
882 | irc_send("%s M %s %s",source,cp->index->name->content,modes); | |
883 | } | |
884 | } | |
885 | ||
886 | /* This function actually sets the topic itself though.. a bit inconsistent :/ */ | |
887 | ||
888 | void localsettopic(nick *np, channel *cp, char *topic) { | |
889 | unsigned long *lp; | |
890 | char source[10]; | |
891 | time_t now=getnettime(); | |
892 | ||
893 | if (np==NULL || (lp=getnumerichandlefromchanhash(cp->users,np->numeric))==NULL) { | |
894 | /* User isn't on channel, hack mode */ | |
895 | strcpy(source,mynumeric->content); | |
896 | } else { | |
897 | /* Check the user is local */ | |
898 | if (homeserver(np->numeric)!=mylongnum) { | |
899 | return; | |
900 | } | |
901 | if ((*lp&CUMODE_OP)==0 && IsTopicLimit(cp)) { | |
902 | localgetops(np,cp); | |
903 | } | |
904 | strcpy(source,longtonumeric(np->numeric,5)); | |
905 | } | |
906 | ||
907 | if (cp->topic) { | |
908 | freesstring(cp->topic); | |
909 | } | |
910 | ||
911 | cp->topic=getsstring(topic,TOPICLEN); | |
912 | ||
913 | /* Update the topic time iff the old time was in the past. This | |
914 | * means if we are bouncing a topic with a TS 1sec newer than ours | |
915 | * we won't use an old timestamp */ | |
916 | if (cp->topictime < now) { | |
917 | cp->topictime=now; | |
918 | } | |
919 | ||
920 | if (connected) { | |
921 | irc_send("%s T %s %jd %jd :%s",source,cp->index->name->content,(intmax_t)cp->timestamp,(intmax_t)cp->topictime,(cp->topic)?cp->topic->content:""); | |
922 | } | |
923 | } | |
924 | ||
925 | void localkickuser(nick *np, channel *cp, nick *target, const char *message) { | |
926 | pendingkick *pk; | |
927 | ||
928 | if (hookqueuelength) { | |
929 | for (pk = pendingkicklist; pk; pk = pk->next) | |
930 | if (pk->target == target && pk->chan == cp) | |
931 | return; | |
932 | ||
933 | Error("localuserchannel", ERR_DEBUG, "Adding pending kick for %s on %s", target->nick, cp->index->name->content); | |
934 | pk = (pendingkick *)malloc(sizeof(pendingkick)); | |
935 | pk->source = np; | |
936 | pk->chan = cp; | |
937 | pk->target = target; | |
938 | pk->reason = getsstring(message, BUFSIZE); | |
939 | pk->next = pendingkicklist; | |
940 | pendingkicklist = pk; | |
941 | } else { | |
942 | _localkickuser(np, cp, target, message); | |
943 | } | |
944 | } | |
945 | ||
946 | void _localkickuser(nick *np, channel *cp, nick *target, const char *message) { | |
947 | unsigned long *lp; | |
948 | char source[10]; | |
949 | ||
950 | if (np==NULL || (lp=getnumerichandlefromchanhash(cp->users,np->numeric))==NULL) { | |
951 | /* User isn't on channel, hack mode */ | |
952 | strcpy(source,mynumeric->content); | |
953 | } else { | |
954 | /* Check the user is local */ | |
955 | if (homeserver(np->numeric)!=mylongnum) { | |
956 | return; | |
957 | } | |
958 | if ((*lp&CUMODE_OP)==0) { | |
959 | localgetops(np,cp); | |
960 | } | |
961 | strcpy(source,longtonumeric(np->numeric,5)); | |
962 | } | |
963 | ||
964 | if ((lp=getnumerichandlefromchanhash(cp->users,target->numeric))==NULL) | |
965 | return; | |
966 | ||
967 | /* Send the message to the network first in case delnickfromchannel() | |
968 | * destroys the channel.. */ | |
969 | if (connected) { | |
970 | irc_send("%s K %s %s :%s",source,cp->index->name->content, | |
971 | longtonumeric(target->numeric,5), message); | |
972 | } | |
973 | ||
974 | delnickfromchannel(cp, target->numeric, 1); | |
975 | } | |
976 | ||
977 | void clearpendingkicks(int hooknum, void *arg) { | |
978 | pendingkick *pk; | |
979 | ||
980 | pk = pendingkicklist; | |
981 | while (pk) { | |
982 | pendingkicklist = pk->next; | |
983 | ||
984 | if (pk->target && pk->chan) { | |
985 | Error("localuserchannel", ERR_DEBUG, "Processing pending kick for %s on %s", pk->target->nick, pk->chan->index->name->content); | |
986 | _localkickuser(pk->source, pk->chan, pk->target, pk->reason->content); | |
987 | } | |
988 | ||
989 | freesstring(pk->reason); | |
990 | free(pk); | |
991 | pk = pendingkicklist; | |
992 | } | |
993 | } | |
994 | ||
995 | void checkpendingkicknicks(int hooknum, void *arg) { | |
996 | nick *np = (nick *)arg; | |
997 | pendingkick *pk; | |
998 | ||
999 | for (pk=pendingkicklist; pk; pk = pk->next) { | |
1000 | if (pk->source == np) { | |
1001 | Error("localuserchannel", ERR_INFO, "Pending kick source %s got deleted, NULL'ing source for pending kick", np->nick); | |
1002 | pk->source = NULL; | |
1003 | } | |
1004 | if (pk->target == np) { | |
1005 | Error("localuserchannel", ERR_INFO, "Pending kick target %s got deleted, NULL'ing target for pending kick", np->nick); | |
1006 | pk->target = NULL; | |
1007 | } | |
1008 | } | |
1009 | } | |
1010 | ||
1011 | void checkpendingkickchannels(int hooknum, void *arg) { | |
1012 | channel *cp = (channel *)arg; | |
1013 | pendingkick *pk; | |
1014 | ||
1015 | for (pk=pendingkicklist; pk; pk = pk->next) { | |
1016 | if (pk->chan == cp) { | |
1017 | Error("localuserchannel", ERR_INFO, "Pending kick channel %s got deleted, NULL'ing channel for pending kick", cp->index->name->content); | |
1018 | pk->chan = NULL; | |
1019 | } | |
1020 | } | |
1021 | } | |
1022 | ||
1023 | void sendmessagetochannel(nick *source, channel *cp, char *format, ... ) { | |
1024 | char buf[BUFSIZE]; | |
1025 | char senderstr[6]; | |
1026 | va_list va; | |
1027 | ||
1028 | if (!source) | |
1029 | return; | |
1030 | ||
1031 | longtonumeric2(source->numeric,5,senderstr); | |
1032 | ||
1033 | va_start(va,format); | |
1034 | /* 10 bytes of numeric, 5 bytes of fixed format + terminator = 17 bytes */ | |
1035 | /* So max sendable message is 495 bytes. Of course, a client won't be able | |
1036 | * to receive this.. */ | |
1037 | ||
1038 | vsnprintf(buf,BUFSIZE-17,format,va); | |
1039 | va_end(va); | |
1040 | ||
1041 | if (connected) { | |
1042 | irc_send("%s P %s :%s",senderstr,cp->index->name->content,buf); | |
1043 | } | |
1044 | } | |
1045 | ||
1046 | void sendopnoticetochannel(nick *source, channel *cp, char *format, ... ) { | |
1047 | char buf[BUFSIZE]; | |
1048 | char senderstr[6]; | |
1049 | va_list va; | |
1050 | ||
1051 | if (!source) | |
1052 | return; | |
1053 | ||
1054 | longtonumeric2(source->numeric,5,senderstr); | |
1055 | ||
1056 | va_start(va,format); | |
1057 | /* 10 bytes of numeric, 5 bytes of fixed format + terminator = 17 bytes */ | |
1058 | /* So max sendable message is 495 bytes. Of course, a client won't be able | |
1059 | * to receive this.. */ | |
1060 | ||
1061 | vsnprintf(buf,BUFSIZE-17,format,va); | |
1062 | va_end(va); | |
1063 | ||
1064 | if (connected) { | |
1065 | irc_send("%s WC %s :%s",senderstr,cp->index->name->content,buf); | |
1066 | } | |
1067 | } | |
1068 | ||
1069 | void localinvite(nick *source, chanindex *cip, nick *target) { | |
1070 | ||
1071 | /* Servers can't send invites */ | |
1072 | if (!source) | |
1073 | return; | |
1074 | ||
1075 | /* CHECK: Does the sender have to be on the relevant channel? */ | |
1076 | ||
1077 | /* For some reason invites are sent with the target nick as | |
1078 | * argument */ | |
1079 | if (connected) { | |
1080 | irc_send("%s I %s :%s",longtonumeric(source->numeric,5), | |
1081 | target->nick, cip->name->content); | |
1082 | } | |
1083 | } | |
1084 |