* | Inspire Internet Relay Chat Daemon |
* +------------------------------------+
*
- * Inspire is copyright (C) 2002-2003 ChatSpike-Dev.
+ * Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
* E-mail:
* <brain@chatspike.net>
* <Craig@chatspike.net>
* This program is free but copyrighted software; see
* the file COPYING for details.
*
- * ---------------------------------------------------
-
- $Log$
- Revision 1.1 2003/01/23 19:45:58 brain
- Initial revision
-
- Revision 1.56 2003/01/22 20:49:16 brain
- Added FileReader file-caching class
- Changed m_randquote to use FileReader class
-
- Revision 1.55 2003/01/21 16:56:19 brain
- Fixed a few minor bugs
-
- Revision 1.54 2003/01/21 02:07:10 brain
- optimisations galore!
-
- Revision 1.53 2003/01/21 00:18:40 brain
- fixed random crash on kill_link (AGAIN) - was /stats
- improved speed 10x (because i can...)
-
- Revision 1.52 2003/01/19 20:12:24 brain
- Fixed ident max length to 10
-
- Revision 1.51 2003/01/18 22:02:11 brain
- fixed multiple /MODE +l bugs (thanks to akky and BOFH bugging meh!)
-
- Revision 1.50 2003/01/18 01:19:14 brain
- Added code to tidy up bans (e.g. max nick length) - i blame mIRC!
-
- Revision 1.49 2003/01/17 22:03:57 brain
- Fixed dodgy mode glitches (the ones Craig loves to play with, awww)
-
- Revision 1.48 2003/01/17 21:20:43 brain
- Implemented usermode +s
-
- Revision 1.47 2003/01/17 21:13:40 brain
- Added channel modes, +k, +l, +i, +m etc
- Added user and channel modes +i, +p, +s
-
- Revision 1.46 2003/01/17 18:44:27 brain
- Implemented channel mode +m
-
- Revision 1.45 2003/01/17 18:26:42 brain
- added /TRACE command
-
- Revision 1.44 2003/01/17 15:21:03 brain
- Fixed: /LUSERS cant count :P
-
- Revision 1.43 2003/01/17 13:21:38 brain
- Added CONNECT ALLOW and CONNECT DENY config tags
- Added PASS command
-
- Revision 1.42 2003/01/17 10:37:55 brain
- Added /INVITE command and relevent structures
-
- Revision 1.41 2003/01/16 20:11:55 brain
- fixed some ugly pointer bugs (thanks dblack and a|KK|y!)
-
- Revision 1.40 2003/01/16 08:31:44 brain
- Fixed parameter error in QUIT code (was showing junk chars on BSD)
-
- Revision 1.39 2003/01/15 22:47:44 brain
- Changed user and channel structs to classes (finally)
-
- Revision 1.38 2003/01/15 20:56:58 brain
- Added wildcard support
- Added channel bans
-
- Revision 1.37 2003/01/15 16:08:42 brain
- Attempted to fix closed client sessions not being detected
-
- Revision 1.36 2003/01/15 09:36:13 brain
- added pause= value to /die and /restart in config
-
- Revision 1.35 2003/01/14 22:08:31 brain
- attemted to fix weird crash on /kill
-
- Revision 1.34 2003/01/14 21:44:25 brain
- Added /USERS stub
- Added /SUMMON stub
- Changed optimisation to -O3 (much faster!)
-
- Revision 1.33 2003/01/14 21:14:30 brain
- added /ISON command (for mIRC etc basic notify)
-
- Revision 1.32 2003/01/14 20:55:02 brain
- Fixed more param crunching bugs
- Added /AWAY
-
- 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! ;) */
+using namespace std;
+
#include "inspircd.h"
#include "inspircd_io.h"
#include "inspircd_util.h"
#include <cstdio>
#include <time.h>
#include <string>
-#include <hash_map.h>
+#ifdef GCC3
+#include <ext/hash_map>
+#else
+#include <hash_map>
+#endif
+#include <map>
#include <sstream>
#include <vector>
#include <errno.h>
#include <deque>
+#include "connection.h"
#include "users.h"
+#include "servers.h"
#include "ctables.h"
#include "globals.h"
#include "modules.h"
#include "dynamic.h"
#include "wildcard.h"
-using namespace std;
+#ifdef GCC3
+#define nspace __gnu_cxx
+#else
+#define nspace std
+#endif
+int LogLevel = DEFAULT;
char ServerName[MAXBUF];
char Network[MAXBUF];
char ServerDesc[MAXBUF];
char PrefixQuit[MAXBUF];
char DieValue[MAXBUF];
int debugging = 0;
-int MODCOUNT = -1;
+int WHOWAS_STALE = 48; // default WHOWAS Entries last 2 days before they go 'stale'
+int WHOWAS_MAX = 100; // default 100 people maximum in the WHOWAS list
int DieDelay = 5;
time_t startup_time = time(NULL);
-template<> struct hash<in_addr>
+extern vector<Module*> modules;
+vector<string> module_names;
+extern vector<ircd_module*> factory;
+vector<int> fd_reap;
+
+extern int MODCOUNT;
+
+bool nofork = false;
+
+namespace nspace
{
- size_t operator()(const struct in_addr &a) const
+ template<> struct nspace::hash<in_addr>
{
- size_t q;
- memcpy(&q,&a,sizeof(size_t));
- return q;
- }
-};
+ 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
+ template<> struct nspace::hash<string>
{
- char a[MAXBUF];
- static struct hash<const char *> strhash;
- strcpy(a,s.c_str());
- strlower(a);
- return strhash(a);
- }
-};
-
+ 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
};
-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 deque<command_t> command_table;
-typedef DLLFactory<ModuleFactory> ircd_module;
+typedef nspace::hash_map<std::string, userrec*, nspace::hash<string>, StrHashComp> user_hash;
+typedef nspace::hash_map<std::string, chanrec*, nspace::hash<string>, StrHashComp> chan_hash;
+typedef nspace::hash_map<in_addr,string*, nspace::hash<in_addr>, InAddr_HashComp> address_cache;
+typedef std::deque<command_t> command_table;
+
+serverrec* me[32];
+serverrec* servers[255];
+
+FILE *log_file;
user_hash clientlist;
chan_hash chanlist;
+user_hash whowas;
command_table cmdlist;
file_cache MOTD;
file_cache RULES;
address_cache IP;
-vector<Module*> modules(255);
-vector<ircd_module*> factory(255);
+
ClassVector Classes;
struct linger linger = { 0 };
char bannerBuffer[MAXBUF];
int boundPortCount = 0;
+int portCount = 0, UDPportCount = 0, ports[MAXSOCKS];
+int defaultRoute = 0;
+
+connection C;
+
+long MyKey = C.GenKey();
/* prototypes */
void update_stats_l(int fd,int data_out);
char* Passwd(userrec *user);
bool IsDenied(userrec *user);
+void AddWhoWas(userrec* u);
+std::stringstream config_f(stringstream::in | stringstream::out);
void safedelete(userrec *p)
{
if (p)
{
- debug("deleting %s %s %s %s",p->nick,p->ident,p->dhost,p->fullname);
- debug("safedelete(userrec*): pointer is safe to delete");
+ log(DEBUG,"deleting %s %s %s %s",p->nick,p->ident,p->dhost,p->fullname);
+ log(DEBUG,"safedelete(userrec*): pointer is safe to delete");
delete p;
}
else
{
- debug("safedelete(userrec*): unsafe pointer operation squished");
+ log(DEBUG,"safedelete(userrec*): unsafe pointer operation squished");
}
}
if (p)
{
delete p;
- debug("safedelete(chanrec*): pointer is safe to delete");
+ log(DEBUG,"safedelete(chanrec*): pointer is safe to delete");
}
else
{
- debug("safedelete(chanrec*): unsafe pointer operation squished");
+ log(DEBUG,"safedelete(chanrec*): unsafe pointer operation squished");
}
}
+void tidystring(char* str)
+{
+ // strips out double spaces before a : parameter
+
+ char temp[MAXBUF];
+ bool go_again = true;
+
+ if (!str)
+ {
+ return;
+ }
+
+ while ((str[0] == ' ') && (strlen(str)>0))
+ {
+ str++;
+ }
+
+ while (go_again)
+ {
+ bool noparse = false;
+ int t = 0, a = 0;
+ go_again = false;
+ while (a < strlen(str))
+ {
+ if ((a<strlen(str)-1) && (noparse==false))
+ {
+ if ((str[a] == ' ') && (str[a+1] == ' '))
+ {
+ log(DEBUG,"Tidied extra space out of string: %s",str);
+ go_again = true;
+ a++;
+ }
+ }
+
+ if (a<strlen(str)-1)
+ {
+ if ((str[a] == ' ') && (str[a+1] == ':'))
+ {
+ noparse = true;
+ }
+ }
+
+ temp[t++] = str[a++];
+ }
+ temp[t] = '\0';
+ strncpy(str,temp,MAXBUF);
+ }
+}
+
/* 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';
- }
+
+ string temp = str;
+ FOREACH_MOD OnServerRaw(temp,false);
+ const char* str2 = temp.c_str();
+ sprintf(str,"%s",str2);
+
+
+ if (strlen(str) >= 512)
+ {
+ str[509] = '\r';
+ str[510] = '\n';
+ str[511] = '\0';
+ }
}
-string getservername()
+std::string getservername()
{
return ServerName;
}
-string getnetworkname()
+std::string getserverdesc()
+{
+ return ServerDesc;
+}
+
+std::string getnetworkname()
{
return Network;
}
-string getadminname()
+std::string getadminname()
{
return AdminName;
}
-string getadminemail()
+std::string getadminemail()
{
return AdminEmail;
}
-string getadminnick()
+std::string getadminnick()
{
return AdminNick;
}
-void debug(char *text, ...)
+void log(int level,char *text, ...)
{
- char textbuffer[MAXBUF];
- va_list argsPtr;
- FILE *f;
- time_t rawtime;
- struct tm * timeinfo;
+ char textbuffer[MAXBUF];
+ va_list argsPtr;
+ time_t rawtime;
+ struct tm * timeinfo;
+ if (level < LogLevel)
+ return;
- time(&rawtime);
- timeinfo = localtime (&rawtime);
+ time(&rawtime);
+ timeinfo = localtime (&rawtime);
- if (debugging)
- {
- f = fopen("ircd.log","a+");
- if (f)
- {
- char b[MAXBUF];
- va_start (argsPtr, text);
- vsnprintf(textbuffer, MAXBUF, text, argsPtr);
- va_end(argsPtr);
- strcpy(b,asctime(timeinfo));
- b[strlen(b)-1] = ':';
- fprintf(f,"%s %s\n",b,textbuffer);
- fclose(f);
- }
- else
- {
- printf("Can't write log file, bailing!!!");
- Exit(ERROR);
- }
- }
+ if (log_file)
+ {
+ char b[MAXBUF];
+ va_start (argsPtr, text);
+ vsnprintf(textbuffer, MAXBUF, text, argsPtr);
+ va_end(argsPtr);
+ strcpy(b,asctime(timeinfo));
+ b[strlen(b)-1] = ':';
+ fprintf(log_file,"%s %s\n",b,textbuffer);
+ if (nofork)
+ {
+ // nofork enabled? display it on terminal too
+ printf("%s %s\n",b,textbuffer);
+ }
+ }
}
void readfile(file_cache &F, const char* fname)
FILE* file;
char linebuf[MAXBUF];
- debug("readfile: loading %s",fname);
+ log(DEBUG,"readfile: loading %s",fname);
F.clear();
file = fopen(fname,"r");
if (file)
}
else
{
- debug("readfile: failed to load file: %s",fname);
+ log(DEBUG,"readfile: failed to load file: %s",fname);
}
- debug("readfile: loaded %s, %d lines",fname,F.size());
+ log(DEBUG,"readfile: loaded %s, %d lines",fname,F.size());
}
void ReadConfig(void)
{
- char dbg[MAXBUF],pauseval[MAXBUF],Value[MAXBUF];
+ char dbg[MAXBUF],pauseval[MAXBUF],Value[MAXBUF],timeout[MAXBUF];
ConnectClass c;
- 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","pause",0,pauseval);
- 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;
- }
- DieDelay = atoi(pauseval);
+ LoadConf(CONFIG_FILE,&config_f);
+
+ ConfValue("server","name",0,ServerName,&config_f);
+ ConfValue("server","description",0,ServerDesc,&config_f);
+ ConfValue("server","network",0,Network,&config_f);
+ ConfValue("admin","name",0,AdminName,&config_f);
+ ConfValue("admin","email",0,AdminEmail,&config_f);
+ ConfValue("admin","nick",0,AdminNick,&config_f);
+ ConfValue("files","motd",0,motd,&config_f);
+ ConfValue("files","rules",0,rules,&config_f);
+ ConfValue("power","diepass",0,diepass,&config_f);
+ ConfValue("power","pause",0,pauseval,&config_f);
+ ConfValue("power","restartpass",0,restartpass,&config_f);
+ ConfValue("options","prefixquit",0,PrefixQuit,&config_f);
+ ConfValue("die","value",0,DieValue,&config_f);
+ ConfValue("options","loglevel",0,dbg,&config_f);
+ if (!strcmp(dbg,"debug"))
+ LogLevel = DEBUG;
+ if (!strcmp(dbg,"verbose"))
+ LogLevel = VERBOSE;
+ if (!strcmp(dbg,"default"))
+ LogLevel = DEFAULT;
+ if (!strcmp(dbg,"sparse"))
+ LogLevel = SPARSE;
+ if (!strcmp(dbg,"none"))
+ LogLevel = NONE;
readfile(MOTD,motd);
+ log(DEBUG,"Reading message of the day");
readfile(RULES,rules);
- debug("Reading connect classes");
+ log(DEBUG,"Reading connect classes");
Classes.clear();
- for (int i = 0; i < ConfValueEnum("connect"); i++)
+ for (int i = 0; i < ConfValueEnum("connect",&config_f); i++)
{
strcpy(Value,"");
- ConfValue("connect","allow",i,Value);
+ ConfValue("connect","allow",i,Value,&config_f);
+ ConfValue("connect","timeout",i,timeout,&config_f);
if (strcmp(Value,""))
{
strcpy(c.host,Value);
c.type = CC_ALLOW;
strcpy(Value,"");
- ConfValue("connect","password",i,Value);
+ ConfValue("connect","password",i,Value,&config_f);
strcpy(c.pass,Value);
+ c.registration_timeout = 90; // default is 2 minutes
+ if (atoi(timeout)>0)
+ {
+ c.registration_timeout = atoi(timeout);
+ }
Classes.push_back(c);
- debug("Read connect class type ALLOW, host=%s password=%s",c.host,c.pass);
+ log(DEBUG,"Read connect class type ALLOW, host=%s password=%s",c.host,c.pass);
}
else
{
- ConfValue("connect","deny",i,Value);
+ ConfValue("connect","deny",i,Value,&config_f);
strcpy(c.host,Value);
c.type = CC_DENY;
Classes.push_back(c);
- debug("Read connect class type DENY, host=%s",c.host);
+ log(DEBUG,"Read connect class type DENY, host=%s",c.host);
}
}
void Blocking(int s)
{
int flags;
- debug("Blocking: %d",s);
+ log(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);
+ log(DEBUG,"NonBlocking: %d",s);
flags = fcntl(s, F_GETFL, 0);
fcntl(s, F_SETFL, flags | O_NONBLOCK);
}
void Write(int sock,char *text, ...)
{
+ if (!text)
+ {
+ log(DEFAULT,"*** BUG *** Write was given an invalid parameter");
+ return;
+ }
char textbuffer[MAXBUF];
va_list argsPtr;
char tb[MAXBUF];
void WriteServ(int sock, char* text, ...)
{
+ if (!text)
+ {
+ log(DEFAULT,"*** BUG *** WriteServ was given an invalid parameter");
+ return;
+ }
char textbuffer[MAXBUF],tb[MAXBUF];
va_list argsPtr;
va_start (argsPtr, text);
void WriteFrom(int sock, userrec *user,char* text, ...)
{
+ if ((!text) || (!user))
+ {
+ log(DEFAULT,"*** BUG *** WriteFrom was given an invalid parameter");
+ return;
+ }
char textbuffer[MAXBUF],tb[MAXBUF];
va_list argsPtr;
va_start (argsPtr, text);
void WriteTo(userrec *source, userrec *dest,char *data, ...)
{
- char textbuffer[MAXBUF],tb[MAXBUF];
- va_list argsPtr;
- va_start (argsPtr, data);
- if ((!dest) || (!source))
+ if ((!source) || (!dest) || (!data))
{
+ log(DEFAULT,"*** BUG *** WriteTo was given an invalid parameter");
return;
}
+ char textbuffer[MAXBUF],tb[MAXBUF];
+ va_list argsPtr;
+ va_start (argsPtr, data);
vsnprintf(textbuffer, MAXBUF, data, argsPtr);
va_end(argsPtr);
chop(tb);
void WriteChannel(chanrec* Ptr, userrec* user, char* text, ...)
{
+ if ((!Ptr) || (!user) || (!text))
+ {
+ log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
+ return;
+ }
char textbuffer[MAXBUF];
va_list argsPtr;
va_start (argsPtr, text);
}
}
+void WriteChannelWithServ(char* ServerName, chanrec* Ptr, userrec* user, char* text, ...)
+{
+ if ((!Ptr) || (!user) || (!text))
+ {
+ log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
+ return;
+ }
+ 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)
+ {
+ if (has_channel(i->second,Ptr))
+ {
+ WriteServ(i->second->fd,"%s",textbuffer);
+ }
+ }
+ }
+}
+
+
/* write formatted text from a source user to all users on a channel except
* for the sender (for privmsg etc) */
void ChanExceptSender(chanrec* Ptr, userrec* user, char* text, ...)
{
+ if ((!Ptr) || (!user) || (!text))
+ {
+ log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
+ return;
+ }
char textbuffer[MAXBUF];
va_list argsPtr;
va_start (argsPtr, text);
for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
{
- if (has_channel(i->second,Ptr) && (user != i->second))
+ if (i->second)
{
- WriteTo(user,i->second,"%s",textbuffer);
+ if (has_channel(i->second,Ptr) && (user != i->second))
+ {
+ WriteTo(user,i->second,"%s",textbuffer);
+ }
}
}
}
{
int z = 0;
for (int i =0; i != MAXCHANS; i++)
- if (u->chans[i].channel)
+ if (u->chans[i].channel != NULL)
z++;
return z;
if ((!u) || (!u2))
{
+ log(DEFAULT,"*** BUG *** common_channels was given an invalid parameter");
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))
+ if ((u->chans[i].channel != NULL) && (u2->chans[z].channel != NULL))
{
- if ((c_count(u)) && (c_count(u2)))
+ 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;
+ if ((c_count(u)) && (c_count(u2)))
+ {
+ return 1;
+ }
}
}
}
void WriteCommon(userrec *u, char* text, ...)
{
+ if (!u)
+ {
+ log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
+ return;
+ }
+
+ if (u->registered != 7) {
+ log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
+ return;
+ }
+
char textbuffer[MAXBUF];
va_list argsPtr;
va_start (argsPtr, text);
for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
{
- if (common_channels(u,i->second) && (i->second != u))
+ if (i->second)
{
- WriteFrom(i->second->fd,u,"%s",textbuffer);
+ if (common_channels(u,i->second) && (i->second != u))
+ {
+ WriteFrom(i->second->fd,u,"%s",textbuffer);
+ }
}
}
}
void WriteCommonExcept(userrec *u, char* text, ...)
{
+ if (!u)
+ {
+ log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
+ return;
+ }
+
+ if (u->registered != 7) {
+ log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
+ return;
+ }
+
char textbuffer[MAXBUF];
va_list argsPtr;
va_start (argsPtr, text);
for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
{
- if ((common_channels(u,i->second)) && (u != i->second))
+ if (i->second)
{
- WriteFrom(i->second->fd,u,"%s",textbuffer);
+ if ((common_channels(u,i->second)) && (u != i->second))
+ {
+ WriteFrom(i->second->fd,u,"%s",textbuffer);
+ }
}
}
}
void WriteOpers(char* text, ...)
{
+ if (!text)
+ {
+ log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
+ return;
+ }
+
+ 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)
+ {
+ if (strchr(i->second->modes,'o'))
+ {
+ if (strchr(i->second->modes,'s'))
+ {
+ // send server notices to all with +s
+ // (TODO: needs SNOMASKs)
+ WriteServ(i->second->fd,"NOTICE %s :%s",i->second->nick,textbuffer);
+ }
+ }
+ }
+ }
+}
+
+bool hasumode(userrec* user, char mode)
+{
+ if (user)
+ {
+ return (strchr(user->modes,mode)>0);
+ }
+ else return false;
+}
+
+void WriteMode(const char* modes, int flags, const char* text, ...)
+{
+ if ((!text) || (!modes) || (!flags))
+ {
+ log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
+ return;
+ }
+
char textbuffer[MAXBUF];
va_list argsPtr;
va_start (argsPtr, text);
for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
{
- if (strchr(i->second->modes,'o'))
+ if (i->second)
{
- if (strchr(i->second->modes,'s'))
+ bool send_to_user = false;
+
+ if (flags == WM_AND)
+ {
+ send_to_user = true;
+ for (int n = 0; n < strlen(modes); n++)
+ {
+ if (!hasumode(i->second,modes[n]))
+ {
+ send_to_user = false;
+ break;
+ }
+ }
+ }
+ else if (flags == WM_OR)
+ {
+ send_to_user = false;
+ for (int n = 0; n < strlen(modes); n++)
+ {
+ if (hasumode(i->second,modes[n]))
+ {
+ send_to_user = true;
+ break;
+ }
+ }
+ }
+
+ if (send_to_user)
{
- // send server notices to all with +s
- // (TODO: needs SNOMASKs)
WriteServ(i->second->fd,"NOTICE %s :%s",i->second->nick,textbuffer);
}
}
void WriteWallOps(userrec *source, char* text, ...)
{
+ if ((!text) || (!source))
+ {
+ log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
+ return;
+ }
+
int i = 0;
char textbuffer[MAXBUF];
va_list argsPtr;
va_end(argsPtr);
for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
- {
- if (strchr(i->second->modes,'w'))
- {
- WriteTo(source,i->second,"WALLOPS %s",textbuffer);
+ {
+ if (i->second)
+ {
+ if (strchr(i->second->modes,'w'))
+ {
+ WriteTo(source,i->second,"WALLOPS %s",textbuffer);
+ }
}
}
}
}
}
-/* verify that a user's nickname is valid */
+/* verify that a user's ident and nickname is valid */
-int isnick(const char* n)
+int isident(const char* n)
+{
+ int i = 0;
+ char v[MAXBUF];
+ if (!n)
+
+ {
+ return 0;
+ }
+ if (!strcmp(n,""))
+ {
+ return 0;
+ }
+ for (i = 0; i != strlen(n); i++)
+ {
+ if ((n[i] < 33) || (n[i] > 125))
+ {
+ return 0;
+ }
+ /* can't occur ANYWHERE in an Ident! */
+ if (strchr("<>,./?:;@'~#=+()*&%$£ \"!",n[i]))
+ {
+ return 0;
+ }
+ }
+ return 1;
+}
+
+
+int isnick(const char* n)
{
int i = 0;
char v[MAXBUF];
{
for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
{
- if (i->second->fd == fd)
+ if (i->second)
{
- i->second->bytes_out+=data_out;
- i->second->cmds_out++;
+ if (i->second->fd == fd)
+ {
+ i->second->bytes_out+=data_out;
+ i->second->cmds_out++;
+ }
}
}
}
chanrec* FindChan(const char* chan)
{
+ if (!chan)
+ {
+ log(DEFAULT,"*** BUG *** Findchan was given an invalid parameter");
+ return NULL;
+ }
+
chan_hash::iterator iter = chanlist.find(chan);
if (iter == chanlist.end())
/* kill the record */
if (i != chanlist.end())
{
- debug("del_channel: destroyed: %s",i->second->name);
+ log(DEBUG,"del_channel: destroyed: %s",i->second->name);
delete i->second;
chanlist.erase(i);
go_again = 1;
break;
}
}
+ else
+ {
+ log(DEBUG,"skipped purge for %s",i->second->name);
+ }
}
}
}
- debug("completed channel purge, killed %d",purge);
+ log(DEBUG,"completed channel purge, killed %d",purge);
}
/* returns the status character for a given user on a channel, e.g. @ for op,
char* cmode(userrec *user, chanrec *chan)
{
+ if ((!user) || (!chan))
+ {
+ log(DEFAULT,"*** BUG *** cmode was given an invalid parameter");
+ return "";
+ }
+
int i;
for (i = 0; i != MAXCHANS; i++)
{
}
}
-char scratch[MAXMODES];
+char scratch[MAXBUF];
+char sparam[MAXBUF];
char* chanmodes(chanrec *chan)
{
+ if (!chan)
+ {
+ log(DEFAULT,"*** BUG *** chanmodes was given an invalid parameter");
+ strcpy(scratch,"");
+ return scratch;
+ }
+
strcpy(scratch,"");
+ strcpy(sparam,"");
if (chan->noexternal)
{
- strcat(scratch,"n");
+ strncat(scratch,"n",MAXMODES);
}
if (chan->topiclock)
{
- strcat(scratch,"t");
+ strncat(scratch,"t",MAXMODES);
}
if (strcmp(chan->key,""))
{
- strcat(scratch,"k");
+ strncat(scratch,"k",MAXMODES);
}
if (chan->limit)
{
- strcat(scratch,"l");
+ strncat(scratch,"l",MAXMODES);
}
if (chan->inviteonly)
{
- strcat(scratch,"i");
+ strncat(scratch,"i",MAXMODES);
}
if (chan->moderated)
{
- strcat(scratch,"m");
+ strncat(scratch,"m",MAXMODES);
}
if (chan->secret)
{
- strcat(scratch,"s");
+ strncat(scratch,"s",MAXMODES);
}
if (chan->c_private)
{
- strcat(scratch,"p");
+ strncat(scratch,"p",MAXMODES);
}
if (strcmp(chan->key,""))
{
- strcat(scratch," ");
- strcat(scratch,chan->key);
+ strncat(sparam,chan->key,MAXBUF);
}
if (chan->limit)
{
char foo[24];
sprintf(foo," %d",chan->limit);
- strcat(scratch,foo);
+ strncat(sparam,foo,MAXBUF);
}
- debug("chanmodes: %s %s",chan->name,scratch);
+ if (strlen(chan->custom_modes))
+ {
+ strncat(scratch,chan->custom_modes,MAXMODES);
+ for (int z = 0; z < strlen(chan->custom_modes); z++)
+ {
+ std::string extparam = chan->GetModeParameter(chan->custom_modes[z]);
+ if (extparam != "")
+ {
+ strncat(sparam," ",MAXBUF);
+ strncat(sparam,extparam.c_str(),MAXBUF);
+ }
+ }
+ }
+ log(DEBUG,"chanmodes: %s %s%s",chan->name,scratch,sparam);
+ strncat(scratch,sparam,MAXMODES);
return scratch;
}
int cstatus(userrec *user, chanrec *chan)
{
+ if ((!chan) || (!user))
+ {
+ log(DEFAULT,"*** BUG *** cstatus was given an invalid parameter");
+ return 0;
+ }
+
int i;
for (i = 0; i != MAXCHANS; i++)
{
void userlist(userrec *user,chanrec *c)
{
+ if ((!c) || (!user))
+ {
+ log(DEFAULT,"*** BUG *** userlist was given an invalid parameter");
+ return;
+ }
+
sprintf(list,"353 %s = %s :", user->nick, c->name);
for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
{
{
int i = 0;
int count = 0;
+
+ if (!c)
+ {
+ log(DEFAULT,"*** BUG *** usercount_i was given an invalid parameter");
+ return 0;
+ }
strcpy(list,"");
for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
{
- if (has_channel(i->second,c))
+ if (i->second)
{
- if (isnick(i->second->nick))
+ if (has_channel(i->second,c))
{
- if ((!has_channel(i->second,c)) && (strchr(i->second->modes,'i')))
+ if (isnick(i->second->nick))
{
- /* user is +i, and source not on the channel, does not show
- * nick in NAMES list */
- continue;
+ if ((!has_channel(i->second,c)) && (strchr(i->second->modes,'i')))
+ {
+ /* user is +i, and source not on the channel, does not show
+ * nick in NAMES list */
+ continue;
+ }
+ count++;
}
- count++;
}
}
}
- debug("usercount_i: %s %d",c->name,count);
+ log(DEBUG,"usercount_i: %s %d",c->name,count);
return count;
}
{
int i = 0;
int count = 0;
+
+ if (!c)
+ {
+ log(DEFAULT,"*** BUG *** usercount was given an invalid parameter");
+ return 0;
+ }
strcpy(list,"");
for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
{
- if (has_channel(i->second,c))
+ if (i->second)
{
- if (isnick(i->second->nick))
+ if (has_channel(i->second,c))
{
- count++;
+ if ((isnick(i->second->nick)) && (i->second->registered == 7))
+ {
+ count++;
+ }
}
}
}
- debug("usercount: %s %d",c->name,count);
+ log(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 */
-chanrec* add_channel(userrec *user, char* cname, char* key)
+chanrec* add_channel(userrec *user, const char* cn, const char* key)
{
+ if ((!user) || (!cn))
+ {
+ log(DEFAULT,"*** BUG *** add_channel was given an invalid parameter");
+ return 0;
+ }
+
int i = 0;
chanrec* Ptr;
int created = 0;
+ char cname[MAXBUF];
+
+ strncpy(cname,cn,MAXBUF);
+
+ // we MUST declare this wherever we use FOREACH_RESULT
+ int MOD_RESULT = 0;
if ((!cname) || (!user))
{
cname[CHANMAX-1] = '\0';
}
- debug("add_channel: %s %s",user->nick,cname);
+ log(DEBUG,"add_channel: %s %s",user->nick,cname);
- if ((has_channel(user,FindChan(cname))) && (FindChan(cname)))
+ if ((FindChan(cname)) && (has_channel(user,FindChan(cname))))
{
return NULL; // already on the channel!
}
-
+
+
if (!FindChan(cname))
{
+ FOREACH_RESULT(OnUserPreJoin(user,NULL,cname));
+ if (MOD_RESULT) {
+ return NULL;
+ }
+
/* create a new one */
- debug("add_channel: creating: %s",cname);
+ log(DEBUG,"add_channel: creating: %s",cname);
{
chanlist[cname] = new chanrec();
strncpy(chanlist[cname]->setby, user->nick,NICKMAX);
chanlist[cname]->topicset = 0;
Ptr = chanlist[cname];
- debug("add_channel: created: %s",cname);
+ log(DEBUG,"add_channel: created: %s",cname);
/* set created to 2 to indicate user
* is the first in the channel
* and should be given ops */
Ptr = FindChan(cname);
if (Ptr)
{
- debug("add_channel: joining to: %s",Ptr->name);
+ FOREACH_RESULT(OnUserPreJoin(user,Ptr,cname));
+ if (MOD_RESULT) {
+ return NULL;
+ }
+
+ log(DEBUG,"add_channel: joining to: %s",Ptr->name);
if (strcmp(Ptr->key,""))
{
- debug("add_channel: %s has key %s",Ptr->name,Ptr->key);
+ log(DEBUG,"add_channel: %s has key %s",Ptr->name,Ptr->key);
if (!key)
{
- debug("add_channel: no key given in JOIN");
+ log(DEBUG,"add_channel: no key given in JOIN");
WriteServ(user->fd,"475 %s %s :Cannot join channel (Requires key)",user->nick, Ptr->name);
return NULL;
}
else
{
- debug("key at %p is %s",key,key);
+ log(DEBUG,"key at %p is %s",key,key);
if (strcasecmp(key,Ptr->key))
{
- debug("add_channel: bad key given in JOIN");
+ log(DEBUG,"add_channel: bad key given in JOIN");
WriteServ(user->fd,"475 %s %s :Cannot join channel (Incorrect key)",user->nick, Ptr->name);
return NULL;
}
}
}
+ log(DEBUG,"add_channel: no key");
if (Ptr->inviteonly)
{
+ log(DEBUG,"add_channel: channel is +i");
if (user->IsInvited(Ptr->name))
{
/* user was invited to channel */
return NULL;
}
}
+ log(DEBUG,"add_channel: channel is not +i");
if (Ptr->limit)
{
}
}
+ log(DEBUG,"add_channel: about to walk banlist");
+
/* check user against the channel banlist */
for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
{
}
}
+ log(DEBUG,"add_channel: bans checked");
+
user->RemoveInvite(Ptr->name);
+
+ log(DEBUG,"add_channel: invites removed");
}
created = 1;
}
+ log(DEBUG,"Passed channel checks");
for (i =0; i != MAXCHANS; i++)
{
if (user->chans[i].channel == NULL)
{
+ log(DEBUG,"Adding into their channel list");
+
if (created == 2)
{
/* first user in is given ops */
}
user->chans[i].channel = Ptr;
WriteChannel(Ptr,user,"JOIN :%s",Ptr->name);
+
+ log(DEBUG,"Sent JOIN to client");
+
if (Ptr->topicset)
{
WriteServ(user->fd,"332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
return Ptr;
}
}
- debug("add_channel: user channel max exceeded: %s %s",user->nick,cname);
+ log(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 */
-chanrec* del_channel(userrec *user, char* cname, char* reason)
+chanrec* del_channel(userrec *user, const char* cname, const char* reason)
{
+ if ((!user) || (!cname))
+ {
+ log(DEFAULT,"*** BUG *** del_channel was given an invalid parameter");
+ return NULL;
+ }
+
int i = 0;
chanrec* Ptr;
int created = 0;
}
FOREACH_MOD OnUserPart(user,Ptr);
- debug("del_channel: removing: %s %s",user->nick,Ptr->name);
+ log(DEBUG,"del_channel: removing: %s %s",user->nick,Ptr->name);
for (i =0; i != MAXCHANS; i++)
{
}
user->chans[i].uc_modes = 0;
user->chans[i].channel = NULL;
- debug("del_channel: unlinked: %s %s",user->nick,Ptr->name);
+ log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
break;
}
}
{
chan_hash::iterator iter = chanlist.find(Ptr->name);
- debug("del_channel: destroying channel: %s",Ptr->name);
+ log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
/* kill the record */
if (iter != chanlist.end())
{
- debug("del_channel: destroyed: %s",Ptr->name);
+ log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
delete iter->second;
chanlist.erase(iter);
}
void kick_channel(userrec *src,userrec *user, chanrec *Ptr, char* reason)
{
+ if ((!src) || (!user) || (!Ptr) || (!reason))
+ {
+ log(DEFAULT,"*** BUG *** kick_channel was given an invalid parameter");
+ return;
+ }
+
int i = 0;
int created = 0;
return;
}
- debug("kick_channel: removing: %s %s %s",user->nick,Ptr->name,src->nick);
+ log(DEBUG,"kick_channel: removing: %s %s %s",user->nick,Ptr->name,src->nick);
if (!has_channel(user,Ptr))
{
}
else
{
- WriteServ(src->fd,"482 %s %s :You must be at least a half-operator",src->nick, Ptr->name);
+ WriteServ(src->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",src->nick, Ptr->name);
}
return;
WriteChannel(Ptr,src,"KICK %s %s :%s",Ptr->name, user->nick, reason);
user->chans[i].uc_modes = 0;
user->chans[i].channel = NULL;
- debug("del_channel: unlinked: %s %s",user->nick,Ptr->name);
+ log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
break;
}
}
{
chan_hash::iterator iter = chanlist.find(Ptr->name);
- debug("del_channel: destroying channel: %s",Ptr->name);
+ log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
/* kill the record */
if (iter != chanlist.end())
{
- debug("del_channel: destroyed: %s",Ptr->name);
+ log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
delete iter->second;
chanlist.erase(iter);
}
{
int i = 0;
- if (!u)
+ if ((!u) || (!c))
{
+ log(DEFAULT,"*** BUG *** has_channel was given an invalid parameter");
return 0;
}
for (i =0; i != MAXCHANS; i++)
if ((!user) || (!dest) || (!chan))
{
+ log(DEFAULT,"*** BUG *** give_ops was given an invalid parameter");
return 0;
}
if (status != STATUS_OP)
return 0;
}
d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_OP;
- debug("gave ops: %s %s",d->chans[i].channel->name,d->nick);
+ log(DEBUG,"gave ops: %s %s",d->chans[i].channel->name,d->nick);
}
}
}
if ((!user) || (!dest) || (!chan))
{
+ log(DEFAULT,"*** BUG *** give_hops was given an invalid parameter");
return 0;
}
if (status != STATUS_OP)
return 0;
}
d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_HOP;
- debug("gave h-ops: %s %s",d->chans[i].channel->name,d->nick);
+ log(DEBUG,"gave h-ops: %s %s",d->chans[i].channel->name,d->nick);
}
}
}
if ((!user) || (!dest) || (!chan))
{
+ log(DEFAULT,"*** BUG *** give_voice was given an invalid parameter");
return 0;
}
if (status < STATUS_HOP)
{
- WriteServ(user->fd,"482 %s %s :You must be at least a half-operator",user->nick, chan->name);
+ WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
return 0;
}
else
return 0;
}
d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_VOICE;
- debug("gave voice: %s %s",d->chans[i].channel->name,d->nick);
+ log(DEBUG,"gave voice: %s %s",d->chans[i].channel->name,d->nick);
}
}
}
if ((!user) || (!dest) || (!chan))
{
+ log(DEFAULT,"*** BUG *** take_ops was given an invalid parameter");
return 0;
}
if (status != STATUS_OP)
return 0;
}
d->chans[i].uc_modes ^= UCMODE_OP;
- debug("took ops: %s %s",d->chans[i].channel->name,d->nick);
+ log(DEBUG,"took ops: %s %s",d->chans[i].channel->name,d->nick);
}
}
}
if ((!user) || (!dest) || (!chan))
{
+ log(DEFAULT,"*** BUG *** take_hops was given an invalid parameter");
return 0;
}
if (status != STATUS_OP)
return 0;
}
d->chans[i].uc_modes ^= UCMODE_HOP;
- debug("took h-ops: %s %s",d->chans[i].channel->name,d->nick);
+ log(DEBUG,"took h-ops: %s %s",d->chans[i].channel->name,d->nick);
}
}
}
if ((!user) || (!dest) || (!chan))
{
+ log(DEFAULT,"*** BUG *** take_voice was given an invalid parameter");
return 0;
}
if (status < STATUS_HOP)
{
- WriteServ(user->fd,"482 %s %s :You must be at least a half-operator",user->nick, chan->name);
+ WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
return 0;
}
else
return 0;
}
d->chans[i].uc_modes ^= UCMODE_VOICE;
- debug("took voice: %s %s",d->chans[i].channel->name,d->nick);
+ log(DEBUG,"took voice: %s %s",d->chans[i].channel->name,d->nick);
}
}
}
void TidyBan(char *ban)
{
+ if (!ban) {
+ log(DEFAULT,"*** BUG *** TidyBan was given an invalid parameter");
+ return;
+ }
+
char temp[MAXBUF],NICK[MAXBUF],IDENT[MAXBUF],HOST[MAXBUF];
strcpy(temp,ban);
int add_ban(userrec *user,char *dest,chanrec *chan,int status)
{
+ if ((!user) || (!dest) || (!chan)) {
+ log(DEFAULT,"*** BUG *** add_ban was given an invalid parameter");
+ return 0;
+ }
+
BanItem b;
if ((!user) || (!dest) || (!chan))
return 0;
c++;
if (c>1)
return 0;
- debug("add_ban: %s %s",chan->name,user->nick);
+ log(DEBUG,"add_ban: %s %s",chan->name,user->nick);
TidyBan(dest);
for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
int take_ban(userrec *user,char *dest,chanrec *chan,int status)
{
- if ((!user) || (!dest) || (!chan))
- {
+ if ((!user) || (!dest) || (!chan)) {
+ log(DEFAULT,"*** BUG *** take_ban was given an invalid parameter");
return 0;
}
- debug("del_ban: %s %s",chan->name,user->nick);
+ log(DEBUG,"del_ban: %s %s",chan->name,user->nick);
for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
{
if (!strcasecmp(i->data,dest))
return 0;
}
-void process_modes(char **parameters,userrec* user,chanrec *chan,int status, int pcnt)
+void process_modes(char **parameters,userrec* user,chanrec *chan,int status, int pcnt, bool servermode)
{
+ if ((!parameters) || (!user)) {
+ log(DEFAULT,"*** BUG *** process_modes was given an invalid parameter");
+ return;
+ }
+
+
char modelist[MAXBUF];
char outlist[MAXBUF];
char outstr[MAXBUF];
return;
}
- debug("process_modes: start");
+ log(DEBUG,"process_modes: start");
strcpy(modelist,parameters[1]); /* mode list, e.g. +oo-o */
/* parameters[2] onwards are parameters for
strcpy(outlist,"+");
mdir = 1;
- debug("process_modes: modelist: %s",modelist);
+ log(DEBUG,"process_modes: modelist: %s",modelist);
for (ptr = 0; ptr < strlen(modelist); ptr++)
{
r = 0;
{
- debug("process_modes: modechar: %c",modelist[ptr]);
+ log(DEBUG,"process_modes: modechar: %c",modelist[ptr]);
+ char modechar = modelist[ptr];
switch (modelist[ptr])
{
case '-':
}
break;
+
case 'k':
if ((param >= pcnt))
break;
if (!strcmp(chan->key,""))
{
strcat(outlist,"k");
- strcpy(outpars[pc++],parameters[param++]);
- strcpy(chan->key,parameters[param-1]);
+ char key[MAXBUF];
+ strcpy(key,parameters[param++]);
+ if (strlen(key)>32) {
+ key[31] = '\0';
+ }
+ strcpy(outpars[pc++],key);
+ strcpy(chan->key,key);
k_set = true;
}
}
else
{
+ /* checks on -k are case sensitive and only accurate to the
+ first 32 characters */
+ char key[MAXBUF];
+ strcpy(key,parameters[param++]);
+ if (strlen(key)>32) {
+ key[31] = '\0';
+ }
/* only allow -k if correct key given */
- if (strcmp(chan->key,""))
+ if (!strcmp(chan->key,key))
{
strcat(outlist,"k");
strcpy(chan->key,"");
+ strcpy(outpars[pc++],key);
}
}
break;
chan->c_private = mdir;
break;
+ default:
+ log(DEBUG,"Preprocessing custom mode %c",modechar);
+ string_list p;
+ p.clear();
+ if (((!strchr(chan->custom_modes,modechar)) && (!mdir)) || ((strchr(chan->custom_modes,modechar)) && (mdir)))
+ {
+ log(DEBUG,"Mode %c isnt set on %s but trying to remove!",modechar,chan->name);
+ break;
+ }
+ if (ModeDefined(modechar,MT_CHANNEL))
+ {
+ log(DEBUG,"A module has claimed this mode");
+ if (param<pcnt)
+ {
+ if ((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir))
+ {
+ p.push_back(parameters[param]);
+ }
+ if ((ModeDefinedOff(modechar,MT_CHANNEL)>0) && (!mdir))
+ {
+ p.push_back(parameters[param]);
+ }
+ }
+ bool handled = false;
+ if (param>=pcnt)
+ {
+ log(DEBUG,"Not enough parameters for module-mode %c",modechar);
+ // we're supposed to have a parameter, but none was given... so dont handle the mode.
+ if (((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir)) || ((ModeDefinedOff(modechar,MT_CHANNEL)>0) && (!mdir)))
+ {
+ handled = true;
+ param++;
+ }
+ }
+ for (int i = 0; i <= MODCOUNT; i++)
+ {
+ if (!handled)
+ {
+ if (modules[i]->OnExtendedMode(user,chan,modechar,MT_CHANNEL,mdir,p))
+ {
+ log(DEBUG,"OnExtendedMode returned nonzero for a module");
+ char app[] = {modechar, 0};
+ if (ptr>0)
+ {
+ if ((modelist[ptr-1] == '+') || (modelist[ptr-1] == '-'))
+ {
+ strcat(outlist, app);
+ }
+ else if (!strchr(outlist,modechar))
+ {
+ strcat(outlist, app);
+ }
+ }
+ chan->SetCustomMode(modechar,mdir);
+ // include parameters in output if mode has them
+ if ((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir))
+ {
+ chan->SetCustomModeParam(modelist[ptr],parameters[param],mdir);
+ strcpy(outpars[pc++],parameters[param++]);
+ }
+ // break, because only one module can handle the mode.
+ handled = true;
+ }
+ }
+ }
+ }
+ break;
+
}
}
}
strcat(outstr," ");
strcat(outstr,outpars[ptr]);
}
- WriteChannel(chan,user,"MODE %s %s",chan->name,outstr);
+ if (servermode)
+ {
+ WriteChannelWithServ(ServerName,chan,user,"MODE %s %s",chan->name,outstr);
+ }
+ else
+ {
+ WriteChannel(chan,user,"MODE %s %s",chan->name,outstr);
+ }
+ }
+}
+
+// based on sourcemodes, return true or false to determine if umode is a valid mode a user may set on themselves or others.
+
+bool allowed_umode(char umode, char* sourcemodes,bool adding)
+{
+ log(DEBUG,"Allowed_umode: %c %s",umode,sourcemodes);
+ // RFC1459 specified modes
+ if ((umode == 'w') || (umode == 's') || (umode == 'i'))
+ {
+ log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
+ return true;
+ }
+
+ // user may not +o themselves or others, but an oper may de-oper other opers or themselves
+ if ((strchr(sourcemodes,'o')) && (!adding))
+ {
+ log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
+ return true;
+ }
+ else if (umode == 'o')
+ {
+ log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
+ return false;
+ }
+
+ // process any module-defined modes that need oper
+ if ((ModeDefinedOper(umode,MT_CLIENT)) && (strchr(sourcemodes,'o')))
+ {
+ log(DEBUG,"umode %c allowed by module handler (oper only mode)",umode);
+ return true;
+ }
+ else
+ if (ModeDefined(umode,MT_CLIENT))
+ {
+ // process any module-defined modes that don't need oper
+ log(DEBUG,"umode %c allowed by module handler (non-oper mode)",umode);
+ if ((ModeDefinedOper(umode,MT_CLIENT)) && (!strchr(sourcemodes,'o')))
+ {
+ // no, this mode needs oper, and this user 'aint got what it takes!
+ return false;
+ }
+ return true;
+ }
+
+ // anything else - return false.
+ log(DEBUG,"umode %c not known by any ruleset",umode);
+ return false;
+}
+
+bool process_module_umode(char umode, userrec* source, void* dest, bool adding)
+{
+ string_list p;
+ p.clear();
+ if (ModeDefined(umode,MT_CLIENT))
+ {
+ for (int i = 0; i <= MODCOUNT; i++)
+ {
+ if (modules[i]->OnExtendedMode(source,(void*)dest,umode,MT_CLIENT,adding,p))
+ {
+ log(DEBUG,"Module claims umode %c",umode);
+ return true;
+ }
+ }
+ log(DEBUG,"No module claims umode %c",umode);
+ return false;
+ }
+ else
+ {
+ log(DEBUG,"*** BUG *** Non-module umode passed to process_module_umode!");
+ return false;
}
}
WriteServ(user->fd,"221 %s :+%s",user->nick,user->modes);
return;
}
+
if ((dest) && (pcnt > 1))
{
+ char dmodes[MAXBUF];
+ strncpy(dmodes,dest->modes,MAXBUF);
+ log(DEBUG,"pulled up dest user modes: %s",dmodes);
+
can_change = 0;
if (user != dest)
{
}
else
{
- if ((parameters[1][i] == 'i') || (parameters[1][i] == 'w') || (parameters[1][i] == 's'))
+ if ((parameters[1][i] == 'i') || (parameters[1][i] == 'w') || (parameters[1][i] == 's') || (allowed_umode(parameters[1][i],user->modes,direction)))
{
can_change = 1;
}
{
if (direction == 1)
{
- if (!strchr(dest->modes,parameters[1][i]))
+ if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true)))
{
- dest->modes[strlen(dest->modes)+1]='\0';
- dest->modes[strlen(dest->modes)] = parameters[1][i];
- outpars[strlen(outpars)+1]='\0';
- outpars[strlen(outpars)] = parameters[1][i];
+ char umode = parameters[1][i];
+ if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
+ {
+ dmodes[strlen(dmodes)+1]='\0';
+ dmodes[strlen(dmodes)] = parameters[1][i];
+ outpars[strlen(outpars)+1]='\0';
+ outpars[strlen(outpars)] = parameters[1][i];
+ }
}
}
else
{
- int q = 0;
- char temp[MAXBUF];
- char moo[MAXBUF];
-
- outpars[strlen(outpars)+1]='\0';
- outpars[strlen(outpars)] = parameters[1][i];
-
- strcpy(temp,"");
- for (q = 0; q < strlen(user->modes); q++)
+ if ((allowed_umode(parameters[1][i],user->modes,false)) && (strchr(dmodes,parameters[1][i])))
{
- if (user->modes[q] != parameters[1][i])
+ char umode = parameters[1][i];
+ if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
{
- moo[0] = user->modes[q];
- moo[1] = '\0';
- strcat(temp,moo);
+ int q = 0;
+ char temp[MAXBUF];
+ char moo[MAXBUF];
+
+ outpars[strlen(outpars)+1]='\0';
+ outpars[strlen(outpars)] = parameters[1][i];
+
+ strcpy(temp,"");
+ for (q = 0; q < strlen(dmodes); q++)
+ {
+ if (dmodes[q] != parameters[1][i])
+ {
+ moo[0] = dmodes[q];
+ moo[1] = '\0';
+ strcat(temp,moo);
+ }
+ }
+ strcpy(dmodes,temp);
}
}
- strcpy(user->modes,temp);
}
}
}
return;
WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
+
+ if (strlen(dmodes)>MAXMODES)
+ {
+ dmodes[MAXMODES-1] = '\0';
+ }
+ log(DEBUG,"Stripped mode line");
+ log(DEBUG,"Line dest is now %s",dmodes);
+ strncpy(dest->modes,dmodes,MAXMODES);
+
}
+
return;
}
if ((cstatus(user,Ptr) < STATUS_HOP) && (Ptr))
{
- WriteServ(user->fd,"482 %s %s :You must be at least a half-operator",user->nick, Ptr->name);
+ WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, Ptr->name);
return;
}
- process_modes(parameters,user,Ptr,cstatus(user,Ptr),pcnt);
+ process_modes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false);
}
else
{
}
}
-/* This function pokes and hacks at a parameter list like the following:
- *
- * PART #winbot, #darkgalaxy :m00!
- *
- * to turn it into a series of individual calls like this:
- *
- * PART #winbot :m00!
- * PART #darkgalaxy :m00!
- *
- * The seperate calls are sent to a callback function provided by the caller
- * (the caller will usually call itself recursively). The callback function
- * must be a command handler. Calling this function on a line with no list causes
- * no action to be taken. You must provide a starting and ending parameter number
- * where the range of the list can be found, useful if you have a terminating
- * parameter as above which is actually not part of the list, or parameters
- * before the actual list as well. This code is used by many functions which
- * can function as "one to list" (see the RFC) */
-int loop_call(handlerfunc fn, char **parameters, int pcnt, userrec *u, int start, int end, int joins)
-{
- char plist[MAXBUF];
- char *param;
- char *pars[32];
- char blog[32][MAXBUF];
- char blog2[32][MAXBUF];
- int i = 0, j = 0, q = 0, total = 0, t = 0, t2 = 0, total2 = 0;
- char keystr[MAXBUF];
- char moo[MAXBUF];
- for (i = 0; i <32; i++)
- strcpy(blog[i],"");
- for (i = 0; i <32; i++)
- strcpy(blog2[i],"");
+void server_mode(char **parameters, int pcnt, userrec *user)
+{
+ chanrec* Ptr;
+ userrec* dest;
+ int can_change,i;
+ int direction = 1;
+ char outpars[MAXBUF];
- strcpy(moo,"");
- for (i = 0; i <10; i++)
- {
- if (!parameters[i])
- {
- parameters[i] = moo;
- }
- }
- if (joins)
+ dest = Find(parameters[0]);
+
+ // fix: ChroNiCk found this - we cant use this as debug if its null!
+ if (dest)
{
- if (pcnt > 1) /* we have a key to copy */
- {
- strcpy(keystr,parameters[1]);
- }
+ log(DEBUG,"server_mode on %s",dest->nick);
}
- if (!parameters[start])
- {
- return 0;
- }
- if (!strchr(parameters[start],','))
- {
- return 0;
- }
- strcpy(plist,"");
- for (i = start; i <= end; i++)
+ if ((dest) && (pcnt > 1))
{
- if (parameters[i])
- {
- strcat(plist,parameters[i]);
- }
- }
-
- j = 0;
- param = plist;
+ log(DEBUG,"params > 1");
- t = strlen(plist);
- for (i = 0; i < t; i++)
- {
- if (plist[i] == ',')
- {
- plist[i] = '\0';
- strcpy(blog[j++],param);
- param = plist+i+1;
- }
- }
+ char dmodes[MAXBUF];
+ strncpy(dmodes,dest->modes,MAXBUF);
+
+ strcpy(outpars,"+");
+ direction = 1;
+
+ if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
+ return;
+
+ for (i = 0; i < strlen(parameters[1]); i++)
+ {
+ if (parameters[1][i] == '+')
+ {
+ if (direction != 1)
+ {
+ if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
+ {
+ outpars[strlen(outpars)-1] = '+';
+ }
+ else
+ {
+ strcat(outpars,"+");
+ }
+ }
+ direction = 1;
+ }
+ else
+ if (parameters[1][i] == '-')
+ {
+ if (direction != 0)
+ {
+ if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
+ {
+ outpars[strlen(outpars)-1] = '-';
+ }
+ else
+ {
+ strcat(outpars,"-");
+ }
+ }
+ direction = 0;
+ }
+ else
+ {
+ log(DEBUG,"begin mode processing entry");
+ can_change = 1;
+ if (can_change)
+ {
+ if (direction == 1)
+ {
+ log(DEBUG,"umode %c being added",parameters[1][i]);
+ if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true)))
+ {
+ char umode = parameters[1][i];
+ log(DEBUG,"umode %c is an allowed umode",umode);
+ if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
+ {
+ dmodes[strlen(dmodes)+1]='\0';
+ dmodes[strlen(dmodes)] = parameters[1][i];
+ outpars[strlen(outpars)+1]='\0';
+ outpars[strlen(outpars)] = parameters[1][i];
+ }
+ }
+ }
+ else
+ {
+ // can only remove a mode they already have
+ log(DEBUG,"umode %c being removed",parameters[1][i]);
+ if ((allowed_umode(parameters[1][i],user->modes,false)) && (strchr(dmodes,parameters[1][i])))
+ {
+ char umode = parameters[1][i];
+ log(DEBUG,"umode %c is an allowed umode",umode);
+ if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
+ {
+ int q = 0;
+ char temp[MAXBUF];
+ char moo[MAXBUF];
+
+ outpars[strlen(outpars)+1]='\0';
+ outpars[strlen(outpars)] = parameters[1][i];
+
+ strcpy(temp,"");
+ for (q = 0; q < strlen(dmodes); q++)
+ {
+ if (dmodes[q] != parameters[1][i])
+ {
+ moo[0] = dmodes[q];
+ moo[1] = '\0';
+ strcat(temp,moo);
+ }
+ }
+ strcpy(dmodes,temp);
+ }
+ }
+ }
+ }
+ }
+ }
+ if (strlen(outpars))
+ {
+ char b[MAXBUF];
+ strcpy(b,"");
+ int z = 0;
+ int i = 0;
+ while (i < strlen (outpars))
+ {
+ b[z++] = outpars[i++];
+ b[z] = '\0';
+ if (i<strlen(outpars)-1)
+ {
+ if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
+ {
+ // someones playing silly buggers and trying
+ // to put a +- or -+ into the line...
+ i++;
+ }
+ }
+ if (i == strlen(outpars)-1)
+ {
+ if ((outpars[i] == '-') || (outpars[i] == '+'))
+ {
+ i++;
+ }
+ }
+ }
+
+ z = strlen(b)-1;
+ if ((b[z] == '-') || (b[z] == '+'))
+ b[z] == '\0';
+
+ if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
+ return;
+
+ WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
+
+ if (strlen(dmodes)>MAXMODES)
+ {
+ dmodes[MAXMODES-1] = '\0';
+ }
+ log(DEBUG,"Stripped mode line");
+ log(DEBUG,"Line dest is now %s",dmodes);
+ strncpy(dest->modes,dmodes,MAXMODES);
+
+ }
+
+ return;
+ }
+
+ Ptr = FindChan(parameters[0]);
+ if (Ptr)
+ {
+ process_modes(parameters,user,Ptr,STATUS_OP,pcnt,true);
+ }
+ else
+ {
+ WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
+ }
+}
+
+
+/* This function pokes and hacks at a parameter list like the following:
+ *
+ * PART #winbot, #darkgalaxy :m00!
+ *
+ * to turn it into a series of individual calls like this:
+ *
+ * PART #winbot :m00!
+ * PART #darkgalaxy :m00!
+ *
+ * The seperate calls are sent to a callback function provided by the caller
+ * (the caller will usually call itself recursively). The callback function
+ * must be a command handler. Calling this function on a line with no list causes
+ * no action to be taken. You must provide a starting and ending parameter number
+ * where the range of the list can be found, useful if you have a terminating
+ * parameter as above which is actually not part of the list, or parameters
+ * before the actual list as well. This code is used by many functions which
+ * can function as "one to list" (see the RFC) */
+
+int loop_call(handlerfunc fn, char **parameters, int pcnt, userrec *u, int start, int end, int joins)
+{
+ char plist[MAXBUF];
+ char *param;
+ char *pars[32];
+ char blog[32][MAXBUF];
+ char blog2[32][MAXBUF];
+ int i = 0, j = 0, q = 0, total = 0, t = 0, t2 = 0, total2 = 0;
+ char keystr[MAXBUF];
+ char moo[MAXBUF];
+
+ for (i = 0; i <32; i++)
+ strcpy(blog[i],"");
+
+ for (i = 0; i <32; i++)
+ strcpy(blog2[i],"");
+
+ strcpy(moo,"");
+ for (i = 0; i <10; i++)
+ {
+ if (!parameters[i])
+ {
+ parameters[i] = moo;
+ }
+ }
+ if (joins)
+ {
+ if (pcnt > 1) /* we have a key to copy */
+ {
+ strcpy(keystr,parameters[1]);
+ }
+ }
+
+ if (!parameters[start])
+ {
+ return 0;
+ }
+ if (!strchr(parameters[start],','))
+ {
+ return 0;
+ }
+ strcpy(plist,"");
+ for (i = start; i <= end; i++)
+ {
+ if (parameters[i])
+ {
+ strcat(plist,parameters[i]);
+ }
+ }
+
+ j = 0;
+ param = plist;
+
+ t = strlen(plist);
+ for (i = 0; i < t; i++)
+ {
+ if (plist[i] == ',')
+ {
+ plist[i] = '\0';
+ strcpy(blog[j++],param);
+ param = plist+i+1;
+ }
+ }
strcpy(blog[j++],param);
total = j;
return 1;
}
+
void handle_join(char **parameters, int pcnt, userrec *user)
{
chanrec* Ptr;
if (pcnt > 2)
{
- kick_channel(user,u,Ptr,parameters[2]);
+ char reason[MAXBUF];
+ strncpy(reason,parameters[2],MAXBUF);
+ if (strlen(reason)>MAXKICK)
+ {
+ reason[MAXKICK-1] = '\0';
+ }
+
+ kick_channel(user,u,Ptr,reason);
}
else
{
void handle_die(char **parameters, int pcnt, userrec *user)
{
- debug("die: %s",user->nick);
+ log(DEBUG,"die: %s",user->nick);
if (!strcmp(parameters[0],diepass))
{
WriteOpers("*** DIE command from %s!%s@%s, terminating...",user->nick,user->ident,user->host);
- sleep(DieDelay);
+ sleep(DieDelay);
Exit(ERROR);
}
else
void handle_restart(char **parameters, int pcnt, userrec *user)
{
- debug("restart: %s",user->nick);
- if (!strcmp(parameters[0],restartpass))
+ log(DEBUG,"restart: %s",user->nick);
+ if (!strcmp(parameters[0],restartpass))
{
- WriteOpers("*** RESTART command from %s!%s@%s, Pretending to restart till this is finished :D",user->nick,user->ident,user->host);
- sleep(DieDelay);
- Exit(ERROR);
+ WriteOpers("*** RESTART command from %s!%s@%s, Pretending to restart till this is finished :D",user->nick,user->ident,user->host);
+ sleep(DieDelay);
+ Exit(ERROR);
/* Will finish this later when i can be arsed :) */
- }
- else
+ }
+ else
{
- WriteOpers("*** Failed RESTART Command from %s!%s@%s.",user->nick,user->ident,user->host);
- }
+ WriteOpers("*** Failed RESTART Command from %s!%s@%s.",user->nick,user->ident,user->host);
+ }
}
-void kill_link(userrec *user,char* reason)
+void kill_link(userrec *user,const char* r)
{
user_hash::iterator iter = clientlist.find(user->nick);
+
+ char reason[MAXBUF];
+
+ strncpy(reason,r,MAXBUF);
- debug("kill_link: %s '%s'",user->nick,reason);
+ if (strlen(reason)>MAXQUIT)
+ {
+ reason[MAXQUIT-1] = '\0';
+ }
+
+ log(DEBUG,"kill_link: %s '%s'",user->nick,reason);
Write(user->fd,"ERROR :Closing link (%s@%s) [%s]",user->ident,user->host,reason);
- fdatasync(user->fd);
- WriteOpers("*** Client exiting: %s!%s@%s [%s]",user->nick,user->ident,user->host,reason);
- FOREACH_MOD OnUserQuit(user);
- debug("closing fd %d",user->fd);
+ log(DEBUG,"closing fd %d",user->fd);
+
/* bugfix, cant close() a nonblocking socket (sux!) */
- WriteCommonExcept(user,"QUIT :%s",reason);
- Blocking(user->fd);
- close(user->fd);
- NonBlocking(user->fd);
+ if (user->registered == 7) {
+ FOREACH_MOD OnUserQuit(user);
+ WriteCommonExcept(user,"QUIT :%s",reason);
+ }
+
+ /* push the socket on a stack of sockets due to be closed at the next opportunity
+ * 'Client exited' is an exception to this as it means the client side has already
+ * closed the socket, we don't need to do it.
+ */
+ fd_reap.push_back(user->fd);
+
+ bool do_purge = false;
+
+ if (user->registered == 7) {
+ WriteOpers("*** Client exiting: %s!%s@%s [%s]",user->nick,user->ident,user->host,reason);
+ AddWhoWas(user);
+ }
if (iter != clientlist.end())
{
- debug("deleting user hash value %p",iter->second);
- delete iter->second;
+ log(DEBUG,"deleting user hash value %d",iter->second);
+ if ((iter->second) && (user->registered == 7)) {
+ delete iter->second;
+ }
clientlist.erase(iter);
}
-
- purge_empty_chans();
+
+ if (user->registered == 7) {
+ purge_empty_chans();
+ }
}
userrec *u = Find(parameters[0]);
char killreason[MAXBUF];
- debug("kill: %s %s",parameters[0],parameters[1]);
+ log(DEBUG,"kill: %s %s",parameters[0],parameters[1]);
if (u)
{
+ WriteTo(user, u, "KILL %s :%s!%s!%s (%s)", u->nick, ServerName,user->dhost,user->nick,parameters[1]);
+ // :Brain!brain@NetAdmin.chatspike.net KILL [Brain] :homer!NetAdmin.chatspike.net!Brain (test kill)
WriteOpers("*** Local Kill by %s: %s!%s@%s (%s)",user->nick,u->nick,u->ident,u->host,parameters[1]);
sprintf(killreason,"Killed (%s (%s))",user->nick,parameters[1]);
kill_link(u,killreason);
bool IsDenied(userrec *user)
{
- for (ClassVector::iterator i = Classes.begin(); i != Classes.end(); i++)
- {
- if (match(user->host,i->host) && (i->type == CC_DENY))
- {
- return true;
- }
- }
- return false;
+ for (ClassVector::iterator i = Classes.begin(); i != Classes.end(); i++)
+ {
+ if (match(user->host,i->host) && (i->type == CC_DENY))
+ {
+ return true;
+ }
+ }
+ return false;
}
+
void handle_pass(char **parameters, int pcnt, userrec *user)
{
+ // Check to make sure they havnt registered -- Fix by FCS
+ if (user->registered == 7)
+ {
+ WriteServ(user->fd,"462 %s :You may not reregister",user->nick);
+ return;
+ }
if (!strcasecmp(parameters[0],Passwd(user)))
{
user->haspassed = true;
{
if (!c)
{
- WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[1]);
+ WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[1]);
}
else
{
{
if (cstatus(user,c) < STATUS_HOP)
{
- WriteServ(user->fd,"482 %s %s :You must be at least a half-operator",user->nick, c->name);
+ WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, c->name);
return;
}
-
- u->InviteTo(c->name);
- WriteFrom(u->fd,user,"INVITE %s :%s",u->nick,c->name);
- WriteServ(user->fd,"341 %s %s %s",user->nick,u->nick,c->name);
}
+ if (has_channel(u,c))
+ {
+ WriteServ(user->fd,"443 %s %s %s :Is already on channel %s",user->nick,u->nick,c->name,c->name);
+ return;
+ }
+ u->InviteTo(c->name);
+ WriteFrom(u->fd,user,"INVITE %s :%s",u->nick,c->name);
+ WriteServ(user->fd,"341 %s %s %s",user->nick,u->nick,c->name);
}
void handle_topic(char **parameters, int pcnt, userrec *user)
}
else
{
- WriteServ(user->fd,"331 %s %s :No topic is set.", user->nick, Ptr->name);
+ WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
}
}
+ return;
}
else if (pcnt>1)
{
- if (loop_call(handle_topic,parameters,pcnt,user,0,pcnt-2,0))
- return;
if (strlen(parameters[0]) <= CHANMAX)
{
Ptr = FindChan(parameters[0]);
{
if ((Ptr->topiclock) && (cstatus(user,Ptr)<STATUS_HOP))
{
- WriteServ(user->fd,"482 %s %s :You must be at least a half-operator", user->nick, Ptr->name);
+ WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel", user->nick, Ptr->name);
return;
}
- strcpy(Ptr->topic,parameters[1]);
+
+ char topic[MAXBUF];
+ strncpy(topic,parameters[1],MAXBUF);
+ if (strlen(topic)>MAXTOPIC)
+ {
+ topic[MAXTOPIC-1] = '\0';
+ }
+
+ strcpy(Ptr->topic,topic);
strcpy(Ptr->setby,user->nick);
Ptr->topicset = time(NULL);
WriteChannel(Ptr,user,"TOPIC %s :%s",Ptr->name, Ptr->topic);
void send_error(char *s)
{
- debug("send_error: %s",s);
+ log(DEBUG,"send_error: %s",s);
for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
{
- WriteServ(i->second->fd,"NOTICE %s :%s",i->second->nick,s);
+ if (isnick(i->second->nick))
+ {
+ WriteServ(i->second->fd,"NOTICE %s :%s",i->second->nick,s);
+ }
+ else
+ {
+ // fix - unregistered connections receive ERROR, not NOTICE
+ Write(i->second->fd,"ERROR :%s",s);
+ }
}
}
void Error(int status)
{
- signal (SIGALRM, SIG_IGN);
- signal (SIGPIPE, SIG_IGN);
- signal (SIGTERM, SIG_IGN);
- signal (SIGABRT, SIG_IGN);
- signal (SIGSEGV, SIG_IGN);
- signal (SIGURG, SIG_IGN);
- signal (SIGKILL, SIG_IGN);
- debug("*** fell down a pothole in the road to perfection ***");
- send_error("Error! Segmentation fault! save meeeeeeeeeeeeee *splat!*");
- exit(status);
+ signal (SIGALRM, SIG_IGN);
+ signal (SIGPIPE, SIG_IGN);
+ signal (SIGTERM, SIG_IGN);
+ signal (SIGABRT, SIG_IGN);
+ signal (SIGSEGV, SIG_IGN);
+ signal (SIGURG, SIG_IGN);
+ signal (SIGKILL, SIG_IGN);
+ log(DEBUG,"*** fell down a pothole in the road to perfection ***");
+ send_error("Error! Segmentation fault! save meeeeeeeeeeeeee *splat!*");
+ exit(status);
}
+
int main (int argc, char *argv[])
{
Start();
- debug("*** InspIRCd starting up!");
- if (!CheckConfig())
+ log(DEBUG,"*** InspIRCd starting up!");
+ if (!FileExists(CONFIG_FILE))
{
- debug("main: no config");
+ printf("ERROR: Cannot open config file: %s\nExiting...\n",CONFIG_FILE);
+ log(DEBUG,"main: no config");
printf("ERROR: Your config file is missing, this IRCd will self destruct in 10 seconds!\n");
Exit(ERROR);
}
+ if (argc > 1) {
+ if (!strcmp(argv[1],"-nofork")) {
+ nofork = true;
+ }
+ }
if (InspIRCd() == ERROR)
{
- debug("main: daemon function bailed");
+ log(DEBUG,"main: daemon function bailed");
printf("ERROR: could not initialise. Shutting down.\n");
Exit(ERROR);
}
user_hash::iterator newnick;
user_hash::iterator oldnick = clientlist.find(Old);
- debug("ReHashNick: %s %s",Old,New);
+ log(DEBUG,"ReHashNick: %s %s",Old,New);
if (!strcasecmp(Old,New))
{
- debug("old nick is new nick, skipping");
+ log(DEBUG,"old nick is new nick, skipping");
return oldnick->second;
}
if (oldnick == clientlist.end()) return NULL; /* doesnt exist */
- debug("ReHashNick: Found hashed nick %s",Old);
+ log(DEBUG,"ReHashNick: Found hashed nick %s",Old);
clientlist[New] = new userrec();
clientlist[New] = oldnick->second;
/*delete oldnick->second; */
clientlist.erase(oldnick);
- debug("ReHashNick: Nick rehashed as %s",New);
+ log(DEBUG,"ReHashNick: Nick rehashed as %s",New);
return clientlist[New];
}
+/* adds or updates an entry in the whowas list */
+void AddWhoWas(userrec* u)
+{
+ user_hash::iterator iter = whowas.find(u->nick);
+ userrec *a = new userrec();
+ strcpy(a->nick,u->nick);
+ strcpy(a->ident,u->ident);
+ strcpy(a->dhost,u->dhost);
+ strcpy(a->host,u->host);
+ strcpy(a->fullname,u->fullname);
+ strcpy(a->server,u->server);
+ a->signon = u->signon;
+
+ /* MAX_WHOWAS: max number of /WHOWAS items
+ * WHOWAS_STALE: number of hours before a WHOWAS item is marked as stale and
+ * can be replaced by a newer one
+ */
+
+ if (iter == whowas.end())
+ {
+ if (whowas.size() == WHOWAS_MAX)
+ {
+ for (user_hash::iterator i = whowas.begin(); i != whowas.end(); i++)
+ {
+ // 3600 seconds in an hour ;)
+ if ((i->second->signon)<(time(NULL)-(WHOWAS_STALE*3600)))
+ {
+ delete i->second;
+ i->second = a;
+ log(DEBUG,"added WHOWAS entry, purged an old record");
+ return;
+ }
+ }
+ }
+ else
+ {
+ log(DEBUG,"added fresh WHOWAS entry");
+ whowas[a->nick] = a;
+ }
+ }
+ else
+ {
+ log(DEBUG,"updated WHOWAS entry");
+ delete iter->second;
+ iter->second = a;
+ }
+}
+
/* add a client connection to the sockets list */
void AddClient(int socket, char* host, int port, bool iscached)
string tempnick;
char tn2[MAXBUF];
user_hash::iterator iter;
-
+
tempnick = ConvToStr(socket) + "-unknown";
sprintf(tn2,"%d-unknown",socket);
clientlist[tempnick] = new userrec();
NonBlocking(socket);
- debug("AddClient: %d %s %d",socket,host,port);
-
+ log(DEBUG,"AddClient: %d %s %d",socket,host,port);
clientlist[tempnick]->fd = socket;
strncpy(clientlist[tempnick]->nick, tn2,NICKMAX);
strncpy(clientlist[tempnick]->host, host,160);
strncpy(clientlist[tempnick]->dhost, host,160);
strncpy(clientlist[tempnick]->server, ServerName,256);
+ strncpy(clientlist[tempnick]->ident, "unknown",9);
clientlist[tempnick]->registered = 0;
clientlist[tempnick]->signon = time(NULL);
clientlist[tempnick]->nping = time(NULL)+240;
WriteServ(socket,"NOTICE Auth :Looking up your hostname...");
}
+ // set the registration timeout for this user
+ unsigned long class_regtimeout = 90;
+ for (ClassVector::iterator i = Classes.begin(); i != Classes.end(); i++)
+ {
+ if (match(clientlist[tempnick]->host,i->host) && (i->type == CC_ALLOW))
+ {
+ class_regtimeout = (unsigned long)i->registration_timeout;
+ break;
+ }
+ }
+ log(DEBUG,"Client has a connection timeout value of %d",class_regtimeout);
+ clientlist[tempnick]->timeout = time(NULL)+class_regtimeout;
+
if (clientlist.size() == MAXCLIENTS)
kill_link(clientlist[tempnick],"No more connections allowed in this class");
}
{
userrec *dest;
chanrec *chan;
+
+ user->idle_lastmsg = time(NULL);
if (loop_call(handle_privmsg,parameters,pcnt,user,0,pcnt-2,0))
return;
WriteServ(user->fd,"404 %s %s :Cannot send to channel (+m)", user->nick, chan->name);
return;
}
+
+ int MOD_RESULT = 0;
+
+ FOREACH_RESULT(OnUserPreMessage(user,chan,TYPE_CHANNEL,std::string(parameters[1])));
+ if (MOD_RESULT) {
+ return;
+ }
+
ChanExceptSender(chan, user, "PRIVMSG %s :%s", chan->name, parameters[1]);
}
else
/* auto respond with aweh msg */
WriteServ(user->fd,"301 %s %s :%s",user->nick,dest->nick,dest->awaymsg);
}
+
+ int MOD_RESULT = 0;
+
+ FOREACH_RESULT(OnUserPreMessage(user,dest,TYPE_USER,std::string(parameters[1])));
+ if (MOD_RESULT) {
+ return;
+ }
+
WriteTo(user, dest, "PRIVMSG %s :%s", dest->nick, parameters[1]);
}
else
userrec *dest;
chanrec *chan;
+ user->idle_lastmsg = time(NULL);
+
if (loop_call(handle_notice,parameters,pcnt,user,0,pcnt-2,0))
return;
if (parameters[0][0] == '#')
WriteServ(user->fd,"404 %s %s :Cannot send to channel (+m)", user->nick, chan->name);
return;
}
+
+ int MOD_RESULT = 0;
+
+ FOREACH_RESULT(OnUserPreNotice(user,chan,TYPE_CHANNEL,std::string(parameters[1])));
+ if (MOD_RESULT) {
+ return;
+ }
+
WriteChannel(chan, user, "NOTICE %s :%s", chan->name, parameters[1]);
}
else
dest = Find(parameters[0]);
if (dest)
{
+ int MOD_RESULT = 0;
+
+ FOREACH_RESULT(OnUserPreNotice(user,dest,TYPE_USER,std::string(parameters[1])));
+ if (MOD_RESULT) {
+ return;
+ }
+
WriteTo(user, dest, "NOTICE %s :%s", dest->nick, parameters[1]);
}
else
int i = 0;
char cmp[MAXBUF];
- debug("chlist: %s",user->nick);
+ log(DEBUG,"chlist: %s",user->nick);
strcpy(lst,"");
if (!user)
{
WriteServ(user->fd,"371 %s :The Inspire IRCd Project Has been brought to you by the following people..",user->nick);
WriteServ(user->fd,"371 %s :Craig Edwards, Craig McLure, and Others..",user->nick);
WriteServ(user->fd,"371 %s :Will finish this later when i can be arsed :p",user->nick);
+ FOREACH_MOD OnInfo(user);
WriteServ(user->fd,"374 %s :End of /INFO list",user->nick);
}
dest = Find(parameters[0]);
if (dest)
{
- WriteServ(user->fd,"311 %s %s %s %s * :%s",user->nick, dest->nick, dest->ident, dest->dhost, dest->fullname);
- if ((user == dest) || (strchr(user->modes,'o')))
+ // bug found by phidjit - were able to whois an incomplete connection if it had sent a NICK or USER
+ if (dest->registered == 7)
{
- WriteServ(user->fd,"378 %s %s :is connecting from *@%s",user->nick, dest->nick, dest->host);
- }
- if (strcmp(chlist(dest),""))
- {
- WriteServ(user->fd,"319 %s %s :%s",user->nick, dest->nick, chlist(dest));
- }
- WriteServ(user->fd,"312 %s %s %s :%s",user->nick, dest->nick, dest->server, ServerDesc);
- if (strcmp(dest->awaymsg,""))
- {
- WriteServ(user->fd,"301 %s %s :%s",user->nick, dest->nick, dest->awaymsg);
+ WriteServ(user->fd,"311 %s %s %s %s * :%s",user->nick, dest->nick, dest->ident, dest->dhost, dest->fullname);
+ if ((user == dest) || (strchr(user->modes,'o')))
+ {
+ WriteServ(user->fd,"378 %s %s :is connecting from *@%s",user->nick, dest->nick, dest->host);
+ }
+ if (strcmp(chlist(dest),""))
+ {
+ WriteServ(user->fd,"319 %s %s :%s",user->nick, dest->nick, chlist(dest));
+ }
+ WriteServ(user->fd,"312 %s %s %s :%s",user->nick, dest->nick, dest->server, ServerDesc);
+ if (strcmp(dest->awaymsg,""))
+ {
+ WriteServ(user->fd,"301 %s %s :%s",user->nick, dest->nick, dest->awaymsg);
+ }
+ if (strchr(dest->modes,'o'))
+ {
+ WriteServ(user->fd,"313 %s %s :is an IRC operator",user->nick, dest->nick);
+ }
+ FOREACH_MOD OnWhois(user,dest);
+ //WriteServ(user->fd,"310 %s %s :is available for help.",user->nick, dest->nick);
+ WriteServ(user->fd,"317 %s %s %d %d :seconds idle, signon time",user->nick, dest->nick, abs((dest->idle_lastmsg)-time(NULL)), dest->signon);
+
+ WriteServ(user->fd,"318 %s %s :End of /WHOIS list.",user->nick, dest->nick);
}
- if (strchr(dest->modes,'o'))
+ else
{
- WriteServ(user->fd,"313 %s %s :is an IRC operator",user->nick, dest->nick);
+ WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
}
- //WriteServ(user->fd,"310 %s %s :is available for help.",user->nick, dest->nick);
- WriteServ(user->fd,"317 %s %s %d %d :seconds idle, signon time",user->nick, dest->nick, abs((dest->idle_lastmsg)-time(NULL)), dest->signon);
-
- WriteServ(user->fd,"318 %s %s :End of /WHOIS list.",user->nick, dest->nick);
}
else
{
void handle_quit(char **parameters, int pcnt, userrec *user)
{
user_hash::iterator iter = clientlist.find(user->nick);
+ char* reason;
- /* theres more to do here, but for now just close the socket */
- if (pcnt == 1)
+ if (user->registered == 7)
{
- if (parameters[0][0] == ':')
+ /* theres more to do here, but for now just close the socket */
+ if (pcnt == 1)
{
- *parameters[0]++;
- }
- Write(user->fd,"ERROR :Closing link (%s@%s) [%s]",user->ident,user->host,parameters[0]);
- WriteOpers("*** Client exiting: %s!%s@%s [%s]",user->nick,user->ident,user->host,parameters[0]);
- WriteCommonExcept(user,"QUIT :%s%s",PrefixQuit,parameters[0]);
- }
- else
- {
- Write(user->fd,"ERROR :Closing link (%s@%s) [QUIT]",user->ident,user->host);
- WriteOpers("*** Client exiting: %s!%s@%s [Client exited]",user->nick,user->ident,user->host);
- WriteCommonExcept(user,"QUIT :Client exited");
- }
+ if (parameters[0][0] == ':')
+ {
+ *parameters[0]++;
+ }
+ reason = parameters[0];
- FOREACH_MOD OnUserQuit(user);
+ if (strlen(reason)>MAXQUIT)
+ {
+ reason[MAXQUIT-1] = '\0';
+ }
- /* confucious say, he who close nonblocking socket, get nothing! */
- Blocking(user->fd);
- close(user->fd);
- NonBlocking(user->fd);
+ Write(user->fd,"ERROR :Closing link (%s@%s) [%s]",user->ident,user->host,parameters[0]);
+ WriteOpers("*** Client exiting: %s!%s@%s [%s]",user->nick,user->ident,user->host,parameters[0]);
+ WriteCommonExcept(user,"QUIT :%s%s",PrefixQuit,parameters[0]);
+ }
+ else
+ {
+ Write(user->fd,"ERROR :Closing link (%s@%s) [QUIT]",user->ident,user->host);
+ WriteOpers("*** Client exiting: %s!%s@%s [Client exited]",user->nick,user->ident,user->host);
+ WriteCommonExcept(user,"QUIT :Client exited");
+ }
+ FOREACH_MOD OnUserQuit(user);
+ AddWhoWas(user);
+ }
+ /* push the socket on a stack of sockets due to be closed at the next opportunity */
+ fd_reap.push_back(user->fd);
+
if (iter != clientlist.end())
{
- debug("deleting user hash value");
- delete iter->second;
+ log(DEBUG,"deleting user hash value %d",iter->second);
+ if ((iter->second) && (user->registered == 7)) {
+ delete iter->second;
+ }
clientlist.erase(iter);
}
-
- purge_empty_chans();
+
+ if (user->registered == 7) {
+ purge_empty_chans();
+ }
}
void handle_who(char **parameters, int pcnt, userrec *user)
{
- chanrec* Ptr;
+ chanrec* Ptr = NULL;
/* theres more to do here, but for now just close the socket */
if (pcnt == 1)
{
if ((!strcmp(parameters[0],"0")) || (!strcmp(parameters[0],"*")))
{
- Ptr = user->chans[0].channel;
- printf(user->chans[0].channel->name);
- for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
+ if (user->chans[0].channel)
{
- if ((common_channels(user,i->second)) && (isnick(i->second->nick)))
+ Ptr = user->chans[0].channel;
+ for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
{
- WriteServ(user->fd,"352 %s %s %s %s %s %s Hr@ :0 %s",user->nick, Ptr->name, i->second->ident, i->second->dhost, ServerName, i->second->nick, i->second->fullname);
+ if ((common_channels(user,i->second)) && (isnick(i->second->nick)))
+ {
+ WriteServ(user->fd,"352 %s %s %s %s %s %s Hr@ :0 %s",user->nick, Ptr->name, i->second->ident, i->second->dhost, ServerName, i->second->nick, i->second->fullname);
+ }
}
}
- WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, Ptr->name);
+ if (Ptr)
+ {
+ WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, Ptr->name);
+ }
+ else
+ {
+ WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, user->nick);
+ }
return;
}
if (parameters[0][0] = '#')
if ((!strcmp(parameters[0],"0")) || (!strcmp(parameters[0],"*")) && (!strcmp(parameters[1],"o")))
{
Ptr = user->chans[0].channel;
- printf(user->chans[0].channel->name);
for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
{
if ((common_channels(user,i->second)) && (isnick(i->second->nick)))
{
WriteServ(user->fd,"382 %s %s :Rehashing",user->nick,CONFIG_FILE);
ReadConfig();
+ FOREACH_MOD OnRehash();
WriteOpers("%s is rehashing config file %s",user->nick,CONFIG_FILE);
}
{
user->registered = 7;
user->idle_lastmsg = time(NULL);
- debug("ConnectUser: %s",user->nick);
+ log(DEBUG,"ConnectUser: %s",user->nick);
if (strcmp(Passwd(user),"") && (!user->haspassed))
{
- Write(user->fd,"ERROR :Closing link: Invalid password");
- fdatasync(user->fd);
kill_link(user,"Invalid password");
return;
}
if (IsDenied(user))
{
- Write(user->fd,"ERROR :Closing link: Unauthorized connection");
- fdatasync(user->fd);
kill_link(user,"Unauthorised connection");
+ return;
}
WriteServ(user->fd,"NOTICE Auth :Welcome to \002%s\002!",Network);
void handle_version(char **parameters, int pcnt, userrec *user)
{
- WriteServ(user->fd,"351 %s :%s %s :%s",user->nick,VERSION,ServerName,SYSTEM);
+ WriteServ(user->fd,"351 %s :%s %s %s :%s",user->nick,VERSION,"$Id$",ServerName,SYSTEM);
}
void handle_ping(char **parameters, int pcnt, userrec *user)
{
if (user->registered < 3)
{
- WriteServ(user->fd,"NOTICE Auth :No ident response, ident prefixed with ~");
- strcpy(user->ident,"~"); /* we arent checking ident... but these days why bother anyway? */
- strncat(user->ident,parameters[0],IDENTMAX);
- strncpy(user->fullname,parameters[3],128);
- user->registered = (user->registered | 1);
+ if (isident(parameters[0]) == 0) {
+ // This kinda Sucks, According to the RFC thou, its either this,
+ // or "You have already registered" :p -- Craig
+ WriteServ(user->fd,"461 %s USER :Not enough parameters",user->nick);
+ }
+ else {
+ WriteServ(user->fd,"NOTICE Auth :No ident response, ident prefixed with ~");
+ strcpy(user->ident,"~"); /* we arent checking ident... but these days why bother anyway? */
+ strncat(user->ident,parameters[0],IDENTMAX);
+ strncpy(user->fullname,parameters[3],128);
+ user->registered = (user->registered | 1);
+ }
}
else
{
}
}
+void handle_whowas(char **parameters, int pcnt, userrec* user)
+{
+ user_hash::iterator i = whowas.find(parameters[0]);
+
+ if (i == whowas.end())
+ {
+ WriteServ(user->fd,"406 %s %s :There was no such nickname",user->nick,parameters[0]);
+ WriteServ(user->fd,"369 %s %s :End of WHOWAS",user->nick,parameters[0]);
+ }
+ else
+ {
+ time_t rawtime = i->second->signon;
+ tm *timeinfo;
+ char b[MAXBUF];
+
+ timeinfo = localtime(&rawtime);
+ strcpy(b,asctime(timeinfo));
+ b[strlen(b)-1] = '\0';
+
+ WriteServ(user->fd,"314 %s %s %s %s * :%s",user->nick,i->second->nick,i->second->ident,i->second->dhost,i->second->fullname);
+ WriteServ(user->fd,"312 %s %s %s :%s",user->nick,i->second->nick,i->second->server,b);
+ WriteServ(user->fd,"369 %s %s :End of WHOWAS",user->nick,parameters[0]);
+ }
+
+}
void handle_trace(char **parameters, int pcnt, userrec *user)
{
}
}
+void handle_modules(char **parameters, int pcnt, userrec *user)
+{
+ for (int i = 0; i < module_names.size(); i++)
+ {
+ Version V = modules[i]->GetVersion();
+ WriteServ(user->fd,"900 %s :0x%08lx %d.%d.%d.%d %s",user->nick,modules[i],V.Major,V.Minor,V.Revision,V.Build,module_names[i].c_str());
+ }
+}
+
void handle_stats(char **parameters, int pcnt, userrec *user)
{
if (pcnt != 1)
/* stats o */
if (!strcasecmp(parameters[0],"o"))
{
- for (int i = 0; i < ConfValueEnum("oper"); i++)
+ for (int i = 0; i < ConfValueEnum("oper",&config_f); i++)
{
char LoginName[MAXBUF];
char HostName[MAXBUF];
char OperType[MAXBUF];
- ConfValue("oper","name",i,LoginName);
- ConfValue("oper","host",i,HostName);
- ConfValue("oper","type",i,OperType);
+ ConfValue("oper","name",i,LoginName,&config_f);
+ ConfValue("oper","host",i,HostName,&config_f);
+ ConfValue("oper","type",i,OperType,&config_f);
WriteServ(user->fd,"243 %s O %s * %s %s 0",user->nick,HostName,LoginName,OperType);
}
}
}
+void handle_connect(char **parameters, int pcnt, userrec *user)
+{
+ char Link_ServerName[1024];
+ char Link_IPAddr[1024];
+ char Link_Port[1024];
+ char Link_Pass[1024];
+ int LinkPort;
+ bool found = false;
+
+ for (int i = 0; i < ConfValueEnum("link",&config_f); i++)
+ {
+ ConfValue("link","name",i,Link_ServerName,&config_f);
+ ConfValue("link","ipaddr",i,Link_IPAddr,&config_f);
+ ConfValue("link","port",i,Link_Port,&config_f);
+ ConfValue("link","sendpass",i,Link_Pass,&config_f);
+ log(DEBUG,"(%d) Comparing against name='%s', ipaddr='%s', port='%s', recvpass='%s'",i,Link_ServerName,Link_IPAddr,Link_Port,Link_Pass);
+ LinkPort = atoi(Link_Port);
+ if (match(Link_ServerName,parameters[0])) {
+ found = true;
+ }
+ }
+
+ if (!found) {
+ WriteServ(user->fd,"NOTICE %s :*** Failed to connect to %s: No servers matching this pattern are configured for linking.",user->nick,parameters[0]);
+ return;
+ }
+
+ // TODO: Perform a check here to stop a server being linked twice!
+
+ WriteServ(user->fd,"NOTICE %s :*** Connecting to %s (%s) port %s...",user->nick,Link_ServerName,Link_IPAddr,Link_Port);
+
+ if (me[defaultRoute])
+ {
+
+ // at this point parameters[0] is an ip in a string.
+ // TODO: Look this up from the <link> blocks instead!
+ for (int j = 0; j < 255; j++) {
+ if (servers[j] == NULL) {
+ servers[j] = new serverrec;
+ strcpy(servers[j]->internal_addr,Link_IPAddr);
+ strcpy(servers[j]->name,Link_ServerName);
+ log(DEBUG,"Allocated new serverrec");
+ if (!me[defaultRoute]->BeginLink(Link_IPAddr,LinkPort,Link_Pass))
+ {
+ WriteServ(user->fd,"NOTICE %s :*** Failed to send auth packet to %s!",user->nick,Link_IPAddr);
+ }
+ return;
+ }
+ }
+ WriteServ(user->fd,"NOTICE %s :*** Failed to create server record for address %s!",user->nick,Link_IPAddr);
+ }
+ else
+ {
+ WriteServ(user->fd,"NOTICE %s :No default route is defined for server connections on this server. You must define a server connection to be default route so that sockets can be bound to it.",user->nick);
+ }
+}
+
+void handle_squit(char **parameters, int pcnt, userrec *user)
+{
+ // send out an squit across the mesh and then clear the server list (for local squit)
+}
+
void handle_oper(char **parameters, int pcnt, userrec *user)
{
char LoginName[MAXBUF];
char Hostname[MAXBUF];
int i,j;
- for (i = 0; i < ConfValueEnum("oper"); i++)
+ for (i = 0; i < ConfValueEnum("oper",&config_f); i++)
{
- ConfValue("oper","name",i,LoginName);
- ConfValue("oper","password",i,Password);
+ ConfValue("oper","name",i,LoginName,&config_f);
+ ConfValue("oper","password",i,Password,&config_f);
if ((!strcmp(LoginName,parameters[0])) && (!strcmp(Password,parameters[1])))
{
/* correct oper credentials */
- ConfValue("oper","type",i,OperType);
+ ConfValue("oper","type",i,OperType,&config_f);
WriteOpers("*** %s (%s@%s) is now an IRC operator of type %s",user->nick,user->ident,user->host,OperType);
WriteServ(user->fd,"381 %s :You are now an IRC operator of type %s",user->nick,OperType);
WriteServ(user->fd,"MODE %s :+o",user->nick);
- for (j =0; j < ConfValueEnum("type"); j++)
+ for (j =0; j < ConfValueEnum("type",&config_f); j++)
{
- ConfValue("type","name",j,TypeName);
+ ConfValue("type","name",j,TypeName,&config_f);
if (!strcmp(TypeName,OperType))
{
/* found this oper's opertype */
- ConfValue("type","host",j,Hostname);
+ ConfValue("type","host",j,Hostname,&config_f);
strncpy(user->dhost,Hostname,256);
}
}
{
strcat(user->modes,"o");
}
+ FOREACH_MOD OnOper(user);
return;
}
}
WriteServ(user->fd,"491 %s :Invalid oper credentials",user->nick);
WriteOpers("*** WARNING! Failed oper attempt by %s!%s@%s!",user->nick,user->ident,user->host);
}
-
+
void handle_nick(char **parameters, int pcnt, userrec *user)
{
if (pcnt < 1)
{
- debug("not enough params for handle_nick");
+ log(DEBUG,"not enough params for handle_nick");
return;
}
if (!parameters[0])
{
- debug("invalid parameter passed to handle_nick");
+ log(DEBUG,"invalid parameter passed to handle_nick");
return;
}
if (!strlen(parameters[0]))
{
- debug("zero length new nick passed to handle_nick");
+ log(DEBUG,"zero length new nick passed to handle_nick");
return;
}
if (!user)
{
- debug("invalid user passed to handle_nick");
+ log(DEBUG,"invalid user passed to handle_nick");
return;
}
if (!user->nick)
{
- debug("invalid old nick passed to handle_nick");
+ log(DEBUG,"invalid old nick passed to handle_nick");
return;
}
if (!strcasecmp(user->nick,parameters[0]))
{
- debug("old nick is new nick, skipping");
+ log(DEBUG,"old nick is new nick, skipping");
return;
}
else
strncpy(user->nick, parameters[0],NICKMAX);
- debug("new nick set: %s",user->nick);
+ log(DEBUG,"new nick set: %s",user->nick);
if (user->registered < 3)
user->registered = (user->registered | 2);
/* user is registered now, bit 0 = USER command, bit 1 = sent a NICK command */
ConnectUser(user);
}
- debug("exit nickchange: %s",user->nick);
+ log(DEBUG,"exit nickchange: %s",user->nick);
+}
+
+void force_nickchange(userrec* user,const char* newnick)
+{
+ char nick[MAXBUF];
+ strcpy(nick,"");
+
+ if (user)
+ {
+ if (newnick)
+ {
+ strncpy(nick,newnick,MAXBUF);
+ }
+ if (user->registered == 7)
+ {
+ char* pars[1];
+ pars[0] = nick;
+ handle_nick(pars,1,user);
+ }
+ }
}
+
int process_parameters(char **command_p,char *parameters)
{
{
return;
}
+
+ int total_params = 0;
+ if (strlen(cmd)>2)
+ {
+ for (int q = 0; q < strlen(cmd)-1; q++)
+ {
+ if ((cmd[q] == ' ') && (cmd[q+1] == ':'))
+ {
+ total_params++;
+ // found a 'trailing', we dont count them after this.
+ break;
+ }
+ if (cmd[q] == ' ')
+ total_params++;
+ }
+ }
+
+ // another phidjit bug...
+ if (total_params > 126)
+ {
+ kill_link(user,"Protocol violation");
+ return;
+ }
+
strcpy(temp,cmd);
+
+ string tmp = cmd;
+ FOREACH_MOD OnServerRaw(tmp,true);
+ const char* cmd2 = tmp.c_str();
+ snprintf(cmd,512,"%s",cmd2);
+
if (!strchr(cmd,' '))
{
/* no parameters, lets skip the formalities and not chop up
* the string */
+ log(DEBUG,"About to preprocess command with no params");
items = 0;
command_p[0] = NULL;
parameters = NULL;
{
cmd[i] = toupper(cmd[i]);
}
+ log(DEBUG,"Preprocess done");
}
else
{
}
cmd_found = 0;
+
+ if (strlen(command)>MAXCOMMAND)
+ {
+ kill_link(user,"Protocol violation");
+ return;
+ }
+
+ for (int x = 0; x < strlen(command); x++)
+ {
+ if (((command[x] < 'A') || (command[x] > 'Z')) && (command[x] != '.'))
+ {
+ if (((command[x] < '0') || (command[x]> '9')) && (command[x] != '-'))
+ {
+ kill_link(user,"Protocol violation");
+ return;
+ }
+ }
+ }
for (i = 0; i != cmdlist.size(); i++)
{
if (strcmp(cmdlist[i].command,""))
{
- if (!strcmp(command, cmdlist[i].command))
+ if (strlen(command)>=(strlen(cmdlist[i].command))) if (!strncmp(command, cmdlist[i].command,MAXCOMMAND))
{
+ log(DEBUG,"Found matching command");
+
if (parameters)
{
if (strcmp(parameters,""))
if (user)
{
- user->idle_lastmsg = time(NULL);
+ log(DEBUG,"Processing command");
+
/* activity resets the ping pending timer */
user->nping = time(NULL) + 120;
if ((items) < cmdlist[i].min_params)
{
- debug("process_command: not enough parameters: %s %s",user->nick,command);
+ log(DEBUG,"process_command: not enough parameters: %s %s",user->nick,command);
WriteServ(user->fd,"461 %s %s :Not enough parameters",user->nick,command);
return;
}
if ((!strchr(user->modes,cmdlist[i].flags_needed)) && (cmdlist[i].flags_needed))
{
- debug("process_command: permission denied: %s %s",user->nick,command);
+ log(DEBUG,"process_command: permission denied: %s %s",user->nick,command);
WriteServ(user->fd,"481 %s :Permission Denied- You do not have the required operator privilages",user->nick);
cmd_found = 1;
return;
}
- /* if the command isnt USER, PASS, or NICK, and nick is empty,
- * deny command! */
- if ((strcmp(command,"USER")) && (strcmp(command,"NICK")) && (strcmp(command,"PASS")))
+ /* if the command isnt USER, PASS, or NICK, and nick is empty,
+ * deny command! */
+ if ((strncmp(command,"USER",4)) && (strncmp(command,"NICK",4)) && (strncmp(command,"PASS",4)))
{
if ((!isnick(user->nick)) || (user->registered != 7))
{
- debug("process_command: not registered: %s %s",user->nick,command);
+ log(DEBUG,"process_command: not registered: %s %s",user->nick,command);
WriteServ(user->fd,"451 %s :You have not registered",command);
return;
}
}
if ((user->registered == 7) || (!strcmp(command,"USER")) || (!strcmp(command,"NICK")) || (!strcmp(command,"PASS")))
{
- debug("process_command: handler: %s %s %d",user->nick,command,items);
+ log(DEBUG,"process_command: handler: %s %s %d",user->nick,command,items);
if (cmdlist[i].handler_function)
{
/* ikky /stats counters */
}
else
{
- debug("process_command: not registered: %s %s",user->nick,command);
+ log(DEBUG,"process_command: not registered: %s %s",user->nick,command);
WriteServ(user->fd,"451 %s :You have not registered",command);
return;
}
}
if ((!cmd_found) && (user))
{
- debug("process_command: not in table: %s %s",user->nick,command);
+ log(DEBUG,"process_command: not in table: %s %s",user->nick,command);
WriteServ(user->fd,"421 %s %s :Unknown command",user->nick,command);
}
}
comm.use_count = 0;
comm.total_bytes = 0;
cmdlist.push_back(comm);
+ log(DEBUG,"Added command %s (%d parameters)",cmd,minparams);
}
void SetupCommandTable(void)
{
- createcommand("USER",handle_user,0,4);
- createcommand("NICK",handle_nick,0,1);
- createcommand("QUIT",handle_quit,0,1);
- createcommand("VERSION",handle_version,0,0);
- createcommand("PING",handle_ping,0,1);
- createcommand("PONG",handle_pong,0,1);
- createcommand("ADMIN",handle_admin,0,0);
- createcommand("PRIVMSG",handle_privmsg,0,2);
- createcommand("INFO",handle_info,0,0);
- createcommand("TIME",handle_time,0,0);
- createcommand("WHOIS",handle_whois,0,1);
- createcommand("WALLOPS",handle_wallops,'o',1);
- createcommand("NOTICE",handle_notice,0,2);
- createcommand("JOIN",handle_join,0,1);
- createcommand("NAMES",handle_names,0,1);
- createcommand("PART",handle_part,0,1);
- createcommand("KICK",handle_kick,0,2);
- createcommand("MODE",handle_mode,0,1);
- createcommand("TOPIC",handle_topic,0,1);
- createcommand("WHO",handle_who,0,1);
- createcommand("MOTD",handle_motd,0,0);
- createcommand("RULES",handle_join,0,0);
- createcommand("OPER",handle_oper,0,2);
- createcommand("LIST",handle_list,0,0);
- createcommand("DIE",handle_die,'o',1);
- createcommand("RESTART",handle_restart,'o',1);
- createcommand("KILL",handle_kill,'o',2);
- createcommand("REHASH",handle_rehash,'o',0);
- createcommand("LUSERS",handle_lusers,0,0);
- createcommand("STATS",handle_stats,0,1);
- createcommand("USERHOST",handle_userhost,0,1);
- createcommand("AWAY",handle_away,0,0);
- createcommand("ISON",handle_ison,0,0);
- createcommand("SUMMON",handle_summon,0,0);
- createcommand("USERS",handle_users,0,0);
- createcommand("INVITE",handle_invite,0,2);
- createcommand("PASS",handle_pass,0,1);
- createcommand("TRACE",handle_trace,'o',0);
+ createcommand("USER",handle_user,0,4);
+ createcommand("NICK",handle_nick,0,1);
+ createcommand("QUIT",handle_quit,0,0);
+ createcommand("VERSION",handle_version,0,0);
+ createcommand("PING",handle_ping,0,1);
+ createcommand("PONG",handle_pong,0,1);
+ createcommand("ADMIN",handle_admin,0,0);
+ createcommand("PRIVMSG",handle_privmsg,0,2);
+ createcommand("INFO",handle_info,0,0);
+ createcommand("TIME",handle_time,0,0);
+ createcommand("WHOIS",handle_whois,0,1);
+ createcommand("WALLOPS",handle_wallops,'o',1);
+ createcommand("NOTICE",handle_notice,0,2);
+ createcommand("JOIN",handle_join,0,1);
+ createcommand("NAMES",handle_names,0,1);
+ createcommand("PART",handle_part,0,1);
+ createcommand("KICK",handle_kick,0,2);
+ createcommand("MODE",handle_mode,0,1);
+ createcommand("TOPIC",handle_topic,0,1);
+ createcommand("WHO",handle_who,0,1);
+ createcommand("MOTD",handle_motd,0,0);
+ createcommand("RULES",handle_rules,0,0);
+ createcommand("OPER",handle_oper,0,2);
+ createcommand("LIST",handle_list,0,0);
+ createcommand("DIE",handle_die,'o',1);
+ createcommand("RESTART",handle_restart,'o',1);
+ createcommand("KILL",handle_kill,'o',2);
+ createcommand("REHASH",handle_rehash,'o',0);
+ createcommand("LUSERS",handle_lusers,0,0);
+ createcommand("STATS",handle_stats,0,1);
+ createcommand("USERHOST",handle_userhost,0,1);
+ createcommand("AWAY",handle_away,0,0);
+ createcommand("ISON",handle_ison,0,0);
+ createcommand("SUMMON",handle_summon,0,0);
+ createcommand("USERS",handle_users,0,0);
+ createcommand("INVITE",handle_invite,0,2);
+ createcommand("PASS",handle_pass,0,1);
+ createcommand("TRACE",handle_trace,'o',0);
+ createcommand("WHOWAS",handle_whowas,0,1);
+ createcommand("CONNECT",handle_connect,'o',1);
+ createcommand("SQUIT",handle_squit,'o',1);
+ createcommand("MODULES",handle_modules,'o',0);
}
void process_buffer(userrec *user)
{
+ if (!user)
+ {
+ log(DEFAULT,"*** BUG *** process_buffer was given an invalid parameter");
+ return;
+ }
char cmd[MAXBUF];
int i;
if (!user->inbuf)
{
+ log(DEFAULT,"*** BUG *** process_buffer was given an invalid parameter");
return;
}
if (!strcmp(user->inbuf,""))
{
return;
}
- debug("InspIRCd: processing: %s %s",user->nick,cmd);
- process_command(user,cmd);
+ log(DEBUG,"InspIRCd: processing: %s %s",user->nick,cmd);
+ tidystring(cmd);
+ if ((user) && (cmd))
+ {
+ process_command(user,cmd);
+ }
}
-int InspIRCd(void)
+void process_restricted_commands(char token,char* params,serverrec* source,serverrec* reply, char* udp_host,int udp_port)
{
- struct sockaddr_in client, server;
- int portCount = 0, ports[MAXSOCKS];
- char addrs[MAXBUF][255];
- int openSockfd[MAXSOCKS], incomingSockfd, result = TRUE;
- socklen_t length;
- int count = 0, scanDetectTrigger = TRUE, showBanner = FALSE;
- int selectResult = 0;
- char *temp, configToken[MAXBUF], stuff[MAXBUF], Addr[MAXBUF];
- char resolvedHost[MAXBUF];
- fd_set selectFds;
- struct timeval tv;
- int count2;
-
- debug("InspIRCd: startup: begin");
- debug("$Id$");
- if ((geteuid()) && (getuid()) == 0)
- {
- printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
- Exit(ERROR);
- debug("InspIRCd: startup: not starting with UID 0!");
- }
- SetupCommandTable();
- debug("InspIRCd: startup: default command table set up");
-
- ReadConfig();
- if (strcmp(DieValue,""))
- {
- printf("WARNING: %s\n\n",DieValue);
- exit(0);
- }
- debug("InspIRCd: startup: read config");
-
- for (count = 0; count < ConfValueEnum("bind"); count++)
- {
- ConfValue("bind","port",count,configToken);
- ConfValue("bind","address",count,Addr);
- ports[count] = atoi(configToken);
- strcpy(addrs[count],Addr);
- debug("InspIRCd: startup: read binding %s:%d from config",addrs[count],ports[count]);
- }
- portCount = ConfValueEnum("bind");
- debug("InspIRCd: startup: read %d total ports",portCount);
+ WriteOpers("Secure-UDP-Channel: Token='%c', Params='%s'",token,params);
+}
- debug("InspIRCd: startup: InspIRCd is now running!");
- printf("\n");
- for (count = 0; count < ConfValueEnum("module"); count++)
- {
- char modfile[MAXBUF];
- ConfValue("module","name",count,configToken);
- sprintf(modfile,"%s/%s",MOD_PATH,configToken);
- printf("Loading module... \033[1;37m%s\033[0;37m\n",modfile);
- debug("InspIRCd: startup: Loading module: %s",modfile);
-
- factory[count] = new ircd_module(modfile);
- if (factory[count]->LastError())
- {
- debug("Unable to load %s: %s",modfile,factory[count]->LastError());
- sprintf("Unable to load %s: %s\nExiting...\n",modfile,factory[count]->LastError());
- Exit(ERROR);
+void handle_link_packet(long theirkey, char* udp_msg, char* udp_host, int udp_port, serverrec *serv)
+{
+ char response[10240];
+ char token = udp_msg[0];
+ char* params = udp_msg + 2;
+ char finalparam[1024];
+ strcpy(finalparam," :xxxx");
+ if (strstr(params," :")) {
+ strncpy(finalparam,strstr(params," :"),1024);
+ }
+ if (token == 'S') {
+ // S test.chatspike.net password :ChatSpike InspIRCd test server
+ char* servername = strtok(params," ");
+ char* password = strtok(NULL," ");
+ char* serverdesc = finalparam+2;
+ WriteOpers("CONNECT from %s (%s)",servername,udp_host,password,serverdesc);
+
+
+ char Link_ServerName[1024];
+ char Link_IPAddr[1024];
+ char Link_Port[1024];
+ char Link_Pass[1024];
+ char Link_SendPass[1024];
+ int LinkPort = 0;
+
+ // search for a corresponding <link> block in the config files
+ for (int i = 0; i < ConfValueEnum("link",&config_f); i++)
+ {
+ ConfValue("link","name",i,Link_ServerName,&config_f);
+ ConfValue("link","ipaddr",i,Link_IPAddr,&config_f);
+ ConfValue("link","port",i,Link_Port,&config_f);
+ ConfValue("link","recvpass",i,Link_Pass,&config_f);
+ ConfValue("link","sendpass",i,Link_SendPass,&config_f);
+ log(DEBUG,"(%d) Comparing against name='%s', ipaddr='%s', port='%s', recvpass='%s'",i,Link_ServerName,Link_IPAddr,Link_Port,Link_Pass);
+ LinkPort = atoi(Link_Port);
+ if (!strcasecmp(Link_ServerName,servername)) {
+ if (!strcasecmp(Link_IPAddr,udp_host)) {
+ if (LinkPort == udp_port) {
+ // we have a matching link line -
+ // send a 'diminutive' server message back...
+ snprintf(response,10240,"s %s %s :%s",ServerName,Link_SendPass,ServerDesc);
+ serv->SendPacket(response,udp_host,udp_port,0);
+ WriteOpers("CONNECT from %s accepted, authenticating",servername);
+ for (int j = 0; j < 255; j++) {
+ if (servers[j] == NULL) {
+ servers[j] = new serverrec;
+ strcpy(servers[j]->internal_addr,udp_host);
+ strcpy(servers[j]->name,servername);
+ // create a server record for this server
+ snprintf(response,10240,"O %d",MyKey);
+ serv->SendPacket(response,udp_host,udp_port,0);
+ return;
+ }
+ }
+ WriteOpers("Internal error connecting to %s, failed to create server record!",servername);
+ return;
+ }
+ else {
+ log(DEBUG,"Port numbers '%d' and '%d' don't match",LinkPort,udp_port);
+ }
+ }
+ else {
+ log(DEBUG,"IP Addresses '%s' and '%s' don't match",Link_IPAddr,udp_host);
+ }
+ }
+ else {
+ log(DEBUG,"Server names '%s' and '%s' don't match",Link_ServerName,servername);
+ }
+ }
+ serv->SendPacket("E :Access is denied (no matching link block)",udp_host,udp_port,0);
+ WriteOpers("CONNECT from %s denied, no matching link block",servername);
+ return;
}
- if (factory[count]->factory)
- {
- modules[count] = factory[count]->factory->CreateModule();
- /* save the module and the module's classfactory, if
- * this isnt done, random crashes can occur :/ */
+ else
+ if (token == 'O') {
+ // if this is received, this means the server-ip that sent it said "OK" to credentials.
+ // only when a server says this do we exchange keys. The server MUST have an entry in the servers
+ // array, which is only added by an 'S' packet or BeginLink().
+ for (int i = 0; i < 255; i++) {
+ if (servers[i] != NULL) {
+ if (!strcasecmp(servers[i]->internal_addr,udp_host)) {
+ servers[i]->key = atoi(params);
+ log(DEBUG,"Key for this server is now %d",servers[i]->key);
+ serv->SendPacket("Z blah blah",udp_host,udp_port,MyKey);
+ return;
+ }
+ }
+ }
+ WriteOpers("\2WARNING!\2 Server ip %s attempted a key exchange, but is not in the authentication state! Possible intrusion attempt!",udp_host);
}
else
- {
- debug("Unable to load %s",modfile);
- sprintf("Unable to load %s\nExiting...\n",modfile);
- Exit(ERROR);
+ if (token == 's') {
+ // S test.chatspike.net password :ChatSpike InspIRCd test server
+ char* servername = strtok(params," ");
+ char* password = strtok(NULL," ");
+ char* serverdesc = finalparam+2;
+
+ // TODO: we should do a check here to ensure that this server is one we recently initiated a
+ // link with, and didnt hear an 's' or 'E' back from yet (these are the only two valid responses
+ // to an 'S' command. If we didn't recently send an 'S' to this server, theyre trying to spoof
+ // a connect, so put out an oper alert!
+
+
+
+
+ // for now, just accept all, we'll fix that later.
+ WriteOpers("%s accepted our link credentials ",servername);
+
+ char Link_ServerName[1024];
+ char Link_IPAddr[1024];
+ char Link_Port[1024];
+ char Link_Pass[1024];
+ char Link_SendPass[1024];
+ int LinkPort = 0;
+
+ // search for a corresponding <link> block in the config files
+ for (int i = 0; i < ConfValueEnum("link",&config_f); i++)
+ {
+ ConfValue("link","name",i,Link_ServerName,&config_f);
+ ConfValue("link","ipaddr",i,Link_IPAddr,&config_f);
+ ConfValue("link","port",i,Link_Port,&config_f);
+ ConfValue("link","recvpass",i,Link_Pass,&config_f);
+ ConfValue("link","sendpass",i,Link_SendPass,&config_f);
+ log(DEBUG,"(%d) Comparing against name='%s', ipaddr='%s', port='%s', recvpass='%s'",i,Link_ServerName,Link_IPAddr,Link_Port,Link_Pass);
+ LinkPort = atoi(Link_Port);
+ if (!strcasecmp(Link_ServerName,servername)) {
+ if (!strcasecmp(Link_IPAddr,udp_host)) {
+ if (LinkPort == udp_port) {
+ // matching link at this end too, we're all done!
+ // at this point we must begin key exchange and insert this
+ // server into our 'active' table.
+ for (int j = 0; j < 255; j++) {
+ if (servers[j] != NULL) {
+ if (!strcasecmp(servers[j]->internal_addr,udp_host)) {
+ WriteOpers("Server %s authenticated, exchanging server keys...",servername);
+ snprintf(response,10240,"O %d",MyKey);
+ serv->SendPacket(response,udp_host,udp_port,0);
+ return;
+ }
+ }
+ }
+ WriteOpers("\2WARNING!\2 %s sent us an authentication packet but we are not authenticating with this server right noe! Possible intrusion attempt!",udp_host);
+ return;
+
+ }
+ else {
+ log(DEBUG,"Port numbers '%d' and '%d' don't match",LinkPort,udp_port);
+ }
+ }
+ else {
+ log(DEBUG,"IP Addresses '%s' and '%s' don't match",Link_IPAddr,udp_host);
+ }
+ }
+ else {
+ log(DEBUG,"Server names '%s' and '%s' don't match",Link_ServerName,servername);
+ }
+ }
+ serv->SendPacket("E :Access is denied (no matching link block)",udp_host,udp_port,0);
+ WriteOpers("CONNECT from %s denied, no matching link block",servername);
+ return;
}
- }
- MODCOUNT = count - 1;
- debug("Total loaded modules: %d",MODCOUNT+1);
+ else
+ if (token == 'E') {
+ char* error_message = finalparam+2;
+ WriteOpers("ERROR from %s: %s",udp_host,error_message);
+ // remove this server from any lists
+ for (int j = 0; j < 255; j++) {
+ if (servers[j] != NULL) {
+ if (!strcasecmp(servers[j]->internal_addr,udp_host)) {
+ delete servers[j];
+ return;
+ }
+ }
+ }
+ return;
+ }
+ else {
- printf("\nInspIRCd is now running!\n");
+ serverrec* source_server = NULL;
- startup_time = time(NULL);
-
- if (DaemonSeed() == ERROR)
- {
- debug("InspIRCd: startup: can't daemonise");
- printf("ERROR: could not go into daemon mode. Shutting down.\n");
- Exit(ERROR);
- }
-
-
- /* setup select call */
- FD_ZERO(&selectFds);
- debug("InspIRCd: startup: zero selects");
+ for (int j = 0; j < 255; j++) {
+ if (servers[j] != NULL) {
+ if (!strcasecmp(servers[j]->internal_addr,udp_host)) {
+ if (servers[j]->key == theirkey) {
+ // found a valid key for this server, can process restricted stuff here
+ process_restricted_commands(token,params,servers[j],serv,udp_host,udp_port);
+
+ return;
+ }
+ }
+ }
+ }
- for (count = 0; count < portCount; count++)
- {
- if ((openSockfd[boundPortCount] = OpenTCPSocket()) == ERROR)
- {
- debug("InspIRCd: startup: bad fd %d",openSockfd[boundPortCount]);
- return(ERROR);
- }
- if (BindSocket(openSockfd[boundPortCount],client,server,ports[count],addrs[count]) == ERROR)
- {
- debug("InspIRCd: startup: failed to bind port %d",ports[count]);
- }
- else /* well we at least bound to one socket so we'll continue */
- {
- boundPortCount++;
- }
- }
+ log(DEBUG,"Unrecognised token or unauthenticated host in datagram from %s:%d: %c",udp_host,udp_port,token);
+ }
+}
- debug("InspIRCd: startup: total bound ports %d",boundPortCount);
-
- /* if we didn't bind to anything then abort */
- if (boundPortCount == 0)
- {
- debug("InspIRCd: startup: no ports bound, bailing!");
- return (ERROR);
- }
+int reap_counter = 0;
- length = sizeof (client);
- int flip_flop = 0;
-
- /* main loop for multiplexing/resetting */
- for (;;)
- {
- /* set up select call */
- for (count = 0; count < boundPortCount; count++)
- {
- FD_SET (openSockfd[count], &selectFds);
- }
+int InspIRCd(void)
+{
+ struct sockaddr_in client, server;
+ char addrs[MAXBUF][255];
+ int openSockfd[MAXSOCKS], incomingSockfd, result = TRUE;
+ socklen_t length;
+ int count = 0, scanDetectTrigger = TRUE, showBanner = FALSE;
+ int selectResult = 0;
+ char *temp, configToken[MAXBUF], stuff[MAXBUF], Addr[MAXBUF], Type[MAXBUF];
+ char resolvedHost[MAXBUF];
+ fd_set selectFds;
+ struct timeval tv;
+
+ log_file = fopen("ircd.log","a+");
+ if (!log_file)
+ {
+ printf("ERROR: Could not write to logfile ircd.log, bailing!\n\n");
+ Exit(ERROR);
+ }
+
+ log(DEBUG,"InspIRCd: startup: begin");
+ log(DEBUG,"$Id$");
+ if (geteuid() == 0)
+ {
+ printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
+ Exit(ERROR);
+ log(DEBUG,"InspIRCd: startup: not starting with UID 0!");
+ }
+ SetupCommandTable();
+ log(DEBUG,"InspIRCd: startup: default command table set up");
+
+ ReadConfig();
+ if (strcmp(DieValue,""))
+ {
+ printf("WARNING: %s\n\n",DieValue);
+ exit(0);
+ }
+ log(DEBUG,"InspIRCd: startup: read config");
+
+ int count2 = 0, count3 = 0;
+
+ for (count = 0; count < ConfValueEnum("bind",&config_f); count++)
+ {
+ ConfValue("bind","port",count,configToken,&config_f);
+ ConfValue("bind","address",count,Addr,&config_f);
+ ConfValue("bind","type",count,Type,&config_f);
+ if (!strcmp(Type,"servers"))
+ {
+ char Default[MAXBUF];
+ strcpy(Default,"no");
+ ConfValue("bind","default",count,Default,&config_f);
+ if (strchr(Default,'y'))
+ {
+ defaultRoute = count3;
+ log(DEBUG,"InspIRCd: startup: binding '%s:%s' is default server route",Addr,configToken);
+ }
+ me[count3] = new serverrec(ServerName,100L,false);
+ me[count3]->CreateListener(Addr,atoi(configToken));
+ count3++;
+ }
+ else
+ {
+ ports[count2] = atoi(configToken);
+ strcpy(addrs[count2],Addr);
+ count2++;
+ }
+ log(DEBUG,"InspIRCd: startup: read binding %s:%s [%s] from config",Addr,configToken, Type);
+ }
+ portCount = count2;
+ UDPportCount = count3;
+
+ log(DEBUG,"InspIRCd: startup: read %d total client ports and %d total server ports",portCount,UDPportCount);
+
+ log(DEBUG,"InspIRCd: startup: InspIRCd is now running!");
+
+ printf("\n");
+
+ /* BugFix By Craig! :p */
+ count2 = 0;
+ for (count = 0; count2 < ConfValueEnum("module",&config_f); count2++)
+ {
+ char modfile[MAXBUF];
+ ConfValue("module","name",count,configToken,&config_f);
+ sprintf(modfile,"%s/%s",MOD_PATH,configToken,&config_f);
+ printf("Loading module... \033[1;37m%s\033[0;37m\n",modfile);
+ log(DEBUG,"InspIRCd: startup: Loading module: %s",modfile);
+ /* If The File Doesnt exist, Trying to load it
+ * Will Segfault the IRCd.. So, check to see if
+ * it Exists, Before Proceeding. */
+ if (FileExists(modfile))
+ {
+ factory[count] = new ircd_module(modfile);
+ if (factory[count]->LastError())
+ {
+ log(DEBUG,"Unable to load %s: %s",modfile,factory[count]->LastError());
+ sprintf("Unable to load %s: %s\nExiting...\n",modfile,factory[count]->LastError());
+ Exit(ERROR);
+ }
+ if (factory[count]->factory)
+ {
+ modules[count] = factory[count]->factory->CreateModule();
+ /* save the module and the module's classfactory, if
+ * this isnt done, random crashes can occur :/ */
+ module_names.push_back(modfile);
+ }
+ else
+ {
+ log(DEBUG,"Unable to load %s",modfile);
+ sprintf("Unable to load %s\nExiting...\n",modfile);
+ Exit(ERROR);
+ }
+ /* Increase the Count */
+ count++;
+ }
+ else
+ {
+ log(DEBUG,"InspIRCd: startup: Module Not Found %s",modfile);
+ printf("Module Not Found: \033[1;37m%s\033[0;37m, Skipping\n",modfile);
+ }
+ }
+ MODCOUNT = count - 1;
+ log(DEBUG,"Total loaded modules: %d",MODCOUNT+1);
+
+ printf("\nInspIRCd is now running!\n");
+
+ startup_time = time(NULL);
+
+ if (nofork)
+ {
+ log(VERBOSE,"Not forking as -nofork was specified");
+ }
+ else
+ {
+ if (DaemonSeed() == ERROR)
+ {
+ log(DEBUG,"InspIRCd: startup: can't daemonise");
+ printf("ERROR: could not go into daemon mode. Shutting down.\n");
+ Exit(ERROR);
+ }
+ }
+
+
+ /* setup select call */
+ FD_ZERO(&selectFds);
+ log(DEBUG,"InspIRCd: startup: zero selects");
+ log(VERBOSE,"InspIRCd: startup: portCount = %d", portCount);
+
+ for (count = 0; count < portCount; count++)
+ {
+ if ((openSockfd[boundPortCount] = OpenTCPSocket()) == ERROR)
+ {
+ log(DEBUG,"InspIRCd: startup: bad fd %d",openSockfd[boundPortCount]);
+ return(ERROR);
+ }
+ if (BindSocket(openSockfd[boundPortCount],client,server,ports[count],addrs[count]) == ERROR)
+ {
+ log(DEBUG,"InspIRCd: startup: failed to bind port %d",ports[count]);
+ }
+ else /* well we at least bound to one socket so we'll continue */
+ {
+ boundPortCount++;
+ }
+ }
- /* added timeout! select was waiting forever... wank... :/ */
- tv.tv_usec = 0;
-
- flip_flop++;
- if (flip_flop > 20)
- {
- tv.tv_usec = 1;
- flip_flop = 0;
- }
+ log(DEBUG,"InspIRCd: startup: total bound ports %d",boundPortCount);
+
+ /* if we didn't bind to anything then abort */
+ if (boundPortCount == 0)
+ {
+ log(DEBUG,"InspIRCd: startup: no ports bound, bailing!");
+ return (ERROR);
+ }
+
+ length = sizeof (client);
+ int flip_flop = 0, udp_port = 0;
+ char udp_msg[MAXBUF], udp_host[MAXBUF];
+
+ /* main loop for multiplexing/resetting */
+ for (;;)
+ {
+ /* set up select call */
+ for (count = 0; count < boundPortCount; count++)
+ {
+ FD_SET (openSockfd[count], &selectFds);
+ }
+
+ /* added timeout! select was waiting forever... wank... :/ */
+ tv.tv_usec = 0;
+
+ flip_flop++;
+ reap_counter++;
+ if (flip_flop > 20)
+ {
+ tv.tv_usec = 1;
+ flip_flop = 0;
+ }
+
+ vector<int>::iterator niterator;
+
+
+ // *FIX* Instead of closing sockets in kill_link when they receive the ERROR :blah line, we should queue
+ // them in a list, then reap the list every second or so.
+ if (reap_counter>5000) {
+ if (fd_reap.size() > 0) {
+ for( int n = 0; n < fd_reap.size(); n++)
+ {
+ Blocking(fd_reap[n]);
+ close(fd_reap[n]);
+ NonBlocking(fd_reap[n]);
+ }
+ }
+ fd_reap.clear();
+ reap_counter=0;
+ }
+
- tv.tv_sec = 0;
- selectResult = select(MAXSOCKS, &selectFds, NULL, NULL, &tv);
+ tv.tv_sec = 0;
+ selectResult = select(MAXSOCKS, &selectFds, NULL, NULL, &tv);
+
+ for (int x = 0; x != UDPportCount; x++)
+ {
+ long theirkey = 0;
+ if (me[x]->RecvPacket(udp_msg, udp_host, udp_port, theirkey))
+ {
+ if (strlen(udp_msg)<1) {
+ log(DEBUG,"Invalid datagram from %s:%d:%d [route%d]",udp_host,udp_port,me[x]->port,x);
+ }
+ else
+ {
+ FOREACH_MOD OnPacketReceive(udp_msg);
+ // Packets must go back via the route they arrived on :)
+ handle_link_packet(theirkey, udp_msg, udp_host, udp_port, me[x]);
+ }
+ }
+ }
- for (user_hash::iterator count2 = clientlist.begin(); count2 != clientlist.end(); count2++)
+ for (user_hash::iterator count2 = clientlist.begin(); count2 != clientlist.end(); count2++)
{
char data[MAXBUF];
if (count2->second)
if (count2->second->fd)
{
+ // registration timeout -- didnt send USER/NICK/HOST in the time specified in
+ // their connection class.
+ if ((time(NULL) > count2->second->timeout) && (count2->second->registered != 7))
+ {
+ log(DEBUG,"InspIRCd: registration timeout: %s",count2->second->nick);
+ kill_link(count2->second,"Registration timeout");
+ break;
+ }
if (((time(NULL)) > count2->second->nping) && (isnick(count2->second->nick)) && (count2->second->registered == 7))
{
if (!count2->second->lastping)
{
- debug("InspIRCd: ping timeout: %s",count2->second->nick);
+ log(DEBUG,"InspIRCd: ping timeout: %s",count2->second->nick);
kill_link(count2->second,"Ping timeout");
break;
}
Write(count2->second->fd,"PING :%s",ServerName);
- debug("InspIRCd: pinging: %s",count2->second->nick);
+ log(DEBUG,"InspIRCd: pinging: %s",count2->second->nick);
count2->second->lastping = 0;
count2->second->nping = time(NULL)+120;
}
else
if (result == 0)
{
- debug("InspIRCd: Exited: %s",count2->second->nick);
- kill_link(count2->second,"Client exited");
+ if (count2->second)
+ {
+ log(DEBUG,"InspIRCd: Exited: %s",count2->second->nick);
+ kill_link(count2->second,"Client exited");
+ // must bail here? kill_link removes the hash, corrupting the iterator
+ log(DEBUG,"Bailing from client exit");
+ break;
+ }
}
else if (result > 0)
{
- strncat(count2->second->inbuf, data, result);
- if (strchr(count2->second->inbuf, '\n') || strchr(count2->second->inbuf, '\r'))
+ if (count2->second)
{
- /* at least one complete line is waiting to be processed */
- if (!count2->second->fd)
- break;
- else
- {
+
+ // until the buffer is at 509 chars anything can be inserted into it.
+ if ((strlen(count2->second->inbuf) < 509) && (data[0] != '\0')) {
+ strncat(count2->second->inbuf, data, result);
+ }
+
+ // once you reach 509 chars, only a \r or \n can be inserted,
+ // completing the line.
+ if ((strlen(count2->second->inbuf) >= 509) && ((data[0] == '\r') || (data[0] == '\n'))) {
+ count2->second->inbuf[509] = '\r';
+ count2->second->inbuf[510] = '\n';
+ count2->second->inbuf[511] = '\0';
process_buffer(count2->second);
break;
}
+
+ if (strchr(count2->second->inbuf, '\n') || strchr(count2->second->inbuf, '\r') || (strlen(count2->second->inbuf) > 509))
+ {
+ /* at least one complete line is waiting to be processed */
+ if (!count2->second->fd)
+ break;
+ else
+ {
+ if (strlen(count2->second->inbuf)<512)
+ {
+ // double check the length before processing!
+ process_buffer(count2->second);
+ }
+ else
+ {
+ strcpy(count2->second->inbuf,"");
+ }
+ break;
+ }
+ }
}
}
}
}
-
- /* select is reporting a waiting socket. Poll them all to find out which */
- if (selectResult > 0)
- {
- char target[MAXBUF], resolved[MAXBUF];
- for (count = 0; count < boundPortCount; count++)
- {
- if (FD_ISSET (openSockfd[count], &selectFds))
- {
- incomingSockfd = accept (openSockfd[count], (struct sockaddr *) &client, &length);
-
- address_cache::iterator iter = IP.find(client.sin_addr);
- bool iscached = false;
- if (iter == IP.end())
- {
- /* ip isn't in cache, add it */
- strncpy (target, (char *) inet_ntoa (client.sin_addr), MAXBUF);
- if(CleanAndResolve(resolved, target) != TRUE)
+
+ /* select is reporting a waiting socket. Poll them all to find out which */
+ if (selectResult > 0)
+ {
+ char target[MAXBUF], resolved[MAXBUF];
+ for (count = 0; count < boundPortCount; count++)
+ {
+ if (FD_ISSET (openSockfd[count], &selectFds))
{
- strncpy(resolved,target,MAXBUF);
+ incomingSockfd = accept (openSockfd[count], (struct sockaddr *) &client, &length);
+
+ address_cache::iterator iter = IP.find(client.sin_addr);
+ bool iscached = false;
+ if (iter == IP.end())
+ {
+ /* ip isn't in cache, add it */
+ strncpy (target, (char *) inet_ntoa (client.sin_addr), MAXBUF);
+ if(CleanAndResolve(resolved, target) != TRUE)
+ {
+ strncpy(resolved,target,MAXBUF);
+ }
+ /* hostname now in 'target' */
+ IP[client.sin_addr] = new string(resolved);
+ /* hostname in cache */
+ }
+ else
+ {
+ /* found ip (cached) */
+ strncpy(resolved, iter->second->c_str(), MAXBUF);
+ iscached = true;
+ }
+
+ if (incomingSockfd < 0)
+ {
+ WriteOpers("*** WARNING: Accept failed on port %d (%s)", ports[count],target);
+ log(DEBUG,"InspIRCd: accept failed: %d",ports[count]);
+ break;
+ }
+ AddClient(incomingSockfd, resolved, ports[count], iscached);
+ log(DEBUG,"InspIRCd: adding client on port %d fd=%d",ports[count],incomingSockfd);
+ break;
}
- /* hostname now in 'target' */
- IP[client.sin_addr] = new string(resolved);
- /* hostname in cache */
- }
- else
- {
- /* found ip (cached) */
- strncpy(resolved, iter->second->c_str(), MAXBUF);
- iscached = true;
- }
-
- if (incomingSockfd < 0)
- {
- WriteOpers("*** WARNING: Accept failed on port %d (%s)", ports[count],target);
- debug("InspIRCd: accept failed: %d",ports[count]);
- break;
- }
- AddClient(incomingSockfd, resolved, ports[count], iscached);
- debug("InspIRCd: adding client on port %d fd=%d",ports[count],incomingSockfd);
- break;
- }
-
- }
- }
- }
-
- /* not reached */
- close (incomingSockfd);
+ }
+ }
+}
+/* not reached */
+close (incomingSockfd);
}