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;
72 void userrec::CloseSocket()
78 char* userrec::GetFullHost()
80 static char result[MAXBUF];
81 snprintf(result,MAXBUF,"%s!%s@%s",nick,ident,dhost);
85 int userrec::ReadData(void* buffer, size_t size)
89 log(DEBUG,"userrec::ReadData on fd %d",this->fd);
90 return read(this->fd, buffer, size);
96 char* userrec::GetFullRealHost()
98 static char fresult[MAXBUF];
99 snprintf(fresult,MAXBUF,"%s!%s@%s",nick,ident,host);
103 bool userrec::IsInvited(char* channel)
105 for (InvitedList::iterator i = invites.begin(); i != invites.end(); i++)
108 if (!strcasecmp(i->channel,channel))
117 InvitedList* userrec::GetInviteList()
122 void userrec::InviteTo(char* channel)
125 strlcpy(i.channel,channel,CHANMAX);
126 invites.push_back(i);
129 void userrec::RemoveInvite(char* channel)
131 log(DEBUG,"Removing invites");
136 for (InvitedList::iterator i = invites.begin(); i != invites.end(); i++)
140 if (!strcasecmp(i->channel,channel))
151 bool userrec::HasPermission(char* command)
153 char TypeName[MAXBUF],Classes[MAXBUF],ClassName[MAXBUF],CommandList[MAXBUF];
158 // users on u-lined servers can completely bypass
159 // all permissions based checks.
161 // of course, if this is sent to a remote server and this
162 // server is not ulined there, then that other server
163 // silently drops the command.
164 if (is_uline(this->server))
167 // are they even an oper at all?
168 if (strchr(this->modes,'o'))
170 log(DEBUG,"*** HasPermission: %s is an oper",this->nick);
171 for (int j =0; j < ConfValueEnum("type",&config_f); j++)
173 ConfValue("type","name",j,TypeName,&config_f);
174 if (!strcmp(TypeName,this->oper))
176 log(DEBUG,"*** HasPermission: %s is an oper of type '%s'",this->nick,this->oper);
177 ConfValue("type","classes",j,Classes,&config_f);
178 char* myclass = strtok_r(Classes," ",&savept);
181 log(DEBUG,"*** HasPermission: checking classtype '%s'",myclass);
182 for (int k =0; k < ConfValueEnum("class",&config_f); k++)
184 ConfValue("class","name",k,ClassName,&config_f);
185 if (!strcmp(ClassName,myclass))
187 ConfValue("class","commands",k,CommandList,&config_f);
188 log(DEBUG,"*** HasPermission: found class named %s with commands: '%s'",ClassName,CommandList);
191 mycmd = strtok_r(CommandList," ",&savept2);
194 if (!strcasecmp(mycmd,command))
196 log(DEBUG,"*** Command %s found, returning true",command);
199 mycmd = strtok_r(NULL," ",&savept2);
203 myclass = strtok_r(NULL," ",&savept);
212 bool userrec::AddBuffer(std::string a)
215 for (unsigned int i = 0; i < a.length(); i++)
216 if ((a[i] != '\r') && (a[i] != '\0') && (a[i] != 7))
218 std::stringstream stream(recvq);
220 recvq = stream.str();
222 // count the size of the first line in the buffer.
223 while (i < recvq.length())
225 if (recvq[i++] == '\n')
228 if (recvq.length() > (unsigned)this->recvqmax)
230 this->SetWriteError("RecvQ exceeded");
231 WriteOpers("*** User %s RecvQ of %d exceeds connect class maximum of %d",this->nick,recvq.length(),this->recvqmax);
233 // return false if we've had more than 600 characters WITHOUT
234 // a carriage return (this is BAD, drop the socket)
238 bool userrec::BufferIsReady()
240 for (unsigned int i = 0; i < recvq.length(); i++)
241 if (recvq[i] == '\n')
246 void userrec::ClearBuffer()
251 std::string userrec::GetBuffer()
255 char* line = (char*)recvq.c_str();
256 std::string ret = "";
257 while ((*line != '\n') && (strlen(line)))
262 if ((*line == '\n') || (*line == '\r'))
268 void userrec::AddWriteBuf(std::string data)
270 if (this->GetWriteError() != "")
272 if (sendq.length() + data.length() > (unsigned)this->sendqmax)
274 WriteOpers("*** User %s SendQ of %d exceeds connect class maximum of %d",this->nick,sendq.length() + data.length(),this->sendqmax);
275 this->SetWriteError("SendQ exceeded");
278 std::stringstream stream;
279 stream << sendq << data;
280 sendq = stream.str();
283 // send AS MUCH OF THE USERS SENDQ as we are able to (might not be all of it)
284 void userrec::FlushWriteBuf()
288 char* tb = (char*)this->sendq.c_str();
289 int n_sent = write(this->fd,tb,this->sendq.length());
292 this->SetWriteError(strerror(errno));
299 // update the user's stats counters
300 this->bytes_out += n_sent;
306 void userrec::SetWriteError(std::string error)
308 log(DEBUG,"Setting error string for %s to '%s'",this->nick,error.c_str());
309 // don't try to set the error twice, its already set take the first string.
310 if (this->WriteError == "")
311 this->WriteError = error;
314 std::string userrec::GetWriteError()
316 return this->WriteError;