This repository has been archived by the owner on Sep 2, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathinspircd.cpp
executable file
·3547 lines (3177 loc) · 83.3 KB
/
inspircd.cpp
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
/* +------------------------------------+
* | Inspire Internet Relay Chat Daemon |
* +------------------------------------+
*
* Inspire is copyright (C) 2002-2003 ChatSpike-Dev.
* E-mail:
*
* Written by Craig Edwards, Craig McLure, and others.
* This program is free but copyrighted software; see
* the file COPYING for details.
*
* ---------------------------------------------------
$Log: inspircd.cpp,v $
Revision 1.31 2003/01/14 00:46:02 brain
Added m_cloaking.so module, provides host masking
Revision 1.30 2003/01/13 22:30:50 brain
Added Admin class (holds /admin info for modules)
Added methods to Server class
Revision 1.29 2003/01/13 00:43:29 brain
Added Server class
Added more code to example module demonstrating use of Server class
Revision 1.28 2003/01/12 17:40:44 brain
./configure improved by Craig (better prompts, dir creation)
'/stats z' added detail
Revision 1.27 2003/01/12 16:49:53 brain
Added '/stats z'
Revision 1.26 2003/01/12 15:01:18 brain
Added hostname/ip caching to speed up connects
Revision 1.25 2003/01/11 21:39:57 brain
Made ircd cache message of the day in a vector (faster!)
Added support for multiple lines of /NAMES on large channels
Revision 1.24 2003/01/11 19:00:10 brain
Added /USERHOST command
Revision 1.23 2003/01/11 17:57:28 brain
Added '/STATS O'
Added more module error checking
Revision 1.22 2003/01/11 00:48:44 brain
removed random debug output
Revision 1.21 2003/01/11 00:06:46 brain
Fixed random crash on nickchange
Fine tuned ability to handle >300 users
Revision 1.20 2003/01/09 22:24:59 brain
added '/stats L' (connect-info)
Revision 1.19 2003/01/09 21:38:51 brain
'/stats u' support added (server uptime)
Revision 1.18 2003/01/09 21:09:50 brain
added '/stats M' command
Revision 1.17 2003/01/08 22:11:38 brain
Added extra dynamic module support, new methods to Module class
Revision 1.16 2003/01/08 17:48:48 brain
fixed "user lingering" problem in kill_link
Revision 1.15 2003/01/07 23:17:51 brain
Fixed wallops and command parameter counting bugs
Revision 1.14 2003/01/07 20:47:34 brain
Fixes random crash on nickchange (must keep classfactory pointers!)
Revision 1.13 2003/01/07 19:57:56 brain
Dynamix module support, preliminary release
Revision 1.12 2003/01/07 01:01:30 brain
Changed command table to a vector of command_t types
Revision 1.11 2003/01/06 23:43:30 brain
extra debug output
Revision 1.10 2003/01/06 23:38:29 brain
just playing with header tags
* ---------------------------------------------------
*/
/* Now with added unF! ;) */
#include "inspircd.h"
#include "inspircd_io.h"
#include "inspircd_util.h"
#include "inspircd_config.h"
#include <unistd.h>
#include <fcntl.h>
#include <sys/errno.h>
#include <sys/ioctl.h>
#include <sys/utsname.h>
#include <cstdio>
#include <time.h>
#include <string>
#include <hash_map.h>
#include <sstream>
#include <vector>
#include "users.h"
#include "ctables.h"
#include "globals.h"
#include "modules.h"
#include "dynamic.h"
using namespace std;
char ServerName[MAXBUF];
char Network[MAXBUF];
char ServerDesc[MAXBUF];
char AdminName[MAXBUF];
char AdminEmail[MAXBUF];
char AdminNick[MAXBUF];
char diepass[MAXBUF];
char restartpass[MAXBUF];
char motd[MAXBUF];
char rules[MAXBUF];
char list[MAXBUF];
char PrefixQuit[MAXBUF];
char DieValue[MAXBUF];
int debugging = 0;
int MODCOUNT = -1;
time_t startup_time = time(NULL);
template<> struct hash<in_addr>
{
size_t operator()(const struct in_addr &a) const
{
size_t q;
memcpy(&q,&a,sizeof(size_t));
return q;
}
};
template<> struct hash<string>
{
size_t operator()(const string &s) const
{
char a[MAXBUF];
static struct hash<const char *> strhash;
strcpy(a,s.c_str());
strlower(a);
return strhash(a);
}
};
struct StrHashComp
{
bool operator()(const string& s1, const string& s2) const
{
char a[MAXBUF],b[MAXBUF];
strcpy(a,s1.c_str());
strcpy(b,s2.c_str());
return (strcasecmp(a,b) == 0);
}
};
struct InAddr_HashComp
{
bool operator()(const in_addr &s1, const in_addr &s2) const
{
size_t q;
size_t p;
memcpy(&q,&s1,sizeof(size_t));
memcpy(&p,&s2,sizeof(size_t));
return (q == p);
}
};
typedef hash_map<string, userrec*, hash<string>, StrHashComp> user_hash;
typedef hash_map<string, chanrec*, hash<string>, StrHashComp> chan_hash;
typedef hash_map<in_addr,string*, hash<in_addr>, InAddr_HashComp> address_cache;
typedef vector<command_t> command_table;
typedef DLLFactory<ModuleFactory> ircd_module;
typedef vector<string> file_cache;
user_hash clientlist;
chan_hash chanlist;
command_table cmdlist;
file_cache MOTD;
file_cache RULES;
address_cache IP;
vector<Module*> modules(255);
vector<ircd_module*> factory(255);
struct linger linger = { 0 };
char bannerBuffer[MAXBUF];
int boundPortCount = 0;
/* prototypes */
int has_channel(struct userrec *u, struct chanrec *c);
int usercount(struct chanrec *c);
void update_stats_l(int fd,int data_out);
/* chop a string down to 512 characters and preserve linefeed (irc max
* line length) */
void chop(char* str)
{
if (strlen(str) > 512)
{
str[510] = '\r';
str[511] = '\n';
str[512] = '\0';
}
}
extern "C" string getservername()
{
return ServerName;
}
extern "C" string getnetworkname()
{
return Network;
}
extern "C" string getadminname()
{
return AdminName;
}
extern "C" string getadminemail()
{
return AdminEmail;
}
extern "C" string getadminnick()
{
return AdminNick;
}
extern "C" void debug(char *text, ...)
{
char textbuffer[MAXBUF];
va_list argsPtr;
FILE *f;
if (debugging)
{
f = fopen("ircd.log","a+");
if (f)
{
va_start (argsPtr, text);
vsnprintf(textbuffer, MAXBUF, text, argsPtr);
va_end(argsPtr);
fprintf(f,"%s\n",textbuffer);
fclose(f);
}
else
{
printf("Can't write log file, bailing!!!");
Exit(ERROR);
}
}
}
void readfile(file_cache &F, char* fname)
{
FILE* file;
char linebuf[MAXBUF];
debug("readfile: loading %s",fname);
F.clear();
file = fopen(fname,"r");
if (file)
{
while (!feof(file))
{
fgets(linebuf,sizeof(linebuf),file);
linebuf[strlen(linebuf)-1]='\0';
if (!strcmp(linebuf,""))
{
strcpy(linebuf," ");
}
if (!feof(file))
{
F.push_back(linebuf);
}
}
fclose(file);
}
else
{
debug("readfile: failed to load file: %s",fname);
}
debug("readfile: loaded %s, %d lines",fname,F.size());
}
void ReadConfig(void)
{
char dbg[MAXBUF];
ConfValue("server","name",0,ServerName);
ConfValue("server","description",0,ServerDesc);
ConfValue("server","network",0,Network);
ConfValue("admin","name",0,AdminName);
ConfValue("admin","email",0,AdminEmail);
ConfValue("admin","nick",0,AdminNick);
ConfValue("files","motd",0,motd);
ConfValue("files", "rules",0,rules);
ConfValue("power", "diepass",0,diepass);
ConfValue("power", "restartpass",0,restartpass);
ConfValue("options","prefixquit",0,PrefixQuit);
ConfValue("die", "value",0,DieValue);
ConfValue("options","debug",0,dbg);
debugging = 0;
if (!strcmp(dbg,"on"))
{
debugging = 1;
}
readfile(MOTD,motd);
readfile(RULES,rules);
}
void Blocking(int s)
{
int flags;
debug("Blocking: %d",s);
flags = fcntl(s, F_GETFL, 0);
fcntl(s, F_SETFL, flags ^ O_NONBLOCK);
}
void NonBlocking(int s)
{
int flags;
debug("NonBlocking: %d",s);
flags = fcntl(s, F_GETFL, 0);
fcntl(s, F_SETFL, flags | O_NONBLOCK);
}
int CleanAndResolve (char *resolvedHost, const char *unresolvedHost)
{
struct hostent *hostPtr = NULL;
struct in_addr addr;
memset (resolvedHost, '\0',MAXBUF);
if(unresolvedHost == NULL)
return(ERROR);
if ((inet_aton(unresolvedHost,&addr)) == 0)
return(ERROR);
hostPtr = gethostbyaddr ((char *)&addr.s_addr,sizeof(addr.s_addr),AF_INET);
if (hostPtr != NULL)
snprintf(resolvedHost,MAXBUF,"%s",hostPtr->h_name);
else
snprintf(resolvedHost,MAXBUF,"%s",unresolvedHost);
return (TRUE);
}
/* write formatted text to a socket, in same format as printf */
extern "C" void Write(int sock,char *text, ...)
{
char textbuffer[MAXBUF];
va_list argsPtr;
char tb[MAXBUF];
va_start (argsPtr, text);
if (!sock)
{
return;
}
vsnprintf(textbuffer, MAXBUF, text, argsPtr);
va_end(argsPtr);
sprintf(tb,"%s\r\n",textbuffer);
chop(tb);
write(sock,tb,strlen(tb));
update_stats_l(sock,strlen(tb)); /* add one line-out to stats L for this fd */
}
/* write a server formatted numeric response to a single socket */
extern "C" void WriteServ(int sock, char* text, ...)
{
char textbuffer[MAXBUF],tb[MAXBUF];
va_list argsPtr;
va_start (argsPtr, text);
if (!sock)
{
return;
}
vsnprintf(textbuffer, MAXBUF, text, argsPtr);
va_end(argsPtr);
sprintf(tb,":%s %s\r\n",ServerName,textbuffer);
chop(tb);
debug("WriteServ: %d %s",sock,tb);
write(sock,tb,strlen(tb));
update_stats_l(sock,strlen(tb)); /* add one line-out to stats L for this fd */
}
/* write text from an originating user to originating user */
extern "C" void WriteFrom(int sock, struct userrec *user,char* text, ...)
{
char textbuffer[MAXBUF],tb[MAXBUF];
va_list argsPtr;
va_start (argsPtr, text);
if (!sock)
{
return;
}
vsnprintf(textbuffer, MAXBUF, text, argsPtr);
va_end(argsPtr);
sprintf(tb,":%s!%s@%s %s\r\n",user->nick,user->ident,user->dhost,textbuffer);
chop(tb);
debug("WriteFrom: %d %s",sock,tb);
write(sock,tb,strlen(tb));
update_stats_l(sock,strlen(tb)); /* add one line-out to stats L for this fd */
}
/* write text to an destination user from a source user (e.g. user privmsg) */
extern "C" void WriteTo(struct userrec *source, struct userrec *dest,char *data, ...)
{
char textbuffer[MAXBUF],tb[MAXBUF];
va_list argsPtr;
va_start (argsPtr, data);
if ((!dest) || (!source))
{
return;
}
vsnprintf(textbuffer, MAXBUF, data, argsPtr);
va_end(argsPtr);
chop(tb);
WriteFrom(dest->fd,source,"%s",textbuffer);
}
/* write formatted text from a source user to all users on a channel
* including the sender (NOT for privmsg, notice etc!) */
extern "C" void WriteChannel(struct chanrec* Ptr, struct userrec* user, char* text, ...)
{
char textbuffer[MAXBUF];
va_list argsPtr;
va_start (argsPtr, text);
vsnprintf(textbuffer, MAXBUF, text, argsPtr);
va_end(argsPtr);
for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
{
if (has_channel(i->second,Ptr) && (i->second->fd != 0))
{
WriteTo(user,i->second,"%s",textbuffer);
}
}
}
/* write formatted text from a source user to all users on a channel except
* for the sender (for privmsg etc) */
extern "C" void ChanExceptSender(struct chanrec* Ptr, struct userrec* user, char* text, ...)
{
char textbuffer[MAXBUF];
va_list argsPtr;
va_start (argsPtr, text);
vsnprintf(textbuffer, MAXBUF, text, argsPtr);
va_end(argsPtr);
for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
{
if (has_channel(i->second,Ptr) && (i->second->fd != 0) && (user != i->second))
{
WriteTo(user,i->second,"%s",textbuffer);
}
}
}
/* return 0 or 1 depending if users u and u2 share one or more common channels
* (used by QUIT, NICK etc which arent channel specific notices) */
extern "C" int common_channels(struct userrec *u, struct userrec *u2)
{
int i = 0;
int z = 0;
if ((!u) || (!u2))
{
return 0;
}
for (i = 0; i <= MAXCHANS; i++)
{
for (z = 0; z <= MAXCHANS; z++)
{
if ((u->chans[i].channel == u2->chans[z].channel) && (u->chans[i].channel) && (u2->chans[z].channel) && (u->registered == 7) && (u2->registered == 7))
{
return 1;
}
}
}
return 0;
}
/* write a formatted string to all users who share at least one common
* channel, including the source user e.g. for use in NICK */
extern "C" void WriteCommon(struct userrec *u, char* text, ...)
{
char textbuffer[MAXBUF];
va_list argsPtr;
va_start (argsPtr, text);
vsnprintf(textbuffer, MAXBUF, text, argsPtr);
va_end(argsPtr);
WriteFrom(u->fd,u,"%s",textbuffer);
for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
{
if (common_channels(u,i->second) && (i->second->fd) && (i->second != u))
{
WriteFrom(i->second->fd,u,"%s",textbuffer);
}
}
}
/* write a formatted string to all users who share at least one common
* channel, NOT including the source user e.g. for use in QUIT */
extern "C" void WriteCommonExcept(struct userrec *u, char* text, ...)
{
char textbuffer[MAXBUF];
va_list argsPtr;
va_start (argsPtr, text);
vsnprintf(textbuffer, MAXBUF, text, argsPtr);
va_end(argsPtr);
for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
{
if ((common_channels(u,i->second)) && (u != i->second))
{
WriteFrom(i->second->fd,u,"%s",textbuffer);
}
}
}
extern "C" void WriteOpers(char* text, ...)
{
char textbuffer[MAXBUF];
va_list argsPtr;
va_start (argsPtr, text);
vsnprintf(textbuffer, MAXBUF, text, argsPtr);
va_end(argsPtr);
for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
{
if ((i->second->fd) && (strstr(i->second->modes,"o")))
{
WriteServ(i->second->fd,"NOTICE %s :%s",i->second->nick,textbuffer);
}
}
}
extern "C" void WriteWallOps(struct userrec *source, char* text, ...)
{
int i = 0;
char textbuffer[MAXBUF];
va_list argsPtr;
va_start (argsPtr, text);
vsnprintf(textbuffer, MAXBUF, text, argsPtr);
va_end(argsPtr);
for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
{
if ((i->second->fd) && (strstr(i->second->modes,"w")))
{
WriteTo(source,i->second,"WALLOPS %s",textbuffer);
}
}
}
/* convert a string to lowercase. Note following special circumstances
* taken from RFC 1459. Many "official" server branches still hold to this
* rule so i will too;
*
* Because of IRC's scandanavian origin, the characters {}| are
* considered to be the lower case equivalents of the characters []\,
* respectively. This is a critical issue when determining the
* equivalence of two nicknames.
*/
void strlower(char *n)
{
int i = 0;
if (!n)
{
return;
}
for (i = 0; i <= strlen(n); i++)
{
n[i] = tolower(n[i]);
if (n[i] == '[')
n[i] = '{';
if (n[i] == ']')
n[i] = '}';
if (n[i] == '\\')
n[i] = '|';
}
}
/* verify that a user's nickname is valid */
extern "C" int isnick(const char* n)
{
int i = 0;
char v[MAXBUF];
if (!n)
{
return 0;
}
if (!strcmp(n,""))
{
return 0;
}
if (strlen(n) > NICKMAX-1)
{
return 0;
}
for (i = 0; i <= strlen(n)-1; i++)
{
if ((n[i] < 33) || (n[i] > 125))
{
return 0;
}
/* can't occur ANYWHERE in a nickname! */
if (strchr("<>,./?:;@'~#=+()*&%$£ \"!",n[i]) != NULL)
{
return 0;
}
/* can't occur as the first char of a nickname... */
if ((strchr("0123456789",n[i]) != NULL) && (i == 0))
{
return 0;
}
}
return 1;
}
/* Find a user record by nickname and return a pointer to it */
extern "C" struct userrec* Find(string nick)
{
user_hash::iterator iter = clientlist.find(nick);
if (iter == clientlist.end())
/* Couldn't find it */
return NULL;
return iter->second;
}
void update_stats_l(int fd,int data_out) /* add one line-out to stats L for this fd */
{
for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
{
if ((i->second->fd == fd) && (i->second->fd != 0))
{
i->second->bytes_out += data_out;
i->second->cmds_out++;
}
}
}
/* find a channel record by channel name and return a pointer to it */
extern "C" struct chanrec* FindChan(const char* chan)
{
chan_hash::iterator iter = chanlist.find(chan);
if (iter == chanlist.end())
/* Couldn't find it */
return NULL;
return iter->second;
}
void purge_empty_chans(void)
{
int go_again = 1, purge = 0;
while (go_again)
{
go_again = 0;
for (chan_hash::iterator i = chanlist.begin(); i != chanlist.end(); i++)
{
if (i->second) {
if (!usercount(i->second))
{
/* kill the record */
if (i != chanlist.end())
{
debug("del_channel: destroyed: %s",i->second->name);
delete i->second;
chanlist.erase(i);
go_again = 1;
purge++;
break;
}
}
}
}
}
debug("completed channel purge, killed %d",purge);
}
/* returns the status character for a given user on a channel, e.g. @ for op,
* % for halfop etc. If the user has several modes set, the highest mode
* the user has must be returned. */
extern "C" char* cmode(struct userrec *user, struct chanrec *chan)
{
int i;
for (i = 0; i <= MAXCHANS; i++)
{
if ((user->chans[i].channel == chan) && (chan != NULL))
{
if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
{
return "@";
}
if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
{
return "%";
}
if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
{
return "+";
}
return "";
}
}
}
char scratch[MAXMODES];
char* chanmodes(struct chanrec *chan)
{
strcpy(scratch,"");
if (chan->noexternal)
{
strcat(scratch,"n");
}
if (chan->topiclock)
{
strcat(scratch,"t");
}
if (strcmp(chan->key,""))
{
strcat(scratch,"k");
}
if (chan->limit)
{
strcat(scratch,"l");
}
if (chan->inviteonly)
{
strcat(scratch,"i");
}
if (chan->moderated)
{
strcat(scratch,"m");
}
if (chan->secret)
{
strcat(scratch,"s");
}
if (chan->c_private)
{
strcat(scratch,"p");
}
if (strcmp(chan->key,""))
{
strcat(scratch," ");
strcat(scratch,chan->key);
}
if (chan->limit)
{
char foo[24];
sprintf(foo," %d",chan->limit);
strcat(scratch,foo);
}
debug("chanmodes: %s %s",chan->name,scratch);
return scratch;
}
/* returns the status value for a given user on a channel, e.g. STATUS_OP for
* op, STATUS_VOICE for voice etc. If the user has several modes set, the
* highest mode the user has must be returned. */
int cstatus(struct userrec *user, struct chanrec *chan)
{
int i;
for (i = 0; i <= MAXCHANS; i++)
{
if ((user->chans[i].channel == chan) && (chan != NULL))
{
if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
{
return STATUS_OP;
}
if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
{
return STATUS_HOP;
}
if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
{
return STATUS_VOICE;
}
return STATUS_NORMAL;
}
}
}
/* compile a userlist of a channel into a string, each nick seperated by
* spaces and op, voice etc status shown as @ and + */
void userlist(struct userrec *user,struct chanrec *c)
{
sprintf(list,"353 %s = %s :", user->nick, c->name);
for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
{
if (has_channel(i->second,c) && (i->second->fd != 0))
{
if (isnick(i->second->nick))
{
strcat(list,cmode(i->second,c));
strcat(list,i->second->nick);
strcat(list," ");
if (strlen(list)>(480-NICKMAX))
{
/* list overflowed into
* multiple numerics */
WriteServ(user->fd,list);
sprintf(list,"353 %s = %s :", user->nick, c->name);
}
}
}
}
/* if whats left in the list isnt empty, send it */
if (strcmp(list,""))
{
WriteServ(user->fd,list);
}
}
/* return a count of the users on a specific channel */
int usercount(struct chanrec *c)
{
int i = 0;
int count = 0;
strcpy(list,"");
for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
{
if (has_channel(i->second,c) && (i->second->fd != 0))
{
if (isnick(i->second->nick))
{
count++;
}
}
}
debug("usercount: %s %d",c->name,count);
return count;
}
/* add a channel to a user, creating the record for it if needed and linking
* it to the user record */
struct chanrec* add_channel(struct userrec *user, char* cname, char* key)
{
int i = 0;
struct chanrec* Ptr;
int created = 0;
if ((!cname) || (!user))
{
return NULL;
}
if (strlen(cname) > CHANMAX-1)
{
cname[CHANMAX-1] = '\0';
}
debug("add_channel: %s %s",user->nick,cname);
if ((has_channel(user,FindChan(cname))) && (FindChan(cname)))
{
return NULL; // already on the channel!
}
if (!FindChan(cname))
{
/* create a new one */
debug("add_channel: creating: %s",cname);
{
chanlist[cname] = new chanrec;
strcpy(chanlist[cname]->name, cname);
chanlist[cname]->topiclock = 1;
chanlist[cname]->noexternal = 1;
chanlist[cname]->created = time(NULL);
strcpy(chanlist[cname]->topic, "");
strncpy(chanlist[cname]->setby, user->nick,NICKMAX);
chanlist[cname]->topicset = 0;
Ptr = chanlist[cname];
debug("add_channel: created: %s",cname);
/* set created to 2 to indicate user
* is the first in the channel
* and should be given ops */
created = 2;
}
}
else
{
/* channel exists, just fish out a pointer to its struct */
Ptr = FindChan(cname);
if (Ptr)
{
debug("add_channel: joining to: %s",Ptr->name);
if (strcmp(Ptr->key,""))
{
/* channel has a key... */
}
}
created = 1;
}
for (i =0; i <= MAXCHANS; i++)
{
if (user->chans[i].channel == NULL)
{
if (created == 2)
{
/* first user in is given ops */
user->chans[i].uc_modes = UCMODE_OP;
}
else
{
user->chans[i].uc_modes = 0;
}
user->chans[i].channel = Ptr;
WriteChannel(Ptr,user,"JOIN :%s",Ptr->name);
if (Ptr->topicset)
{
WriteServ(user->fd,"332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
WriteServ(user->fd,"333 %s %s %s %d", user->nick, Ptr->name, Ptr->setby, Ptr->topicset);
}
userlist(user,Ptr);
WriteServ(user->fd,"366 %s %s :End of /NAMES list.", user->nick, Ptr->name);
WriteServ(user->fd,"324 %s %s +%s",user->nick, Ptr->name,chanmodes(Ptr));
WriteServ(user->fd,"329 %s %s %d", user->nick, Ptr->name, Ptr->created);
FOREACH_MOD OnUserJoin(user,Ptr);
return Ptr;
}
}
debug("add_channel: user channel max exceeded: %s %s",user->nick,cname);
WriteServ(user->fd,"405 %s %s :You are on too many channels",user->nick, cname);
return NULL;
}
/* remove a channel from a users record, and remove the record from memory
* if the channel has become empty */
struct chanrec* del_channel(struct userrec *user, char* cname, char* reason)
{
int i = 0;