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 * ---------------------------------------------------
17 #include "inspircd_config.h"
22 #include "inspstring.h"
23 #include "helperfuncs.h"
25 extern std::stringstream config_f;
31 // the PROPER way to do it, AVOID bzero at *ALL* costs
33 strcpy(ip,"127.0.0.1");
45 fd = lastping = signon = idle_lastmsg = nping = registered = 0;
46 flood = port = bytes_in = bytes_out = cmds_in = cmds_out = 0;
52 for (int i = 0; i < MAXCHANS; i++)
54 this->chans[i].channel = NULL;
55 this->chans[i].uc_modes = 0;
62 char* userrec::GetFullHost()
64 snprintf(result,MAXBUF,"%s!%s@%s",nick,ident,dhost);
69 char* userrec::GetFullRealHost()
71 snprintf(result,MAXBUF,"%s!%s@%s",nick,ident,host);
75 bool userrec::IsInvited(char* channel)
77 for (InvitedList::iterator i = invites.begin(); i != invites.end(); i++)
80 if (!strcasecmp(i->channel,channel))
89 InvitedList* userrec::GetInviteList()
94 void userrec::InviteTo(char* channel)
97 strlcpy(i.channel,channel,CHANMAX);
101 void userrec::RemoveInvite(char* channel)
103 log(DEBUG,"Removing invites");
108 for (InvitedList::iterator i = invites.begin(); i != invites.end(); i++)
112 if (!strcasecmp(i->channel,channel))
123 bool userrec::HasPermission(char* command)
125 char TypeName[MAXBUF],Classes[MAXBUF],ClassName[MAXBUF],CommandList[MAXBUF];
130 // are they even an oper at all?
131 if (strchr(this->modes,'o'))
133 log(DEBUG,"*** HasPermission: %s is an oper",this->nick);
134 for (int j =0; j < ConfValueEnum("type",&config_f); j++)
136 ConfValue("type","name",j,TypeName,&config_f);
137 if (!strcmp(TypeName,this->oper))
139 log(DEBUG,"*** HasPermission: %s is an oper of type '%s'",this->nick,this->oper);
140 ConfValue("type","classes",j,Classes,&config_f);
141 char* myclass = strtok_r(Classes," ",&savept);
144 log(DEBUG,"*** HasPermission: checking classtype '%s'",myclass);
145 for (int k =0; k < ConfValueEnum("class",&config_f); k++)
147 ConfValue("class","name",k,ClassName,&config_f);
148 if (!strcmp(ClassName,myclass))
150 ConfValue("class","commands",k,CommandList,&config_f);
151 log(DEBUG,"*** HasPermission: found class named %s with commands: '%s'",ClassName,CommandList);
154 mycmd = strtok_r(CommandList," ",&savept2);
157 if (!strcasecmp(mycmd,command))
159 log(DEBUG,"*** Command %s found, returning true",command);
162 mycmd = strtok_r(NULL," ",&savept2);
166 myclass = strtok_r(NULL," ",&savept);
175 bool userrec::AddBuffer(std::string a)
178 for (int i = 0; i < a.length(); i++)
179 if ((a[i] != '\r') && (a[i] != '\0') && (a[i] != 7))
181 std::stringstream stream(recvq);
183 recvq = stream.str();
185 // count the size of the first line in the buffer.
186 while (i < recvq.length())
188 if (recvq[i++] == '\n')
191 if (recvq.length() > this->recvqmax)
193 this->SetWriteError("RecvQ exceeded");
194 WriteOpers("*** User %s RecvQ of %d exceeds connect class maximum of %d",this->nick,recvq.length(),this->recvqmax);
196 // return false if we've had more than 600 characters WITHOUT
197 // a carriage return (this is BAD, drop the socket)
201 bool userrec::BufferIsReady()
203 for (int i = 0; i < recvq.length(); i++)
204 if (recvq[i] == '\n')
209 void userrec::ClearBuffer()
214 std::string userrec::GetBuffer()
218 char* line = (char*)recvq.c_str();
219 std::string ret = "";
220 while ((*line != '\n') && (strlen(line)))
225 if ((*line == '\n') || (*line == '\r'))
231 void userrec::AddWriteBuf(std::string data)
233 if (this->GetWriteError() != "")
235 if (sendq.length() + data.length() > this->sendqmax)
237 WriteOpers("*** User %s SendQ of %d exceeds connect class maximum of %d",this->nick,sendq.length() + data.length(),this->sendqmax);
238 this->SetWriteError("SendQ exceeded");
241 std::stringstream stream;
242 stream << sendq << data;
243 sendq = stream.str();
246 // send AS MUCH OF THE USERS SENDQ as we are able to (might not be all of it)
247 void userrec::FlushWriteBuf()
251 char* tb = (char*)this->sendq.c_str();
252 int n_sent = write(this->fd,tb,this->sendq.length());
255 this->SetWriteError(strerror(errno));
262 // update the user's stats counters
263 this->bytes_out += n_sent;
269 void userrec::SetWriteError(std::string error)
271 log(DEBUG,"Setting error string for %s to '%s'",this->nick,error.c_str());
272 // don't try to set the error twice, its already set take the first string.
273 if (this->WriteError == "")
274 this->WriteError = error;
277 std::string userrec::GetWriteError()
279 return this->WriteError;