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"
31 #include "helperfuncs.h"
33 extern std::stringstream config_f;
34 extern char ServerName[MAXBUF];
40 // the PROPER way to do it, AVOID bzero at *ALL* costs
42 strcpy(ip,"127.0.0.1");
49 server = (char*)FindServerNamePtr(ServerName);
54 fd = lastping = signon = idle_lastmsg = nping = registered = 0;
55 flood = port = bytes_in = bytes_out = cmds_in = cmds_out = 0;
60 for (int i = 0; i < MAXCHANS; i++)
62 this->chans[i].channel = NULL;
63 this->chans[i].uc_modes = 0;
71 // for local clients, clean up their dns thread
72 if (!strcmp(this->server,ServerName))
74 pthread_kill(this->dnsthread,9);
79 void userrec::CloseSocket()
85 char* userrec::GetFullHost()
87 static char result[MAXBUF];
88 snprintf(result,MAXBUF,"%s!%s@%s",nick,ident,dhost);
92 int userrec::ReadData(void* buffer, size_t size)
96 log(DEBUG,"userrec::ReadData on fd %d",this->fd);
97 return read(this->fd, buffer, size);
103 char* userrec::GetFullRealHost()
105 static char fresult[MAXBUF];
106 snprintf(fresult,MAXBUF,"%s!%s@%s",nick,ident,host);
110 bool userrec::IsInvited(char* channel)
112 for (InvitedList::iterator i = invites.begin(); i != invites.end(); i++)
115 if (!strcasecmp(i->channel,channel))
124 InvitedList* userrec::GetInviteList()
129 void userrec::InviteTo(char* channel)
132 strlcpy(i.channel,channel,CHANMAX);
133 invites.push_back(i);
136 void userrec::RemoveInvite(char* channel)
138 log(DEBUG,"Removing invites");
143 for (InvitedList::iterator i = invites.begin(); i != invites.end(); i++)
147 if (!strcasecmp(i->channel,channel))
158 bool userrec::HasPermission(char* command)
160 char TypeName[MAXBUF],Classes[MAXBUF],ClassName[MAXBUF],CommandList[MAXBUF];
165 // users on u-lined servers can completely bypass
166 // all permissions based checks.
168 // of course, if this is sent to a remote server and this
169 // server is not ulined there, then that other server
170 // silently drops the command.
171 if (is_uline(this->server))
174 // are they even an oper at all?
175 if (strchr(this->modes,'o'))
177 log(DEBUG,"*** HasPermission: %s is an oper",this->nick);
178 for (int j =0; j < ConfValueEnum("type",&config_f); j++)
180 ConfValue("type","name",j,TypeName,&config_f);
181 if (!strcmp(TypeName,this->oper))
183 log(DEBUG,"*** HasPermission: %s is an oper of type '%s'",this->nick,this->oper);
184 ConfValue("type","classes",j,Classes,&config_f);
185 char* myclass = strtok_r(Classes," ",&savept);
188 log(DEBUG,"*** HasPermission: checking classtype '%s'",myclass);
189 for (int k =0; k < ConfValueEnum("class",&config_f); k++)
191 ConfValue("class","name",k,ClassName,&config_f);
192 if (!strcmp(ClassName,myclass))
194 ConfValue("class","commands",k,CommandList,&config_f);
195 log(DEBUG,"*** HasPermission: found class named %s with commands: '%s'",ClassName,CommandList);
198 mycmd = strtok_r(CommandList," ",&savept2);
201 if (!strcasecmp(mycmd,command))
203 log(DEBUG,"*** Command %s found, returning true",command);
206 mycmd = strtok_r(NULL," ",&savept2);
210 myclass = strtok_r(NULL," ",&savept);
219 bool userrec::AddBuffer(std::string a)
222 for (unsigned int i = 0; i < a.length(); i++)
223 if ((a[i] != '\r') && (a[i] != '\0') && (a[i] != 7))
225 std::stringstream stream(recvq);
227 recvq = stream.str();
229 // count the size of the first line in the buffer.
230 while (i < recvq.length())
232 if (recvq[i++] == '\n')
235 if (recvq.length() > (unsigned)this->recvqmax)
237 this->SetWriteError("RecvQ exceeded");
238 WriteOpers("*** User %s RecvQ of %d exceeds connect class maximum of %d",this->nick,recvq.length(),this->recvqmax);
240 // return false if we've had more than 600 characters WITHOUT
241 // a carriage return (this is BAD, drop the socket)
245 bool userrec::BufferIsReady()
247 for (unsigned int i = 0; i < recvq.length(); i++)
248 if (recvq[i] == '\n')
253 void userrec::ClearBuffer()
258 std::string userrec::GetBuffer()
262 char* line = (char*)recvq.c_str();
263 std::string ret = "";
264 while ((*line != '\n') && (strlen(line)))
269 if ((*line == '\n') || (*line == '\r'))
275 void userrec::AddWriteBuf(std::string data)
277 if (this->GetWriteError() != "")
279 if (sendq.length() + data.length() > (unsigned)this->sendqmax)
281 WriteOpers("*** User %s SendQ of %d exceeds connect class maximum of %d",this->nick,sendq.length() + data.length(),this->sendqmax);
282 this->SetWriteError("SendQ exceeded");
285 std::stringstream stream;
286 stream << sendq << data;
287 sendq = stream.str();
290 // send AS MUCH OF THE USERS SENDQ as we are able to (might not be all of it)
291 void userrec::FlushWriteBuf()
295 char* tb = (char*)this->sendq.c_str();
296 int n_sent = write(this->fd,tb,this->sendq.length());
299 this->SetWriteError(strerror(errno));
306 // update the user's stats counters
307 this->bytes_out += n_sent;
313 void userrec::SetWriteError(std::string error)
315 log(DEBUG,"Setting error string for %s to '%s'",this->nick,error.c_str());
316 // don't try to set the error twice, its already set take the first string.
317 if (this->WriteError == "")
318 this->WriteError = error;
321 std::string userrec::GetWriteError()
323 return this->WriteError;