]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/users.cpp
6d4195f456e75437dce69999208f7b899cd47b3f
[user/henk/code/inspircd.git] / src / users.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include "inspircd_config.h" 
20 #include "channels.h"
21 #include "connection.h"
22 #include "users.h"
23 #include "inspircd.h"
24 #include <stdio.h>
25 #ifdef THREADED_DNS
26 #include <pthread.h>
27 #include <signal.h>
28 #endif
29 #include "inspstring.h"
30 #include "commands.h"
31 #include "helperfuncs.h"
32
33 extern ServerConfig* Config;
34 extern time_t TIME;
35
36 userrec::userrec()
37 {
38         // the PROPER way to do it, AVOID bzero at *ALL* costs
39         strcpy(nick,"");
40         strcpy(ip,"127.0.0.1");
41         timeout = 0;
42         strcpy(ident,"");
43         strcpy(host,"");
44         strcpy(dhost,"");
45         strcpy(fullname,"");
46         strcpy(modes,"");
47         server = (char*)FindServerNamePtr(Config->ServerName);
48         strcpy(awaymsg,"");
49         strcpy(oper,"");
50         reset_due = TIME;
51         lines_in = 0;
52         fd = lastping = signon = idle_lastmsg = nping = registered = 0;
53         flood = port = bytes_in = bytes_out = cmds_in = cmds_out = 0;
54         haspassed = false;
55         dns_done = false;
56         recvq = "";
57         sendq = "";
58         chans.clear();
59         invites.clear();
60 }
61
62 userrec::~userrec()
63 {
64 }
65
66 void userrec::CloseSocket()
67 {
68         shutdown(this->fd,2);
69         close(this->fd);
70 }
71  
72 char* userrec::GetFullHost()
73 {
74         static char result[MAXBUF];
75         snprintf(result,MAXBUF,"%s!%s@%s",nick,ident,dhost);
76         return result;
77 }
78
79 int userrec::ReadData(void* buffer, size_t size)
80 {
81         if (this->fd > -1)
82         {
83                 return read(this->fd, buffer, size);
84         }
85         else return 0;
86 }
87
88
89 char* userrec::GetFullRealHost()
90 {
91         static char fresult[MAXBUF];
92         snprintf(fresult,MAXBUF,"%s!%s@%s",nick,ident,host);
93         return fresult;
94 }
95
96 bool userrec::IsInvited(char* channel)
97 {
98         for (InvitedList::iterator i = invites.begin(); i != invites.end(); i++)
99         {
100                 if (i->channel) {
101                         if (!strcasecmp(i->channel,channel))
102                         {
103                                 return true;
104                         }
105                 }
106         }
107         return false;
108 }
109
110 InvitedList* userrec::GetInviteList()
111 {
112         return &invites;
113 }
114
115 void userrec::InviteTo(char* channel)
116 {
117         Invited i;
118         strlcpy(i.channel,channel,CHANMAX);
119         invites.push_back(i);
120 }
121
122 void userrec::RemoveInvite(char* channel)
123 {
124         log(DEBUG,"Removing invites");
125         if (channel)
126         {
127                 if (invites.size())
128                 {
129                         for (InvitedList::iterator i = invites.begin(); i != invites.end(); i++)
130                         {
131                                 if (i->channel)
132                                 {
133                                         if (!strcasecmp(i->channel,channel))
134                                         {
135                                                 invites.erase(i);
136                                                 return;
137                                         }
138                                 }
139                         }
140                 }
141         }
142 }
143
144 bool userrec::HasPermission(char* command)
145 {
146         char TypeName[MAXBUF],Classes[MAXBUF],ClassName[MAXBUF],CommandList[MAXBUF];
147         char* mycmd;
148         char* savept;
149         char* savept2;
150         
151         // users on u-lined servers can completely bypass
152         // all permissions based checks.
153         //
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))
158                 return true;
159         
160         // are they even an oper at all?
161         if (strchr(this->modes,'o'))
162         {
163                 for (int j =0; j < ConfValueEnum("type",&Config->config_f); j++)
164                 {
165                         ConfValue("type","name",j,TypeName,&Config->config_f);
166                         if (!strcmp(TypeName,this->oper))
167                         {
168                                 ConfValue("type","classes",j,Classes,&Config->config_f);
169                                 char* myclass = strtok_r(Classes," ",&savept);
170                                 while (myclass)
171                                 {
172                                         for (int k =0; k < ConfValueEnum("class",&Config->config_f); k++)
173                                         {
174                                                 ConfValue("class","name",k,ClassName,&Config->config_f);
175                                                 if (!strcmp(ClassName,myclass))
176                                                 {
177                                                         ConfValue("class","commands",k,CommandList,&Config->config_f);
178                                                         mycmd = strtok_r(CommandList," ",&savept2);
179                                                         while (mycmd)
180                                                         {
181                                                                 if ((!strcasecmp(mycmd,command)) || (*mycmd == '*'))
182                                                                 {
183                                                                         return true;
184                                                                 }
185                                                                 mycmd = strtok_r(NULL," ",&savept2);
186                                                         }
187                                                 }
188                                         }
189                                         myclass = strtok_r(NULL," ",&savept);
190                                 }
191                         }
192                 }
193         }
194         return false;
195 }
196
197
198 bool userrec::AddBuffer(std::string a)
199 {
200         std::string b = "";
201         for (unsigned int i = 0; i < a.length(); i++)
202                 if ((a[i] != '\r') && (a[i] != '\0') && (a[i] != 7))
203                         b = b + a[i];
204         std::stringstream stream(recvq);
205         stream << b;
206         recvq = stream.str();
207         unsigned int i = 0;
208         // count the size of the first line in the buffer.
209         while (i < recvq.length())
210         {
211                 if (recvq[i++] == '\n')
212                         break;
213         }
214         if (recvq.length() > (unsigned)this->recvqmax)
215         {
216                 this->SetWriteError("RecvQ exceeded");
217                 WriteOpers("*** User %s RecvQ of %d exceeds connect class maximum of %d",this->nick,recvq.length(),this->recvqmax);
218         }
219         // return false if we've had more than 600 characters WITHOUT
220         // a carriage return (this is BAD, drop the socket)
221         return (i < 600);
222 }
223
224 bool userrec::BufferIsReady()
225 {
226         for (unsigned int i = 0; i < recvq.length(); i++)
227                 if (recvq[i] == '\n')
228                         return true;
229         return false;
230 }
231
232 void userrec::ClearBuffer()
233 {
234         recvq = "";
235 }
236
237 std::string userrec::GetBuffer()
238 {
239         if (recvq == "")
240                 return "";
241         char* line = (char*)recvq.c_str();
242         std::string ret = "";
243         while ((*line != '\n') && (strlen(line)))
244         {
245                 ret = ret + *line;
246                 line++;
247         }
248         if ((*line == '\n') || (*line == '\r'))
249                 line++;
250         recvq = line;
251         return ret;
252 }
253
254 void userrec::AddWriteBuf(std::string data)
255 {
256         if (this->GetWriteError() != "")
257                 return;
258         if (sendq.length() + data.length() > (unsigned)this->sendqmax)
259         {
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");
262                 return;
263         }
264         std::stringstream stream;
265         stream << sendq << data;
266         sendq = stream.str();
267 }
268
269 // send AS MUCH OF THE USERS SENDQ as we are able to (might not be all of it)
270 void userrec::FlushWriteBuf()
271 {
272         if (sendq.length())
273         {
274                 char* tb = (char*)this->sendq.c_str();
275                 int n_sent = write(this->fd,tb,this->sendq.length());
276                 if (n_sent == -1)
277                 {
278                         this->SetWriteError(strerror(errno));
279                 }
280                 else
281                 {
282                         // advance the queue
283                         tb += n_sent;
284                         this->sendq = tb;
285                         // update the user's stats counters
286                         this->bytes_out += n_sent;
287                         this->cmds_out++;
288                 }
289         }
290 }
291
292 void userrec::SetWriteError(std::string error)
293 {
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;
298 }
299
300 std::string userrec::GetWriteError()
301 {
302         return this->WriteError;
303 }