-
Notifications
You must be signed in to change notification settings - Fork 0
/
memoserv.c
1256 lines (1138 loc) · 37.5 KB
/
memoserv.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
/* MemoServ functions.
*
* Services is copyright (c) 1996-1999 Andrew Church.
* E-mail: <achurch@dragonfire.net>
* Services is copyright (c) 1999-2000 Andrew Kempe.
* E-mail: <theshadow@shadowfire.org>
* This program is free but copyrighted software; see the file COPYING for
* details.
*/
#include "services.h"
#include "pseudo.h"
/*************************************************************************/
static int delmemo(MemoInfo *mi, int num);
static void do_help(User *u);
static void do_credits(User *u);
static void do_send(User *u);
static void do_cancel(User *u);
static void do_list(User *u);
static void do_read(User *u);
static void do_del(User *u);
static void do_set(User *u);
static void do_set_notify(User *u, MemoInfo *mi, char *param);
static void do_set_limit(User *u, MemoInfo *mi, char *param);
static void do_set_fullmemo(User *u, MemoInfo *mi, char *param);
static void do_info(User *u);
/*************************************************************************/
static Command cmds[] = {
{ "AYUDA", do_help, NULL, -1, -1,-1,-1,-1 },
{ "HELP", do_help, NULL, -1, -1,-1,-1,-1 },
{ "?", do_help, NULL, -1, -1,-1,-1,-1 },
{ ":?", do_help, NULL, -1, -1,-1,-1,-1 },
{ "CREDITS", do_credits, NULL, SERVICES_CREDITS_TERRA, -1,-1,-1,-1 },
{ "CREDITOS", do_credits, NULL, SERVICES_CREDITS_TERRA, -1,-1,-1,-1 },
{ "ENVIA", do_send, NULL, MEMO_HELP_SEND, -1,-1,-1,-1 },
{ "SEND", do_send, NULL, MEMO_HELP_SEND, -1,-1,-1,-1 },
{ "CANCELA", do_cancel, NULL, MEMO_HELP_CANCEL, -1,-1,-1,-1 },
{ "CANCEL", do_cancel, NULL, MEMO_HELP_CANCEL, -1,-1,-1,-1 },
{ "LISTA", do_list, NULL, MEMO_HELP_LIST, -1,-1,-1,-1 },
{ "LIST", do_list, NULL, MEMO_HELP_LIST, -1,-1,-1,-1 },
{ "LEE", do_read, NULL, MEMO_HELP_READ, -1,-1,-1,-1 },
{ "READ", do_read, NULL, MEMO_HELP_READ, -1,-1,-1,-1 },
{ "BORRA", do_del, NULL, MEMO_HELP_DEL, -1,-1,-1,-1 },
{ "DEL", do_del, NULL, MEMO_HELP_DEL, -1,-1,-1,-1 },
{ "SET", do_set, NULL, MEMO_HELP_SET, -1,-1,-1,-1 },
{ "SET NOTIFY", NULL, NULL, MEMO_HELP_SET_NOTIFY, -1,-1,-1,-1 },
{ "SET FULLMEMO", NULL, NULL, MEMO_HELP_SET_FULLMEMO, -1,-1,-1,-1 },
{ "SET LIMIT", NULL, NULL, -1,
MEMO_HELP_SET_LIMIT, MEMO_SERVADMIN_HELP_SET_LIMIT,
MEMO_SERVADMIN_HELP_SET_LIMIT, MEMO_SERVADMIN_HELP_SET_LIMIT },
{ "INFO", do_info, NULL, -1,
MEMO_HELP_INFO, MEMO_SERVADMIN_HELP_INFO,
MEMO_SERVADMIN_HELP_INFO, MEMO_SERVADMIN_HELP_INFO },
{ NULL }
};
/*************************************************************************/
/*************************************************************************/
/* MemoServ initialization. */
void ms_init(void)
{
Command *cmd;
cmd = lookup_cmd(cmds, "SET LIMIT");
if (cmd)
cmd->help_param1 = (char *)(long)MSMaxMemos;
}
/*************************************************************************/
/* memoserv: Main MemoServ routine.
* Note that the User structure passed to the do_* routines will
* always be valid (non-NULL) and will always have a valid
* NickInfo pointer in the `ni' field.
*/
void memoserv(const char *source, char *buf)
{
char *cmd, *s;
User *u = finduser(source);
if (!u) {
log("%s: user record for %s not found", s_MemoServ, source);
privmsg(s_MemoServ, source,
getstring((NickInfo *)NULL, USER_RECORD_NOT_FOUND));
return;
}
cmd = strtok(buf, " ");
if (!cmd) {
return;
} else if (stricmp(cmd, "\1PING") == 0) {
if (!(s = strtok(NULL, "")))
s = "\1";
notice(s_MemoServ, source, "\1PING %s", s);
} else if (stricmp(cmd, "\1VERSION\1") == 0) {
notice(s_MemoServ, source, "\1VERSION ircservices-%s+Terra-%s %s -- %s\1",
version_number, version_terra, s_MemoServ, version_build);
} else if (skeleton) {
notice_lang(s_MemoServ, u, SERVICE_OFFLINE, s_MemoServ);
} else {
if (!u->ni && stricmp(cmd, "HELP") != 0)
notice_lang(s_MemoServ, u, NICK_NOT_REGISTERED_HELP, s_NickServ);
else
run_cmd(s_MemoServ, u, cmds, cmd);
}
}
/*************************************************************************/
/* Compatibility memo load routine. */
#define SAFE(x) do { \
if ((x) < 0) { \
if (!forceload) \
fatal("Read error on memo.db"); \
failed = 1; \
break; \
} \
} while (0)
void load_old_ms_dbase(void)
{
dbFILE *f = NULL;
int ver = 0, i, j, c;
NickInfo *ni;
Memo *memos;
struct memolist_ {
struct memolist_ *next, *prev;
char nick[NICKMAX];
long n_memos;
Memo *memos;
long reserved[4];
} old_memolist;
struct {
char sender[NICKMAX];
long number;
time_t time;
char *text;
short flags;
short reserved_s;
long reserved[3];
} oldmemo;
int failed = 0;
// if (!(f = open_db(s_MemoServ, "memo.db", "r")))
// return;
switch (ver) {
case 4:
case 3:
case 2:
case 1:
for (i = 33; i < 256 && !failed; ++i) {
while ((c = getc_db(f)) != 0) {
if (c != 1)
fatal("Invalid format in memo.db");
SAFE(read_variable(old_memolist, f));
if (debug >= 3)
log("debug: load_old_ms_dbase: got memolist for %s",
old_memolist.nick);
old_memolist.memos = memos =
smalloc(sizeof(Memo) * old_memolist.n_memos);
for (j = 0; j < old_memolist.n_memos; j++, memos++) {
SAFE(read_variable(oldmemo, f));
strscpy(memos->sender, oldmemo.sender, NICKMAX);
memos->number = oldmemo.number;
memos->time = oldmemo.time;
memos->flags = oldmemo.flags;
}
memos = old_memolist.memos;
for (j = 0; j < old_memolist.n_memos; j++) {
if (read_string(&memos[j].text, f) < 0)
fatal("Read error on memo.db");
}
ni = findnick(old_memolist.nick);
if (ni) {
ni->memos.memocount = old_memolist.n_memos;
ni->memos.memos = old_memolist.memos;
}
}
}
break;
default:
fatal("Unsupported version number (%d) on memo.db", ver);
} /* switch (version) */
close_db(f);
}
/*************************************************************************/
/* check_memos: See if the given user has any unread memos, and send a
* NOTICE to that user if so (and if the appropriate flag is
* set).
*/
void check_memos(User *u)
{
NickInfo *ni;
int i, newcnt = 0;
if (!(ni = u->ni) || !nick_recognized(u) ||
!(ni->flags & NI_MEMO_SIGNON))
return;
for (i = 0; i < ni->memos.memocount; i++) {
if (ni->memos.memos[i].flags & MF_UNREAD)
newcnt++;
}
if (newcnt > 0) {
notice_lang(s_MemoServ, u,
newcnt==1 ? MEMO_HAVE_NEW_MEMO : MEMO_HAVE_NEW_MEMOS, newcnt);
if (newcnt == 1 && (ni->memos.memos[i-1].flags & MF_UNREAD)) {
notice_lang(s_MemoServ, u, MEMO_TYPE_READ_LAST, s_MemoServ);
} else if (newcnt == 1) {
for (i = 0; i < ni->memos.memocount; i++) {
if (ni->memos.memos[i].flags & MF_UNREAD)
break;
}
notice_lang(s_MemoServ, u, MEMO_TYPE_READ_NUM, s_MemoServ,
ni->memos.memos[i].number);
} else {
notice_lang(s_MemoServ, u, MEMO_TYPE_LIST_NEW, s_MemoServ);
}
}
if (ni->memos.memomax > 0 && ni->memos.memocount >= ni->memos.memomax) {
if (ni->memos.memocount > ni->memos.memomax)
notice_lang(s_MemoServ, u, MEMO_OVER_LIMIT, ni->memos.memomax);
else
notice_lang(s_MemoServ, u, MEMO_AT_LIMIT, ni->memos.memomax);
}
}
/* Notificacion de Memos de canales
* Al hacer un check_all_cs_memos(),
* hay ke mirar los memos de los canales
* que está en ese momento
* - zoltan 8/12/2000
*/
void check_all_cs_memos(User *u)
{
struct u_chanlist *ul;
ChannelInfo *ci;
for (ul = u->chans ; ul; ul = ul->next)
if ((ci = cs_findchan(ul->chan->name))) {
check_cs_memos(u,ci);
}
}
/* Chequea los memos de canales */
void check_cs_memos(User *u, ChannelInfo *ci)
{
if (!u || !ci)
return; /* No registrado */
if ((!ci->flags & CI_MEMOALERT) || (ci->flags & CI_SUSPENDED))
return; /* Aviso de memos desactivado */
if (check_access(u, ci, CA_MEMO_READ) && (ci->memos.memocount > 0)) {
if (ci->memos.memocount == 1) {
notice(s_MemoServ, u->nick, "3Eo! Tienes12 13 mensaje del canal 12%s.",
ci->name);
// privmsg(s_MemoServ, u->nick, "Escribe 12/msg %s READ %s LAST para leer el mensaje.",
// s_MemoServ, ci->name);
} else {
notice(s_MemoServ, u->nick, "3Eo! Tienes 12%d3 mensajes del canal 12%s.",
ci->memos.memocount, ci->name);
// privmsg(s_MemoServ, u->nick, "Escribe 12/msg %s LIST %s para listar los mensajes.",
// s_MemoServ, ci->name);
}
}
}
/*************************************************************************/
/*********************** MemoServ private routines ***********************/
/*************************************************************************/
/* Return the MemoInfo corresponding to the given nick or channel name.
* Return in `ischan' 1 if the name was a channel name, else 0.
*
* Return in `isverboten' 1 if the nick/chan is forbidden, else 0. This is
* a bit of a hack, but I can't think of any better way to indicate that a
* NULL memoinfo was returned because the nick/chan is forbidden. -TheShadow
*/
static MemoInfo *getmemoinfo(const char *name, int *ischan, int *isverboten)
{
if (*name == '#') {
ChannelInfo *ci;
if (ischan)
*ischan = 1;
ci = cs_findchan(name);
if (ci)
{
if (ci->flags & CI_VERBOTEN) {
*isverboten = 1;
return NULL;
} else {
return &ci->memos;
}
}
else
return NULL;
} else {
NickInfo *ni;
if (ischan)
*ischan = 0;
ni = findnick(name);
if (ni)
{
if (ni->status & NS_VERBOTEN) {
*isverboten = 1;
return NULL;
} else {
return &getlink(ni)->memos;
}
}
else
return NULL;
}
}
/*************************************************************************/
/* Delete a memo by number. Return 1 if the memo was found, else 0. */
static int delmemo(MemoInfo *mi, int num)
{
int i;
for (i = 0; i < mi->memocount; i++) {
if (mi->memos[i].number == num)
break;
}
if (i < mi->memocount) {
free(mi->memos[i].text); /* Deallocate memo text memory */
mi->memocount--; /* One less memo now */
if (i < mi->memocount) /* Move remaining memos down a slot */
memmove(mi->memos + i, mi->memos + i+1,
sizeof(Memo) * (mi->memocount - i));
if (mi->memocount == 0) { /* If no more memos, free array */
free(mi->memos);
mi->memos = NULL;
}
return 1;
} else {
return 0;
}
}
/*************************************************************************/
/*********************** MemoServ command routines ***********************/
/*************************************************************************/
/* Return a help message. */
static void do_help(User *u)
{
char *cmd = strtok(NULL, "");
if (!cmd) {
notice_help(s_MemoServ, u, MEMO_HELP, s_ChanServ);
} else {
help_cmd(s_MemoServ, u, cmds, cmd);
}
}
/*************************************************************************/
static void do_credits(User *u)
{
notice_lang(s_MemoServ, u, SERVICES_CREDITS_TERRA);
}
/*************************************************************************/
/* Send a memo to a nick/channel. */
static void do_send(User *u)
{
char *source = u->nick;
int ischan, isverboten;
MemoInfo *mi;
Memo *m;
char *name = strtok(NULL, " ");
char *text = strtok(NULL, "");
time_t now = time(NULL);
int is_servadmin = is_services_admin(u);
if (!text) {
syntax_error(s_MemoServ, u, "SEND", MEMO_SEND_SYNTAX);
} else if (!nick_recognized(u)) {
notice_lang(s_MemoServ, u, NICK_IDENTIFY_REQUIRED, s_NickServ);
} else if (!(mi = getmemoinfo(name, &ischan, &isverboten))) {
if (isverboten)
notice_lang(s_MemoServ, u,
ischan ? CHAN_X_FORBIDDEN : NICK_X_FORBIDDEN,
name);
else
notice_lang(s_MemoServ, u,
ischan ? CHAN_X_NOT_REGISTERED : NICK_X_NOT_REGISTERED, name);
} else if (MSSendDelay > 0 &&
u && u->lastmemosend+MSSendDelay > now && !is_services_oper(u)) {
u->lastmemosend = now;
notice_lang(s_MemoServ, u, MEMO_SEND_PLEASE_WAIT, MSSendDelay);
} else if (mi->memomax == 0 && !is_servadmin) {
notice_lang(s_MemoServ, u, MEMO_X_GETS_NO_MEMOS, name);
} else if (mi->memomax > 0 && mi->memocount >= mi->memomax
&& !is_servadmin) {
NickInfo *ni;
notice_lang(s_MemoServ, u, MEMO_X_HAS_TOO_MANY_MEMOS, name);
if (!ischan) {
if ((ni = findnick(name)) && ni->msg_fullmemo)
notice_lang(s_MemoServ, u, MEMO_X_FULLMEMO, ni->msg_fullmemo);
}
} else {
u->lastmemosend = now;
mi->memocount++;
mi->memos = srealloc(mi->memos, sizeof(Memo) * mi->memocount);
m = &mi->memos[mi->memocount-1];
strscpy(m->sender, source, NICKMAX);
if (mi->memocount > 1) {
m->number = m[-1].number + 1;
if (m->number < 1) {
int i;
for (i = 0; i < mi->memocount; i++)
mi->memos[i].number = i+1;
}
} else {
m->number = 1;
}
m->time = time(NULL);
m->text = sstrdup(text);
m->flags = MF_UNREAD;
notice_lang(s_MemoServ, u, MEMO_SENT, name);
if (!ischan) {
NickInfo *ni = getlink(findnick(name));
if (ni->flags & NI_MEMO_RECEIVE) {
if (MSNotifyAll) {
for (u = firstuser(); u; u = nextuser()) {
if (u->real_ni == ni) {
notice_lang(s_MemoServ, u, MEMO_NEW_MEMO_ARRIVED,
source, s_MemoServ, m->number);
}
}
} else {
u = finduser(name);
/* Solo avisar si está identificado */
if (u && (u->real_ni->status & NS_RECOGNIZED)) {
notice_lang(s_MemoServ, u, MEMO_NEW_MEMO_ARRIVED,
source, s_MemoServ, m->number);
}
} /* if (MSNotifyAll) */
} /* if (flags & MEMO_RECEIVE) */
} /* if (!ischan) */
} /* if command is valid */
}
/*************************************************************************/
/* Cancela el memo enviado por si has equivocado
* de usuario o de mensaje y quieres borrarlo
* solo borra SI el memo AUN NO está leido por
* el remitente
* - zoltan 25 Noviembre 2000
*/
static void do_cancel(User *u)
{
char *nick = strtok(NULL, " ");
MemoInfo *mi;
int ischan, isverboten;
if (!nick) {
syntax_error(s_MemoServ, u, "CANCEL", MEMO_CANCEL_SYNTAX);
} else if (!nick_recognized(u)) {
notice_lang(s_MemoServ, u, NICK_IDENTIFY_REQUIRED, s_NickServ);
} else if (!(mi = getmemoinfo(nick, &ischan, &isverboten))) {
if (isverboten)
notice_lang(s_MemoServ, u,
ischan ? CHAN_X_FORBIDDEN : NICK_X_FORBIDDEN,
nick);
else
notice_lang(s_MemoServ, u,
ischan ? CHAN_X_NOT_REGISTERED : NICK_X_NOT_REGISTERED, nick);
} else {
int i;
/* Empiezo desde el final, ya que solo quiero
* ver memos NO Leidos y los no leidos suelen
* estar en los ultimos puestos
*/
for (i = mi->memocount -1; i >= 0; i--) {
if ((mi->memos[i].flags & MF_UNREAD) &&
!stricmp(mi->memos[i].sender, u->ni->nick)) {
delmemo(mi, mi->memos[i].number);
notice_lang(s_MemoServ, u, MEMO_CANCEL_SUCCEEDED, nick);
return;
}
}
notice_lang(s_MemoServ, u, MEMO_CANCEL_NONE);
}
}
/*************************************************************************/
/* Display a single memo entry, possibly printing the header first. */
static int list_memo(User *u, int index, MemoInfo *mi, int *sent_header,
int new, const char *chan)
{
Memo *m;
char timebuf[64];
struct tm tm;
if (index < 0 || index >= mi->memocount)
return 0;
if (!*sent_header) {
if (chan) {
notice_lang(s_MemoServ, u,
new ? MEMO_LIST_CHAN_NEW_MEMOS : MEMO_LIST_CHAN_MEMOS,
chan, s_MemoServ, chan);
} else {
notice_lang(s_MemoServ, u,
new ? MEMO_LIST_NEW_MEMOS : MEMO_LIST_MEMOS,
u->nick, s_MemoServ);
}
notice_lang(s_MemoServ, u, MEMO_LIST_HEADER);
*sent_header = 1;
}
m = &mi->memos[index];
tm = *localtime(&m->time);
strftime_lang(timebuf, sizeof(timebuf),
u, STRFTIME_DATE_TIME_FORMAT, &tm);
timebuf[sizeof(timebuf)-1] = 0; /* just in case */
notice_lang(s_MemoServ, u, MEMO_LIST_FORMAT,
(m->flags & MF_UNREAD) ? '*' : ' ',
m->number, m->sender, timebuf);
return 1;
}
static int list_memo_callback(User *u, int num, va_list args)
{
MemoInfo *mi = va_arg(args, MemoInfo *);
int *sent_header = va_arg(args, int *);
const char *chan = va_arg(args, const char *);
int i;
for (i = 0; i < mi->memocount; i++) {
if (mi->memos[i].number == num)
break;
}
/* Range checking done by list_memo() */
return list_memo(u, i, mi, sent_header, 0, chan);
}
/* List the memos (if any) for the source nick or given channel. */
static void do_list(User *u)
{
char *param = strtok(NULL, " "), *chan = NULL;
ChannelInfo *ci;
MemoInfo *mi;
Memo *m;
int i;
if (param && *param == '#') {
chan = param;
param = strtok(NULL, " ");
if (!(ci = cs_findchan(chan))) {
notice_lang(s_MemoServ, u, CHAN_X_NOT_REGISTERED, chan);
return;
} else if (ci->flags & CI_VERBOTEN) {
notice_lang(s_MemoServ, u, CHAN_X_FORBIDDEN, chan);
return;
} else if (ci->flags & CI_SUSPENDED) {
notice_lang(s_MemoServ, u, CHAN_X_SUSPENDED, chan);
return;
} else if ((!check_access(u, ci, CA_MEMO_READ)) && !is_services_admin(u)) {
notice_lang(s_MemoServ, u, ACCESS_DENIED);
return;
}
mi = &ci->memos;
} else {
if (!nick_identified(u)) {
notice_lang(s_MemoServ, u, NICK_IDENTIFY_REQUIRED, s_NickServ);
return;
}
mi = &u->ni->memos;
}
if (param && !isdigit((int)*param) && stricmp(param, "NEW") != 0) {
syntax_error(s_MemoServ, u, "LIST", MEMO_LIST_SYNTAX);
} else if (mi->memocount == 0) {
if (chan)
notice_lang(s_MemoServ, u, MEMO_X_HAS_NO_MEMOS, chan);
else
notice_lang(s_MemoServ, u, MEMO_HAVE_NO_MEMOS);
} else {
int sent_header = 0;
if (param && isdigit((int)*param)) {
process_numlist(param, NULL, list_memo_callback, u,
mi, &sent_header, chan);
} else {
if (param) {
for (i = 0, m = mi->memos; i < mi->memocount; i++, m++) {
if (m->flags & MF_UNREAD)
break;
}
if (i == mi->memocount) {
if (chan)
notice_lang(s_MemoServ, u, MEMO_X_HAS_NO_NEW_MEMOS,
chan);
else
notice_lang(s_MemoServ, u, MEMO_HAVE_NO_NEW_MEMOS);
return;
}
}
for (i = 0, m = mi->memos; i < mi->memocount; i++, m++) {
if (param && !(m->flags & MF_UNREAD))
continue;
list_memo(u, i, mi, &sent_header, param != NULL, chan);
}
}
}
}
/*************************************************************************/
/* Send a single memo to the given user. */
static int read_memo(User *u, int index, MemoInfo *mi, const char *chan)
{
Memo *m;
char timebuf[64];
struct tm tm;
if (index < 0 || index >= mi->memocount)
return 0;
m = &mi->memos[index];
tm = *localtime(&m->time);
strftime_lang(timebuf, sizeof(timebuf),
u, STRFTIME_DATE_TIME_FORMAT, &tm);
timebuf[sizeof(timebuf)-1] = 0;
if (chan)
notice_lang(s_MemoServ, u, MEMO_CHAN_HEADER, m->number,
m->sender, timebuf, s_MemoServ, chan, m->number);
else
notice_lang(s_MemoServ, u, MEMO_HEADER, m->number,
m->sender, timebuf, s_MemoServ, m->number);
notice_lang(s_MemoServ, u, MEMO_TEXT, m->text);
m->flags &= ~MF_UNREAD;
return 1;
}
static int read_memo_callback(User *u, int num, va_list args)
{
MemoInfo *mi = va_arg(args, MemoInfo *);
const char *chan = va_arg(args, const char *);
int i;
for (i = 0; i < mi->memocount; i++) {
if (mi->memos[i].number == num)
break;
}
/* Range check done in read_memo */
return read_memo(u, i, mi, chan);
}
/* Read memos. */
static void do_read(User *u)
{
MemoInfo *mi;
ChannelInfo *ci;
char *numstr = strtok(NULL, " "), *chan = NULL;
int num, count;
if (numstr && *numstr == '#') {
chan = numstr;
numstr = strtok(NULL, " ");
if (!(ci = cs_findchan(chan))) {
notice_lang(s_MemoServ, u, CHAN_X_NOT_REGISTERED, chan);
return;
} else if (ci->flags & CI_VERBOTEN) {
notice_lang(s_MemoServ, u, CHAN_X_FORBIDDEN, chan);
return;
} else if (ci->flags & CI_SUSPENDED) {
notice_lang(s_MemoServ, u, CHAN_X_SUSPENDED, chan);
return;
} else if ((!check_access(u, ci, CA_MEMO_READ)) && !is_services_admin(u)) {
notice_lang(s_MemoServ, u, ACCESS_DENIED);
return;
}
mi = &ci->memos;
} else {
if (!nick_identified(u)) {
notice_lang(s_MemoServ, u, NICK_IDENTIFY_REQUIRED, s_NickServ);
return;
}
mi = &u->ni->memos;
}
num = numstr ? atoi(numstr) : -1;
if (!numstr || (stricmp(numstr,"LAST") != 0 && stricmp(numstr,"NEW") != 0
&& num <= 0)) {
syntax_error(s_MemoServ, u, "READ", MEMO_READ_SYNTAX);
} else if (mi->memocount == 0) {
if (chan)
notice_lang(s_MemoServ, u, MEMO_X_HAS_NO_MEMOS, chan);
else
notice_lang(s_MemoServ, u, MEMO_HAVE_NO_MEMOS);
} else {
int i;
if (stricmp(numstr, "NEW") == 0) {
int readcount = 0;
for (i = 0; i < mi->memocount; i++) {
if (mi->memos[i].flags & MF_UNREAD) {
read_memo(u, i, mi, chan);
readcount++;
}
}
if (!readcount) {
if (chan)
notice_lang(s_MemoServ, u, MEMO_X_HAS_NO_NEW_MEMOS, chan);
else
notice_lang(s_MemoServ, u, MEMO_HAVE_NO_NEW_MEMOS);
}
} else if (stricmp(numstr, "LAST") == 0) {
for (i = 0; i < mi->memocount-1; i++)
;
read_memo(u, i, mi, chan);
} else { /* number[s] */
if (!process_numlist(numstr, &count, read_memo_callback, u,
mi, chan)) {
if (count == 1)
notice_lang(s_MemoServ, u, MEMO_DOES_NOT_EXIST, num);
else
notice_lang(s_MemoServ, u, MEMO_LIST_NOT_FOUND, numstr);
}
}
}
}
/*************************************************************************/
/* Delete a single memo from a MemoInfo. */
static int del_memo_callback(User *u, int num, va_list args)
{
MemoInfo *mi = va_arg(args, MemoInfo *);
int *last = va_arg(args, int *);
int *last0 = va_arg(args, int *);
char **end = va_arg(args, char **);
int *left = va_arg(args, int *);
if (delmemo(mi, num)) {
if (num != (*last)+1) {
if (*last != -1) {
int len;
if (*last0 != *last)
len = snprintf(*end, *left, ",%d-%d", *last0, *last);
else
len = snprintf(*end, *left, ",%d", *last);
*end += len;
*left -= len;
}
*last0 = num;
}
*last = num;
return 1;
} else {
return 0;
}
}
/* Delete memos. */
static void do_del(User *u)
{
MemoInfo *mi;
ChannelInfo *ci;
char *numstr = strtok(NULL, ""), *chan = NULL;
int last, last0, i;
char buf[BUFSIZE], *end;
int delcount, count, left;
if (numstr && *numstr == '#') {
chan = strtok(numstr, " ");
numstr = strtok(NULL, "");
if (!(ci = cs_findchan(chan))) {
notice_lang(s_MemoServ, u, CHAN_X_NOT_REGISTERED, chan);
return;
} else if (ci->flags & CI_VERBOTEN) {
notice_lang(s_MemoServ, u, CHAN_X_FORBIDDEN, chan);
return;
} else if (ci->flags & CI_SUSPENDED) {
notice_lang(s_MemoServ, u, CHAN_X_SUSPENDED, chan);
return;
} else if ((!check_access(u, ci, CA_MEMO_DEL)) && !is_services_admin(u)) {
notice_lang(s_MemoServ, u, ACCESS_DENIED);
return;
}
mi = &ci->memos;
} else {
if (!nick_identified(u)) {
notice_lang(s_MemoServ, u, NICK_IDENTIFY_REQUIRED, s_NickServ);
return;
}
mi = &u->ni->memos;
}
if (!numstr || (!isdigit((int)*numstr) && stricmp(numstr, "ALL") != 0)) {
syntax_error(s_MemoServ, u, "DEL", MEMO_DEL_SYNTAX);
} else if (mi->memocount == 0) {
if (chan)
notice_lang(s_MemoServ, u, MEMO_X_HAS_NO_MEMOS, chan);
else
notice_lang(s_MemoServ, u, MEMO_HAVE_NO_MEMOS);
} else {
if (isdigit((int)*numstr)) {
/* Delete a specific memo or memos. */
last = -1; /* Last memo deleted */
last0 = -1; /* Beginning of range of last memos deleted */
end = buf;
left = sizeof(buf);
delcount = process_numlist(numstr, &count, del_memo_callback, u, mi,
&last, &last0, &end, &left);
if (last != -1) {
/* Some memos got deleted; tell them which ones. */
if (delcount > 1) {
if (last0 != last)
end += snprintf(end, sizeof(buf)-(end-buf),
",%d-%d", last0, last);
else
end += snprintf(end, sizeof(buf)-(end-buf),
",%d", last);
/* "buf+1" here because *buf == ',' */
notice_lang(s_MemoServ, u, MEMO_DELETED_SEVERAL, buf+1);
} else {
notice_lang(s_MemoServ, u, MEMO_DELETED_ONE, last);
}
} else {
/* No memos were deleted. Tell them so. */
if (count == 1)
notice_lang(s_MemoServ, u, MEMO_DOES_NOT_EXIST,
atoi(numstr));
else
notice_lang(s_MemoServ, u, MEMO_DELETED_NONE);
}
} else {
/* Delete all memos. */
for (i = 0; i < mi->memocount; i++)
free(mi->memos[i].text);
free(mi->memos);
mi->memos = NULL;
mi->memocount = 0;
notice_lang(s_MemoServ, u, MEMO_DELETED_ALL);
}
}
}
/*************************************************************************/
static void do_set(User *u)
{
char *cmd = strtok(NULL, " ");
char *param = strtok(NULL, "");
MemoInfo *mi = &u->ni->memos;
if (readonly) {
notice_lang(s_MemoServ, u, MEMO_SET_DISABLED);
return;
}
if (!param) {
syntax_error(s_MemoServ, u, "SET", MEMO_SET_SYNTAX);
} else if (!nick_identified(u)) {
notice_lang(s_MemoServ, u, NICK_IDENTIFY_REQUIRED, s_NickServ);
return;
} else if (stricmp(cmd, "NOTIFY") == 0) {
do_set_notify(u, mi, param);
} else if (stricmp(cmd, "LIMIT") == 0) {
do_set_limit(u, mi, param);
} else if (stricmp(cmd, "FULLMEMO") == 0) {
do_set_fullmemo(u, mi, param);
} else {
notice_lang(s_MemoServ, u, MEMO_SET_UNKNOWN_OPTION, strupper(cmd));
notice_lang(s_MemoServ, u, MORE_INFO, s_MemoServ, "SET");
}
}
/*************************************************************************/
static void do_set_notify(User *u, MemoInfo *mi, char *param)
{
if (stricmp(param, "ON") == 0) {
u->ni->flags |= NI_MEMO_SIGNON | NI_MEMO_RECEIVE;
notice_lang(s_MemoServ, u, MEMO_SET_NOTIFY_ON, s_MemoServ);
} else if (stricmp(param, "LOGON") == 0) {
u->ni->flags |= NI_MEMO_SIGNON;
u->ni->flags &= ~NI_MEMO_RECEIVE;
notice_lang(s_MemoServ, u, MEMO_SET_NOTIFY_LOGON, s_MemoServ);
} else if (stricmp(param, "NEW") == 0) {
u->ni->flags &= ~NI_MEMO_SIGNON;
u->ni->flags |= NI_MEMO_RECEIVE;
notice_lang(s_MemoServ, u, MEMO_SET_NOTIFY_NEW, s_MemoServ);
} else if (stricmp(param, "OFF") == 0) {
u->ni->flags &= ~(NI_MEMO_SIGNON | NI_MEMO_RECEIVE);
notice_lang(s_MemoServ, u, MEMO_SET_NOTIFY_OFF, s_MemoServ);
} else {
syntax_error(s_MemoServ, u, "SET NOTIFY", MEMO_SET_NOTIFY_SYNTAX);
}
}
/*************************************************************************/
static void do_set_limit(User *u, MemoInfo *mi, char *param)
{
char *p1 = strtok(param, " ");
char *p2 = strtok(NULL, " ");
char *p3 = strtok(NULL, " ");
char *user = NULL, *chan = NULL;
int32 limit;
NickInfo *ni = u->ni;
ChannelInfo *ci = NULL;
int is_servadmin = is_services_admin(u);
if (p1 && *p1 == '#') {
chan = p1;
p1 = p2;
p2 = p3;
p3 = strtok(NULL, " ");
if (!(ci = cs_findchan(chan))) {
notice_lang(s_MemoServ, u, CHAN_X_NOT_REGISTERED, chan);
return;
} else if (ci->flags & CI_VERBOTEN) {
notice_lang(s_MemoServ, u, CHAN_X_FORBIDDEN, chan);
return;
} else if (ci->flags & CI_SUSPENDED) {
notice_lang(s_MemoServ, u, CHAN_X_SUSPENDED, chan);
return;
} else if (!is_servadmin && !check_access(u, ci, CA_MEMO_DEL)) {
notice_lang(s_MemoServ, u, ACCESS_DENIED);
return;
}
mi = &ci->memos;
}
if (is_servadmin) {
if (p2 && stricmp(p2, "HARD") != 0 && !chan) {
if (!(ni = findnick(p1))) {
notice_lang(s_MemoServ, u, NICK_X_NOT_REGISTERED, p1);
return;
}
ni = getlink(ni);
user = p1;
mi = &ni->memos;
p1 = p2;
p2 = p3;
} else if (!p1) {
syntax_error(s_MemoServ, u, "SET LIMIT",
MEMO_SET_LIMIT_SERVADMIN_SYNTAX);
return;
}
if ((!isdigit((int)*p1) && stricmp(p1, "NONE") != 0) ||
(p2 && stricmp(p2, "HARD") != 0)) {
syntax_error(s_MemoServ, u, "SET LIMIT",
MEMO_SET_LIMIT_SERVADMIN_SYNTAX);
return;
}
if (chan) {
if (p2)
ci->flags |= CI_MEMO_HARDMAX;
else
ci->flags &= ~CI_MEMO_HARDMAX;
} else {
if (p2)
ni->flags |= NI_MEMO_HARDMAX;