X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fhelperfuncs.cpp;h=02a23c0acf73ad33137d80a35db158ce54b7afdb;hb=c4458ecc70025aeac7ca87115ed0a698e7bbcdad;hp=3a0f39c6d47e12e106ccc4783f12780a68257f20;hpb=d54fbc71e2a2e0f70002e2d36c669c4aa3d59e17;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/helperfuncs.cpp b/src/helperfuncs.cpp index 3a0f39c6d..02a23c0ac 100644 --- a/src/helperfuncs.cpp +++ b/src/helperfuncs.cpp @@ -3,13 +3,13 @@ * +------------------------------------+ * * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev. - * E-mail: - * - * + * E-mail: + * + * * * Written by Craig Edwards, Craig McLure, and others. * This program is free but copyrighted software; see - * the file COPYING for details. + * the file COPYING for details. * * --------------------------------------------------- */ @@ -21,9 +21,13 @@ #include #include #include +#include #include #include #include +#ifdef HAS_EXECINFO +#include +#endif #include "connection.h" #include "users.h" #include "ctables.h" @@ -47,7 +51,7 @@ extern InspIRCd* ServerInstance; extern time_t TIME; extern char lowermap[255]; extern userrec* fd_ref_table[MAX_DESCRIPTORS]; -static char already_sent[MAX_DESCRIPTORS]; +static unsigned long already_sent[MAX_DESCRIPTORS] = {0}; extern std::vector all_opers; extern user_hash clientlist; extern chan_hash chanlist; @@ -59,11 +63,14 @@ extern std::vector local_users; static char TIMESTR[26]; static time_t LAST = 0; +/* XXX: Used for speeding up WriteCommon operations */ +unsigned long uniq_id = 0; + /** log() * Write a line of text `text' to the logfile (and stdout, if in nofork) if the level `level' * is greater than the configured loglevel. */ -void log(int level, char *text, ...) +void do_log(int level, const char *text, ...) { va_list argsPtr; char textbuffer[MAXBUF]; @@ -88,7 +95,10 @@ void log(int level, char *text, ...) va_end(argsPtr); if (Config->writelog) + { fprintf(Config->log_file,"%s %s\n",TIMESTR,textbuffer); + fflush(Config->log_file); + } } if (Config->nofork) @@ -151,11 +161,10 @@ void Write_NoFormat(int sock, const char *text) if ((sock < 0) || (!text) || (sock > MAX_DESCRIPTORS)) return; - bytes = snprintf(tb,MAXBUF,"%s\r\n",text); - chop(tb); - if (fd_ref_table[sock]) { + bytes = snprintf(tb,MAXBUF,"%s\r\n",text); + if (Config->GetIOHook(fd_ref_table[sock]->port)) { try @@ -196,14 +205,14 @@ void Write(int sock, char *text, ...) return; } - va_start(argsPtr, text); - vsnprintf(textbuffer, MAXBUF, text, argsPtr); - va_end(argsPtr); - bytes = snprintf(tb,MAXBUF,"%s\r\n",textbuffer); - chop(tb); - if (fd_ref_table[sock]) { + + va_start(argsPtr, text); + vsnprintf(textbuffer, MAXBUF, text, argsPtr); + va_end(argsPtr); + bytes = snprintf(tb,MAXBUF,"%s\r\n",textbuffer); + if (Config->GetIOHook(fd_ref_table[sock]->port)) { try @@ -236,11 +245,10 @@ void WriteServ_NoFormat(int sock, const char* text) if ((sock < 0) || (!text) || (sock > MAX_DESCRIPTORS)) return; - bytes = snprintf(tb,MAXBUF,":%s %s\r\n",Config->ServerName,text); - chop(tb); - if (fd_ref_table[sock]) { + bytes = snprintf(tb,MAXBUF,":%s %s\r\n",Config->ServerName,text); + if (Config->GetIOHook(fd_ref_table[sock]->port)) { try @@ -279,6 +287,9 @@ void WriteServ(int sock, char* text, ...) return; } + if (!fd_ref_table[sock]) + return; + va_start(argsPtr, text); vsnprintf(textbuffer, MAXBUF, text, argsPtr); va_end(argsPtr); @@ -298,11 +309,10 @@ void WriteFrom_NoFormat(int sock, userrec *user, const char* text) if ((sock < 0) || (!text) || (!user) || (sock > MAX_DESCRIPTORS)) return; - bytes = snprintf(tb,MAXBUF,":%s %s\r\n",user->GetFullHost(),text); - chop(tb); - if (fd_ref_table[sock]) { + bytes = snprintf(tb,MAXBUF,":%s %s\r\n",user->GetFullHost(),text); + if (Config->GetIOHook(fd_ref_table[sock]->port)) { try @@ -342,14 +352,14 @@ void WriteFrom(int sock, userrec *user,char* text, ...) return; } - va_start(argsPtr, text); - vsnprintf(textbuffer, MAXBUF, text, argsPtr); - va_end(argsPtr); - bytes = snprintf(tb,MAXBUF,":%s %s\r\n",user->GetFullHost(),textbuffer); - chop(tb); - if (fd_ref_table[sock]) { + + va_start(argsPtr, text); + vsnprintf(textbuffer, MAXBUF, text, argsPtr); + va_end(argsPtr); + bytes = snprintf(tb,MAXBUF,":%s %s\r\n",user->GetFullHost(),textbuffer); + if (Config->GetIOHook(fd_ref_table[sock]->port)) { try @@ -391,7 +401,6 @@ void WriteTo(userrec *source, userrec *dest,char *data, ...) va_start(argsPtr, data); vsnprintf(textbuffer, MAXBUF, data, argsPtr); va_end(argsPtr); - chop(textbuffer); // if no source given send it from the server. if (!source) @@ -535,7 +544,7 @@ void WriteChannelLocal_NoFormat(chanrec* Ptr, userrec* user, const char* text) -void WriteChannelWithServ(char* ServName, chanrec* Ptr, char* text, ...) +void WriteChannelWithServ(const char* ServName, chanrec* Ptr, const char* text, ...) { char textbuffer[MAXBUF]; va_list argsPtr; @@ -560,7 +569,7 @@ void WriteChannelWithServ(char* ServName, chanrec* Ptr, char* text, ...) } } -void WriteChannelWithServ_NoFormat(char* ServName, chanrec* Ptr, const char* text) +void WriteChannelWithServ_NoFormat(const char* ServName, chanrec* Ptr, const char* text) { CUList *ulist; @@ -658,7 +667,7 @@ void ChanExceptSender_NoFormat(chanrec* Ptr, userrec* user, char status, const c } } -std::string GetServerDescription(char* servername) +std::string GetServerDescription(const char* servername) { std::string description = ""; @@ -700,8 +709,8 @@ void WriteCommon(userrec *u, char* text, ...) vsnprintf(textbuffer, MAXBUF, text, argsPtr); va_end(argsPtr); - // FIX: Stops a message going to the same person more than once - memset(&already_sent,0,MAX_DESCRIPTORS); + // XXX: Save on memset calls by using an ID. This clever trick thought of during discussion with nazzy and w00t. + uniq_id++; for (std::vector::const_iterator v = u->chans.begin(); v != u->chans.end(); v++) { @@ -711,9 +720,9 @@ void WriteCommon(userrec *u, char* text, ...) for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++) { - if ((i->second->fd > -1) && (!already_sent[i->second->fd])) + if ((i->second->fd > -1) && (already_sent[i->second->fd] != uniq_id)) { - already_sent[i->second->fd] = 1; + already_sent[i->second->fd] = uniq_id; WriteFrom_NoFormat(i->second->fd,u,textbuffer); sent_to_at_least_one = true; } @@ -747,8 +756,8 @@ void WriteCommon_NoFormat(userrec *u, const char* text) return; } - // FIX: Stops a message going to the same person more than once - memset(&already_sent,0,MAX_DESCRIPTORS); + // XXX: See comment in WriteCommon + uniq_id++; for (std::vector::const_iterator v = u->chans.begin(); v != u->chans.end(); v++) { @@ -758,9 +767,9 @@ void WriteCommon_NoFormat(userrec *u, const char* text) for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++) { - if ((i->second->fd > -1) && (!already_sent[i->second->fd])) + if ((i->second->fd > -1) && (already_sent[i->second->fd] != uniq_id)) { - already_sent[i->second->fd] = 1; + already_sent[i->second->fd] = uniq_id; WriteFrom_NoFormat(i->second->fd,u,text); sent_to_at_least_one = true; } @@ -848,7 +857,7 @@ void WriteCommonExcept(userrec *u, char* text, ...) } } - memset(&already_sent,0,MAX_DESCRIPTORS); + uniq_id++; for (std::vector::const_iterator v = u->chans.begin(); v != u->chans.end(); v++) { @@ -860,9 +869,9 @@ void WriteCommonExcept(userrec *u, char* text, ...) { if (u != i->second) { - if ((i->second->fd > -1) && (!already_sent[i->second->fd])) + if ((i->second->fd > -1) && (already_sent[i->second->fd] != uniq_id)) { - already_sent[i->second->fd] = 1; + already_sent[i->second->fd] = uniq_id; if (quit_munge) { @@ -891,7 +900,7 @@ void WriteCommonExcept_NoFormat(userrec *u, const char* text) return; } - memset(&already_sent,0,MAX_DESCRIPTORS); + uniq_id++; for (std::vector::const_iterator v = u->chans.begin(); v != u->chans.end(); v++) { @@ -903,9 +912,9 @@ void WriteCommonExcept_NoFormat(userrec *u, const char* text) { if (u != i->second) { - if ((i->second->fd > -1) && (!already_sent[i->second->fd])) + if ((i->second->fd > -1) && (already_sent[i->second->fd] != uniq_id)) { - already_sent[i->second->fd] = 1; + already_sent[i->second->fd] = uniq_id; WriteFrom_NoFormat(i->second->fd,u,text); } } @@ -954,7 +963,7 @@ void WriteOpers_NoFormat(const char* text) if (IS_LOCAL(a)) { - if (a->modebits & UM_SERVERNOTICE) + if (a->modes[UM_SERVERNOTICE]) { // send server notices to all with +s WriteServ(a->fd,"NOTICE %s :%s",a->nick,text); @@ -1033,7 +1042,7 @@ void WriteMode(const char* modes, int flags, const char* text, ...) for (int n = 0; n < modelen; n++) { - if (!hasumode(t,modes[n])) + if (!t->modes[modes[n]-65]) { send_to_user = false; break; @@ -1046,7 +1055,7 @@ void WriteMode(const char* modes, int flags, const char* text, ...) for (int n = 0; n < modelen; n++) { - if (hasumode(t,modes[n])) + if (t->modes[modes[n]-65]) { send_to_user = true; break; @@ -1109,7 +1118,7 @@ void WriteWallOps(userrec *source, bool local_only, char* text, ...) { userrec* t = (userrec*)(*i); - if ((IS_LOCAL(t)) && (t->modebits & UM_WALLOPS)) + if ((IS_LOCAL(t)) && (t->modes[UM_WALLOPS])) { WriteTo_NoFormat(source,t,formatbuffer); } @@ -1205,13 +1214,14 @@ void purge_empty_chans(userrec* u) // firstly decrement the count on each channel for (std::vector::iterator f = u->chans.begin(); f != u->chans.end(); f++) { - if (((ucrec*)(*f))->channel) + ucrec* uc = (ucrec*)(*f); + if (uc->channel) { - if (((ucrec*)(*f))->channel->DelUser(u) == 0) + if (uc->channel->DelUser(u) == 0) { /* No users left in here, mark it for deletion */ - to_delete.push_back(((ucrec*)(*f))->channel); - ((ucrec*)(*f))->channel = NULL; + to_delete.push_back(uc->channel); + uc->channel = NULL; } } } @@ -1225,7 +1235,7 @@ void purge_empty_chans(userrec* u) if (i2 != chanlist.end()) { FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(i2->second)); - delete i2->second; + DELETE(i2->second); chanlist.erase(i2); } } @@ -1289,7 +1299,7 @@ char* chanmodes(chanrec *chan, bool showkey) /* Null terminate scratch */ *offset = '\0'; - strlcat(scratch,sparam,MAXMODES); + strlcat(scratch,sparam,MAXBUF); return scratch; } @@ -1322,7 +1332,7 @@ void userlist(userrec *user,chanrec *c) for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++) { - if ((!has_user) && (i->second->modebits & UM_INVISIBLE)) + if ((!has_user) && (i->second->modes[UM_INVISIBLE])) { /* * user is +i, and source not on the channel, does not show @@ -1373,7 +1383,7 @@ int usercount_i(chanrec *c) CUList *ulist= c->GetUsers(); for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++) { - if (!(i->second->modebits & UM_INVISIBLE)) + if (!(i->second->modes[UM_INVISIBLE])) count++; } @@ -1386,12 +1396,16 @@ int usercount(chanrec *c) } -/* looks up a users password for their connection class (/ tags) */ +/* looks up a users password for their connection class (/ tags) + * NOTE: If the or tag specifies an ip, and this user resolves, + * then their ip will be taken as 'priority' anyway, so for example, + * will match joe!bloggs@localhost + */ ConnectClass GetClass(userrec *user) { for (ClassVector::iterator i = Config->Classes.begin(); i != Config->Classes.end(); i++) { - if (match(user->host,i->host.c_str())) + if ((match(inet_ntoa(user->ip4),i->host.c_str())) || (match(user->host,i->host.c_str()))) { return *i; } @@ -1425,6 +1439,10 @@ void send_error(char *s) void Error(int status) { + void *array[300]; + size_t size; + char **strings; + signal(SIGALRM, SIG_IGN); signal(SIGPIPE, SIG_IGN); signal(SIGTERM, SIG_IGN); @@ -1433,7 +1451,24 @@ void Error(int status) signal(SIGURG, SIG_IGN); signal(SIGKILL, SIG_IGN); log(DEFAULT,"*** fell down a pothole in the road to perfection ***"); - send_error("Error! Segmentation fault! save meeeeeeeeeeeeee *splat!*"); +#ifdef HAS_EXECINFO + log(DEFAULT,"Please report the backtrace lines shown below with any bugreport to the bugtracker at http://www.inspircd.org/bugtrack/"); + size = backtrace(array, 30); + strings = backtrace_symbols(array, size); + for (size_t i = 0; i < size; i++) { + log(DEFAULT,"[%d] %s", i, strings[i]); + } + free(strings); + WriteOpers("*** SIGSEGV: Please see the ircd.log for backtrace and report the error to http://www.inspircd.org/bugtrack/"); +#else + log(DEFAULT,"You do not have execinfo.h so i could not backtrace -- on FreeBSD, please install the libexecinfo port."); +#endif + send_error("Somebody screwed up... Whoops. IRC Server terminating."); + signal(SIGSEGV, SIG_DFL); + if (raise(SIGSEGV) == -1) + { + log(DEFAULT,"What the hell, i couldnt re-raise SIGSEGV! Error: %s",strerror(errno)); + } Exit(status); } @@ -1462,7 +1497,7 @@ int usercount_invisible(void) for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++) { - if ((i->second->registered == 7) && (i->second->modebits & UM_INVISIBLE)) + if ((i->second->registered == 7) && (i->second->modes[UM_INVISIBLE])) c++; } @@ -1516,44 +1551,17 @@ long local_count() void ShowMOTD(userrec *user) { - static char mbuf[MAXBUF]; - static char crud[MAXBUF]; - std::string WholeMOTD = ""; - if (!Config->MOTD.size()) { WriteServ(user->fd,"422 %s :Message of the day file is missing.",user->nick); return; } - - snprintf(crud,MAXBUF,":%s 372 %s :- ", Config->ServerName, user->nick); - snprintf(mbuf,MAXBUF,":%s 375 %s :- %s message of the day\r\n", Config->ServerName, user->nick, Config->ServerName); - WholeMOTD = WholeMOTD + mbuf; + WriteServ(user->fd,"375 %s :%s message of the day", user->nick, Config->ServerName); for (unsigned int i = 0; i < Config->MOTD.size(); i++) - WholeMOTD = WholeMOTD + std::string(crud) + Config->MOTD[i].c_str() + std::string("\r\n"); - - snprintf(mbuf,MAXBUF,":%s 376 %s :End of message of the day.\r\n", Config->ServerName, user->nick); - WholeMOTD = WholeMOTD + mbuf; - - // only one write operation - if (Config->GetIOHook(user->port)) - { - try - { - Config->GetIOHook(user->port)->OnRawSocketWrite(user->fd,(char*)WholeMOTD.c_str(),WholeMOTD.length()); - } - catch (ModuleException& modexcept) - { - log(DEBUG,"Module exception caught: %s",modexcept.GetReason()); - } - } - else - { - user->AddWriteBuf(WholeMOTD); - } + WriteServ(user->fd,"372 %s :- %s",user->nick,Config->MOTD[i].c_str()); - ServerInstance->stats->statsSent += WholeMOTD.length(); + WriteServ(user->fd,"376 %s :End of message of the day.", user->nick); } void ShowRULES(userrec *user) @@ -1863,7 +1871,7 @@ void LoadAllModules(InspIRCd* ServerInstance) Config->ConfValue(Config->config_data, "module","name",count,configToken,MAXBUF); printf("[\033[1;32m*\033[0m] Loading module:\t\033[1;32m%s\033[0m\n",configToken); - if (!ServerInstance->LoadModule(configToken)) + if (!ServerInstance->LoadModule(configToken)) { log(DEFAULT,"Exiting due to a module loader error."); printf("\nThere was an error loading a module: %s\n\n",ServerInstance->ModuleError());