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 * ---------------------------------------------------
18 #include "inspircd_config.h"
20 #include "configreader.h"
23 #include <sys/errno.h>
31 #include "connection.h"
42 #include "inspstring.h"
43 #include "helperfuncs.h"
48 extern ModuleList modules;
49 extern ServerConfig *Config;
50 extern InspIRCd* ServerInstance;
52 extern char lowermap[255];
53 extern userrec* fd_ref_table[MAX_DESCRIPTORS];
54 static unsigned long already_sent[MAX_DESCRIPTORS] = {0};
55 extern std::vector<userrec*> all_opers;
56 extern user_hash clientlist;
57 extern chan_hash chanlist;
59 char LOG_FILE[MAXBUF];
61 extern std::vector<userrec*> local_users;
63 static char TIMESTR[26];
64 static time_t LAST = 0;
66 /* XXX: Used for speeding up WriteCommon operations */
67 unsigned long uniq_id = 0;
70 * Write a line of text `text' to the logfile (and stdout, if in nofork) if the level `level'
71 * is greater than the configured loglevel.
73 void do_log(int level, const char *text, ...)
76 char textbuffer[MAXBUF];
78 /* If we were given -debug we output all messages, regardless of configured loglevel */
79 if ((level < Config->LogLevel) && !Config->forcedebug)
84 struct tm *timeinfo = localtime(&TIME);
86 strlcpy(TIMESTR,asctime(timeinfo),26);
93 va_start(argsPtr, text);
94 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
99 fprintf(Config->log_file,"%s %s\n",TIMESTR,textbuffer);
100 fflush(Config->log_file);
106 printf("%s %s\n", TIMESTR, textbuffer);
111 * Read the contents of a file located by `fname' into a file_cache pointed at by `F'.
113 * XXX - we may want to consider returning a file_cache or pointer to one, less confusing.
115 void readfile(file_cache &F, const char* fname)
118 char linebuf[MAXBUF];
120 log(DEBUG,"readfile: loading %s",fname);
122 file = fopen(fname,"r");
128 fgets(linebuf,sizeof(linebuf),file);
129 linebuf[strlen(linebuf)-1]='\0';
138 F.push_back(linebuf);
146 log(DEBUG,"readfile: failed to load file: %s",fname);
149 log(DEBUG,"readfile: loaded %s, %lu lines",fname,(unsigned long)F.size());
153 * Writes a given string in `text' to the socket on fd `sock' - only if the socket
154 * is a valid entry in the local FD table.
156 void Write_NoFormat(int sock, const char *text)
161 if ((sock < 0) || (!text) || (sock > MAX_DESCRIPTORS))
164 if (fd_ref_table[sock])
166 bytes = snprintf(tb,MAXBUF,"%s\r\n",text);
168 if (Config->GetIOHook(fd_ref_table[sock]->GetPort()))
172 Config->GetIOHook(fd_ref_table[sock]->GetPort())->OnRawSocketWrite(sock,tb,bytes);
174 catch (ModuleException& modexcept)
176 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
181 fd_ref_table[sock]->AddWriteBuf(tb);
183 ServerInstance->stats->statsSent += bytes;
186 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
190 * Same as Write_NoFormat(), but formatted printf() style first.
192 void Write(int sock, char *text, ...)
195 char textbuffer[MAXBUF];
199 if ((sock < 0) || (sock > MAX_DESCRIPTORS))
204 log(DEFAULT,"*** BUG *** Write was given an invalid parameter");
208 if (fd_ref_table[sock])
211 va_start(argsPtr, text);
212 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
214 bytes = snprintf(tb,MAXBUF,"%s\r\n",textbuffer);
216 if (Config->GetIOHook(fd_ref_table[sock]->GetPort()))
220 Config->GetIOHook(fd_ref_table[sock]->GetPort())->OnRawSocketWrite(sock,tb,bytes);
222 catch (ModuleException& modexcept)
224 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
229 fd_ref_table[sock]->AddWriteBuf(tb);
231 ServerInstance->stats->statsSent += bytes;
234 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
237 /** WriteServ_NoFormat()
238 * Same as Write_NoFormat(), except prefixes `text' with `:server.name '.
240 void WriteServ_NoFormat(int sock, const char* text)
245 if ((sock < 0) || (!text) || (sock > MAX_DESCRIPTORS))
248 if (fd_ref_table[sock])
250 bytes = snprintf(tb,MAXBUF,":%s %s\r\n",Config->ServerName,text);
252 if (Config->GetIOHook(fd_ref_table[sock]->GetPort()))
256 Config->GetIOHook(fd_ref_table[sock]->GetPort())->OnRawSocketWrite(sock,tb,bytes);
258 catch (ModuleException& modexcept)
260 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
265 fd_ref_table[sock]->AddWriteBuf(tb);
267 ServerInstance->stats->statsSent += bytes;
270 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
274 * Same as Write(), except `text' is prefixed with `:server.name '.
276 void WriteServ(int sock, char* text, ...)
279 char textbuffer[MAXBUF];
281 if ((sock < 0) || (sock > MAX_DESCRIPTORS))
286 log(DEFAULT,"*** BUG *** WriteServ was given an invalid parameter");
290 if (!fd_ref_table[sock])
293 va_start(argsPtr, text);
294 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
297 WriteServ_NoFormat(sock, textbuffer);
300 /** WriteFrom_NoFormat()
301 * Write `text' to a socket with fd `sock' prefixed with `:n!u@h' - taken from
302 * the nick, user, and host of `user'.
304 void WriteFrom_NoFormat(int sock, userrec *user, const char* text)
309 if ((sock < 0) || (!text) || (!user) || (sock > MAX_DESCRIPTORS))
312 if (fd_ref_table[sock])
314 bytes = snprintf(tb,MAXBUF,":%s %s\r\n",user->GetFullHost(),text);
316 if (Config->GetIOHook(fd_ref_table[sock]->GetPort()))
320 Config->GetIOHook(fd_ref_table[sock]->GetPort())->OnRawSocketWrite(sock,tb,bytes);
322 catch (ModuleException& modexcept)
324 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
329 fd_ref_table[sock]->AddWriteBuf(tb);
331 ServerInstance->stats->statsSent += bytes;
334 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
337 /* write text from an originating user to originating user */
339 void WriteFrom(int sock, userrec *user,char* text, ...)
342 char textbuffer[MAXBUF];
346 if ((sock < 0) || (sock > MAX_DESCRIPTORS))
349 if ((!text) || (!user))
351 log(DEFAULT,"*** BUG *** WriteFrom was given an invalid parameter");
355 if (fd_ref_table[sock])
358 va_start(argsPtr, text);
359 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
361 bytes = snprintf(tb,MAXBUF,":%s %s\r\n",user->GetFullHost(),textbuffer);
363 if (Config->GetIOHook(fd_ref_table[sock]->GetPort()))
367 Config->GetIOHook(fd_ref_table[sock]->GetPort())->OnRawSocketWrite(sock,tb,bytes);
369 catch (ModuleException& modexcept)
371 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
376 fd_ref_table[sock]->AddWriteBuf(tb);
379 ServerInstance->stats->statsSent += bytes;
382 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
385 /* write text to an destination user from a source user (e.g. user privmsg) */
387 void WriteTo(userrec *source, userrec *dest,char *data, ...)
389 char textbuffer[MAXBUF];
392 if ((!dest) || (!data))
394 log(DEFAULT,"*** BUG *** WriteTo was given an invalid parameter");
401 va_start(argsPtr, data);
402 vsnprintf(textbuffer, MAXBUF, data, argsPtr);
405 // if no source given send it from the server.
408 WriteServ_NoFormat(dest->fd,textbuffer);
412 WriteFrom_NoFormat(dest->fd,source,textbuffer);
416 void WriteTo_NoFormat(userrec *source, userrec *dest, const char *data)
418 if ((!dest) || (!data))
423 WriteServ_NoFormat(dest->fd,data);
427 WriteFrom_NoFormat(dest->fd,source,data);
431 std::string GetServerDescription(const char* servername)
433 std::string description = "";
435 FOREACH_MOD(I_OnGetServerDescription,OnGetServerDescription(servername,description));
437 if (description != "")
443 // not a remote server that can be found, it must be me.
444 return Config->ServerDesc;
448 /* write a formatted string to all users who share at least one common
449 * channel, including the source user e.g. for use in NICK */
451 void WriteCommon(userrec *u, char* text, ...)
453 char textbuffer[MAXBUF];
455 bool sent_to_at_least_one = false;
459 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
463 if (u->registered != REG_ALL)
465 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
469 va_start(argsPtr, text);
470 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
473 // XXX: Save on memset calls by using an ID. This clever trick thought of during discussion with nazzy and w00t.
476 for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
478 if (((ucrec*)(*v))->channel)
480 CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
482 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
484 if ((IS_LOCAL(i->second)) && (already_sent[i->second->fd] != uniq_id))
486 already_sent[i->second->fd] = uniq_id;
487 WriteFrom_NoFormat(i->second->fd,u,textbuffer);
488 sent_to_at_least_one = true;
495 * if the user was not in any channels, no users will receive the text. Make sure the user
496 * receives their OWN message for WriteCommon
498 if (!sent_to_at_least_one)
500 WriteFrom_NoFormat(u->fd,u,textbuffer);
504 void WriteCommon_NoFormat(userrec *u, const char* text)
506 bool sent_to_at_least_one = false;
510 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
514 if (u->registered != REG_ALL)
516 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
520 // XXX: See comment in WriteCommon
523 for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
525 if (((ucrec*)(*v))->channel)
527 CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
529 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
531 if ((IS_LOCAL(i->second)) && (already_sent[i->second->fd] != uniq_id))
533 already_sent[i->second->fd] = uniq_id;
534 WriteFrom_NoFormat(i->second->fd,u,text);
535 sent_to_at_least_one = true;
542 * if the user was not in any channels, no users will receive the text. Make sure the user
543 * receives their OWN message for WriteCommon
545 if (!sent_to_at_least_one)
547 WriteFrom_NoFormat(u->fd,u,text);
552 /* write a formatted string to all users who share at least one common
553 * channel, NOT including the source user e.g. for use in QUIT
556 void WriteCommonExcept(userrec *u, char* text, ...)
558 char textbuffer[MAXBUF];
559 char oper_quit[MAXBUF];
560 bool quit_munge = false;
566 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
570 if (u->registered != REG_ALL)
572 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
576 va_start(argsPtr, text);
577 total = vsnprintf(textbuffer, MAXBUF, text, argsPtr);
580 if ((Config->HideSplits) && (total > 6))
582 /* Yeah yeah, this is ugly. But its fast, live with it. */
583 char* check = textbuffer;
585 if ((*check++ == 'Q') && (*check++ == 'U') && (*check++ == 'I') && (*check++ == 'T') && (*check++ == ' ') && (*check++ == ':'))
587 std::stringstream split(check);
588 std::string server_one;
589 std::string server_two;
594 if ((FindServerName(server_one)) && (FindServerName(server_two)))
596 strlcpy(oper_quit,textbuffer,MAXQUIT);
597 strlcpy(check,"*.net *.split",MAXQUIT);
603 if ((Config->HideBans) && (total > 13) && (!quit_munge))
605 char* check = textbuffer;
608 if ((*check++ == 'Q') && (*check++ == 'U') && (*check++ == 'I') && (*check++ == 'T') && (*check++ == ' ') && (*check++ == ':'))
612 if ((*check++ == '-') && (*check++ == 'L') && (*check++ == 'i') && (*check++ == 'n') && (*check++ == 'e') && (*check++ == 'd') && (*check++ == ':'))
614 strlcpy(oper_quit,textbuffer,MAXQUIT);
615 *(--check) = 0; // We don't need to strlcpy, we just chop it from the :
623 for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
625 if (((ucrec*)(*v))->channel)
627 CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
629 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
633 if ((IS_LOCAL(i->second)) && (already_sent[i->second->fd] != uniq_id))
635 already_sent[i->second->fd] = uniq_id;
639 WriteFrom_NoFormat(i->second->fd,u,*i->second->oper ? oper_quit : textbuffer);
642 WriteFrom_NoFormat(i->second->fd,u,textbuffer);
650 void WriteCommonExcept_NoFormat(userrec *u, const char* text)
654 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
658 if (u->registered != REG_ALL)
660 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
666 for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
668 if (((ucrec*)(*v))->channel)
670 CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
672 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
676 if ((IS_LOCAL(i->second)) && (already_sent[i->second->fd] != uniq_id))
678 already_sent[i->second->fd] = uniq_id;
679 WriteFrom_NoFormat(i->second->fd,u,text);
688 /* XXX - We don't use WriteMode for this because WriteMode is very slow and
689 * this isnt. Basically WriteMode has to iterate ALL the users 'n' times for
690 * the number of modes provided, e.g. if you send WriteMode 'og' to write to
691 * opers with globops, and you have 2000 users, thats 4000 iterations. WriteOpers
692 * uses the oper list, which means if you have 2000 users but only 5 opers,
693 * it iterates 5 times.
695 void WriteOpers(const char* text, ...)
697 char textbuffer[MAXBUF];
702 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
706 va_start(argsPtr, text);
707 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
710 WriteOpers_NoFormat(textbuffer);
713 void WriteOpers_NoFormat(const char* text)
717 log(DEFAULT,"*** BUG *** WriteOpers_NoFormat was given an invalid parameter");
721 for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
727 if (a->modes[UM_SERVERNOTICE])
729 // send server notices to all with +s
730 WriteServ(a->fd,"NOTICE %s :%s",a->nick,text);
736 void ServerNoticeAll(char* text, ...)
741 char textbuffer[MAXBUF];
742 char formatbuffer[MAXBUF];
744 va_start (argsPtr, text);
745 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
748 snprintf(formatbuffer,MAXBUF,"NOTICE $%s :%s",Config->ServerName,textbuffer);
750 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
752 userrec* t = (userrec*)(*i);
753 WriteServ_NoFormat(t->fd,formatbuffer);
757 void ServerPrivmsgAll(char* text, ...)
762 char textbuffer[MAXBUF];
763 char formatbuffer[MAXBUF];
765 va_start (argsPtr, text);
766 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
769 snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s",Config->ServerName,textbuffer);
771 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
773 userrec* t = (userrec*)(*i);
774 WriteServ_NoFormat(t->fd,formatbuffer);
778 void WriteMode(const char* modes, int flags, const char* text, ...)
780 char textbuffer[MAXBUF];
784 if ((!text) || (!modes) || (!flags))
786 log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
790 va_start(argsPtr, text);
791 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
793 modelen = strlen(modes);
795 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
797 userrec* t = (userrec*)(*i);
798 bool send_to_user = false;
804 for (int n = 0; n < modelen; n++)
806 if (!t->modes[modes[n]-65])
808 send_to_user = false;
813 else if (flags == WM_OR)
815 send_to_user = false;
817 for (int n = 0; n < modelen; n++)
819 if (t->modes[modes[n]-65])
829 WriteServ(t->fd,"NOTICE %s :%s",t->nick,textbuffer);
834 void NoticeAll(userrec *source, bool local_only, char* text, ...)
836 char textbuffer[MAXBUF];
837 char formatbuffer[MAXBUF];
840 if ((!text) || (!source))
842 log(DEFAULT,"*** BUG *** NoticeAll was given an invalid parameter");
846 va_start(argsPtr, text);
847 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
850 snprintf(formatbuffer,MAXBUF,"NOTICE $* :%s",textbuffer);
852 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
854 userrec* t = (userrec*)(*i);
855 WriteFrom_NoFormat(t->fd,source,formatbuffer);
860 void WriteWallOps(userrec *source, bool local_only, char* text, ...)
862 char textbuffer[MAXBUF];
863 char formatbuffer[MAXBUF];
866 if ((!text) || (!source))
868 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
872 va_start(argsPtr, text);
873 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
876 snprintf(formatbuffer,MAXBUF,"WALLOPS :%s",textbuffer);
878 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
880 userrec* t = (userrec*)(*i);
882 if ((IS_LOCAL(t)) && (t->modes[UM_WALLOPS]))
884 WriteTo_NoFormat(source,t,formatbuffer);
889 /* convert a string to lowercase. Note following special circumstances
890 * taken from RFC 1459. Many "official" server branches still hold to this
891 * rule so i will too;
893 * Because of IRC's scandanavian origin, the characters {}| are
894 * considered to be the lower case equivalents of the characters []\,
895 * respectively. This is a critical issue when determining the
896 * equivalence of two nicknames.
898 void strlower(char *n)
902 for (char* t = n; *t; t++)
903 *t = lowermap[(unsigned char)*t];
907 /* Find a user record by nickname and return a pointer to it */
909 userrec* Find(const std::string &nick)
911 user_hash::iterator iter = clientlist.find(nick);
913 if (iter == clientlist.end())
914 /* Couldn't find it */
920 userrec* Find(const char* nick)
922 user_hash::iterator iter;
927 iter = clientlist.find(nick);
929 if (iter == clientlist.end())
935 /* find a channel record by channel name and return a pointer to it */
937 chanrec* FindChan(const char* chan)
939 chan_hash::iterator iter;
943 log(DEFAULT,"*** BUG *** Findchan was given an invalid parameter");
947 iter = chanlist.find(chan);
949 if (iter == chanlist.end())
950 /* Couldn't find it */
957 long GetMaxBans(char* name)
960 for (std::map<std::string,int>::iterator n = Config->maxbans.begin(); n != Config->maxbans.end(); n++)
963 if (match(name,x.c_str()))
971 void purge_empty_chans(userrec* u)
973 std::vector<chanrec*> to_delete;
975 // firstly decrement the count on each channel
976 for (std::vector<ucrec*>::iterator f = u->chans.begin(); f != u->chans.end(); f++)
978 ucrec* uc = (ucrec*)(*f);
981 if (uc->channel->DelUser(u) == 0)
983 /* No users left in here, mark it for deletion */
984 to_delete.push_back(uc->channel);
990 log(DEBUG,"purge_empty_chans: %d channels to delete",to_delete.size());
992 for (std::vector<chanrec*>::iterator n = to_delete.begin(); n != to_delete.end(); n++)
994 chanrec* thischan = (chanrec*)*n;
995 chan_hash::iterator i2 = chanlist.find(thischan->name);
996 if (i2 != chanlist.end())
998 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(i2->second));
1009 char* chanmodes(chanrec *chan, bool showkey)
1011 static char scratch[MAXBUF];
1012 static char sparam[MAXBUF];
1013 char* offset = scratch;
1014 std::string extparam = "";
1018 log(DEFAULT,"*** BUG *** chanmodes was given an invalid parameter");
1026 /* This was still iterating up to 190, chanrec::custom_modes is only 64 elements -- Om */
1027 for(int n = 0; n < 64; n++)
1036 extparam = (showkey ? chan->key : "<key>");
1039 extparam = ConvToStr(chan->limit);
1047 /* We know these have no parameters */
1050 extparam = chan->GetModeParameter(n+65);
1055 charlcat(sparam,' ',MAXBUF);
1056 strlcat(sparam,extparam.c_str(),MAXBUF);
1061 /* Null terminate scratch */
1063 strlcat(scratch,sparam,MAXBUF);
1068 /* compile a userlist of a channel into a string, each nick seperated by
1069 * spaces and op, voice etc status shown as @ and + */
1071 void userlist(userrec *user,chanrec *c)
1073 if ((!c) || (!user))
1075 log(DEFAULT,"*** BUG *** userlist was given an invalid parameter");
1080 size_t dlen, curlen;
1082 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1085 char* ptr = list + dlen;
1087 CUList *ulist= c->GetUsers();
1089 /* Improvement by Brain - this doesnt change in value, so why was it inside
1092 bool has_user = c->HasUser(user);
1094 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
1096 if ((!has_user) && (i->second->modes[UM_INVISIBLE]))
1099 * user is +i, and source not on the channel, does not show
1100 * nick in NAMES list
1105 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", cmode(i->second, c), i->second->nick);
1112 if (curlen > (480-NICKMAX))
1114 /* list overflowed into multiple numerics */
1115 WriteServ_NoFormat(user->fd,list);
1117 /* reset our lengths */
1118 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1126 /* if whats left in the list isnt empty, send it */
1129 WriteServ_NoFormat(user->fd,list);
1134 * return a count of the users on a specific channel accounting for
1135 * invisible users who won't increase the count. e.g. for /LIST
1137 int usercount_i(chanrec *c)
1144 CUList *ulist= c->GetUsers();
1145 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
1147 if (!(i->second->modes[UM_INVISIBLE]))
1154 int usercount(chanrec *c)
1156 return (c ? c->GetUserCounter() : 0);
1160 /* looks up a users password for their connection class (<ALLOW>/<DENY> tags)
1161 * NOTE: If the <ALLOW> or <DENY> tag specifies an ip, and this user resolves,
1162 * then their ip will be taken as 'priority' anyway, so for example,
1163 * <connect allow="127.0.0.1"> will match joe!bloggs@localhost
1165 ConnectClass GetClass(userrec *user)
1167 for (ClassVector::iterator i = Config->Classes.begin(); i != Config->Classes.end(); i++)
1169 if ((match(user->GetIPString(),i->host.c_str(),true)) || (match(user->host,i->host.c_str())))
1175 return *(Config->Classes.begin());
1179 * sends out an error notice to all connected clients (not to be used
1182 void send_error(char *s)
1184 log(DEBUG,"send_error: %s",s);
1186 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1188 userrec* t = (userrec*)(*i);
1189 if (t->registered == REG_ALL)
1191 WriteServ(t->fd,"NOTICE %s :%s",t->nick,s);
1195 // fix - unregistered connections receive ERROR, not NOTICE
1196 Write(t->fd,"ERROR :%s",s);
1201 void Error(int status)
1207 signal(SIGALRM, SIG_IGN);
1208 signal(SIGPIPE, SIG_IGN);
1209 signal(SIGTERM, SIG_IGN);
1210 signal(SIGABRT, SIG_IGN);
1211 signal(SIGSEGV, SIG_IGN);
1212 signal(SIGURG, SIG_IGN);
1213 signal(SIGKILL, SIG_IGN);
1214 log(DEFAULT,"*** fell down a pothole in the road to perfection ***");
1216 log(DEFAULT,"Please report the backtrace lines shown below with any bugreport to the bugtracker at http://www.inspircd.org/bugtrack/");
1217 size = backtrace(array, 30);
1218 strings = backtrace_symbols(array, size);
1219 for (size_t i = 0; i < size; i++) {
1220 log(DEFAULT,"[%d] %s", i, strings[i]);
1223 WriteOpers("*** SIGSEGV: Please see the ircd.log for backtrace and report the error to http://www.inspircd.org/bugtrack/");
1225 log(DEFAULT,"You do not have execinfo.h so i could not backtrace -- on FreeBSD, please install the libexecinfo port.");
1227 send_error("Somebody screwed up... Whoops. IRC Server terminating.");
1228 signal(SIGSEGV, SIG_DFL);
1229 if (raise(SIGSEGV) == -1)
1231 log(DEFAULT,"What the hell, i couldnt re-raise SIGSEGV! Error: %s",strerror(errno));
1236 // this function counts all users connected, wether they are registered or NOT.
1239 return clientlist.size();
1242 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
1243 int registered_usercount(void)
1247 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1249 c += (i->second->registered == REG_ALL);
1255 int usercount_invisible(void)
1259 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1261 c += ((i->second->registered == REG_ALL) && (i->second->modes[UM_INVISIBLE]));
1267 int usercount_opers(void)
1271 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1273 if (*(i->second->oper))
1279 int usercount_unknown(void)
1283 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1285 userrec* t = (userrec*)(*i);
1286 if (t->registered != REG_ALL)
1293 long chancount(void)
1295 return chanlist.size();
1302 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1304 userrec* t = (userrec*)(*i);
1305 if (t->registered == REG_ALL)
1312 void ShowMOTD(userrec *user)
1314 if (!Config->MOTD.size())
1316 WriteServ(user->fd,"422 %s :Message of the day file is missing.",user->nick);
1319 WriteServ(user->fd,"375 %s :%s message of the day", user->nick, Config->ServerName);
1321 for (unsigned int i = 0; i < Config->MOTD.size(); i++)
1322 WriteServ(user->fd,"372 %s :- %s",user->nick,Config->MOTD[i].c_str());
1324 WriteServ(user->fd,"376 %s :End of message of the day.", user->nick);
1327 void ShowRULES(userrec *user)
1329 if (!Config->RULES.size())
1331 WriteServ(user->fd,"NOTICE %s :Rules file is missing.",user->nick);
1334 WriteServ(user->fd,"NOTICE %s :%s rules",user->nick,Config->ServerName);
1336 for (unsigned int i = 0; i < Config->RULES.size(); i++)
1337 WriteServ(user->fd,"NOTICE %s :%s",user->nick,Config->RULES[i].c_str());
1339 WriteServ(user->fd,"NOTICE %s :End of %s rules.",user->nick,Config->ServerName);
1342 // this returns 1 when all modules are satisfied that the user should be allowed onto the irc server
1343 // (until this returns true, a user will block in the waiting state, waiting to connect up to the
1344 // registration timeout maximum seconds)
1345 bool AllModulesReportReady(userrec* user)
1347 if (!Config->global_implementation[I_OnCheckReady])
1350 for (int i = 0; i <= MODCOUNT; i++)
1352 if (Config->implement_lists[i][I_OnCheckReady])
1354 int res = modules[i]->OnCheckReady(user);
1363 /* Make Sure Modules Are Avaliable!
1364 * (BugFix By Craig.. See? I do work! :p)
1365 * Modified by brain, requires const char*
1366 * to work with other API functions
1370 bool FileExists (const char* file)
1373 if ((input = fopen (file, "r")) == NULL)
1384 char* CleanFilename(char* name)
1386 char* p = name + strlen(name);
1387 while ((p != name) && (*p != '/')) p--;
1388 return (p != name ? ++p : p);
1391 bool DirValid(char* dirandfile)
1394 char buffer[MAXBUF];
1395 char otherdir[MAXBUF];
1398 strlcpy(work, dirandfile, MAXBUF);
1401 // we just want the dir
1413 // Get the current working directory
1414 if (getcwd(buffer, MAXBUF ) == NULL )
1419 if (getcwd(otherdir, MAXBUF ) == NULL )
1424 size_t t = strlen(work);
1426 if (strlen(otherdir) >= t)
1430 if (!strcmp(otherdir,work))
1443 std::string GetFullProgDir(char** argv, int argc)
1446 char buffer[MAXBUF];
1447 char otherdir[MAXBUF];
1450 strlcpy(work,argv[0],MAXBUF);
1453 // we just want the dir
1465 // Get the current working directory
1466 if (getcwd(buffer, MAXBUF) == NULL)
1471 if (getcwd(otherdir, MAXBUF) == NULL)
1478 int InsertMode(std::string &output, const char* mode, unsigned short section)
1480 unsigned short currsection = 1;
1481 unsigned int pos = output.find("CHANMODES=", 0) + 10; // +10 for the length of "CHANMODES="
1483 if(section > 4 || section == 0)
1485 log(DEBUG, "InsertMode: CHANMODES doesn't have a section %dh :/", section);
1489 for(; pos < output.size(); pos++)
1491 if(section == currsection)
1494 if(output[pos] == ',')
1498 output.insert(pos, mode);
1502 bool IsValidChannelName(const char *chname)
1506 /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
1507 if (!chname || *chname != '#')
1512 c = (char *)chname + 1;
1526 /* too long a name - note funky pointer arithmetic here. */
1527 if ((c - chname) > CHANMAX)
1535 inline int charlcat(char* x,char y,int z)
1552 bool charremove(char* mp, char remove)
1555 bool shift_down = false;
1559 if (*mptr == remove)
1571 void OpenLog(char** argv, int argc)
1575 if (Config->logpath == "")
1577 Config->logpath = GetFullProgDir(argv,argc) + "/ircd.log";
1582 Config->log_file = fopen(LOG_FILE,"a+");
1584 if (!Config->log_file)
1586 printf("ERROR: Could not write to logfile %s, bailing!\n\n",Config->logpath.c_str());
1593 Config->log_file = fopen(Config->logpath.c_str(),"a+");
1595 if (!Config->log_file)
1597 printf("ERROR: Could not write to logfile %s, bailing!\n\n",Config->logpath.c_str());
1606 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
1607 log(DEFAULT,"InspIRCd: startup: not starting with UID 0!");
1614 if (*Config->DieValue)
1616 printf("WARNING: %s\n\n",Config->DieValue);
1617 log(DEFAULT,"Uh-Oh, somebody didn't read their config file: '%s'",Config->DieValue);
1622 /* We must load the modules AFTER initializing the socket engine, now */
1623 void LoadAllModules(InspIRCd* ServerInstance)
1625 char configToken[MAXBUF];
1626 Config->module_names.clear();
1629 for (int count = 0; count < Config->ConfValueEnum(Config->config_data, "module"); count++)
1631 Config->ConfValue(Config->config_data, "module","name",count,configToken,MAXBUF);
1632 printf("[\033[1;32m*\033[0m] Loading module:\t\033[1;32m%s\033[0m\n",configToken);
1634 if (!ServerInstance->LoadModule(configToken))
1636 log(DEFAULT,"Exiting due to a module loader error.");
1637 printf("\nThere was an error loading a module: %s\n\n",ServerInstance->ModuleError());
1642 log(DEFAULT,"Total loaded modules: %lu",(unsigned long)MODCOUNT+1);