This repository has been archived by the owner on Jun 24, 2021. It is now read-only.
/
send.c
1467 lines (1231 loc) · 35.1 KB
/
send.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* ircd-ratbox: A slightly useful ircd.
* send.c: Functions for sending messages.
*
* Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
* Copyright (C) 1996-2002 Hybrid Development Team
* Copyright (C) 2002-2005 ircd-ratbox development team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*/
#include "stdinc.h"
#include "send.h"
#include "channel.h"
#include "class.h"
#include "client.h"
#include "match.h"
#include "ircd.h"
#include "numeric.h"
#include "s_assert.h"
#include "s_serv.h"
#include "s_conf.h"
#include "s_newconf.h"
#include "logger.h"
#include "hook.h"
#include "monitor.h"
#include "msgbuf.h"
/* send the message to the link the target is attached to */
#define send_linebuf(a,b) _send_linebuf((a->from ? a->from : a) ,b)
static void send_queued_write(rb_fde_t *F, void *data);
unsigned long current_serial = 0L;
struct Client *remote_rehash_oper_p;
/* send_linebuf()
*
* inputs - client to send to, linebuf to attach
* outputs -
* side effects - linebuf is attached to client
*/
static int
_send_linebuf(struct Client *to, buf_head_t *linebuf)
{
if(IsMe(to))
{
sendto_realops_snomask(SNO_GENERAL, L_ALL, "Trying to send message to myself!");
return 0;
}
if(!MyConnect(to) || IsIOError(to))
return 0;
if(rb_linebuf_len(&to->localClient->buf_sendq) > get_sendq(to))
{
if(IsServer(to))
{
sendto_realops_snomask(SNO_GENERAL, L_ALL,
"Max SendQ limit exceeded for %s: %u > %lu",
to->name,
rb_linebuf_len(&to->localClient->buf_sendq),
get_sendq(to));
ilog(L_SERVER, "Max SendQ limit exceeded for %s: %u > %lu",
log_client_name(to, SHOW_IP),
rb_linebuf_len(&to->localClient->buf_sendq),
get_sendq(to));
}
dead_link(to, 1);
return -1;
}
else
{
/* just attach the linebuf to the sendq instead of
* generating a new one
*/
rb_linebuf_attach(&to->localClient->buf_sendq, linebuf);
}
/*
** Update statistics. The following is slightly incorrect
** because it counts messages even if queued, but bytes
** only really sent. Queued bytes get updated in SendQueued.
*/
to->localClient->sendM += 1;
me.localClient->sendM += 1;
if(rb_linebuf_len(&to->localClient->buf_sendq) > 0)
send_queued(to);
return 0;
}
/* send_linebuf_remote()
*
* inputs - client to attach to, sender, linebuf
* outputs -
* side effects - client has linebuf attached
*/
static void
send_linebuf_remote(struct Client *to, struct Client *from, buf_head_t *linebuf)
{
if(to->from)
to = to->from;
/* we assume the caller has already tested for fake direction */
_send_linebuf(to, linebuf);
}
/* send_queued_write()
*
* inputs - fd to have queue sent, client we're sending to
* outputs - contents of queue
* side effects - write is rescheduled if queue isnt emptied
*/
void
send_queued(struct Client *to)
{
int retlen;
rb_fde_t *F = to->localClient->F;
if (!F)
return;
/* cant write anything to a dead socket. */
if(IsIOError(to))
return;
/* try to flush later when the write event resets this */
if(IsFlush(to))
return;
if(rb_linebuf_len(&to->localClient->buf_sendq))
{
while ((retlen =
rb_linebuf_flush(F, &to->localClient->buf_sendq)) > 0)
{
/* We have some data written .. update counters */
ClearFlush(to);
to->localClient->sendB += retlen;
me.localClient->sendB += retlen;
if(to->localClient->sendB > 1023)
{
to->localClient->sendK += (to->localClient->sendB >> 10);
to->localClient->sendB &= 0x03ff; /* 2^10 = 1024, 3ff = 1023 */
}
else if(me.localClient->sendB > 1023)
{
me.localClient->sendK += (me.localClient->sendB >> 10);
me.localClient->sendB &= 0x03ff;
}
}
if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno)))
{
dead_link(to, 0);
return;
}
}
if(rb_linebuf_len(&to->localClient->buf_sendq))
{
SetFlush(to);
rb_setselect(to->localClient->F, RB_SELECT_WRITE,
send_queued_write, to);
}
else
ClearFlush(to);
}
void
send_pop_queue(struct Client *to)
{
if(to->from != NULL)
to = to->from;
if(!MyConnect(to) || IsIOError(to))
return;
if(rb_linebuf_len(&to->localClient->buf_sendq) > 0)
send_queued(to);
}
/* send_queued_write()
*
* inputs - fd to have queue sent, client we're sending to
* outputs - contents of queue
* side effects - write is scheduled if queue isnt emptied
*/
static void
send_queued_write(rb_fde_t *F, void *data)
{
struct Client *to = data;
ClearFlush(to);
send_queued(to);
}
/*
* linebuf_put_msgvbuf
*
* inputs - msgbuf header, linebuf object, capability mask, pattern, arguments
* outputs - none
* side effects - the linebuf object is cleared, then populated using rb_linebuf_putmsg().
*/
static void
linebuf_put_msgvbuf(struct MsgBuf *msgbuf, buf_head_t *linebuf, unsigned int capmask, const char *pattern, va_list *va)
{
static char buf[EXT_BUFSIZE];
size_t buflen = sizeof(buf);
rb_linebuf_newbuf(linebuf);
msgbuf_unparse_prefix(buf, &buflen, msgbuf, capmask);
rb_linebuf_putprefix(linebuf, pattern, va, buf);
}
/* linebuf_put_msgbuf
*
* inputs - msgbuf header, linebuf object, capability mask, pattern, arguments
* outputs - none
* side effects - the linebuf object is cleared, then populated using rb_linebuf_putmsg().
*/
static void
linebuf_put_msgbuf(struct MsgBuf *msgbuf, buf_head_t *linebuf, unsigned int capmask, const char *pattern, ...)
{
va_list va;
va_start(va, pattern);
linebuf_put_msgvbuf(msgbuf, linebuf, capmask, pattern, &va);
va_end(va);
}
/* build_msgbuf_from
*
* inputs - msgbuf object, client the message is from
* outputs - none
* side effects - a msgbuf object is populated with an origin and relevant tags
* notes - to make this reentrant, find a solution for `buf` below
*/
static void
build_msgbuf_from(struct MsgBuf *msgbuf, struct Client *from, const char *cmd)
{
static char buf[BUFSIZE];
hook_data hdata;
msgbuf_init(msgbuf);
msgbuf->origin = buf;
msgbuf->cmd = cmd;
if (from != NULL && IsPerson(from))
snprintf(buf, sizeof buf, "%s!%s@%s", from->name, from->username, from->host);
else if (from != NULL)
rb_strlcpy(buf, from->name, sizeof buf);
else
rb_strlcpy(buf, me.name, sizeof buf);
hdata.client = from;
hdata.arg1 = msgbuf;
call_hook(h_outbound_msgbuf, &hdata);
}
/* sendto_one()
*
* inputs - client to send to, va_args
* outputs - client has message put into its queue
* side effects -
*/
void
sendto_one(struct Client *target_p, const char *pattern, ...)
{
va_list args;
buf_head_t linebuf;
/* send remote if to->from non NULL */
if(target_p->from != NULL)
target_p = target_p->from;
if(IsIOError(target_p))
return;
rb_linebuf_newbuf(&linebuf);
va_start(args, pattern);
rb_linebuf_putmsg(&linebuf, pattern, &args, NULL);
va_end(args);
_send_linebuf(target_p, &linebuf);
rb_linebuf_donebuf(&linebuf);
}
/* sendto_one_prefix()
*
* inputs - client to send to, va_args
* outputs - client has message put into its queue
* side effects - source(us)/target is chosen based on TS6 capability
*/
void
sendto_one_prefix(struct Client *target_p, struct Client *source_p,
const char *command, const char *pattern, ...)
{
struct Client *dest_p = target_p->from;
va_list args;
buf_head_t linebuf;
if(IsIOError(dest_p))
return;
if(IsMe(dest_p))
{
sendto_realops_snomask(SNO_GENERAL, L_ALL, "Trying to send to myself!");
return;
}
rb_linebuf_newbuf(&linebuf);
va_start(args, pattern);
rb_linebuf_putmsg(&linebuf, pattern, &args,
":%s %s %s ",
get_id(source_p, target_p),
command, get_id(target_p, target_p));
va_end(args);
_send_linebuf(dest_p, &linebuf);
rb_linebuf_donebuf(&linebuf);
}
/* sendto_one_notice()
*
* inputs - client to send to, va_args
* outputs - client has a NOTICE put into its queue
* side effects - source(us)/target is chosen based on TS6 capability
*/
void
sendto_one_notice(struct Client *target_p, const char *pattern, ...)
{
struct Client *dest_p = target_p->from;
va_list args;
buf_head_t linebuf;
char *to;
if(IsIOError(dest_p))
return;
if(IsMe(dest_p))
{
sendto_realops_snomask(SNO_GENERAL, L_ALL, "Trying to send to myself!");
return;
}
rb_linebuf_newbuf(&linebuf);
va_start(args, pattern);
rb_linebuf_putmsg(&linebuf, pattern, &args,
":%s NOTICE %s ",
get_id(&me, target_p), *(to = get_id(target_p, target_p)) != '\0' ? to : "*");
va_end(args);
_send_linebuf(dest_p, &linebuf);
rb_linebuf_donebuf(&linebuf);
}
/* sendto_one_numeric()
*
* inputs - client to send to, va_args
* outputs - client has message put into its queue
* side effects - source/target is chosen based on TS6 capability
*/
void
sendto_one_numeric(struct Client *target_p, int numeric, const char *pattern, ...)
{
struct Client *dest_p = target_p->from;
va_list args;
buf_head_t linebuf;
char *to;
if(IsIOError(dest_p))
return;
if(IsMe(dest_p))
{
sendto_realops_snomask(SNO_GENERAL, L_ALL, "Trying to send to myself!");
return;
}
rb_linebuf_newbuf(&linebuf);
va_start(args, pattern);
rb_linebuf_putmsg(&linebuf, pattern, &args,
":%s %03d %s ",
get_id(&me, target_p),
numeric, *(to = get_id(target_p, target_p)) != '\0' ? to : "*");
va_end(args);
_send_linebuf(dest_p, &linebuf);
rb_linebuf_donebuf(&linebuf);
}
/*
* sendto_server
*
* inputs - pointer to client to NOT send to
* - caps or'd together which must ALL be present
* - caps or'd together which must ALL NOT be present
* - printf style format string
* - args to format string
* output - NONE
* side effects - Send a message to all connected servers, except the
* client 'one' (if non-NULL), as long as the servers
* support ALL capabs in 'caps', and NO capabs in 'nocaps'.
*
* This function was written in an attempt to merge together the other
* billion sendto_*serv*() functions, which sprung up with capabs, uids etc
* -davidt
*/
void
sendto_server(struct Client *one, struct Channel *chptr, unsigned long caps,
unsigned long nocaps, const char *format, ...)
{
va_list args;
struct Client *target_p;
rb_dlink_node *ptr;
rb_dlink_node *next_ptr;
buf_head_t linebuf;
/* noone to send to.. */
if(rb_dlink_list_length(&serv_list) == 0)
return;
if(chptr != NULL && *chptr->chname != '#')
return;
rb_linebuf_newbuf(&linebuf);
va_start(args, format);
rb_linebuf_putmsg(&linebuf, format, &args, NULL);
va_end(args);
RB_DLINK_FOREACH_SAFE(ptr, next_ptr, serv_list.head)
{
target_p = ptr->data;
/* check against 'one' */
if(one != NULL && (target_p == one->from))
continue;
/* check we have required capabs */
if(!IsCapable(target_p, caps))
continue;
/* check we don't have any forbidden capabs */
if(!NotCapable(target_p, nocaps))
continue;
_send_linebuf(target_p, &linebuf);
}
rb_linebuf_donebuf(&linebuf);
}
/* sendto_channel_flags()
*
* inputs - server not to send to, flags needed, source, channel, va_args
* outputs - message is sent to channel members
* side effects -
*/
void
sendto_channel_flags(struct Client *one, int type, struct Client *source_p,
struct Channel *chptr, const char *pattern, ...)
{
static char buf[BUFSIZE];
va_list args;
buf_head_t rb_linebuf_local;
buf_head_t rb_linebuf_id;
struct Client *target_p;
struct membership *msptr;
rb_dlink_node *ptr;
rb_dlink_node *next_ptr;
int current_capmask = 0;
struct MsgBuf msgbuf;
rb_linebuf_newbuf(&rb_linebuf_local);
rb_linebuf_newbuf(&rb_linebuf_id);
current_serial++;
build_msgbuf_from(&msgbuf, source_p, NULL);
va_start(args, pattern);
vsnprintf(buf, sizeof buf, pattern, args);
va_end(args);
linebuf_put_msgbuf(&msgbuf, &rb_linebuf_local, NOCAPS, "%s", buf);
rb_linebuf_putmsg(&rb_linebuf_id, NULL, NULL, ":%s %s", use_id(source_p), buf);
RB_DLINK_FOREACH_SAFE(ptr, next_ptr, chptr->members.head)
{
msptr = ptr->data;
target_p = msptr->client_p;
if(!MyClient(source_p) && (IsIOError(target_p->from) || target_p->from == one))
continue;
if(MyClient(source_p) && target_p == one)
continue;
if(type && ((msptr->flags & type) == 0))
continue;
if(IsDeaf(target_p))
continue;
if(!MyClient(target_p))
{
/* if we've got a specific type, target must support
* CHW.. --fl
*/
if(type && NotCapable(target_p->from, CAP_CHW))
continue;
if(target_p->from->serial != current_serial)
{
send_linebuf_remote(target_p, source_p, &rb_linebuf_id);
target_p->from->serial = current_serial;
}
}
else
{
if (target_p->localClient->caps != current_capmask)
{
/* reset the linebuf */
rb_linebuf_donebuf(&rb_linebuf_local);
rb_linebuf_newbuf(&rb_linebuf_local);
/* render the new linebuf and attach it */
current_capmask = target_p->localClient->caps;
linebuf_put_msgbuf(&msgbuf, &rb_linebuf_local, current_capmask, "%s", buf);
}
_send_linebuf(target_p, &rb_linebuf_local);
}
}
/* source client may not be on the channel, send echo separately */
if(MyClient(source_p) && IsCapable(source_p, CLICAP_ECHO_MESSAGE))
{
target_p = one;
if (target_p->localClient->caps != current_capmask)
{
/* reset the linebuf */
rb_linebuf_donebuf(&rb_linebuf_local);
rb_linebuf_newbuf(&rb_linebuf_local);
/* render the new linebuf and attach it */
current_capmask = target_p->localClient->caps;
linebuf_put_msgbuf(&msgbuf, &rb_linebuf_local, current_capmask, "%s", buf);
}
_send_linebuf(target_p, &rb_linebuf_local);
}
rb_linebuf_donebuf(&rb_linebuf_local);
rb_linebuf_donebuf(&rb_linebuf_id);
}
/* sendto_channel_flags()
*
* inputs - server not to send to, flags needed, source, channel, va_args
* outputs - message is sent to channel members
* side effects -
*/
void
sendto_channel_opmod(struct Client *one, struct Client *source_p,
struct Channel *chptr, const char *command,
const char *text)
{
buf_head_t rb_linebuf_local;
buf_head_t rb_linebuf_old;
buf_head_t rb_linebuf_new;
struct Client *target_p;
struct membership *msptr;
rb_dlink_node *ptr;
rb_dlink_node *next_ptr;
rb_linebuf_newbuf(&rb_linebuf_local);
rb_linebuf_newbuf(&rb_linebuf_old);
rb_linebuf_newbuf(&rb_linebuf_new);
current_serial++;
if(IsServer(source_p))
rb_linebuf_putmsg(&rb_linebuf_local, NULL, NULL,
":%s %s %s :%s",
source_p->name, command, chptr->chname, text);
else
rb_linebuf_putmsg(&rb_linebuf_local, NULL, NULL,
":%s!%s@%s %s %s :%s",
source_p->name, source_p->username,
source_p->host, command, chptr->chname, text);
if (chptr->mode.mode & MODE_MODERATED)
rb_linebuf_putmsg(&rb_linebuf_old, NULL, NULL,
":%s %s %s :%s",
use_id(source_p), command, chptr->chname, text);
else
rb_linebuf_putmsg(&rb_linebuf_old, NULL, NULL,
":%s NOTICE @%s :<%s:%s> %s",
use_id(source_p->servptr), chptr->chname,
source_p->name, chptr->chname, text);
rb_linebuf_putmsg(&rb_linebuf_new, NULL, NULL,
":%s %s =%s :%s",
use_id(source_p), command, chptr->chname, text);
RB_DLINK_FOREACH_SAFE(ptr, next_ptr, chptr->members.head)
{
msptr = ptr->data;
target_p = msptr->client_p;
if(!MyClient(source_p) && (IsIOError(target_p->from) || target_p->from == one))
continue;
if(MyClient(source_p) && target_p == one)
continue;
if((msptr->flags & CHFL_CHANOP) == 0)
continue;
if(IsDeaf(target_p))
continue;
if(!MyClient(target_p))
{
/* if we've got a specific type, target must support
* CHW.. --fl
*/
if(NotCapable(target_p->from, CAP_CHW))
continue;
if(target_p->from->serial != current_serial)
{
if (IsCapable(target_p->from, CAP_EOPMOD))
send_linebuf_remote(target_p, source_p, &rb_linebuf_new);
else
send_linebuf_remote(target_p, source_p, &rb_linebuf_old);
target_p->from->serial = current_serial;
}
}
else
_send_linebuf(target_p, &rb_linebuf_local);
}
/* source client may not be on the channel, send echo separately */
if(MyClient(source_p) && IsCapable(source_p, CLICAP_ECHO_MESSAGE))
{
target_p = one;
_send_linebuf(target_p, &rb_linebuf_local);
}
rb_linebuf_donebuf(&rb_linebuf_local);
rb_linebuf_donebuf(&rb_linebuf_old);
rb_linebuf_donebuf(&rb_linebuf_new);
}
/* sendto_channel_local()
*
* inputs - flags to send to, channel to send to, va_args
* outputs - message to local channel members
* side effects -
*/
void
sendto_channel_local(int type, struct Channel *chptr, const char *pattern, ...)
{
va_list args;
buf_head_t linebuf;
struct membership *msptr;
struct Client *target_p;
rb_dlink_node *ptr;
rb_dlink_node *next_ptr;
rb_linebuf_newbuf(&linebuf);
va_start(args, pattern);
rb_linebuf_putmsg(&linebuf, pattern, &args, NULL);
va_end(args);
RB_DLINK_FOREACH_SAFE(ptr, next_ptr, chptr->locmembers.head)
{
msptr = ptr->data;
target_p = msptr->client_p;
if(IsIOError(target_p))
continue;
if(type == ONLY_OPERS)
{
if (!IsOper(target_p))
continue;
}
else if(type && ((msptr->flags & type) == 0))
continue;
_send_linebuf(target_p, &linebuf);
}
rb_linebuf_donebuf(&linebuf);
}
/*
* _sendto_channel_local_with_capability_butone()
*
* Shared implementation of sendto_channel_local_with_capability and sendto_channel_local_with_capability_butone
*/
static void
_sendto_channel_local_with_capability_butone(struct Client *one, int type, int caps, int negcaps, struct Channel *chptr,
const char *pattern, va_list * args)
{
buf_head_t linebuf;
struct membership *msptr;
struct Client *target_p;
rb_dlink_node *ptr;
rb_dlink_node *next_ptr;
rb_linebuf_newbuf(&linebuf);
rb_linebuf_putmsg(&linebuf, pattern, args, NULL);
RB_DLINK_FOREACH_SAFE(ptr, next_ptr, chptr->locmembers.head)
{
msptr = ptr->data;
target_p = msptr->client_p;
if (target_p == one)
continue;
if(IsIOError(target_p) ||
!IsCapable(target_p, caps) ||
!NotCapable(target_p, negcaps))
continue;
if(type && ((msptr->flags & type) == 0))
continue;
_send_linebuf(target_p, &linebuf);
}
rb_linebuf_donebuf(&linebuf);
}
/* sendto_channel_local_with_capability()
*
* inputs - flags to send to, caps, negate caps, channel to send to, va_args
* outputs - message to local channel members
* side effects -
*/
void
sendto_channel_local_with_capability(int type, int caps, int negcaps, struct Channel *chptr, const char *pattern, ...)
{
va_list args;
va_start(args, pattern);
_sendto_channel_local_with_capability_butone(NULL, type, caps, negcaps, chptr, pattern, &args);
va_end(args);
}
/* sendto_channel_local_with_capability()
*
* inputs - flags to send to, caps, negate caps, channel to send to, va_args
* outputs - message to local channel members
* side effects -
*/
void
sendto_channel_local_with_capability_butone(struct Client *one, int type, int caps, int negcaps, struct Channel *chptr,
const char *pattern, ...)
{
va_list args;
va_start(args, pattern);
_sendto_channel_local_with_capability_butone(one, type, caps, negcaps, chptr, pattern, &args);
va_end(args);
}
/* sendto_channel_local_butone()
*
* inputs - flags to send to, channel to send to, va_args
* - user to ignore when sending
* outputs - message to local channel members
* side effects -
*/
void
sendto_channel_local_butone(struct Client *one, int type, struct Channel *chptr, const char *pattern, ...)
{
va_list args;
buf_head_t linebuf;
struct membership *msptr;
struct Client *target_p;
struct MsgBuf msgbuf;
rb_dlink_node *ptr;
rb_dlink_node *next_ptr;
rb_linebuf_newbuf(&linebuf);
build_msgbuf_from(&msgbuf, one, NULL);
va_start(args, pattern);
rb_linebuf_putmsg(&linebuf, pattern, &args, NULL);
va_end(args);
RB_DLINK_FOREACH_SAFE(ptr, next_ptr, chptr->locmembers.head)
{
msptr = ptr->data;
target_p = msptr->client_p;
if(target_p == one)
continue;
if(IsIOError(target_p))
continue;
if(type && ((msptr->flags & type) == 0))
continue;
/* attach the present linebuf to the target */
_send_linebuf(target_p, &linebuf);
}
rb_linebuf_donebuf(&linebuf);
}
/*
* sendto_common_channels_local()
*
* inputs - pointer to client
* - capability mask
* - negated capability mask
* - pattern to send
* output - NONE
* side effects - Sends a message to all people on local server who are
* in same channel with user.
* used by m_nick.c and exit_one_client.
*/
void
sendto_common_channels_local(struct Client *user, int cap, int negcap, const char *pattern, ...)
{
va_list args;
rb_dlink_node *ptr;
rb_dlink_node *next_ptr;
rb_dlink_node *uptr;
rb_dlink_node *next_uptr;
struct Channel *chptr;
struct Client *target_p;
struct membership *msptr;
struct membership *mscptr;
buf_head_t linebuf;
rb_linebuf_newbuf(&linebuf);
va_start(args, pattern);
rb_linebuf_putmsg(&linebuf, pattern, &args, NULL);
va_end(args);
++current_serial;
RB_DLINK_FOREACH_SAFE(ptr, next_ptr, user->user->channel.head)
{
mscptr = ptr->data;
chptr = mscptr->chptr;
RB_DLINK_FOREACH_SAFE(uptr, next_uptr, chptr->locmembers.head)
{
msptr = uptr->data;
target_p = msptr->client_p;
if(IsIOError(target_p) ||
target_p->serial == current_serial ||
!IsCapable(target_p, cap) ||
!NotCapable(target_p, negcap))
continue;
target_p->serial = current_serial;
send_linebuf(target_p, &linebuf);
}
}
/* this can happen when the user isnt in any channels, but we still
* need to send them the data, ie a nick change
*/
if(MyConnect(user) && (user->serial != current_serial))
send_linebuf(user, &linebuf);
rb_linebuf_donebuf(&linebuf);
}
/*
* sendto_common_channels_local_butone()
*
* inputs - pointer to client
* - capability mask
* - negated capability mask
* - pattern to send
* output - NONE
* side effects - Sends a message to all people on local server who are
* in same channel with user, except for user itself.
*/
void
sendto_common_channels_local_butone(struct Client *user, int cap, int negcap, const char *pattern, ...)
{
va_list args;
rb_dlink_node *ptr;
rb_dlink_node *next_ptr;
rb_dlink_node *uptr;
rb_dlink_node *next_uptr;
struct Channel *chptr;
struct Client *target_p;
struct membership *msptr;
struct membership *mscptr;
buf_head_t linebuf;
rb_linebuf_newbuf(&linebuf);
va_start(args, pattern);
rb_linebuf_putmsg(&linebuf, pattern, &args, NULL);
va_end(args);
++current_serial;
/* Skip them -- jilles */
user->serial = current_serial;
RB_DLINK_FOREACH_SAFE(ptr, next_ptr, user->user->channel.head)
{
mscptr = ptr->data;
chptr = mscptr->chptr;
RB_DLINK_FOREACH_SAFE(uptr, next_uptr, chptr->locmembers.head)
{
msptr = uptr->data;
target_p = msptr->client_p;
if(IsIOError(target_p) ||
target_p->serial == current_serial ||
!IsCapable(target_p, cap) ||
!NotCapable(target_p, negcap))
continue;
target_p->serial = current_serial;
send_linebuf(target_p, &linebuf);
}
}
rb_linebuf_donebuf(&linebuf);
}
/* sendto_match_butone()
*
* inputs - server not to send to, source, mask, type of mask, va_args
* output -
* side effects - message is sent to matching clients
*/
void
sendto_match_butone(struct Client *one, struct Client *source_p,
const char *mask, int what, const char *pattern, ...)
{
static char buf[BUFSIZE];
va_list args;
struct Client *target_p;
rb_dlink_node *ptr;
rb_dlink_node *next_ptr;
buf_head_t rb_linebuf_local;
buf_head_t rb_linebuf_id;
rb_linebuf_newbuf(&rb_linebuf_local);
rb_linebuf_newbuf(&rb_linebuf_id);
va_start(args, pattern);
vsnprintf(buf, sizeof(buf), pattern, args);
va_end(args);
if(IsServer(source_p))
rb_linebuf_putmsg(&rb_linebuf_local, NULL, NULL,
":%s %s", source_p->name, buf);
else
rb_linebuf_putmsg(&rb_linebuf_local, NULL, NULL,
":%s!%s@%s %s",
source_p->name, source_p->username,
source_p->host, buf);
rb_linebuf_putmsg(&rb_linebuf_id, NULL, NULL, ":%s %s", use_id(source_p), buf);