Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

users.cpp

Go to the documentation of this file.
00001 /*       +------------------------------------+
00002  *       | Inspire Internet Relay Chat Daemon |
00003  *       +------------------------------------+
00004  *
00005  *  Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
00006  *                       E-mail:
00007  *                <brain@chatspike.net>
00008  *                <Craig@chatspike.net>
00009  *     
00010  * Written by Craig Edwards, Craig McLure, and others.
00011  * This program is free but copyrighted software; see
00012  *            the file COPYING for details.
00013  *
00014  * ---------------------------------------------------
00015  */
00016 
00017 using namespace std;
00018 
00019 #include "inspircd_config.h" 
00020 #include "channels.h"
00021 #include "users.h"
00022 #include "inspircd.h"
00023 #include <stdio.h>
00024 #include "inspstring.h"
00025 #include "helperfuncs.h"
00026 
00027 extern std::stringstream config_f;
00028 
00029 extern time_t TIME;
00030 
00031 userrec::userrec()
00032 {
00033         // the PROPER way to do it, AVOID bzero at *ALL* costs
00034         strcpy(nick,"");
00035         strcpy(ip,"127.0.0.1");
00036         timeout = 0;
00037         strcpy(ident,"");
00038         strcpy(host,"");
00039         strcpy(dhost,"");
00040         strcpy(fullname,"");
00041         strcpy(modes,"");
00042         strcpy(server,"");
00043         strcpy(awaymsg,"");
00044         strcpy(oper,"");
00045         reset_due = TIME;
00046         lines_in = 0;
00047         fd = lastping = signon = idle_lastmsg = nping = registered = 0;
00048         flood = port = bytes_in = bytes_out = cmds_in = cmds_out = 0;
00049         haspassed = false;
00050         dns_done = false;
00051         recvq = "";
00052         sendq = "";
00053         strcpy(result,"");
00054         for (int i = 0; i < MAXCHANS; i++)
00055         {
00056                 this->chans[i].channel = NULL;
00057                 this->chans[i].uc_modes = 0;
00058         }
00059         invites.clear();
00060 }
00061 
00062 void userrec::CloseSocket()
00063 {
00064         shutdown(this->fd,2);
00065         close(this->fd);
00066 }
00067  
00068 char* userrec::GetFullHost()
00069 {
00070         snprintf(result,MAXBUF,"%s!%s@%s",nick,ident,dhost);
00071         return result;
00072 }
00073 
00074 int userrec::ReadData(void* buffer, size_t size)
00075 {
00076         if (this->fd > -1)
00077         {
00078                 return read(this->fd, buffer, size);
00079         }
00080         else return 0;
00081 }
00082 
00083 
00084 char* userrec::GetFullRealHost()
00085 {
00086         snprintf(result,MAXBUF,"%s!%s@%s",nick,ident,host);
00087         return result;
00088 }
00089 
00090 bool userrec::IsInvited(char* channel)
00091 {
00092         for (InvitedList::iterator i = invites.begin(); i != invites.end(); i++)
00093         {
00094                 if (i->channel) {
00095                         if (!strcasecmp(i->channel,channel))
00096                         {
00097                                 return true;
00098                         }
00099                 }
00100         }
00101         return false;
00102 }
00103 
00104 InvitedList* userrec::GetInviteList()
00105 {
00106         return &invites;
00107 }
00108 
00109 void userrec::InviteTo(char* channel)
00110 {
00111         Invited i;
00112         strlcpy(i.channel,channel,CHANMAX);
00113         invites.push_back(i);
00114 }
00115 
00116 void userrec::RemoveInvite(char* channel)
00117 {
00118         log(DEBUG,"Removing invites");
00119         if (channel)
00120         {
00121                 if (invites.size())
00122                 {
00123                         for (InvitedList::iterator i = invites.begin(); i != invites.end(); i++)
00124                         {
00125                                 if (i->channel)
00126                                 {
00127                                         if (!strcasecmp(i->channel,channel))
00128                                         {
00129                                                 invites.erase(i);
00130                                                 return;
00131                                         }
00132                                 }
00133                         }
00134                 }
00135         }
00136 }
00137 
00138 bool userrec::HasPermission(char* command)
00139 {
00140         char TypeName[MAXBUF],Classes[MAXBUF],ClassName[MAXBUF],CommandList[MAXBUF];
00141         char* mycmd;
00142         char* savept;
00143         char* savept2;
00144         
00145         // are they even an oper at all?
00146         if (strchr(this->modes,'o'))
00147         {
00148                 log(DEBUG,"*** HasPermission: %s is an oper",this->nick);
00149                 for (int j =0; j < ConfValueEnum("type",&config_f); j++)
00150                 {
00151                         ConfValue("type","name",j,TypeName,&config_f);
00152                         if (!strcmp(TypeName,this->oper))
00153                         {
00154                                 log(DEBUG,"*** HasPermission: %s is an oper of type '%s'",this->nick,this->oper);
00155                                 ConfValue("type","classes",j,Classes,&config_f);
00156                                 char* myclass = strtok_r(Classes," ",&savept);
00157                                 while (myclass)
00158                                 {
00159                                         log(DEBUG,"*** HasPermission: checking classtype '%s'",myclass);
00160                                         for (int k =0; k < ConfValueEnum("class",&config_f); k++)
00161                                         {
00162                                                 ConfValue("class","name",k,ClassName,&config_f);
00163                                                 if (!strcmp(ClassName,myclass))
00164                                                 {
00165                                                         ConfValue("class","commands",k,CommandList,&config_f);
00166                                                         log(DEBUG,"*** HasPermission: found class named %s with commands: '%s'",ClassName,CommandList);
00167                                                         
00168                                                         
00169                                                         mycmd = strtok_r(CommandList," ",&savept2);
00170                                                         while (mycmd)
00171                                                         {
00172                                                                 if (!strcasecmp(mycmd,command))
00173                                                                 {
00174                                                                         log(DEBUG,"*** Command %s found, returning true",command);
00175                                                                         return true;
00176                                                                 }
00177                                                                 mycmd = strtok_r(NULL," ",&savept2);
00178                                                         }
00179                                                 }
00180                                         }
00181                                         myclass = strtok_r(NULL," ",&savept);
00182                                 }
00183                         }
00184                 }
00185         }
00186         return false;
00187 }
00188 
00189 
00190 bool userrec::AddBuffer(std::string a)
00191 {
00192         std::string b = "";
00193         for (int i = 0; i < a.length(); i++)
00194                 if ((a[i] != '\r') && (a[i] != '\0') && (a[i] != 7))
00195                         b = b + a[i];
00196         std::stringstream stream(recvq);
00197         stream << b;
00198         recvq = stream.str();
00199         int i = 0;
00200         // count the size of the first line in the buffer.
00201         while (i < recvq.length())
00202         {
00203                 if (recvq[i++] == '\n')
00204                         break;
00205         }
00206         if (recvq.length() > this->recvqmax)
00207         {
00208                 this->SetWriteError("RecvQ exceeded");
00209                 WriteOpers("*** User %s RecvQ of %d exceeds connect class maximum of %d",this->nick,recvq.length(),this->recvqmax);
00210         }
00211         // return false if we've had more than 600 characters WITHOUT
00212         // a carriage return (this is BAD, drop the socket)
00213         return (i < 600);
00214 }
00215 
00216 bool userrec::BufferIsReady()
00217 {
00218         for (int i = 0; i < recvq.length(); i++)
00219                 if (recvq[i] == '\n')
00220                         return true;
00221         return false;
00222 }
00223 
00224 void userrec::ClearBuffer()
00225 {
00226         recvq = "";
00227 }
00228 
00229 std::string userrec::GetBuffer()
00230 {
00231         if (recvq == "")
00232                 return "";
00233         char* line = (char*)recvq.c_str();
00234         std::string ret = "";
00235         while ((*line != '\n') && (strlen(line)))
00236         {
00237                 ret = ret + *line;
00238                 line++;
00239         }
00240         if ((*line == '\n') || (*line == '\r'))
00241                 line++;
00242         recvq = line;
00243         return ret;
00244 }
00245 
00246 void userrec::AddWriteBuf(std::string data)
00247 {
00248         if (this->GetWriteError() != "")
00249                 return;
00250         if (sendq.length() + data.length() > this->sendqmax)
00251         {
00252                 WriteOpers("*** User %s SendQ of %d exceeds connect class maximum of %d",this->nick,sendq.length() + data.length(),this->sendqmax);
00253                 this->SetWriteError("SendQ exceeded");
00254                 return;
00255         }
00256         std::stringstream stream;
00257         stream << sendq << data;
00258         sendq = stream.str();
00259 }
00260 
00261 // send AS MUCH OF THE USERS SENDQ as we are able to (might not be all of it)
00262 void userrec::FlushWriteBuf()
00263 {
00264         if (sendq.length())
00265         {
00266                 char* tb = (char*)this->sendq.c_str();
00267                 int n_sent = write(this->fd,tb,this->sendq.length());
00268                 if (n_sent == -1)
00269                 {
00270                         this->SetWriteError(strerror(errno));
00271                 }
00272                 else
00273                 {
00274                         // advance the queue
00275                         tb += n_sent;
00276                         this->sendq = tb;
00277                         // update the user's stats counters
00278                         this->bytes_out += n_sent;
00279                         this->cmds_out++;
00280                 }
00281         }
00282 }
00283 
00284 void userrec::SetWriteError(std::string error)
00285 {
00286         log(DEBUG,"Setting error string for %s to '%s'",this->nick,error.c_str());
00287         // don't try to set the error twice, its already set take the first string.
00288         if (this->WriteError == "")
00289                 this->WriteError = error;
00290 }
00291 
00292 std::string userrec::GetWriteError()
00293 {
00294         return this->WriteError;
00295 }

Generated on Wed May 25 22:02:06 2005 for InspIRCd by doxygen 1.3.3