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

Generated on Thu May 12 02:25:13 2005 for InspIRCd by doxygen 1.3.3