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;
68 void userrec::CloseSocket()
74 char* userrec::GetFullHost()
76 static char result[MAXBUF];
77 snprintf(result,MAXBUF,"%s!%s@%s",nick,ident,dhost);
81 int userrec::ReadData(void* buffer, size_t size)
85 return read(this->fd, buffer, size);
91 char* userrec::GetFullRealHost()
93 static char fresult[MAXBUF];
94 snprintf(fresult,MAXBUF,"%s!%s@%s",nick,ident,host);
98 bool userrec::IsInvited(char* channel)
100 for (InvitedList::iterator i = invites.begin(); i != invites.end(); i++)
103 if (!strcasecmp(i->channel,channel))
112 InvitedList* userrec::GetInviteList()
117 void userrec::InviteTo(char* channel)
120 strlcpy(i.channel,channel,CHANMAX);
121 invites.push_back(i);
124 void userrec::RemoveInvite(char* channel)
126 log(DEBUG,"Removing invites");
131 for (InvitedList::iterator i = invites.begin(); i != invites.end(); i++)
135 if (!strcasecmp(i->channel,channel))
146 bool userrec::HasPermission(char* command)
148 char TypeName[MAXBUF],Classes[MAXBUF],ClassName[MAXBUF],CommandList[MAXBUF];
153 // users on u-lined servers can completely bypass
154 // all permissions based checks.
156 // of course, if this is sent to a remote server and this
157 // server is not ulined there, then that other server
158 // silently drops the command.
159 if (is_uline(this->server))
162 // are they even an oper at all?
163 if (strchr(this->modes,'o'))
165 for (int j =0; j < ConfValueEnum("type",&config_f); j++)
167 ConfValue("type","name",j,TypeName,&config_f);
168 if (!strcmp(TypeName,this->oper))
170 ConfValue("type","classes",j,Classes,&config_f);
171 char* myclass = strtok_r(Classes," ",&savept);
174 for (int k =0; k < ConfValueEnum("class",&config_f); k++)
176 ConfValue("class","name",k,ClassName,&config_f);
177 if (!strcmp(ClassName,myclass))
179 ConfValue("class","commands",k,CommandList,&config_f);
180 mycmd = strtok_r(CommandList," ",&savept2);
183 if ((!strcasecmp(mycmd,command)) || (*mycmd == '*'))
187 mycmd = strtok_r(NULL," ",&savept2);
191 myclass = strtok_r(NULL," ",&savept);
200 bool userrec::AddBuffer(std::string a)
203 for (unsigned int i = 0; i < a.length(); i++)
204 if ((a[i] != '\r') && (a[i] != '\0') && (a[i] != 7))
206 std::stringstream stream(recvq);
208 recvq = stream.str();
210 // count the size of the first line in the buffer.
211 while (i < recvq.length())
213 if (recvq[i++] == '\n')
216 if (recvq.length() > (unsigned)this->recvqmax)
218 this->SetWriteError("RecvQ exceeded");
219 WriteOpers("*** User %s RecvQ of %d exceeds connect class maximum of %d",this->nick,recvq.length(),this->recvqmax);
221 // return false if we've had more than 600 characters WITHOUT
222 // a carriage return (this is BAD, drop the socket)
226 bool userrec::BufferIsReady()
228 for (unsigned int i = 0; i < recvq.length(); i++)
229 if (recvq[i] == '\n')
234 void userrec::ClearBuffer()
239 std::string userrec::GetBuffer()
243 char* line = (char*)recvq.c_str();
244 std::string ret = "";
245 while ((*line != '\n') && (strlen(line)))
250 if ((*line == '\n') || (*line == '\r'))
256 void userrec::AddWriteBuf(std::string data)
258 if (this->GetWriteError() != "")
260 if (sendq.length() + data.length() > (unsigned)this->sendqmax)
262 WriteOpers("*** User %s SendQ of %d exceeds connect class maximum of %d",this->nick,sendq.length() + data.length(),this->sendqmax);
263 this->SetWriteError("SendQ exceeded");
266 std::stringstream stream;
267 stream << sendq << data;
268 sendq = stream.str();
271 // send AS MUCH OF THE USERS SENDQ as we are able to (might not be all of it)
272 void userrec::FlushWriteBuf()
276 char* tb = (char*)this->sendq.c_str();
277 int n_sent = write(this->fd,tb,this->sendq.length());
280 this->SetWriteError(strerror(errno));
287 // update the user's stats counters
288 this->bytes_out += n_sent;
294 void userrec::SetWriteError(std::string error)
296 log(DEBUG,"Setting error string for %s to '%s'",this->nick,error.c_str());
297 // don't try to set the error twice, its already set take the first string.
298 if (this->WriteError == "")
299 this->WriteError = error;
302 std::string userrec::GetWriteError()
304 return this->WriteError;