]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/commands.cpp
/LOADMODULE and /UNLOADMODULE all successfully working!
[user/henk/code/inspircd.git] / src / commands.cpp
index 0173f91130799cbd4e3153561983ae8fceddce66..19dd9f200a070cf9be2106efa08a6229fd31c377 100644 (file)
@@ -50,6 +50,8 @@
 #include "message.h"
 #include "mode.h"
 #include "xline.h"
+#include "inspstring.h"
+#include "dnsqueue.h"
 
 #ifdef GCC3
 #define nspace __gnu_cxx
@@ -89,6 +91,8 @@ extern time_t nb_start;
 
 extern bool nofork;
 
+extern time_t TIME;
+
 extern std::vector<int> fd_reap;
 extern std::vector<std::string> module_names;
 
@@ -266,6 +270,31 @@ void handle_kick(char **parameters, int pcnt, userrec *user)
        NetSendToAll(buffer);
 }
 
+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)
 {
@@ -333,10 +362,16 @@ 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)
        {
+               int MOD_RESULT = 0;
+                FOREACH_RESULT(OnKill(user,u,parameters[1]));
+                if (MOD_RESULT) {
+                        return;
+                }
+
                if (strcmp(ServerName,u->server))
                {
                        // remote kill
@@ -466,6 +501,11 @@ void handle_topic(char **parameters, int pcnt, userrec *user)
                        Ptr = FindChan(parameters[0]);
                        if (Ptr)
                        {
+                               if (((Ptr) && (!has_channel(user,Ptr))) && (Ptr->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);
@@ -490,6 +530,11 @@ void handle_topic(char **parameters, int pcnt, userrec *user)
                        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->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);
@@ -505,7 +550,7 @@ void handle_topic(char **parameters, int pcnt, userrec *user)
                                        
                                strlcpy(Ptr->topic,topic,MAXBUF);
                                strlcpy(Ptr->setby,user->nick,NICKMAX);
-                               Ptr->topicset = time(NULL);
+                               Ptr->topicset = TIME;
                                WriteChannel(Ptr,user,"TOPIC %s :%s",Ptr->name, Ptr->topic);
 
                                // t token must go to ALL servers!!!
@@ -530,7 +575,11 @@ void handle_names(char **parameters, int pcnt, userrec *user)
        c = FindChan(parameters[0]);
        if (c)
        {
-               /*WriteServ(user->fd,"353 %s = %s :%s", user->nick, c->name,*/
+                if (((c) && (!has_channel(user,c))) && (c->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);
        }
@@ -545,7 +594,7 @@ void handle_privmsg(char **parameters, int pcnt, userrec *user)
        userrec *dest;
        chanrec *chan;
 
-       user->idle_lastmsg = time(NULL);
+       user->idle_lastmsg = TIME;
        
        if (loop_call(handle_privmsg,parameters,pcnt,user,0,pcnt-2,0))
                return;
@@ -633,7 +682,7 @@ void handle_notice(char **parameters, int pcnt, userrec *user)
        userrec *dest;
        chanrec *chan;
 
-       user->idle_lastmsg = time(NULL);
+       user->idle_lastmsg = TIME;
        
        if (loop_call(handle_notice,parameters,pcnt,user,0,pcnt-2,0))
                return;
@@ -761,7 +810,7 @@ void handle_whois(char **parameters, int pcnt, userrec *user)
                        {
                                WriteServ(user->fd,"301 %s %s :%s",user->nick, dest->nick, dest->awaymsg);
                        }
-                       if (strchr(dest->modes,'o'))
+                       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);
@@ -770,7 +819,7 @@ void handle_whois(char **parameters, int pcnt, userrec *user)
                        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(NULL)), dest->signon);
+                               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);
@@ -883,11 +932,11 @@ void handle_who(char **parameters, int pcnt, userrec *user)
                        }
                        if (Ptr)
                        {
-                               WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, Ptr->name);
+                               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, user->nick);
+                               WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, parameters[0]);
                        }
                        return;
                }
@@ -912,7 +961,7 @@ void handle_who(char **parameters, int pcnt, userrec *user)
                                                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);
                                        }
                                }
-                               WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, Ptr->name);
+                               WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, parameters[0]);
                        }
                        else
                        {
@@ -960,7 +1009,7 @@ void handle_who(char **parameters, int pcnt, userrec *user)
                                         }
                                 }
                         }
-                        WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, user->nick);
+                        WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, parameters[0]);
                         return;
                 }
        }
@@ -1243,7 +1292,7 @@ void handle_stats(char **parameters, int pcnt, userrec *user)
                {
                        if (strchr(i->second->modes,'o'))
                        {
-                               WriteServ(user->fd,"249 %s :%s (%s@%s) Idle: %d",user->nick,i->second->nick,i->second->ident,i->second->dhost,(time(NULL) - i->second->idle_lastmsg));
+                               WriteServ(user->fd,"249 %s :%s (%s@%s) Idle: %d",user->nick,i->second->nick,i->second->ident,i->second->dhost,(TIME - i->second->idle_lastmsg));
                                idx++;
                        }
                }
@@ -1271,6 +1320,11 @@ void handle_stats(char **parameters, int pcnt, userrec *user)
                stats_z(user);
        }
 
+       if (!strcmp(parameters[0],"e"))
+       {
+               stats_e(user);
+       }
+
        /* stats m (list number of times each command has been used, plus bytecount) */
        if (!strcmp(parameters[0],"m"))
        {
@@ -1338,7 +1392,7 @@ void handle_stats(char **parameters, int pcnt, userrec *user)
        if (!strcmp(parameters[0],"u"))
        {
                time_t current_time = 0;
-               current_time = time(NULL);
+               current_time = TIME;
                time_t server_uptime = current_time - startup_time;
                struct tm* stime;
                stime = gmtime(&server_uptime);
@@ -1444,7 +1498,7 @@ void handle_map(char **parameters, int pcnt, userrec *user)
        snprintf(line,MAXBUF,"006 %s :%s",user->nick,ServerName);
        while (strlen(line) < 50)
                strcat(line," ");
-       WriteServ(user->fd,"%s%d (%.2f%%)",line,local_count(),(float)(((float)local_count()/(float)usercnt())*100));
+       WriteServ(user->fd,"%s%d (%.2f%%)",line,local_count(),(float)(((float)local_count()/(float)registered_usercount())*100));
        for (int j = 0; j < 32; j++)
        {
                if (me[j] != NULL)
@@ -1454,7 +1508,7 @@ void handle_map(char **parameters, int pcnt, userrec *user)
                                snprintf(line,MAXBUF,"006 %s :%c-%s",user->nick,islast(me[j]->connectors[k].GetServerName().c_str()),me[j]->connectors[k].GetServerName().c_str());
                                while (strlen(line) < 50)
                                        strcat(line," ");
-                               WriteServ(user->fd,"%s%d (%.2f%%)",line,map_count(me[j]->connectors[k].GetServerName().c_str()),(float)(((float)map_count(me[j]->connectors[k].GetServerName().c_str())/(float)usercnt())*100));
+                               WriteServ(user->fd,"%s%d (%.2f%%)",line,map_count(me[j]->connectors[k].GetServerName().c_str()),(float)(((float)map_count(me[j]->connectors[k].GetServerName().c_str())/(float)registered_usercount())*100));
                        }
                }
        }
@@ -1484,47 +1538,76 @@ void handle_oper(char **parameters, int pcnt, userrec *user)
        char Password[MAXBUF];
        char OperType[MAXBUF];
        char TypeName[MAXBUF];
-       char Hostname[MAXBUF];
+       char HostName[MAXBUF];
+       char TheHost[MAXBUF];
        int i,j;
+       bool found = false;
+       bool fail2 = false;
+       char global[MAXBUF];
+
+       snprintf(TheHost,MAXBUF,"%s@%s",user->ident,user->host);
 
        for (int i = 0; i < ConfValueEnum("oper",&config_f); i++)
        {
                ConfValue("oper","name",i,LoginName,&config_f);
                ConfValue("oper","password",i,Password,&config_f);
-               if ((!strcmp(LoginName,parameters[0])) && (!strcmp(Password,parameters[1])))
+               ConfValue("oper","type",i,OperType,&config_f);
+               ConfValue("oper","host",i,HostName,&config_f);
+               if ((!strcmp(LoginName,parameters[0])) && (!strcmp(Password,parameters[1])) && (match(TheHost,HostName)))
                {
-                       /* correct oper credentials */
-                       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);
-                       char global[MAXBUF];
-                       snprintf(global,MAXBUF,"M %s +o",user->nick);
-                       NetSendToAll(global);
+                       fail2 = true;
                        for (j =0; j < ConfValueEnum("type",&config_f); j++)
                        {
                                ConfValue("type","name",j,TypeName,&config_f);
+
                                if (!strcmp(TypeName,OperType))
                                {
                                        /* found this oper's opertype */
                                        snprintf(global,MAXBUF,"| %s %s",user->nick,TypeName);
                                        NetSendToAll(global);
-                                       ConfValue("type","host",j,Hostname,&config_f);
-                                       ChangeDisplayedHost(user,Hostname);
+                                       ConfValue("type","host",j,HostName,&config_f);
+                                       ChangeDisplayedHost(user,HostName);
                                        strlcpy(user->oper,TypeName,NICKMAX);
+                                       found = true;
+                                       fail2 = false;
+                                       break;
                                }
                        }
-                       if (!strchr(user->modes,'o'))
-                       {
-                               strcat(user->modes,"o");
-                       }
+               }
+               if (found)
+                       break;
+       }
+       if (found)
+       {
+                /* correct oper credentials */
+                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);
+               if (!strchr(user->modes,'o'))
+               {
+                       strcat(user->modes,"o");
+                       WriteServ(user->fd,"MODE %s :+o",user->nick);
+                        snprintf(global,MAXBUF,"M %s +o",user->nick);
+                        NetSendToAll(global);
                        FOREACH_MOD OnOper(user);
-                       return;
+                       log(DEFAULT,"OPER: %s!%s@%s opered as type: %s",user->nick,user->ident,user->host,OperType);
+               }
+       }
+       else
+       {
+               if (!fail2)
+               {
+                       WriteServ(user->fd,"491 %s :Invalid oper credentials",user->nick);
+                       WriteOpers("*** WARNING! qFailed oper attempt by %s!%s@%s!",user->nick,user->ident,user->host);
+                       log(DEFAULT,"OPER: Failed oper attempt by %s!%s@%s: user, host or password did not match.",user->nick,user->ident,user->host);
+               }
+               else
+               {
+                       WriteServ(user->fd,"491 %s :Your oper block does not have a valid opertype associated with it",user->nick);
+                       WriteOpers("*** CONFIGURATION ERROR! Oper block mismatch for OperType %s",OperType);
+                        log(DEFAULT,"OPER: Failed oper attempt by %s!%s@%s: credentials valid, but oper type nonexistent.",user->nick,user->ident,user->host);
                }
        }
-       /* no such oper */
-       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);
+       return;
 }
 
 void handle_nick(char **parameters, int pcnt, userrec *user)
@@ -1601,9 +1684,11 @@ void handle_nick(char **parameters, int pcnt, userrec *user)
                char buffer[MAXBUF];
                snprintf(buffer,MAXBUF,"n %s %s",user->nick,parameters[0]);
                NetSendToAll(buffer);
-               
        }
        
+       char oldnick[NICKMAX];
+       strlcpy(oldnick,user->nick,NICKMAX);
+
        /* change the nick of the user in the users_hash */
        user = ReHashNick(user->nick, parameters[0]);
        /* actually change the nick within the record */
@@ -1615,12 +1700,23 @@ void handle_nick(char **parameters, int pcnt, userrec *user)
        log(DEBUG,"new nick set: %s",user->nick);
        
        if (user->registered < 3)
+       {
                user->registered = (user->registered | 2);
+               // dont attempt to look up the dns until they pick a nick... because otherwise their pointer WILL change
+               // and unless we're lucky we'll get a duff one later on.
+               user->dns_done = (!lookup_dns(user->nick));
+               if (user->dns_done)
+                       log(DEBUG,"Aborting dns lookup of %s because dns server experienced a failure.",user->nick);
+       }
        if (user->registered == 3)
        {
                /* user is registered now, bit 0 = USER command, bit 1 = sent a NICK command */
                ConnectUser(user);
        }
+       if (user->registered == 7)
+       {
+               FOREACH_MOD OnUserPostNick(user,oldnick);
+       }
 }
 
 
@@ -1710,7 +1806,7 @@ void handle_t(char token,char* params,serverrec* source,serverrec* reply, char*
                WriteChannelLocal(c,u,"TOPIC %s :%s",c->name,topic);
                strlcpy(c->topic,topic,MAXTOPIC);
                strlcpy(c->setby,u->nick,NICKMAX);
-               c->topicset = time(NULL);
+               c->topicset = TIME;
        }       
 }
        
@@ -2014,7 +2110,7 @@ void handle_N(char token,char* params,serverrec* source,serverrec* reply, char*
        clientlist[nick]->lastping = 1;
        clientlist[nick]->port = 0; // so is this...
        clientlist[nick]->registered = 7; // this however we need to set for them to receive messages and appear online
-       clientlist[nick]->idle_lastmsg = time(NULL); // this is unrealiable and wont actually be used locally
+       clientlist[nick]->idle_lastmsg = TIME; // this is unrealiable and wont actually be used locally
        for (int i = 0; i < MAXCHANS; i++)
        {
                clientlist[nick]->chans[i].channel = NULL;
@@ -2024,7 +2120,7 @@ void handle_N(char token,char* params,serverrec* source,serverrec* reply, char*
 
 void handle_F(char token,char* params,serverrec* source,serverrec* reply, char* tcp_host)
 {
-       long tdiff = time(NULL) - atoi(params);
+       long tdiff = TIME - atoi(params);
        if (tdiff)
                WriteOpers("TS split for %s -> %s: %d",source->name,reply->name,tdiff);
 }
@@ -2376,7 +2472,7 @@ void process_restricted_commands(char token,char* params,serverrec* source,serve
                // Y <TS>
                // start netburst
                case 'Y':
-                       nb_start = time(NULL);
+                       nb_start = TIME;
                        WriteOpers("Server %s is starting netburst.",tcp_host);
                        // now broadcast this new servers address out to all servers that are linked to us,
                        // except the newcomer. They'll all attempt to connect back to it.
@@ -2554,7 +2650,7 @@ void process_restricted_commands(char token,char* params,serverrec* source,serve
                // F <TS>
                // end netburst
                case 'F':
-                       WriteOpers("Server %s has completed netburst. (%d secs)",tcp_host,time(NULL)-nb_start);
+                       WriteOpers("Server %s has completed netburst. (%d secs)",tcp_host,TIME-nb_start);
                        handle_F(token,params,source,reply,tcp_host);
                        nb_start = 0;
                        // tell all the other servers to use this authcookie to connect back again
@@ -2569,7 +2665,7 @@ void process_restricted_commands(char token,char* params,serverrec* source,serve
                // F <TS>
                // end netburst with no mesh creation
                case 'f':
-                       WriteOpers("Server %s has completed netburst. (%d secs)",tcp_host,time(NULL)-nb_start);
+                       WriteOpers("Server %s has completed netburst. (%d secs)",tcp_host,TIME-nb_start);
                        handle_F(token,params,source,reply,tcp_host);
                        nb_start = 0;
                        // tell everyone else about the new server name so they just add it in the disconnected
@@ -2824,7 +2920,7 @@ void handle_link_packet(char* udp_msg, char* tcp_host, serverrec *serv)
                        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_ServerName,servername)) && (!strcmp(Link_Pass,password)))
                        {
                                // we have a matching link line -
                                // send a 'diminutive' server message back...
@@ -2948,7 +3044,7 @@ void handle_link_packet(char* udp_msg, char* tcp_host, serverrec *serv)
                        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_ServerName,servername)) && (!strcmp(Link_Pass,password)))
                        {
                                // matching link at this end too, we're all done!
                                // at this point we must begin key exchange and insert this
@@ -3109,6 +3205,34 @@ void handle_kline(char **parameters, int pcnt, userrec *user)
        apply_lines();
 }
 
+void handle_eline(char **parameters, int pcnt, userrec *user)
+{
+        if (pcnt >= 3)
+        {
+                add_eline(duration(parameters[1]),user->nick,parameters[2],parameters[0]);
+                if (!duration(parameters[1]))
+                {
+                        WriteOpers("*** %s added permenant E-line for %s.",user->nick,parameters[0]);
+                }
+                else
+                {
+                        WriteOpers("*** %s added timed E-line for %s, expires in %d seconds.",user->nick,parameters[0],duration(parameters[1]));
+                }
+        }
+        else
+        {
+                if (del_eline(parameters[0]))
+                {
+                        WriteOpers("*** %s Removed E-line on %s.",user->nick,parameters[0]);
+                }
+                else
+                {
+                        WriteServ(user->fd,"NOTICE %s :*** E-Line %s not found in list, try /stats e.",user->nick,parameters[0]);
+                }
+        }
+       // no need to apply the lines for an eline
+}
+
 void handle_gline(char **parameters, int pcnt, userrec *user)
 {
        char netdata[MAXBUF];
@@ -3116,7 +3240,7 @@ void handle_gline(char **parameters, int pcnt, userrec *user)
        {
                add_gline(duration(parameters[1]),user->nick,parameters[2],parameters[0]);
                // # <mask> <who-set-it> <time-set> <duration> :<reason>
-               snprintf(netdata,MAXBUF,"# %s %s %ld %ld :%s",parameters[0],user->nick,time(NULL),duration(parameters[1]),parameters[2]);
+               snprintf(netdata,MAXBUF,"# %s %s %ld %ld :%s",parameters[0],user->nick,TIME,duration(parameters[1]),parameters[2]);
                NetSendToAll(netdata);
                if (!duration(parameters[1]))
                {