-#include "inspircd_io.h"
-#include "inspircd_util.h"
-#include <unistd.h>
-#include <sys/errno.h>
-#include <sys/ioctl.h>
-#include <sys/utsname.h>
-
-#ifdef USE_KQUEUE
-#include <sys/types.h>
-#include <sys/event.h>
-#include <sys/time.h>
-#endif
-
-#ifdef USE_EPOLL
-#include <sys/epoll.h>
-#endif
-
-#include <cstdio>
-#include <time.h>
-#include <string>
-#ifdef GCC3
-#include <ext/hash_map>
-#else
-#include <hash_map>
-#endif
-#include <map>
-#include <sstream>
-#include <vector>
-#include <deque>
-#include <sys/types.h>
-#include <sys/time.h>
-#include <sys/resource.h>
-#ifndef RUSAGE_SELF
-#define RUSAGE_SELF 0
-#define RUSAGE_CHILDREN -1
-#endif
-#include "users.h"
-#include "ctables.h"
-#include "globals.h"
-#include "modules.h"
-#include "dynamic.h"
-#include "wildcard.h"
-#include "message.h"
-#include "mode.h"
-#include "xline.h"
-#include "inspstring.h"
-#include "dnsqueue.h"
-#include "helperfuncs.h"
-#include "hashcomp.h"
-
-#ifdef USE_KQUEUE
-extern int kq;
-#endif
-
-#ifdef USE_EPOLL
-int ep;
-#endif
-
-extern int MODCOUNT;
-extern std::vector<Module*> modules;
-extern std::vector<ircd_module*> factory;
-
-extern int LogLevel;
-extern char ServerName[MAXBUF];
-extern char Network[MAXBUF];
-extern char ServerDesc[MAXBUF];
-extern char AdminName[MAXBUF];
-extern char AdminEmail[MAXBUF];
-extern char AdminNick[MAXBUF];
-extern char diepass[MAXBUF];
-extern char restartpass[MAXBUF];
-extern char motd[MAXBUF];
-extern char rules[MAXBUF];
-extern char list[MAXBUF];
-extern char PrefixQuit[MAXBUF];
-extern char DieValue[MAXBUF];
-
-extern int debugging;
-extern int WHOWAS_STALE;
-extern int WHOWAS_MAX;
-extern int DieDelay;
-extern time_t startup_time;
-extern int NetBufferSize;
-extern int MaxWhoResults;
-extern time_t nb_start;
-
-extern bool nofork;
-
-extern time_t TIME;
-
-extern std::vector<std::string> module_names;
-
-extern char MyExecutable[1024];
-extern int boundPortCount;
-extern int portCount;
-
-extern int ports[MAXSOCKS];
-
-extern std::stringstream config_f;
-
-
-
-extern FILE *log_file;
-
-extern ClassVector Classes;
-
-const long duration_m = 60;
-const long duration_h = duration_m * 60;
-const long duration_d = duration_h * 24;
-const long duration_w = duration_d * 7;
-const long duration_y = duration_w * 52;
-
-typedef nspace::hash_map<std::string, userrec*, nspace::hash<string>, irc::StrHashComp> user_hash;
-typedef nspace::hash_map<std::string, chanrec*, nspace::hash<string>, irc::StrHashComp> chan_hash;
-typedef nspace::hash_map<in_addr,string*, nspace::hash<in_addr>, irc::InAddr_HashComp> address_cache;
-typedef nspace::hash_map<std::string, WhoWasUser*, nspace::hash<string>, irc::StrHashComp> whowas_hash;
-typedef std::deque<command_t> command_table;
-
-
-extern user_hash clientlist;
-extern chan_hash chanlist;
-extern whowas_hash whowas;
-extern command_table cmdlist;
-extern file_cache MOTD;
-extern file_cache RULES;
-extern address_cache IP;
-
-extern std::vector<userrec*> all_opers;
-
-// This table references users by file descriptor.
-// its an array to make it VERY fast, as all lookups are referenced
-// by an integer, meaning there is no need for a scan/search operation.
-extern userrec* fd_ref_table[65536];
-
-extern int statsAccept,statsRefused,statsUnknown,statsCollisions,statsDns,statsDnsGood,statsDnsBad,statsConnects,statsSent,statsRecv;
-
-void handle_join(char **parameters, int pcnt, userrec *user)
-{
- chanrec* Ptr;
-
- if (loop_call(handle_join,parameters,pcnt,user,0,0,1))
- return;
- if (parameters[0][0] == '#')
- {
- Ptr = add_channel(user,parameters[0],parameters[1],false);
- }
-}
-
-
-void handle_part(char **parameters, int pcnt, userrec *user)
-{
- if (pcnt > 1)
- {
- if (loop_call(handle_part,parameters,pcnt,user,0,pcnt-2,0))
- return;
- del_channel(user,parameters[0],parameters[1],false);
- }
- else
- {
- if (loop_call(handle_part,parameters,pcnt,user,0,pcnt-1,0))
- return;
- del_channel(user,parameters[0],NULL,false);
- }
-}
-
-void handle_commands(char **parameters, int pcnt, userrec *user)
-{
- for (unsigned int i = 0; i < cmdlist.size(); i++)
- {
- WriteServ(user->fd,"902 %s :%s %s %d",user->nick,cmdlist[i].command,cmdlist[i].source,cmdlist[i].min_params);
- }
- WriteServ(user->fd,"903 %s :End of COMMANDS list",user->nick);
-}
-
-void handle_kick(char **parameters, int pcnt, userrec *user)
-{
- chanrec* Ptr = FindChan(parameters[0]);
- userrec* u = Find(parameters[1]);
-
- if ((!u) || (!Ptr))
- {
- WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
- return;
- }
-
- if (!has_channel(user,Ptr))
- {
- WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, parameters[0]);
- return;
- }
-
- char reason[MAXBUF];
-
- if (pcnt > 2)
- {
- strlcpy(reason,parameters[2],MAXBUF);
- if (strlen(reason)>MAXKICK)
- {
- reason[MAXKICK-1] = '\0';
- }
-
- kick_channel(user,u,Ptr,reason);
- }
- else
- {
- strlcpy(reason,user->nick,MAXBUF);
- kick_channel(user,u,Ptr,reason);
- }
-
-}
-
-void handle_loadmodule(char **parameters, int pcnt, userrec *user)
-{
- if (LoadModule(parameters[0]))
- {
- WriteOpers("*** NEW MODULE: %s",parameters[0]);
- WriteServ(user->fd,"975 %s %s :Module successfully loaded.",user->nick, parameters[0]);
- }
- else
- {
- WriteServ(user->fd,"974 %s %s :Failed to load module: %s",user->nick, parameters[0],ModuleError());
- }
-}
-
-void handle_unloadmodule(char **parameters, int pcnt, userrec *user)
-{
- if (UnloadModule(parameters[0]))
- {
- WriteOpers("*** MODULE UNLOADED: %s",parameters[0]);
- WriteServ(user->fd,"973 %s %s :Module successfully unloaded.",user->nick, parameters[0]);
- }
- else
- {
- WriteServ(user->fd,"972 %s %s :Failed to unload module: %s",user->nick, parameters[0],ModuleError());
- }
-}
-
-void handle_die(char **parameters, int pcnt, userrec *user)
-{
- 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);
- Exit(ERROR);
- }
- else
- {
- WriteOpers("*** Failed DIE Command from %s!%s@%s.",user->nick,user->ident,user->host);
- }
-}
-
-void handle_restart(char **parameters, int pcnt, userrec *user)
-{
- char *argv[32];
- log(DEFAULT,"Restart: %s",user->nick);
- if (!strcmp(parameters[0],restartpass))
- {
- WriteOpers("*** RESTART command from %s!%s@%s, restarting server.",user->nick,user->ident,user->host);
-
- argv[0] = MyExecutable;
- argv[1] = "-wait";
- if (nofork)
- {
- argv[2] = "-nofork";
- }
- else
- {
- argv[2] = NULL;
- }
- argv[3] = NULL;
-
- // close ALL file descriptors
- send_error("Server restarting.");
- sleep(1);
- for (int i = 0; i < 65536; i++)
- {
- shutdown(i,2);
- close(i);
- }
- sleep(2);
-
- execv(MyExecutable,argv);
-
- exit(0);
- }
- else
- {
- WriteOpers("*** Failed RESTART Command from %s!%s@%s.",user->nick,user->ident,user->host);
- }
-}
-
-void handle_kill(char **parameters, int pcnt, userrec *user)
-{
- userrec *u = Find(parameters[0]);
- char killreason[MAXBUF];
-
- log(DEBUG,"kill: %s %s",parameters[0],parameters[1]);
- if (u)
- {
- log(DEBUG,"into kill mechanism");
- int MOD_RESULT = 0;
- FOREACH_RESULT(OnKill(user,u,parameters[1]));
- if (MOD_RESULT) {
- log(DEBUG,"A module prevented the kill with result %d",MOD_RESULT);
- return;
- }
-
- if (strcmp(ServerName,u->server))
- {
- // remote kill
- WriteOpers("*** Remote kill by %s: %s!%s@%s (%s)",user->nick,u->nick,u->ident,u->host,parameters[1]);
- snprintf(killreason,MAXBUF,"[%s] Killed (%s (%s))",ServerName,user->nick,parameters[1]);
- WriteCommonExcept(u,"QUIT :%s",killreason);
-
- user_hash::iterator iter = clientlist.find(u->nick);
- if (iter != clientlist.end())
- {
- log(DEBUG,"deleting user hash value %d",iter->second);
- clientlist.erase(iter);
- }
- if (u->registered == 7)
- {
- purge_empty_chans(u);
- }
- if (u->fd > -1)
- fd_ref_table[u->fd] = NULL;
- delete u;
- }
- else
- {
- // local kill
- log(DEFAULT,"LOCAL KILL: %s :%s!%s!%s (%s)", u->nick, ServerName,user->dhost,user->nick,parameters[1]);
- WriteTo(user, u, "KILL %s :%s!%s!%s (%s)", u->nick, ServerName,user->dhost,user->nick,parameters[1]);
- WriteOpers("*** Local Kill by %s: %s!%s@%s (%s)",user->nick,u->nick,u->ident,u->host,parameters[1]);
- snprintf(killreason,MAXBUF,"Killed (%s (%s))",user->nick,parameters[1]);
- kill_link(u,killreason);
- }
- }
- else
- {
- WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
- }
-}
-
-void handle_summon(char **parameters, int pcnt, userrec *user)
-{
- WriteServ(user->fd,"445 %s :SUMMON has been disabled (depreciated command)",user->nick);
-}
-
-void handle_users(char **parameters, int pcnt, userrec *user)
-{
- WriteServ(user->fd,"445 %s :USERS has been disabled (depreciated command)",user->nick);
-}
-
-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;
- }
- strlcpy(user->password,parameters[0],MAXBUF);
- if (!strcasecmp(parameters[0],Passwd(user)))
- {
- user->haspassed = true;
- }
-}
-
-void handle_invite(char **parameters, int pcnt, userrec *user)
-{
- if (pcnt == 2)
- {
- userrec* u = Find(parameters[0]);
- chanrec* c = FindChan(parameters[1]);
-
- if ((!c) || (!u))
- {
- if (!c)
- {
- WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[1]);
- }
- else
- {
- if (c->binarymodes & CM_INVITEONLY)
- {
- WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
- }
- }
-
- return;
- }
-
- if (c->binarymodes & CM_INVITEONLY)
- {
- if (cstatus(user,c) < STATUS_HOP)
- {
- 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;
- }
- }
- 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;
- }
- if (!has_channel(user,c))
- {
- WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, c->name);
- return;
- }
-
- int MOD_RESULT = 0;
- FOREACH_RESULT(OnUserPreInvite(user,u,c));
- if (MOD_RESULT == 1) {
- 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);
-
- }
- else
- {
- // pinched from ircu - invite with not enough parameters shows channels
- // youve been invited to but haven't joined yet.
- InvitedList* il = user->GetInviteList();
- for (InvitedList::iterator i = il->begin(); i != il->end(); i++)
- {
- if (i->channel) {
- WriteServ(user->fd,"346 %s :%s",user->nick,i->channel);
- }
- }
- WriteServ(user->fd,"347 %s :End of INVITE list",user->nick);
- }
-}
-
-void handle_topic(char **parameters, int pcnt, userrec *user)
-{
- chanrec* Ptr;
-
- if (pcnt == 1)
- {
- if (strlen(parameters[0]) <= CHANMAX)
- {
- Ptr = FindChan(parameters[0]);
- if (Ptr)
- {
- if (((Ptr) && (!has_channel(user,Ptr))) && (Ptr->binarymodes & CM_SECRET))
- {
- WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, Ptr->name);
- return;
- }
- if (Ptr->topicset)
- {
- WriteServ(user->fd,"332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
- WriteServ(user->fd,"333 %s %s %s %d", user->nick, Ptr->name, Ptr->setby, Ptr->topicset);
- }
- else
- {
- WriteServ(user->fd,"331 %s %s :No topic is set.", user->nick, Ptr->name);
- }
- }
- else
- {
- WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
- }
- }
- return;
- }
- else if (pcnt>1)
- {
- if (strlen(parameters[0]) <= CHANMAX)
- {
- Ptr = FindChan(parameters[0]);
- if (Ptr)
- {
- if ((Ptr) && (!has_channel(user,Ptr)))
- {
- WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, Ptr->name);
- return;
- }
- if ((Ptr->binarymodes & CM_TOPICLOCK) && (cstatus(user,Ptr)<STATUS_HOP))
- {
- 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;
- }
-
- char topic[MAXBUF];
- strlcpy(topic,parameters[1],MAXBUF);
- if (strlen(topic)>MAXTOPIC)
- {
- topic[MAXTOPIC] = '\0';
- }
-
- if (!strcasecmp(user->server,ServerName))
- {
- int MOD_RESULT = 0;
- FOREACH_RESULT(OnLocalTopicChange(user,Ptr,topic));
- if (MOD_RESULT)
- return;
- }
-
- strlcpy(Ptr->topic,topic,MAXBUF);
- strlcpy(Ptr->setby,user->nick,NICKMAX);
- Ptr->topicset = TIME;
- WriteChannel(Ptr,user,"TOPIC %s :%s",Ptr->name, Ptr->topic);
-
- }
- else
- {
- WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
- }
- }
- }
-}
-
-void handle_names(char **parameters, int pcnt, userrec *user)
-{
- chanrec* c;
-
- if (!pcnt)
- {
- WriteServ(user->fd,"366 %s * :End of /NAMES list.",user->nick);
- return;
- }
-
- if (loop_call(handle_names,parameters,pcnt,user,0,pcnt-1,0))
- return;
- c = FindChan(parameters[0]);
- if (c)
- {
- if (((c) && (!has_channel(user,c))) && (c->binarymodes & CM_SECRET))
- {
- WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, c->name);
- return;
- }
- userlist(user,c);
- WriteServ(user->fd,"366 %s %s :End of /NAMES list.", user->nick, c->name);
- }
- else
- {
- WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
- }
-}
-
-void handle_privmsg(char **parameters, int pcnt, userrec *user)
-{
- userrec *dest;
- chanrec *chan;
-
- user->idle_lastmsg = TIME;
-
- if (loop_call(handle_privmsg,parameters,pcnt,user,0,pcnt-2,0))
- return;
- if (parameters[0][0] == '#')
- {
- chan = FindChan(parameters[0]);
- if (chan)
- {
- if ((chan->binarymodes & CM_NOEXTERNAL) && (!has_channel(user,chan)))
- {
- WriteServ(user->fd,"404 %s %s :Cannot send to channel (no external messages)", user->nick, chan->name);
- return;
- }
- if ((chan->binarymodes & CM_MODERATED) && (cstatus(user,chan)<STATUS_VOICE))
- {
- WriteServ(user->fd,"404 %s %s :Cannot send to channel (+m)", user->nick, chan->name);
- return;
- }
-
- int MOD_RESULT = 0;
-
- std::string temp = parameters[1];
- FOREACH_RESULT(OnUserPreMessage(user,chan,TYPE_CHANNEL,temp));
- if (MOD_RESULT) {
- return;
- }
- parameters[1] = (char*)temp.c_str();
-
- if (temp == "")
- {
- WriteServ(user->fd,"412 %s No text to send", user->nick);
- return;
- }
-
- ChanExceptSender(chan, user, "PRIVMSG %s :%s", chan->name, parameters[1]);
- FOREACH_MOD OnUserMessage(user,chan,TYPE_CHANNEL,parameters[1]);
- }
- else
- {
- /* no such nick/channel */
- WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
- }
- return;
- }
-
- log(DEBUG,"*** PRIVMSG HANDLER");
- dest = Find(parameters[0]);
- if (dest)
- {
- log(DEBUG,"*** FOUND NICK %s",dest->nick);
- if (strcmp(dest->awaymsg,""))
- {
- /* auto respond with aweh msg */
- WriteServ(user->fd,"301 %s %s :%s",user->nick,dest->nick,dest->awaymsg);
- }
-
- int MOD_RESULT = 0;
-
- std::string temp = parameters[1];
- FOREACH_RESULT(OnUserPreMessage(user,dest,TYPE_USER,temp));
- if (MOD_RESULT) {
- return;
- }
- parameters[1] = (char*)temp.c_str();
-
- if (!strcmp(dest->server,ServerName))
- {
- // direct write, same server
- log(DEBUG,"*** CALL WRITETO");
- WriteTo(user, dest, "PRIVMSG %s :%s", dest->nick, parameters[1]);
- }
-
- FOREACH_MOD OnUserMessage(user,dest,TYPE_USER,parameters[1]);
- }
- else
- {
- /* no such nick/channel */
- WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
- }
-}
-
-void handle_notice(char **parameters, int pcnt, userrec *user)
-{
- userrec *dest;
- chanrec *chan;
-
- user->idle_lastmsg = TIME;
-
- if (loop_call(handle_notice,parameters,pcnt,user,0,pcnt-2,0))
- return;
- if (parameters[0][0] == '#')
- {
- chan = FindChan(parameters[0]);
- if (chan)
- {
- if ((chan->binarymodes & CM_NOEXTERNAL) && (!has_channel(user,chan)))
- {
- WriteServ(user->fd,"404 %s %s :Cannot send to channel (no external messages)", user->nick, chan->name);
- return;
- }
- if ((chan->binarymodes & CM_MODERATED) && (cstatus(user,chan)<STATUS_VOICE))
- {
- WriteServ(user->fd,"404 %s %s :Cannot send to channel (+m)", user->nick, chan->name);
- return;
- }
-
- int MOD_RESULT = 0;
-
- std::string temp = parameters[1];
- FOREACH_RESULT(OnUserPreNotice(user,chan,TYPE_CHANNEL,temp));
- if (MOD_RESULT) {
- return;
- }
- parameters[1] = (char*)temp.c_str();
-
- if (temp == "")
- {
- WriteServ(user->fd,"412 %s No text to send", user->nick);
- return;
- }
-
- ChanExceptSender(chan, user, "NOTICE %s :%s", chan->name, parameters[1]);
-
- FOREACH_MOD OnUserNotice(user,chan,TYPE_CHANNEL,parameters[1]);
- }
- else
- {
- /* no such nick/channel */
- WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
- }
- return;
- }
-
- dest = Find(parameters[0]);
- if (dest)
- {
- int MOD_RESULT = 0;
-
- std::string temp = parameters[1];
- FOREACH_RESULT(OnUserPreNotice(user,dest,TYPE_USER,temp));
- if (MOD_RESULT) {
- return;
- }
- parameters[1] = (char*)temp.c_str();
-
- if (!strcmp(dest->server,ServerName))
- {
- // direct write, same server
- WriteTo(user, dest, "NOTICE %s :%s", dest->nick, parameters[1]);
- }
-
- FOREACH_MOD OnUserNotice(user,dest,TYPE_USER,parameters[1]);
- }
- else
- {
- /* no such nick/channel */
- WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
- }
-}
-
-void handle_server(char **parameters, int pcnt, userrec *user)
-{
- WriteServ(user->fd,"666 %s :You cannot identify as a server, you are a USER. IRC Operators informed.",user->nick);
- WriteOpers("*** WARNING: %s attempted to issue a SERVER command and is registered as a user!",user->nick);
-}
-
-void handle_info(char **parameters, int pcnt, userrec *user)
-{
- WriteServ(user->fd,"371 %s :. o O (The Inspire Internet Relay Chat Server) O o .",user->nick);
- WriteServ(user->fd,"371 %s : ",user->nick);
- WriteServ(user->fd,"371 %s :Core developers: Craig Edwards (Brain)",user->nick);
- WriteServ(user->fd,"371 %s : Craig McLure",user->nick);
- WriteServ(user->fd,"371 %s : ",user->nick);
- WriteServ(user->fd,"371 %s :Contributors: typobox43",user->nick);
- WriteServ(user->fd,"371 %s : w00t",user->nick);
- WriteServ(user->fd,"371 %s : Om",user->nick);
- WriteServ(user->fd,"371 %s : Jazza",user->nick);
- WriteServ(user->fd,"371 %s : ",user->nick);
- WriteServ(user->fd,"371 %s :Testers: CC",user->nick);
- WriteServ(user->fd,"371 %s : Piggles",user->nick);
- WriteServ(user->fd,"371 %s : Foamy",user->nick);
- WriteServ(user->fd,"371 %s : Hart",user->nick);
- WriteServ(user->fd,"371 %s : RageD",user->nick);
- WriteServ(user->fd,"371 %s : [ed]",user->nick);
- WriteServ(user->fd,"371 %s : Azhrarn",user->nick);
- WriteServ(user->fd,"371 %s : nenolod",user->nick);
- WriteServ(user->fd,"371 %s : luigiman",user->nick);
- WriteServ(user->fd,"371 %s : Chu",user->nick);
- WriteServ(user->fd,"371 %s : aquanight",user->nick);
- WriteServ(user->fd,"371 %s : xptek",user->nick);
- WriteServ(user->fd,"371 %s : Grantlinks",user->nick);
- WriteServ(user->fd,"371 %s : Rob",user->nick);
- WriteServ(user->fd,"371 %s : angelic",user->nick);
- WriteServ(user->fd,"371 %s : Jason",user->nick);
- WriteServ(user->fd,"371 %s : ",user->nick);
- WriteServ(user->fd,"371 %s :Thanks to irc-junkie and searchirc",user->nick);
- WriteServ(user->fd,"371 %s :for the nice comments and the help",user->nick);
- WriteServ(user->fd,"371 %s :you gave us in attracting users to",user->nick);
- WriteServ(user->fd,"371 %s :this software.",user->nick);
- WriteServ(user->fd,"371 %s : ",user->nick);
- WriteServ(user->fd,"371 %s :Best experienced with: An IRC client.",user->nick);
- FOREACH_MOD OnInfo(user);
- WriteServ(user->fd,"374 %s :End of /INFO list",user->nick);
-}
-
-void handle_time(char **parameters, int pcnt, userrec *user)
-{
- time_t rawtime;
- struct tm * timeinfo;
-
- time ( &rawtime );
- timeinfo = localtime ( &rawtime );
- WriteServ(user->fd,"391 %s %s :%s",user->nick,ServerName, asctime (timeinfo) );
-
-}
-
-void handle_whois(char **parameters, int pcnt, userrec *user)
-{
- userrec *dest;
-
- if (loop_call(handle_whois,parameters,pcnt,user,0,pcnt-1,0))
- return;
- dest = Find(parameters[0]);
- if (dest)
- {
- // 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,"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 %s",user->nick, dest->nick, dest->host, dest->ip);
- }
- char* cl = chlist(dest,user);
- if (strcmp(cl,""))
- {
- WriteServ(user->fd,"319 %s %s :%s",user->nick, dest->nick, cl);
- }
- WriteServ(user->fd,"312 %s %s %s :%s",user->nick, dest->nick, dest->server, GetServerDescription(dest->server).c_str());
- if (strcmp(dest->awaymsg,""))
- {
- WriteServ(user->fd,"301 %s %s :%s",user->nick, dest->nick, dest->awaymsg);
- }
- if ((strchr(dest->modes,'o')) && (strcmp(dest->oper,"")))
- {
- WriteServ(user->fd,"313 %s %s :is %s %s on %s",user->nick, dest->nick,
- (strchr("aeiou",dest->oper[0]) ? "an" : "a"),dest->oper, Network);
- }
- FOREACH_MOD OnWhois(user,dest);
- if (!strcasecmp(user->server,dest->server))
- {
- // idle time and signon line can only be sent if youre on the same server (according to RFC)
- WriteServ(user->fd,"317 %s %s %d %d :seconds idle, signon time",user->nick, dest->nick, abs((dest->idle_lastmsg)-TIME), dest->signon);
- }
-
- WriteServ(user->fd,"318 %s %s :End of /WHOIS list.",user->nick, dest->nick);
- }
- else
- {
- WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
- WriteServ(user->fd,"318 %s %s :End of /WHOIS list.",user->nick, parameters[0]);
- }
- }
- else
- {
- /* no such nick/channel */
- WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
- WriteServ(user->fd,"318 %s %s :End of /WHOIS list.",user->nick, parameters[0]);
- }
-}
-
-void handle_quit(char **parameters, int pcnt, userrec *user)
-{
- user_hash::iterator iter = clientlist.find(user->nick);
- char* reason;
-
- if (user->registered == 7)
- {
- /* theres more to do here, but for now just close the socket */
- if (pcnt == 1)
- {
- if (parameters[0][0] == ':')
- {
- *parameters[0]++;
- }
- reason = parameters[0];
-
- if (strlen(reason)>MAXQUIT)
- {
- reason[MAXQUIT-1] = '\0';
- }
-
- Write(user->fd,"ERROR :Closing link (%s@%s) [%s%s]",user->ident,user->host,PrefixQuit,parameters[0]);
- WriteOpers("*** Client exiting: %s!%s@%s [%s%s]",user->nick,user->ident,user->host,PrefixQuit,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);
- }
-
- FOREACH_MOD OnUserDisconnect(user);
-
- /* push the socket on a stack of sockets due to be closed at the next opportunity */
- if (user->fd > -1)
- {
-#ifdef USE_KQUEUE
- struct kevent ke;
- EV_SET(&ke, user->fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
- int i = kevent(kq, &ke, 1, 0, 0, NULL);
- if (i == -1)
- {
- log(DEBUG,"kqueue: Failed to remove user from queue!");
- }
-#endif
-#ifdef USE_EPOLL
- struct epoll_event ev;
- ev.events = EPOLLIN | EPOLLET;
- ev.data.fd = user->fd;
- int i = epoll_ctl(ep, EPOLL_CTL_DEL, user->fd, &ev);
- if (i < 0)
- {
- log(DEBUG,"epoll: List deletion failure!");
- }
-#endif
- user->CloseSocket();
- }
-
- if (iter != clientlist.end())
- {
- clientlist.erase(iter);
- }
-
- if (user->registered == 7) {
- purge_empty_chans(user);
- }
- if (user->fd > -1)
- fd_ref_table[user->fd] = NULL;
- delete user;
-}
-
-void handle_who(char **parameters, int pcnt, userrec *user)
-{
- chanrec* Ptr = NULL;
- char tmp[10];
-
- /* theres more to do here, but for now just close the socket */
- if (pcnt == 1)
- {
- if ((!strcmp(parameters[0],"0")) || (!strcmp(parameters[0],"*")))
- {
- if (user->chans[0].channel)
- {
- int n_list = 0;
- for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
- {
- Ptr = i->second->chans[0].channel;
- // suggested by phidjit and FCS
- if ((!common_channels(user,i->second)) && (isnick(i->second->nick)))
- {
- // Bug Fix #29
- strcpy(tmp, "");
- if (strcmp(i->second->awaymsg, "")) {
- strlcat(tmp, "G", 9);
- } else {
- strlcat(tmp, "H", 9);
- }
- if (strchr(i->second->modes,'o')) { strlcat(tmp, "*", 9); }
- WriteServ(user->fd,"352 %s %s %s %s %s %s %s :0 %s",user->nick, Ptr ? Ptr->name : "*", i->second->ident, i->second->dhost, i->second->server, i->second->nick, tmp, i->second->fullname);
- n_list++;
- if (n_list > MaxWhoResults)
- {
- WriteServ(user->fd,"523 %s WHO :Command aborted: More results than configured limit",user->nick);
- break;
- }
- }
- }
- }
- if (Ptr)
- {
- WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick , parameters[0]);
- }
- else
- {
- WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, parameters[0]);
- }
- return;
- }
- if (parameters[0][0] == '#')
- {
- Ptr = FindChan(parameters[0]);
- if (Ptr)
- {
- int n_list = 0;
- for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
- {
- if ((has_channel(i->second,Ptr)) && (isnick(i->second->nick)))
- {
- // Fix Bug #29 - Part 2..
- strcpy(tmp, "");
- if (strcmp(i->second->awaymsg, "")) {
- strlcat(tmp, "G", 9);
- } else {
- strlcat(tmp, "H", 9);
- }
- if (strchr(i->second->modes,'o')) { strlcat(tmp, "*", 9); }
- strlcat(tmp, cmode(i->second, Ptr),5);
- WriteServ(user->fd,"352 %s %s %s %s %s %s %s :0 %s",user->nick, Ptr->name, i->second->ident, i->second->dhost, i->second->server, i->second->nick, tmp, i->second->fullname);
- n_list++;
- if (n_list > MaxWhoResults)
- {
- WriteServ(user->fd,"523 %s WHO :Command aborted: More results than configured limit",user->nick);
- break;
- }
-
- }
- }
- WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, parameters[0]);
- }
- else
- {
- WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
- }
- }
- else
- {
- userrec* u = Find(parameters[0]);
- if (u)
- {
- // Bug Fix #29 -- Part 29..
- strcpy(tmp, "");
- if (strcmp(u->awaymsg, "")) {
- strlcat(tmp, "G" ,9);
- } else {
- strlcat(tmp, "H" ,9);
- }
- if (strchr(u->modes,'o')) { strlcat(tmp, "*" ,9); }
- WriteServ(user->fd,"352 %s %s %s %s %s %s %s :0 %s",user->nick, u->chans[0].channel ? u->chans[0].channel->name
- : "*", u->ident, u->dhost, u->server, u->nick, tmp, u->fullname);
- }
- WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, parameters[0]);
- }
- }
- if (pcnt == 2)
- {
- if ((!strcmp(parameters[0],"0")) || (!strcmp(parameters[0],"*")) && (!strcmp(parameters[1],"o")))
- {
- for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
- {
- // If i were a rich man.. I wouldn't need to me making these bugfixes..
- // But i'm a poor bastard with nothing better to do.
- userrec* oper = *i;
- strcpy(tmp, "");
- if (strcmp(oper->awaymsg, "")) {
- strlcat(tmp, "G" ,9);
- } else {
- strlcat(tmp, "H" ,9);
- }
- WriteServ(user->fd,"352 %s %s %s %s %s %s %s* :0 %s", user->nick, oper->chans[0].channel ? oper->chans[0].channel->name
- : "*", oper->ident, oper->dhost, oper->server, oper->nick, tmp, oper->fullname);
- }
- WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, parameters[0]);
- return;
- }
- }
-}