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 ServerConfig* Config;
38 // the PROPER way to do it, AVOID bzero at *ALL* costs
40 strcpy(ip,"127.0.0.1");
47 server = (char*)FindServerNamePtr(Config->ServerName);
52 fd = lastping = signon = idle_lastmsg = nping = registered = 0;
53 flood = port = bytes_in = bytes_out = cmds_in = cmds_out = 0;
66 void userrec::CloseSocket()
72 char* userrec::GetFullHost()
74 static char result[MAXBUF];
75 snprintf(result,MAXBUF,"%s!%s@%s",nick,ident,dhost);
79 int userrec::ReadData(void* buffer, size_t size)
83 return read(this->fd, buffer, size);
89 char* userrec::GetFullRealHost()
91 static char fresult[MAXBUF];
92 snprintf(fresult,MAXBUF,"%s!%s@%s",nick,ident,host);
96 bool userrec::IsInvited(char* channel)
98 for (InvitedList::iterator i = invites.begin(); i != invites.end(); i++)
101 if (!strcasecmp(i->channel,channel))
110 InvitedList* userrec::GetInviteList()
115 void userrec::InviteTo(char* channel)
118 strlcpy(i.channel,channel,CHANMAX);
119 invites.push_back(i);
122 void userrec::RemoveInvite(char* channel)
124 log(DEBUG,"Removing invites");
129 for (InvitedList::iterator i = invites.begin(); i != invites.end(); i++)
133 if (!strcasecmp(i->channel,channel))
144 bool userrec::HasPermission(char* command)
146 char TypeName[MAXBUF],Classes[MAXBUF],ClassName[MAXBUF],CommandList[MAXBUF];
151 // users on u-lined servers can completely bypass
152 // all permissions based checks.
154 // of course, if this is sent to a remote server and this
155 // server is not ulined there, then that other server
156 // silently drops the command.
157 if (is_uline(this->server))
160 // are they even an oper at all?
161 if (strchr(this->modes,'o'))
163 for (int j =0; j < Config->ConfValueEnum("type",&Config->config_f); j++)
165 Config->ConfValue("type","name",j,TypeName,&Config->config_f);
166 if (!strcmp(TypeName,this->oper))
168 Config->ConfValue("type","classes",j,Classes,&Config->config_f);
169 char* myclass = strtok_r(Classes," ",&savept);
172 for (int k =0; k < Config->ConfValueEnum("class",&Config->config_f); k++)
174 Config->ConfValue("class","name",k,ClassName,&Config->config_f);
175 if (!strcmp(ClassName,myclass))
177 Config->ConfValue("class","commands",k,CommandList,&Config->config_f);
178 mycmd = strtok_r(CommandList," ",&savept2);
181 if ((!strcasecmp(mycmd,command)) || (*mycmd == '*'))
185 mycmd = strtok_r(NULL," ",&savept2);
189 myclass = strtok_r(NULL," ",&savept);
198 bool userrec::AddBuffer(std::string a)
201 for (unsigned int i = 0; i < a.length(); i++)
202 if ((a[i] != '\r') && (a[i] != '\0') && (a[i] != 7))
204 std::stringstream stream(recvq);
206 recvq = stream.str();
208 // count the size of the first line in the buffer.
209 while (i < recvq.length())
211 if (recvq[i++] == '\n')
214 if (recvq.length() > (unsigned)this->recvqmax)
216 this->SetWriteError("RecvQ exceeded");
217 WriteOpers("*** User %s RecvQ of %d exceeds connect class maximum of %d",this->nick,recvq.length(),this->recvqmax);
219 // return false if we've had more than 600 characters WITHOUT
220 // a carriage return (this is BAD, drop the socket)
224 bool userrec::BufferIsReady()
226 for (unsigned int i = 0; i < recvq.length(); i++)
227 if (recvq[i] == '\n')
232 void userrec::ClearBuffer()
237 std::string userrec::GetBuffer()
241 char* line = (char*)recvq.c_str();
242 std::string ret = "";
243 while ((*line != '\n') && (strlen(line)))
248 if ((*line == '\n') || (*line == '\r'))
254 void userrec::AddWriteBuf(std::string data)
256 if (this->GetWriteError() != "")
258 if (sendq.length() + data.length() > (unsigned)this->sendqmax)
260 WriteOpers("*** User %s SendQ of %d exceeds connect class maximum of %d",this->nick,sendq.length() + data.length(),this->sendqmax);
261 this->SetWriteError("SendQ exceeded");
264 std::stringstream stream;
265 stream << sendq << data;
266 sendq = stream.str();
269 // send AS MUCH OF THE USERS SENDQ as we are able to (might not be all of it)
270 void userrec::FlushWriteBuf()
274 char* tb = (char*)this->sendq.c_str();
275 int n_sent = write(this->fd,tb,this->sendq.length());
278 this->SetWriteError(strerror(errno));
285 // update the user's stats counters
286 this->bytes_out += n_sent;
292 void userrec::SetWriteError(std::string error)
294 log(DEBUG,"Setting error string for %s to '%s'",this->nick,error.c_str());
295 // don't try to set the error twice, its already set take the first string.
296 if (this->WriteError == "")
297 this->WriteError = error;
300 std::string userrec::GetWriteError()
302 return this->WriteError;