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;
28 extern char ServerName[MAXBUF];
34 // the PROPER way to do it, AVOID bzero at *ALL* costs
36 strcpy(ip,"127.0.0.1");
43 server = (char*)FindServerNamePtr(ServerName);
48 fd = lastping = signon = idle_lastmsg = nping = registered = 0;
49 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 static char result[MAXBUF];
71 snprintf(result,MAXBUF,"%s!%s@%s",nick,ident,dhost);
75 int userrec::ReadData(void* buffer, size_t size)
79 return read(this->fd, buffer, size);
85 char* userrec::GetFullRealHost()
87 static char fresult[MAXBUF];
88 snprintf(fresult,MAXBUF,"%s!%s@%s",nick,ident,host);
92 bool userrec::IsInvited(char* channel)
94 for (InvitedList::iterator i = invites.begin(); i != invites.end(); i++)
97 if (!strcasecmp(i->channel,channel))
106 InvitedList* userrec::GetInviteList()
111 void userrec::InviteTo(char* channel)
114 strlcpy(i.channel,channel,CHANMAX);
115 invites.push_back(i);
118 void userrec::RemoveInvite(char* channel)
120 log(DEBUG,"Removing invites");
125 for (InvitedList::iterator i = invites.begin(); i != invites.end(); i++)
129 if (!strcasecmp(i->channel,channel))
140 bool userrec::HasPermission(char* command)
142 char TypeName[MAXBUF],Classes[MAXBUF],ClassName[MAXBUF],CommandList[MAXBUF];
147 // are they even an oper at all?
148 if (strchr(this->modes,'o'))
150 log(DEBUG,"*** HasPermission: %s is an oper",this->nick);
151 for (int j =0; j < ConfValueEnum("type",&config_f); j++)
153 ConfValue("type","name",j,TypeName,&config_f);
154 if (!strcmp(TypeName,this->oper))
156 log(DEBUG,"*** HasPermission: %s is an oper of type '%s'",this->nick,this->oper);
157 ConfValue("type","classes",j,Classes,&config_f);
158 char* myclass = strtok_r(Classes," ",&savept);
161 log(DEBUG,"*** HasPermission: checking classtype '%s'",myclass);
162 for (int k =0; k < ConfValueEnum("class",&config_f); k++)
164 ConfValue("class","name",k,ClassName,&config_f);
165 if (!strcmp(ClassName,myclass))
167 ConfValue("class","commands",k,CommandList,&config_f);
168 log(DEBUG,"*** HasPermission: found class named %s with commands: '%s'",ClassName,CommandList);
171 mycmd = strtok_r(CommandList," ",&savept2);
174 if (!strcasecmp(mycmd,command))
176 log(DEBUG,"*** Command %s found, returning true",command);
179 mycmd = strtok_r(NULL," ",&savept2);
183 myclass = strtok_r(NULL," ",&savept);
192 bool userrec::AddBuffer(std::string a)
195 for (int i = 0; i < a.length(); i++)
196 if ((a[i] != '\r') && (a[i] != '\0') && (a[i] != 7))
198 std::stringstream stream(recvq);
200 recvq = stream.str();
202 // count the size of the first line in the buffer.
203 while (i < recvq.length())
205 if (recvq[i++] == '\n')
208 if (recvq.length() > this->recvqmax)
210 this->SetWriteError("RecvQ exceeded");
211 WriteOpers("*** User %s RecvQ of %d exceeds connect class maximum of %d",this->nick,recvq.length(),this->recvqmax);
213 // return false if we've had more than 600 characters WITHOUT
214 // a carriage return (this is BAD, drop the socket)
218 bool userrec::BufferIsReady()
220 for (int i = 0; i < recvq.length(); i++)
221 if (recvq[i] == '\n')
226 void userrec::ClearBuffer()
231 std::string userrec::GetBuffer()
235 char* line = (char*)recvq.c_str();
236 std::string ret = "";
237 while ((*line != '\n') && (strlen(line)))
242 if ((*line == '\n') || (*line == '\r'))
248 void userrec::AddWriteBuf(std::string data)
250 if (this->GetWriteError() != "")
252 if (sendq.length() + data.length() > this->sendqmax)
254 WriteOpers("*** User %s SendQ of %d exceeds connect class maximum of %d",this->nick,sendq.length() + data.length(),this->sendqmax);
255 this->SetWriteError("SendQ exceeded");
258 std::stringstream stream;
259 stream << sendq << data;
260 sendq = stream.str();
263 // send AS MUCH OF THE USERS SENDQ as we are able to (might not be all of it)
264 void userrec::FlushWriteBuf()
268 char* tb = (char*)this->sendq.c_str();
269 int n_sent = write(this->fd,tb,this->sendq.length());
272 this->SetWriteError(strerror(errno));
279 // update the user's stats counters
280 this->bytes_out += n_sent;
286 void userrec::SetWriteError(std::string error)
288 log(DEBUG,"Setting error string for %s to '%s'",this->nick,error.c_str());
289 // don't try to set the error twice, its already set take the first string.
290 if (this->WriteError == "")
291 this->WriteError = error;
294 std::string userrec::GetWriteError()
296 return this->WriteError;