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"
29 #include "inspstring.h"
30 #include "helperfuncs.h"
32 extern std::stringstream config_f;
33 extern char ServerName[MAXBUF];
39 // the PROPER way to do it, AVOID bzero at *ALL* costs
41 strcpy(ip,"127.0.0.1");
48 server = (char*)FindServerNamePtr(ServerName);
53 fd = lastping = signon = idle_lastmsg = nping = registered = 0;
54 flood = port = bytes_in = bytes_out = cmds_in = cmds_out = 0;
59 for (int i = 0; i < MAXCHANS; i++)
61 this->chans[i].channel = NULL;
62 this->chans[i].uc_modes = 0;
70 // for local clients, clean up their dns thread
71 if (!strcmp(this->server,ServerName))
73 pthread_kill(this->dnsthread,9);
78 void userrec::CloseSocket()
84 char* userrec::GetFullHost()
86 static char result[MAXBUF];
87 snprintf(result,MAXBUF,"%s!%s@%s",nick,ident,dhost);
91 int userrec::ReadData(void* buffer, size_t size)
95 log(DEBUG,"userrec::ReadData on fd %d",this->fd);
96 return read(this->fd, buffer, size);
102 char* userrec::GetFullRealHost()
104 static char fresult[MAXBUF];
105 snprintf(fresult,MAXBUF,"%s!%s@%s",nick,ident,host);
109 bool userrec::IsInvited(char* channel)
111 for (InvitedList::iterator i = invites.begin(); i != invites.end(); i++)
114 if (!strcasecmp(i->channel,channel))
123 InvitedList* userrec::GetInviteList()
128 void userrec::InviteTo(char* channel)
131 strlcpy(i.channel,channel,CHANMAX);
132 invites.push_back(i);
135 void userrec::RemoveInvite(char* channel)
137 log(DEBUG,"Removing invites");
142 for (InvitedList::iterator i = invites.begin(); i != invites.end(); i++)
146 if (!strcasecmp(i->channel,channel))
157 bool userrec::HasPermission(char* command)
159 char TypeName[MAXBUF],Classes[MAXBUF],ClassName[MAXBUF],CommandList[MAXBUF];
164 // are they even an oper at all?
165 if (strchr(this->modes,'o'))
167 log(DEBUG,"*** HasPermission: %s is an oper",this->nick);
168 for (int j =0; j < ConfValueEnum("type",&config_f); j++)
170 ConfValue("type","name",j,TypeName,&config_f);
171 if (!strcmp(TypeName,this->oper))
173 log(DEBUG,"*** HasPermission: %s is an oper of type '%s'",this->nick,this->oper);
174 ConfValue("type","classes",j,Classes,&config_f);
175 char* myclass = strtok_r(Classes," ",&savept);
178 log(DEBUG,"*** HasPermission: checking classtype '%s'",myclass);
179 for (int k =0; k < ConfValueEnum("class",&config_f); k++)
181 ConfValue("class","name",k,ClassName,&config_f);
182 if (!strcmp(ClassName,myclass))
184 ConfValue("class","commands",k,CommandList,&config_f);
185 log(DEBUG,"*** HasPermission: found class named %s with commands: '%s'",ClassName,CommandList);
188 mycmd = strtok_r(CommandList," ",&savept2);
191 if (!strcasecmp(mycmd,command))
193 log(DEBUG,"*** Command %s found, returning true",command);
196 mycmd = strtok_r(NULL," ",&savept2);
200 myclass = strtok_r(NULL," ",&savept);
209 bool userrec::AddBuffer(std::string a)
212 for (unsigned int i = 0; i < a.length(); i++)
213 if ((a[i] != '\r') && (a[i] != '\0') && (a[i] != 7))
215 std::stringstream stream(recvq);
217 recvq = stream.str();
219 // count the size of the first line in the buffer.
220 while (i < recvq.length())
222 if (recvq[i++] == '\n')
225 if (recvq.length() > (unsigned)this->recvqmax)
227 this->SetWriteError("RecvQ exceeded");
228 WriteOpers("*** User %s RecvQ of %d exceeds connect class maximum of %d",this->nick,recvq.length(),this->recvqmax);
230 // return false if we've had more than 600 characters WITHOUT
231 // a carriage return (this is BAD, drop the socket)
235 bool userrec::BufferIsReady()
237 for (unsigned int i = 0; i < recvq.length(); i++)
238 if (recvq[i] == '\n')
243 void userrec::ClearBuffer()
248 std::string userrec::GetBuffer()
252 char* line = (char*)recvq.c_str();
253 std::string ret = "";
254 while ((*line != '\n') && (strlen(line)))
259 if ((*line == '\n') || (*line == '\r'))
265 void userrec::AddWriteBuf(std::string data)
267 if (this->GetWriteError() != "")
269 if (sendq.length() + data.length() > (unsigned)this->sendqmax)
271 WriteOpers("*** User %s SendQ of %d exceeds connect class maximum of %d",this->nick,sendq.length() + data.length(),this->sendqmax);
272 this->SetWriteError("SendQ exceeded");
275 std::stringstream stream;
276 stream << sendq << data;
277 sendq = stream.str();
280 // send AS MUCH OF THE USERS SENDQ as we are able to (might not be all of it)
281 void userrec::FlushWriteBuf()
285 char* tb = (char*)this->sendq.c_str();
286 int n_sent = write(this->fd,tb,this->sendq.length());
289 this->SetWriteError(strerror(errno));
296 // update the user's stats counters
297 this->bytes_out += n_sent;
303 void userrec::SetWriteError(std::string error)
305 log(DEBUG,"Setting error string for %s to '%s'",this->nick,error.c_str());
306 // don't try to set the error twice, its already set take the first string.
307 if (this->WriteError == "")
308 this->WriteError = error;
311 std::string userrec::GetWriteError()
313 return this->WriteError;