1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
19 #include "inspircd_config.h"
21 #include "inspircd_io.h"
24 #include <sys/errno.h>
28 #include <ext/hash_map>
36 #include "connection.h"
47 #include "inspstring.h"
48 #include "helperfuncs.h"
53 extern std::vector<Module*> modules;
54 extern ServerConfig *Config;
55 extern InspIRCd* ServerInstance;
57 extern char lowermap[255];
58 static char list[MAXBUF];
59 extern userrec* fd_ref_table[MAX_DESCRIPTORS];
60 static char already_sent[MAX_DESCRIPTORS];
61 extern std::vector<userrec*> all_opers;
62 extern user_hash clientlist;
63 extern chan_hash chanlist;
65 extern std::vector<userrec*> local_users;
67 void log(int level,char *text, ...)
71 if (level < Config->LogLevel)
73 char textbuffer[MAXBUF];
74 timeinfo = localtime(&TIME);
79 va_start (argsPtr, text);
80 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
82 strlcpy(b,asctime(timeinfo),26);
83 b[24] = ':'; // we know this is the end of the time string
85 fprintf(Config->log_file,"%s %s\n",b,textbuffer);
88 // nofork enabled? display it on terminal too
89 printf("%s %s\n",b,textbuffer);
94 void readfile(file_cache &F, const char* fname)
99 log(DEBUG,"readfile: loading %s",fname);
101 file = fopen(fname,"r");
106 fgets(linebuf,sizeof(linebuf),file);
107 linebuf[strlen(linebuf)-1]='\0';
114 F.push_back(linebuf);
121 log(DEBUG,"readfile: failed to load file: %s",fname);
123 log(DEBUG,"readfile: loaded %s, %lu lines",fname,(unsigned long)F.size());
126 void Write_NoFormat(int sock, const char *text)
128 if ((sock < 0) || (!text) || (sock > MAX_DESCRIPTORS))
132 int bytes = snprintf(tb,MAXBUF,"%s\r\n",text);
134 if (fd_ref_table[sock])
136 if (Config->GetIOHook(fd_ref_table[sock]->port))
138 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
142 fd_ref_table[sock]->AddWriteBuf(tb);
144 ServerInstance->stats->statsSent += bytes;
146 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
149 void Write(int sock,char *text, ...)
151 if ((sock < 0) || (sock > MAX_DESCRIPTORS))
155 log(DEFAULT,"*** BUG *** Write was given an invalid parameter");
159 char textbuffer[MAXBUF],tb[MAXBUF];
160 va_start (argsPtr, text);
161 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
163 int bytes = snprintf(tb,MAXBUF,"%s\r\n",textbuffer);
165 if (fd_ref_table[sock])
167 if (Config->GetIOHook(fd_ref_table[sock]->port))
169 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
173 fd_ref_table[sock]->AddWriteBuf(tb);
175 ServerInstance->stats->statsSent += bytes;
177 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
180 void WriteServ_NoFormat(int sock, const char* text)
182 if ((sock < 0) || (!text) || (sock > MAX_DESCRIPTORS))
185 int bytes = snprintf(tb,MAXBUF,":%s %s\r\n",Config->ServerName,text);
187 if (fd_ref_table[sock])
189 if (Config->GetIOHook(fd_ref_table[sock]->port))
191 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
195 fd_ref_table[sock]->AddWriteBuf(tb);
197 ServerInstance->stats->statsSent += bytes;
199 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
202 /* write a server formatted numeric response to a single socket */
204 void WriteServ(int sock, char* text, ...)
206 if ((sock < 0) || (sock > MAX_DESCRIPTORS))
210 log(DEFAULT,"*** BUG *** WriteServ was given an invalid parameter");
214 va_start (argsPtr, text);
215 char textbuffer[MAXBUF],tb[MAXBUF];
216 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
218 int bytes = snprintf(tb,MAXBUF,":%s %s\r\n",Config->ServerName,textbuffer);
220 if (fd_ref_table[sock])
222 if (Config->GetIOHook(fd_ref_table[sock]->port))
224 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
228 fd_ref_table[sock]->AddWriteBuf(tb);
230 ServerInstance->stats->statsSent += bytes;
232 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
235 void WriteFrom_NoFormat(int sock, userrec *user, const char* text)
237 if ((sock < 0) || (!text) || (!user) || (sock > MAX_DESCRIPTORS))
240 int bytes = snprintf(tb,MAXBUF,":%s %s\r\n",user->GetFullHost(),text);
242 if (fd_ref_table[sock])
244 if (Config->GetIOHook(fd_ref_table[sock]->port))
246 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
250 fd_ref_table[sock]->AddWriteBuf(tb);
252 ServerInstance->stats->statsSent += bytes;
254 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
257 /* write text from an originating user to originating user */
259 void WriteFrom(int sock, userrec *user,char* text, ...)
261 if ((sock < 0) || (sock > MAX_DESCRIPTORS))
263 if ((!text) || (!user))
265 log(DEFAULT,"*** BUG *** WriteFrom was given an invalid parameter");
269 va_start (argsPtr, text);
270 char textbuffer[MAXBUF],tb[MAXBUF];
271 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
273 int bytes = snprintf(tb,MAXBUF,":%s %s\r\n",user->GetFullHost(),textbuffer);
275 if (fd_ref_table[sock])
277 if (Config->GetIOHook(fd_ref_table[sock]->port))
279 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
283 fd_ref_table[sock]->AddWriteBuf(tb);
285 ServerInstance->stats->statsSent += bytes;
287 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
290 /* write text to an destination user from a source user (e.g. user privmsg) */
292 void WriteTo(userrec *source, userrec *dest,char *data, ...)
294 if ((!dest) || (!data))
296 log(DEFAULT,"*** BUG *** WriteTo was given an invalid parameter");
301 char textbuffer[MAXBUF],tb[MAXBUF];
303 va_start (argsPtr, data);
304 vsnprintf(textbuffer, MAXBUF, data, argsPtr);
308 // if no source given send it from the server.
311 WriteServ_NoFormat(dest->fd,textbuffer);
315 WriteFrom_NoFormat(dest->fd,source,textbuffer);
319 void WriteTo_NoFormat(userrec *source, userrec *dest, const char *data)
321 if ((!dest) || (!data))
325 WriteServ_NoFormat(dest->fd,data);
329 WriteFrom_NoFormat(dest->fd,source,data);
333 /* write formatted text from a source user to all users on a channel
334 * including the sender (NOT for privmsg, notice etc!) */
336 void WriteChannel(chanrec* Ptr, userrec* user, char* text, ...)
338 if ((!Ptr) || (!user) || (!text))
340 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
343 char textbuffer[MAXBUF];
345 va_start (argsPtr, text);
346 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
349 std::map<char*,char*> *ulist= Ptr->GetUsers();
350 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
353 userrec* otheruser = (userrec*)o;
354 if (otheruser->fd != FD_MAGIC_NUMBER)
355 WriteTo_NoFormat(user,otheruser,textbuffer);
359 void WriteChannel_NoFormat(chanrec* Ptr, userrec* user, const char* text)
361 if ((!Ptr) || (!user) || (!text))
363 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
366 std::map<char*,char*> *ulist= Ptr->GetUsers();
367 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
370 userrec* otheruser = (userrec*)o;
371 if (otheruser->fd != FD_MAGIC_NUMBER)
372 WriteTo_NoFormat(user,otheruser,text);
377 /* write formatted text from a source user to all users on a channel
378 * including the sender (NOT for privmsg, notice etc!) doesnt send to
379 * users on remote servers */
381 void WriteChannelLocal(chanrec* Ptr, userrec* user, char* text, ...)
383 if ((!Ptr) || (!text))
385 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
388 char textbuffer[MAXBUF];
390 va_start (argsPtr, text);
391 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
394 std::map<char*,char*> *ulist= Ptr->GetUsers();
395 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
398 userrec* otheruser = (userrec*)o;
399 if ((otheruser->fd != FD_MAGIC_NUMBER) && (otheruser != user))
403 WriteServ_NoFormat(otheruser->fd,textbuffer);
407 WriteTo_NoFormat(user,otheruser,textbuffer);
413 void WriteChannelLocal_NoFormat(chanrec* Ptr, userrec* user, const char* text)
415 if ((!Ptr) || (!text))
417 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
420 std::map<char*,char*> *ulist= Ptr->GetUsers();
421 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
424 userrec* otheruser = (userrec*)o;
425 if ((otheruser->fd != FD_MAGIC_NUMBER) && (otheruser != user))
429 WriteServ_NoFormat(otheruser->fd,text);
433 WriteTo_NoFormat(user,otheruser,text);
441 void WriteChannelWithServ(char* ServName, chanrec* Ptr, char* text, ...)
443 if ((!Ptr) || (!text))
445 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
448 char textbuffer[MAXBUF];
450 va_start (argsPtr, text);
451 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
455 std::map<char*,char*> *ulist= Ptr->GetUsers();
456 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
459 userrec* otheruser = (userrec*)o;
460 if (IS_LOCAL(otheruser))
461 WriteServ_NoFormat(otheruser->fd,textbuffer);
465 void WriteChannelWithServ_NoFormat(char* ServName, chanrec* Ptr, const char* text)
467 if ((!Ptr) || (!text))
469 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
472 std::map<char*,char*> *ulist= Ptr->GetUsers();
473 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
476 userrec* otheruser = (userrec*)o;
477 if (IS_LOCAL(otheruser))
478 WriteServ_NoFormat(otheruser->fd,text);
484 /* write formatted text from a source user to all users on a channel except
485 * for the sender (for privmsg etc) */
487 void ChanExceptSender(chanrec* Ptr, userrec* user, char status, char* text, ...)
489 if ((!Ptr) || (!user) || (!text))
491 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
494 char textbuffer[MAXBUF];
496 va_start (argsPtr, text);
497 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
500 std::map<char*,char*> *ulist;
504 ulist = Ptr->GetOppedUsers();
507 ulist = Ptr->GetHalfoppedUsers();
510 ulist = Ptr->GetVoicedUsers();
513 ulist = Ptr->GetUsers();
516 log(DEBUG,"%d users to write to",ulist->size());
517 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
520 userrec* otheruser = (userrec*)o;
521 if ((IS_LOCAL(otheruser)) && (user != otheruser))
522 WriteFrom_NoFormat(otheruser->fd,user,textbuffer);
526 void ChanExceptSender_NoFormat(chanrec* Ptr, userrec* user, char status, const char* text)
528 if ((!Ptr) || (!user) || (!text))
530 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
533 std::map<char*,char*> *ulist;
537 ulist = Ptr->GetOppedUsers();
540 ulist = Ptr->GetHalfoppedUsers();
543 ulist = Ptr->GetVoicedUsers();
546 ulist = Ptr->GetUsers();
549 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
552 userrec* otheruser = (userrec*)o;
553 if ((IS_LOCAL(otheruser)) && (user != otheruser))
554 WriteFrom_NoFormat(otheruser->fd,user,text);
558 std::string GetServerDescription(char* servername)
560 std::string description = "";
561 FOREACH_MOD(I_OnGetServerDescription,OnGetServerDescription(servername,description));
562 if (description != "")
568 return Config->ServerDesc; // not a remote server that can be found, it must be me.
572 /* write a formatted string to all users who share at least one common
573 * channel, including the source user e.g. for use in NICK */
575 void WriteCommon(userrec *u, char* text, ...)
579 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
583 if (u->registered != 7) {
584 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
588 char textbuffer[MAXBUF];
590 va_start (argsPtr, text);
591 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
594 // FIX: Stops a message going to the same person more than once
595 memset(&already_sent,0,MAX_DESCRIPTORS);
597 bool sent_to_at_least_one = false;
599 unsigned int y = u->chans.size();
600 for (unsigned int i = 0; i < y; i++)
602 if (u->chans[i].channel)
604 std::map<char*,char*> *ulist= u->chans[i].channel->GetUsers();
605 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
608 userrec* otheruser = (userrec*)o;
609 if ((otheruser->fd > -1) && (!already_sent[otheruser->fd]))
611 already_sent[otheruser->fd] = 1;
612 WriteFrom_NoFormat(otheruser->fd,u,textbuffer);
613 sent_to_at_least_one = true;
618 // if the user was not in any channels, no users will receive the text. Make sure the user
619 // receives their OWN message for WriteCommon
620 if (!sent_to_at_least_one)
622 WriteFrom_NoFormat(u->fd,u,textbuffer);
626 void WriteCommon_NoFormat(userrec *u, const char* text)
630 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
634 if (u->registered != 7) {
635 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
638 // FIX: Stops a message going to the same person more than once
639 memset(&already_sent,0,MAX_DESCRIPTORS);
641 bool sent_to_at_least_one = false;
643 unsigned int y = u->chans.size();
644 for (unsigned int i = 0; i < y; i++)
646 if (u->chans[i].channel)
648 std::map<char*,char*> *ulist= u->chans[i].channel->GetUsers();
649 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
652 userrec* otheruser = (userrec*)o;
653 if ((otheruser->fd > -1) && (!already_sent[otheruser->fd]))
655 already_sent[otheruser->fd] = 1;
656 WriteFrom_NoFormat(otheruser->fd,u,text);
657 sent_to_at_least_one = true;
662 // if the user was not in any channels, no users will receive the text. Make sure the user
663 // receives their OWN message for WriteCommon
664 if (!sent_to_at_least_one)
666 WriteFrom_NoFormat(u->fd,u,text);
671 /* write a formatted string to all users who share at least one common
672 * channel, NOT including the source user e.g. for use in QUIT */
674 void WriteCommonExcept(userrec *u, char* text, ...)
678 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
682 if (u->registered != 7) {
683 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
687 char textbuffer[MAXBUF];
688 char oper_quit[MAXBUF];
689 bool quit_munge = false;
692 va_start (argsPtr, text);
693 int total = vsnprintf(textbuffer, MAXBUF, text, argsPtr);
696 if ((Config->HideSplits) && (total > 6))
698 /* Yeah yeah, this is ugly. But its fast, live with it. */
699 char* check = textbuffer;
700 if ((*check++ == 'Q') && (*check++ == 'U') && (*check++ == 'I') && (*check++ == 'T') && (*check++ == ' ') && (*check++ == ':'))
702 std::stringstream split(check);
703 std::string server_one;
704 std::string server_two;
707 if ((FindServerName(server_one)) && (FindServerName(server_two)))
709 strlcpy(oper_quit,textbuffer,MAXBUF);
710 strlcpy(check,"*.net *.split",MAXQUIT);
716 memset(&already_sent,0,MAX_DESCRIPTORS);
718 unsigned int y = u->chans.size();
719 for (unsigned int i = 0; i < y; i++)
721 if (u->chans[i].channel)
723 std::map<char*,char*> *ulist= u->chans[i].channel->GetUsers();
724 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
727 userrec* otheruser = (userrec*)o;
730 if ((otheruser->fd > -1) && (!already_sent[otheruser->fd]))
732 already_sent[otheruser->fd] = 1;
735 if (*otheruser->oper)
737 WriteFrom_NoFormat(otheruser->fd,u,oper_quit);
741 WriteFrom_NoFormat(otheruser->fd,u,textbuffer);
744 else WriteFrom_NoFormat(otheruser->fd,u,textbuffer);
752 void WriteCommonExcept_NoFormat(userrec *u, const char* text)
756 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
760 if (u->registered != 7) {
761 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
765 memset(&already_sent,0,MAX_DESCRIPTORS);
767 unsigned int y = u->chans.size();
768 for (unsigned int i = 0; i < y; i++)
770 if (u->chans[i].channel)
772 std::map<char*,char*> *ulist= u->chans[i].channel->GetUsers();
773 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
776 userrec* otheruser = (userrec*)o;
779 if ((otheruser->fd > -1) && (!already_sent[otheruser->fd]))
781 already_sent[otheruser->fd] = 1;
782 WriteFrom_NoFormat(otheruser->fd,u,text);
792 void WriteOpers(char* text, ...)
796 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
800 char textbuffer[MAXBUF];
802 va_start (argsPtr, text);
803 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
806 for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
811 if (strchr(a->modes,'s'))
813 // send server notices to all with +s
814 WriteServ(a->fd,"NOTICE %s :%s",a->nick,textbuffer);
820 void ServerNoticeAll(char* text, ...)
825 char textbuffer[MAXBUF];
827 va_start (argsPtr, text);
828 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
831 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
833 userrec* t = (userrec*)(*i);
834 WriteServ(t->fd,"NOTICE $%s :%s",Config->ServerName,textbuffer);
838 void ServerPrivmsgAll(char* text, ...)
843 char textbuffer[MAXBUF];
845 va_start (argsPtr, text);
846 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
849 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
851 userrec* t = (userrec*)(*i);
852 WriteServ(t->fd,"PRIVMSG $%s :%s",Config->ServerName,textbuffer);
856 void WriteMode(const char* modes, int flags, const char* text, ...)
858 if ((!text) || (!modes) || (!flags))
860 log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
864 char textbuffer[MAXBUF];
866 va_start (argsPtr, text);
867 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
869 int modelen = strlen(modes);
871 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
873 userrec* t = (userrec*)(*i);
874 bool send_to_user = false;
878 for (int n = 0; n < modelen; n++)
880 if (!hasumode(t,modes[n]))
882 send_to_user = false;
887 else if (flags == WM_OR)
889 send_to_user = false;
890 for (int n = 0; n < modelen; n++)
892 if (hasumode(t,modes[n]))
901 WriteServ(t->fd,"NOTICE %s :%s",t->nick,textbuffer);
906 void NoticeAll(userrec *source, bool local_only, char* text, ...)
908 if ((!text) || (!source))
910 log(DEFAULT,"*** BUG *** NoticeAll was given an invalid parameter");
914 char textbuffer[MAXBUF];
916 va_start (argsPtr, text);
917 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
920 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
922 userrec* t = (userrec*)(*i);
923 WriteFrom(t->fd,source,"NOTICE $* :%s",textbuffer);
929 void WriteWallOps(userrec *source, bool local_only, char* text, ...)
931 if ((!text) || (!source))
933 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
937 char textbuffer[MAXBUF];
939 va_start (argsPtr, text);
940 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
943 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
945 userrec* t = (userrec*)(*i);
946 if ((IS_LOCAL(t)) && (strchr(t->modes,'w')))
948 WriteTo(source,t,"WALLOPS :%s",textbuffer);
953 /* convert a string to lowercase. Note following special circumstances
954 * taken from RFC 1459. Many "official" server branches still hold to this
955 * rule so i will too;
957 * Because of IRC's scandanavian origin, the characters {}| are
958 * considered to be the lower case equivalents of the characters []\,
959 * respectively. This is a critical issue when determining the
960 * equivalence of two nicknames.
963 void strlower(char *n)
967 for (char* t = n; *t; t++)
968 *t = lowermap[(unsigned)*t];
972 /* Find a user record by nickname and return a pointer to it */
974 userrec* Find(std::string nick)
976 user_hash::iterator iter = clientlist.find(nick);
978 if (iter == clientlist.end())
979 /* Couldn't find it */
985 /* find a channel record by channel name and return a pointer to it */
987 chanrec* FindChan(const char* chan)
991 log(DEFAULT,"*** BUG *** Findchan was given an invalid parameter");
995 chan_hash::iterator iter = chanlist.find(chan);
997 if (iter == chanlist.end())
998 /* Couldn't find it */
1001 return iter->second;
1005 long GetMaxBans(char* name)
1008 for (int count = 0; count < Config->ConfValueEnum("banlist",&Config->config_f); count++)
1010 Config->ConfValue("banlist","chan",count,CM,&Config->config_f);
1013 Config->ConfValue("banlist","limit",count,CM,&Config->config_f);
1020 void purge_empty_chans(userrec* u)
1025 // firstly decrement the count on each channel
1026 for (unsigned int f = 0; f < u->chans.size(); f++)
1028 if (u->chans[f].channel)
1030 u->chans[f].channel->DelUser((char*)u);
1034 for (unsigned int i = 0; i < u->chans.size(); i++)
1036 if (u->chans[i].channel)
1038 if (!usercount(u->chans[i].channel))
1040 chan_hash::iterator i2 = chanlist.find(u->chans[i].channel->name);
1041 /* kill the record */
1042 if (i2 != chanlist.end())
1044 log(DEBUG,"del_channel: destroyed: %s",i2->second->name);
1047 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(i2->second));
1052 u->chans[i].channel = NULL;
1057 log(DEBUG,"skipped purge for %s",u->chans[i].channel->name);
1061 log(DEBUG,"completed channel purge, killed %lu",(unsigned long)purge);
1067 char* chanmodes(chanrec *chan, bool showkey)
1069 static char scratch[MAXBUF];
1070 static char sparam[MAXBUF];
1071 char* offset = scratch;
1075 log(DEFAULT,"*** BUG *** chanmodes was given an invalid parameter");
1082 if (chan->binarymodes & CM_NOEXTERNAL)
1084 if (chan->binarymodes & CM_TOPICLOCK)
1090 if (chan->binarymodes & CM_INVITEONLY)
1092 if (chan->binarymodes & CM_MODERATED)
1094 if (chan->binarymodes & CM_SECRET)
1096 if (chan->binarymodes & CM_PRIVATE)
1100 snprintf(sparam,MAXBUF," %s",showkey ? chan->key : "<key>");
1105 sprintf(foo," %lu",(unsigned long)chan->limit);
1106 strlcat(sparam,foo,MAXBUF);
1108 if (*chan->custom_modes)
1110 for (char* t = chan->custom_modes; *t; t++)
1112 for (int z = 0; chan->custom_modes[z]; z++)
1114 std::string extparam = chan->GetModeParameter(chan->custom_modes[z]);
1117 strlcat(sparam," ",MAXBUF);
1118 strlcat(sparam,extparam.c_str(),MAXBUF);
1122 /* Null terminate scratch */
1124 strlcat(scratch,sparam,MAXMODES);
1129 /* compile a userlist of a channel into a string, each nick seperated by
1130 * spaces and op, voice etc status shown as @ and + */
1132 void userlist(userrec *user,chanrec *c)
1134 if ((!c) || (!user))
1136 log(DEFAULT,"*** BUG *** userlist was given an invalid parameter");
1140 snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1142 std::map<char*,char*> *ulist= c->GetUsers();
1143 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
1145 char* o = i->second;
1146 userrec* otheruser = (userrec*)o;
1147 if ((!has_channel(user,c)) && (strchr(otheruser->modes,'i')))
1149 /* user is +i, and source not on the channel, does not show
1150 * nick in NAMES list */
1153 strlcat(list,cmode(otheruser,c),MAXBUF);
1154 strlcat(list,otheruser->nick,MAXBUF);
1155 strlcat(list," ",MAXBUF);
1156 if (strlen(list)>(480-NICKMAX))
1158 /* list overflowed into
1159 * multiple numerics */
1160 WriteServ_NoFormat(user->fd,list);
1161 snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1164 /* if whats left in the list isnt empty, send it */
1165 if (list[strlen(list)-1] != ':')
1167 WriteServ_NoFormat(user->fd,list);
1171 /* return a count of the users on a specific channel accounting for
1172 * invisible users who won't increase the count. e.g. for /LIST */
1174 int usercount_i(chanrec *c)
1180 log(DEFAULT,"*** BUG *** usercount_i was given an invalid parameter");
1184 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1186 if (has_channel(i->second,c))
1188 if (i->second->registered == 7)
1190 if ((!has_channel(i->second,c)) && (strchr(i->second->modes,'i')))
1192 /* user is +i, and source not on the channel, does not show
1193 * nick in NAMES list */
1200 log(DEBUG,"usercount_i: %s %lu",c->name,(unsigned long)count);
1205 int usercount(chanrec *c)
1207 return (c ? c->GetUserCounter() : 0);
1211 // looks up a users password for their connection class (<ALLOW>/<DENY> tags)
1213 ConnectClass GetClass(userrec *user)
1215 for (ClassVector::iterator i = Config->Classes.begin(); i != Config->Classes.end(); i++)
1217 if (match(user->host,i->host.c_str()))
1222 return *(Config->Classes.begin());
1225 /* sends out an error notice to all connected clients (not to be used
1228 void send_error(char *s)
1230 log(DEBUG,"send_error: %s",s);
1231 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1233 userrec* t = (userrec*)(*i);
1234 if (t->registered == 7)
1236 WriteServ(t->fd,"NOTICE %s :%s",t->nick,s);
1240 // fix - unregistered connections receive ERROR, not NOTICE
1241 Write(t->fd,"ERROR :%s",s);
1246 void Error(int status)
1248 signal (SIGALRM, SIG_IGN);
1249 signal (SIGPIPE, SIG_IGN);
1250 signal (SIGTERM, SIG_IGN);
1251 signal (SIGABRT, SIG_IGN);
1252 signal (SIGSEGV, SIG_IGN);
1253 signal (SIGURG, SIG_IGN);
1254 signal (SIGKILL, SIG_IGN);
1255 log(DEFAULT,"*** fell down a pothole in the road to perfection ***");
1256 send_error("Error! Segmentation fault! save meeeeeeeeeeeeee *splat!*");
1260 // this function counts all users connected, wether they are registered or NOT.
1263 return clientlist.size();
1266 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
1267 int registered_usercount(void)
1270 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1272 if (i->second->registered == 7) c++;
1277 int usercount_invisible(void)
1280 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1282 if ((i->second->registered == 7) && (strchr(i->second->modes,'i'))) c++;
1287 int usercount_opers(void)
1289 return all_opers.size();
1292 int usercount_unknown(void)
1295 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1297 userrec* t = (userrec*)(*i);
1298 if (t->registered != 7)
1304 long chancount(void)
1306 return chanlist.size();
1312 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1314 userrec* t = (userrec*)(*i);
1315 if (t->registered == 7) c++;
1320 void ShowMOTD(userrec *user)
1322 static char mbuf[MAXBUF];
1323 static char crud[MAXBUF];
1324 std::string WholeMOTD = "";
1325 if (!Config->MOTD.size())
1327 WriteServ(user->fd,"422 %s :Message of the day file is missing.",user->nick);
1330 snprintf(crud,MAXBUF,":%s 372 %s :- ", Config->ServerName, user->nick);
1331 snprintf(mbuf,MAXBUF,":%s 375 %s :- %s message of the day\r\n", Config->ServerName, user->nick, Config->ServerName);
1332 WholeMOTD = WholeMOTD + mbuf;
1333 for (unsigned int i = 0; i < Config->MOTD.size(); i++)
1334 WholeMOTD = WholeMOTD + std::string(crud) + Config->MOTD[i].c_str() + std::string("\r\n");
1335 snprintf(mbuf,MAXBUF,":%s 376 %s :End of message of the day.\r\n", Config->ServerName, user->nick);
1336 WholeMOTD = WholeMOTD + mbuf;
1337 // only one write operation
1338 if (Config->GetIOHook(user->port))
1340 Config->GetIOHook(user->port)->OnRawSocketWrite(user->fd,(char*)WholeMOTD.c_str(),WholeMOTD.length());
1344 user->AddWriteBuf(WholeMOTD);
1346 ServerInstance->stats->statsSent += WholeMOTD.length();
1349 void ShowRULES(userrec *user)
1351 if (!Config->RULES.size())
1353 WriteServ(user->fd,"NOTICE %s :Rules file is missing.",user->nick);
1356 WriteServ(user->fd,"NOTICE %s :%s rules",user->nick,Config->ServerName);
1357 for (unsigned int i = 0; i < Config->RULES.size(); i++)
1358 WriteServ(user->fd,"NOTICE %s :%s",user->nick,Config->RULES[i].c_str());
1359 WriteServ(user->fd,"NOTICE %s :End of %s rules.",user->nick,Config->ServerName);
1362 // this returns 1 when all modules are satisfied that the user should be allowed onto the irc server
1363 // (until this returns true, a user will block in the waiting state, waiting to connect up to the
1364 // registration timeout maximum seconds)
1365 bool AllModulesReportReady(userrec* user)
1367 if (!Config->global_implementation[I_OnCheckReady])
1369 for (int i = 0; i <= MODCOUNT; i++)
1371 if (Config->implement_lists[i][I_OnCheckReady])
1373 int res = modules[i]->OnCheckReady(user);
1381 bool DirValid(char* dirandfile)
1384 char buffer[MAXBUF], otherdir[MAXBUF];
1385 strlcpy(work,dirandfile,MAXBUF);
1386 int p = strlen(work);
1387 // we just want the dir
1397 // Get the current working directory
1398 if( getcwd( buffer, MAXBUF ) == NULL )
1401 if( getcwd( otherdir, MAXBUF ) == NULL )
1404 if (strlen(otherdir) >= strlen(work))
1406 otherdir[strlen(work)] = '\0';
1407 if (!strcmp(otherdir,work))
1416 std::string GetFullProgDir(char** argv, int argc)
1419 char buffer[MAXBUF], otherdir[MAXBUF];
1420 strlcpy(work,argv[0],MAXBUF);
1421 int p = strlen(work);
1422 // we just want the dir
1432 // Get the current working directory
1433 if( getcwd( buffer, MAXBUF ) == NULL )
1436 if( getcwd( otherdir, MAXBUF ) == NULL )
1442 int InsertMode(std::string &output, const char* mode, unsigned short section)
1444 unsigned short currsection = 1;
1445 unsigned int pos = output.find("CHANMODES=", 0) + 10; // +10 for the length of "CHANMODES="
1447 if(section > 4 || section == 0)
1449 log(DEBUG, "InsertMode: CHANMODES doesn't have a section %dh :/", section);
1453 for(; pos < output.size(); pos++)
1455 if(section == currsection)
1458 if(output[pos] == ',')
1462 output.insert(pos, mode);