]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/users.cpp
Added basic sendq stuff - WARNING, there is no configuration yet, this CVS allows...
[user/henk/code/inspircd.git] / src / users.cpp
index 6fb0e6a5c7b0e3752e52c6b1c42c386acbe93581..0e676600edb5b6571a67375644b3e844d7419228 100644 (file)
@@ -1,12 +1,29 @@
-/*
-
-*/
+/*       +------------------------------------+
+ *       | Inspire Internet Relay Chat Daemon |
+ *       +------------------------------------+
+ *
+ *  Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
+ *                       E-mail:
+ *                <brain@chatspike.net>
+ *               <Craig@chatspike.net>
+ *     
+ * Written by Craig Edwards, Craig McLure, and others.
+ * This program is free but copyrighted software; see
+ *            the file COPYING for details.
+ *
+ * ---------------------------------------------------
+ */
 
 #include "inspircd_config.h" 
 #include "channels.h"
 #include "users.h"
 #include "inspircd.h"
 #include <stdio.h>
+#include "inspstring.h"
+
+extern std::stringstream config_f;
+
+extern time_t TIME;
 
 userrec::userrec()
 {
@@ -22,9 +39,15 @@ userrec::userrec()
        strcpy(inbuf,"");
        strcpy(server,"");
        strcpy(awaymsg,"");
+       strcpy(oper,"");
+       reset_due = TIME;
+       lines_in = 0;
        fd = lastping = signon = idle_lastmsg = nping = registered = 0;
        flood = port = bytes_in = bytes_out = cmds_in = cmds_out = 0;
        haspassed = false;
+       dns_done = false;
+       recvq = "";
+       sendq = "";
        strcpy(result,"");
        for (int i = 0; i < MAXCHANS; i++)
        {
@@ -38,14 +61,14 @@ userrec::userrec()
  
 char* userrec::GetFullHost()
 {
-       sprintf(result,"%s!%s@%s",nick,ident,dhost);
+       snprintf(result,MAXBUF,"%s!%s@%s",nick,ident,dhost);
        return result;
 }
 
 
 char* userrec::GetFullRealHost()
 {
-       sprintf(result,"%s!%s@%s",nick,ident,host);
+       snprintf(result,MAXBUF,"%s!%s@%s",nick,ident,host);
        return result;
 }
 
@@ -66,7 +89,7 @@ bool userrec::IsInvited(char* channel)
 void userrec::InviteTo(char* channel)
 {
        Invited i;
-       strcpy(i.channel,channel);
+       strlcpy(i.channel,channel,CHANMAX);
        invites.push_back(i);
 }
 
@@ -91,3 +114,149 @@ void userrec::RemoveInvite(char* channel)
                }
         }
 }
+
+bool userrec::HasPermission(char* command)
+{
+       char TypeName[MAXBUF],Classes[MAXBUF],ClassName[MAXBUF],CommandList[MAXBUF];
+       char* mycmd;
+       char* savept;
+       char* savept2;
+       
+       // are they even an oper at all?
+       if (strchr(this->modes,'o'))
+       {
+               log(DEBUG,"*** HasPermission: %s is an oper",this->nick);
+               for (int j =0; j < ConfValueEnum("type",&config_f); j++)
+               {
+                       ConfValue("type","name",j,TypeName,&config_f);
+                       if (!strcmp(TypeName,this->oper))
+                       {
+                               log(DEBUG,"*** HasPermission: %s is an oper of type '%s'",this->nick,this->oper);
+                               ConfValue("type","classes",j,Classes,&config_f);
+                               char* myclass = strtok_r(Classes," ",&savept);
+                               while (myclass)
+                               {
+                                       log(DEBUG,"*** HasPermission: checking classtype '%s'",myclass);
+                                       for (int k =0; k < ConfValueEnum("class",&config_f); k++)
+                                       {
+                                               ConfValue("class","name",k,ClassName,&config_f);
+                                               if (!strcmp(ClassName,myclass))
+                                               {
+                                                       ConfValue("class","commands",k,CommandList,&config_f);
+                                                       log(DEBUG,"*** HasPermission: found class named %s with commands: '%s'",ClassName,CommandList);
+                                                       
+                                                       
+                                                       mycmd = strtok_r(CommandList," ",&savept2);
+                                                       while (mycmd)
+                                                       {
+                                                               if (!strcasecmp(mycmd,command))
+                                                               {
+                                                                       log(DEBUG,"*** Command %s found, returning true",command);
+                                                                       return true;
+                                                               }
+                                                               mycmd = strtok_r(NULL," ",&savept2);
+                                                       }
+                                               }
+                                       }
+                                       myclass = strtok_r(NULL," ",&savept);
+                               }
+                       }
+               }
+       }
+       return false;
+}
+
+
+bool userrec::AddBuffer(std::string a)
+{
+        std::string b = "";
+        for (int i = 0; i < a.length(); i++)
+                if ((a[i] != '\r') && (a[i] != '\0') && (a[i] != 7))
+                        b = b + a[i];
+        std::stringstream stream(recvq);
+        stream << b;
+        recvq = stream.str();
+       int i = 0;
+       // count the size of the first line in the buffer.
+       while (i < recvq.length())
+       {
+               if (recvq[i++] == '\n')
+                       break;
+       }
+       // return false if we've had more than 600 characters WITHOUT
+       // a carriage return (this is BAD, drop the socket)
+       return (i < 600);
+}
+
+bool userrec::BufferIsReady()
+{
+        for (int i = 0; i < recvq.length(); i++)
+               if (recvq[i] == '\n')
+                       return true;
+        return false;
+}
+
+void userrec::ClearBuffer()
+{
+        recvq = "";
+}
+
+std::string userrec::GetBuffer()
+{
+       if (recvq == "")
+               return "";
+        char* line = (char*)recvq.c_str();
+        std::string ret = "";
+        while ((*line != '\n') && (strlen(line)))
+        {
+                ret = ret + *line;
+                line++;
+        }
+        if ((*line == '\n') || (*line == '\r'))
+                line++;
+        recvq = line;
+        return ret;
+}
+
+void userrec::AddWriteBuf(std::string data)
+{
+        std::stringstream stream;
+        stream << sendq << data;
+        sendq = stream.str();
+}
+
+// send AS MUCH OF THE USERS SENDQ as we are able to (might not be all of it)
+void userrec::FlushWriteBuf()
+{
+       if (sendq.length())
+       {
+               char* tb = (char*)this->sendq.c_str();
+               int n_sent = write(this->fd,tb,this->sendq.length());
+               if (n_sent == -1)
+               {
+                       this->SetWriteError(strerror(errno));
+               }
+               else
+               {
+                       // advance the queue
+                       tb += n_sent;
+                       this->sendq = tb;
+                       // update the user's stats counters
+                       this->bytes_out += n_sent;
+                       this->cmds_out++;
+               }
+       }
+}
+
+void userrec::SetWriteError(std::string error)
+{
+       log(DEBUG,"Setting error string for %s to '%s'",this->nick,error.c_str());
+       // don't try to set the error twice, its already set take the first string.
+       if (this->WriteError == "")
+               this->WriteError = error;
+}
+
+std::string userrec::GetWriteError()
+{
+       return this->WriteError;
+}