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"
21 #include "connection.h"
27 #include "inspstring.h"
28 #include "helperfuncs.h"
30 extern std::stringstream config_f;
31 extern char ServerName[MAXBUF];
37 // the PROPER way to do it, AVOID bzero at *ALL* costs
39 strcpy(ip,"127.0.0.1");
46 server = (char*)FindServerNamePtr(ServerName);
51 fd = lastping = signon = idle_lastmsg = nping = registered = 0;
52 flood = port = bytes_in = bytes_out = cmds_in = cmds_out = 0;
57 for (int i = 0; i < MAXCHANS; i++)
59 this->chans[i].channel = NULL;
60 this->chans[i].uc_modes = 0;
67 //pthread_kill(this->dnsthread,9);
70 void userrec::CloseSocket()
76 char* userrec::GetFullHost()
78 static char result[MAXBUF];
79 snprintf(result,MAXBUF,"%s!%s@%s",nick,ident,dhost);
83 int userrec::ReadData(void* buffer, size_t size)
87 log(DEBUG,"userrec::ReadData on fd %d",this->fd);
88 return read(this->fd, buffer, size);
94 char* userrec::GetFullRealHost()
96 static char fresult[MAXBUF];
97 snprintf(fresult,MAXBUF,"%s!%s@%s",nick,ident,host);
101 bool userrec::IsInvited(char* channel)
103 for (InvitedList::iterator i = invites.begin(); i != invites.end(); i++)
106 if (!strcasecmp(i->channel,channel))
115 InvitedList* userrec::GetInviteList()
120 void userrec::InviteTo(char* channel)
123 strlcpy(i.channel,channel,CHANMAX);
124 invites.push_back(i);
127 void userrec::RemoveInvite(char* channel)
129 log(DEBUG,"Removing invites");
134 for (InvitedList::iterator i = invites.begin(); i != invites.end(); i++)
138 if (!strcasecmp(i->channel,channel))
149 bool userrec::HasPermission(char* command)
151 char TypeName[MAXBUF],Classes[MAXBUF],ClassName[MAXBUF],CommandList[MAXBUF];
156 // are they even an oper at all?
157 if (strchr(this->modes,'o'))
159 log(DEBUG,"*** HasPermission: %s is an oper",this->nick);
160 for (int j =0; j < ConfValueEnum("type",&config_f); j++)
162 ConfValue("type","name",j,TypeName,&config_f);
163 if (!strcmp(TypeName,this->oper))
165 log(DEBUG,"*** HasPermission: %s is an oper of type '%s'",this->nick,this->oper);
166 ConfValue("type","classes",j,Classes,&config_f);
167 char* myclass = strtok_r(Classes," ",&savept);
170 log(DEBUG,"*** HasPermission: checking classtype '%s'",myclass);
171 for (int k =0; k < ConfValueEnum("class",&config_f); k++)
173 ConfValue("class","name",k,ClassName,&config_f);
174 if (!strcmp(ClassName,myclass))
176 ConfValue("class","commands",k,CommandList,&config_f);
177 log(DEBUG,"*** HasPermission: found class named %s with commands: '%s'",ClassName,CommandList);
180 mycmd = strtok_r(CommandList," ",&savept2);
183 if (!strcasecmp(mycmd,command))
185 log(DEBUG,"*** Command %s found, returning true",command);
188 mycmd = strtok_r(NULL," ",&savept2);
192 myclass = strtok_r(NULL," ",&savept);
201 bool userrec::AddBuffer(std::string a)
204 for (unsigned int i = 0; i < a.length(); i++)
205 if ((a[i] != '\r') && (a[i] != '\0') && (a[i] != 7))
207 std::stringstream stream(recvq);
209 recvq = stream.str();
211 // count the size of the first line in the buffer.
212 while (i < recvq.length())
214 if (recvq[i++] == '\n')
217 if (recvq.length() > (unsigned)this->recvqmax)
219 this->SetWriteError("RecvQ exceeded");
220 WriteOpers("*** User %s RecvQ of %d exceeds connect class maximum of %d",this->nick,recvq.length(),this->recvqmax);
222 // return false if we've had more than 600 characters WITHOUT
223 // a carriage return (this is BAD, drop the socket)
227 bool userrec::BufferIsReady()
229 for (unsigned int i = 0; i < recvq.length(); i++)
230 if (recvq[i] == '\n')
235 void userrec::ClearBuffer()
240 std::string userrec::GetBuffer()
244 char* line = (char*)recvq.c_str();
245 std::string ret = "";
246 while ((*line != '\n') && (strlen(line)))
251 if ((*line == '\n') || (*line == '\r'))
257 void userrec::AddWriteBuf(std::string data)
259 if (this->GetWriteError() != "")
261 if (sendq.length() + data.length() > (unsigned)this->sendqmax)
263 WriteOpers("*** User %s SendQ of %d exceeds connect class maximum of %d",this->nick,sendq.length() + data.length(),this->sendqmax);
264 this->SetWriteError("SendQ exceeded");
267 std::stringstream stream;
268 stream << sendq << data;
269 sendq = stream.str();
272 // send AS MUCH OF THE USERS SENDQ as we are able to (might not be all of it)
273 void userrec::FlushWriteBuf()
277 char* tb = (char*)this->sendq.c_str();
278 int n_sent = write(this->fd,tb,this->sendq.length());
281 this->SetWriteError(strerror(errno));
288 // update the user's stats counters
289 this->bytes_out += n_sent;
295 void userrec::SetWriteError(std::string error)
297 log(DEBUG,"Setting error string for %s to '%s'",this->nick,error.c_str());
298 // don't try to set the error twice, its already set take the first string.
299 if (this->WriteError == "")
300 this->WriteError = error;
303 std::string userrec::GetWriteError()
305 return this->WriteError;