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