1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
19 #include "inspircd_config.h"
24 #include "inspstring.h"
25 #include "helperfuncs.h"
27 extern std::stringstream config_f;
33 // the PROPER way to do it, AVOID bzero at *ALL* costs
35 strcpy(ip,"127.0.0.1");
47 fd = lastping = signon = idle_lastmsg = nping = registered = 0;
48 flood = port = bytes_in = bytes_out = cmds_in = cmds_out = 0;
54 for (int i = 0; i < MAXCHANS; i++)
56 this->chans[i].channel = NULL;
57 this->chans[i].uc_modes = 0;
62 void userrec::CloseSocket()
68 char* userrec::GetFullHost()
70 snprintf(result,MAXBUF,"%s!%s@%s",nick,ident,dhost);
74 int userrec::ReadData(void* buffer, size_t size)
78 return read(this->fd, buffer, size)
84 char* userrec::GetFullRealHost()
86 snprintf(result,MAXBUF,"%s!%s@%s",nick,ident,host);
90 bool userrec::IsInvited(char* channel)
92 for (InvitedList::iterator i = invites.begin(); i != invites.end(); i++)
95 if (!strcasecmp(i->channel,channel))
104 InvitedList* userrec::GetInviteList()
109 void userrec::InviteTo(char* channel)
112 strlcpy(i.channel,channel,CHANMAX);
113 invites.push_back(i);
116 void userrec::RemoveInvite(char* channel)
118 log(DEBUG,"Removing invites");
123 for (InvitedList::iterator i = invites.begin(); i != invites.end(); i++)
127 if (!strcasecmp(i->channel,channel))
138 bool userrec::HasPermission(char* command)
140 char TypeName[MAXBUF],Classes[MAXBUF],ClassName[MAXBUF],CommandList[MAXBUF];
145 // are they even an oper at all?
146 if (strchr(this->modes,'o'))
148 log(DEBUG,"*** HasPermission: %s is an oper",this->nick);
149 for (int j =0; j < ConfValueEnum("type",&config_f); j++)
151 ConfValue("type","name",j,TypeName,&config_f);
152 if (!strcmp(TypeName,this->oper))
154 log(DEBUG,"*** HasPermission: %s is an oper of type '%s'",this->nick,this->oper);
155 ConfValue("type","classes",j,Classes,&config_f);
156 char* myclass = strtok_r(Classes," ",&savept);
159 log(DEBUG,"*** HasPermission: checking classtype '%s'",myclass);
160 for (int k =0; k < ConfValueEnum("class",&config_f); k++)
162 ConfValue("class","name",k,ClassName,&config_f);
163 if (!strcmp(ClassName,myclass))
165 ConfValue("class","commands",k,CommandList,&config_f);
166 log(DEBUG,"*** HasPermission: found class named %s with commands: '%s'",ClassName,CommandList);
169 mycmd = strtok_r(CommandList," ",&savept2);
172 if (!strcasecmp(mycmd,command))
174 log(DEBUG,"*** Command %s found, returning true",command);
177 mycmd = strtok_r(NULL," ",&savept2);
181 myclass = strtok_r(NULL," ",&savept);
190 bool userrec::AddBuffer(std::string a)
193 for (int i = 0; i < a.length(); i++)
194 if ((a[i] != '\r') && (a[i] != '\0') && (a[i] != 7))
196 std::stringstream stream(recvq);
198 recvq = stream.str();
200 // count the size of the first line in the buffer.
201 while (i < recvq.length())
203 if (recvq[i++] == '\n')
206 if (recvq.length() > this->recvqmax)
208 this->SetWriteError("RecvQ exceeded");
209 WriteOpers("*** User %s RecvQ of %d exceeds connect class maximum of %d",this->nick,recvq.length(),this->recvqmax);
211 // return false if we've had more than 600 characters WITHOUT
212 // a carriage return (this is BAD, drop the socket)
216 bool userrec::BufferIsReady()
218 for (int i = 0; i < recvq.length(); i++)
219 if (recvq[i] == '\n')
224 void userrec::ClearBuffer()
229 std::string userrec::GetBuffer()
233 char* line = (char*)recvq.c_str();
234 std::string ret = "";
235 while ((*line != '\n') && (strlen(line)))
240 if ((*line == '\n') || (*line == '\r'))
246 void userrec::AddWriteBuf(std::string data)
248 if (this->GetWriteError() != "")
250 if (sendq.length() + data.length() > this->sendqmax)
252 WriteOpers("*** User %s SendQ of %d exceeds connect class maximum of %d",this->nick,sendq.length() + data.length(),this->sendqmax);
253 this->SetWriteError("SendQ exceeded");
256 std::stringstream stream;
257 stream << sendq << data;
258 sendq = stream.str();
261 // send AS MUCH OF THE USERS SENDQ as we are able to (might not be all of it)
262 void userrec::FlushWriteBuf()
266 char* tb = (char*)this->sendq.c_str();
267 int n_sent = write(this->fd,tb,this->sendq.length());
270 this->SetWriteError(strerror(errno));
277 // update the user's stats counters
278 this->bytes_out += n_sent;
284 void userrec::SetWriteError(std::string error)
286 log(DEBUG,"Setting error string for %s to '%s'",this->nick,error.c_str());
287 // don't try to set the error twice, its already set take the first string.
288 if (this->WriteError == "")
289 this->WriteError = error;
292 std::string userrec::GetWriteError()
294 return this->WriteError;