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"
30 #include "helperfuncs.h"
32 extern std::stringstream config_f;
33 extern char ServerName[MAXBUF];
39 // the PROPER way to do it, AVOID bzero at *ALL* costs
41 strcpy(ip,"127.0.0.1");
48 server = (char*)FindServerNamePtr(ServerName);
53 fd = lastping = signon = idle_lastmsg = nping = registered = 0;
54 flood = port = bytes_in = bytes_out = cmds_in = cmds_out = 0;
59 for (int i = 0; i < MAXCHANS; i++)
61 this->chans[i].channel = NULL;
62 this->chans[i].uc_modes = 0;
70 // for local clients, clean up their dns thread
71 if (!strcmp(this->server,ServerName))
73 pthread_kill(this->dnsthread,9);
78 void userrec::CloseSocket()
84 char* userrec::GetFullHost()
86 static char result[MAXBUF];
87 snprintf(result,MAXBUF,"%s!%s@%s",nick,ident,dhost);
91 int userrec::ReadData(void* buffer, size_t size)
95 log(DEBUG,"userrec::ReadData on fd %d",this->fd);
96 return read(this->fd, buffer, size);
102 char* userrec::GetFullRealHost()
104 static char fresult[MAXBUF];
105 snprintf(fresult,MAXBUF,"%s!%s@%s",nick,ident,host);
109 bool userrec::IsInvited(char* channel)
111 for (InvitedList::iterator i = invites.begin(); i != invites.end(); i++)
114 if (!strcasecmp(i->channel,channel))
123 InvitedList* userrec::GetInviteList()
128 void userrec::InviteTo(char* channel)
131 strlcpy(i.channel,channel,CHANMAX);
132 invites.push_back(i);
135 void userrec::RemoveInvite(char* channel)
137 log(DEBUG,"Removing invites");
142 for (InvitedList::iterator i = invites.begin(); i != invites.end(); i++)
146 if (!strcasecmp(i->channel,channel))
157 bool userrec::HasPermission(char* command)
159 char TypeName[MAXBUF],Classes[MAXBUF],ClassName[MAXBUF],CommandList[MAXBUF];
164 // users on u-lined servers can completely bypass
165 // all permissions based checks.
167 // of course, if this is sent to a remote server and this
168 // server is not ulined there, then that other server
169 // silently drops the command.
170 if (is_uline(this->server))
173 // are they even an oper at all?
174 if (strchr(this->modes,'o'))
176 log(DEBUG,"*** HasPermission: %s is an oper",this->nick);
177 for (int j =0; j < ConfValueEnum("type",&config_f); j++)
179 ConfValue("type","name",j,TypeName,&config_f);
180 if (!strcmp(TypeName,this->oper))
182 log(DEBUG,"*** HasPermission: %s is an oper of type '%s'",this->nick,this->oper);
183 ConfValue("type","classes",j,Classes,&config_f);
184 char* myclass = strtok_r(Classes," ",&savept);
187 log(DEBUG,"*** HasPermission: checking classtype '%s'",myclass);
188 for (int k =0; k < ConfValueEnum("class",&config_f); k++)
190 ConfValue("class","name",k,ClassName,&config_f);
191 if (!strcmp(ClassName,myclass))
193 ConfValue("class","commands",k,CommandList,&config_f);
194 log(DEBUG,"*** HasPermission: found class named %s with commands: '%s'",ClassName,CommandList);
197 mycmd = strtok_r(CommandList," ",&savept2);
200 if (!strcasecmp(mycmd,command))
202 log(DEBUG,"*** Command %s found, returning true",command);
205 mycmd = strtok_r(NULL," ",&savept2);
209 myclass = strtok_r(NULL," ",&savept);
218 bool userrec::AddBuffer(std::string a)
221 for (unsigned int i = 0; i < a.length(); i++)
222 if ((a[i] != '\r') && (a[i] != '\0') && (a[i] != 7))
224 std::stringstream stream(recvq);
226 recvq = stream.str();
228 // count the size of the first line in the buffer.
229 while (i < recvq.length())
231 if (recvq[i++] == '\n')
234 if (recvq.length() > (unsigned)this->recvqmax)
236 this->SetWriteError("RecvQ exceeded");
237 WriteOpers("*** User %s RecvQ of %d exceeds connect class maximum of %d",this->nick,recvq.length(),this->recvqmax);
239 // return false if we've had more than 600 characters WITHOUT
240 // a carriage return (this is BAD, drop the socket)
244 bool userrec::BufferIsReady()
246 for (unsigned int i = 0; i < recvq.length(); i++)
247 if (recvq[i] == '\n')
252 void userrec::ClearBuffer()
257 std::string userrec::GetBuffer()
261 char* line = (char*)recvq.c_str();
262 std::string ret = "";
263 while ((*line != '\n') && (strlen(line)))
268 if ((*line == '\n') || (*line == '\r'))
274 void userrec::AddWriteBuf(std::string data)
276 if (this->GetWriteError() != "")
278 if (sendq.length() + data.length() > (unsigned)this->sendqmax)
280 WriteOpers("*** User %s SendQ of %d exceeds connect class maximum of %d",this->nick,sendq.length() + data.length(),this->sendqmax);
281 this->SetWriteError("SendQ exceeded");
284 std::stringstream stream;
285 stream << sendq << data;
286 sendq = stream.str();
289 // send AS MUCH OF THE USERS SENDQ as we are able to (might not be all of it)
290 void userrec::FlushWriteBuf()
294 char* tb = (char*)this->sendq.c_str();
295 int n_sent = write(this->fd,tb,this->sendq.length());
298 this->SetWriteError(strerror(errno));
305 // update the user's stats counters
306 this->bytes_out += n_sent;
312 void userrec::SetWriteError(std::string error)
314 log(DEBUG,"Setting error string for %s to '%s'",this->nick,error.c_str());
315 // don't try to set the error twice, its already set take the first string.
316 if (this->WriteError == "")
317 this->WriteError = error;
320 std::string userrec::GetWriteError()
322 return this->WriteError;