]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/users.cpp
b5a30093a64745dfc8fb4b5ec642ea8ce8984ecc
[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 #ifdef THREADED_DNS
71         // for local clients, clean up their dns thread
72         if (!strcmp(this->server,ServerName))
73         {
74                 pthread_kill(this->dnsthread,9);
75         }
76 #endif
77 }
78
79 void userrec::CloseSocket()
80 {
81         shutdown(this->fd,2);
82         close(this->fd);
83 }
84  
85 char* userrec::GetFullHost()
86 {
87         static char result[MAXBUF];
88         snprintf(result,MAXBUF,"%s!%s@%s",nick,ident,dhost);
89         return result;
90 }
91
92 int userrec::ReadData(void* buffer, size_t size)
93 {
94         if (this->fd > -1)
95         {
96                 log(DEBUG,"userrec::ReadData on fd %d",this->fd);
97                 return read(this->fd, buffer, size);
98         }
99         else return 0;
100 }
101
102
103 char* userrec::GetFullRealHost()
104 {
105         static char fresult[MAXBUF];
106         snprintf(fresult,MAXBUF,"%s!%s@%s",nick,ident,host);
107         return fresult;
108 }
109
110 bool userrec::IsInvited(char* channel)
111 {
112         for (InvitedList::iterator i = invites.begin(); i != invites.end(); i++)
113         {
114                 if (i->channel) {
115                         if (!strcasecmp(i->channel,channel))
116                         {
117                                 return true;
118                         }
119                 }
120         }
121         return false;
122 }
123
124 InvitedList* userrec::GetInviteList()
125 {
126         return &invites;
127 }
128
129 void userrec::InviteTo(char* channel)
130 {
131         Invited i;
132         strlcpy(i.channel,channel,CHANMAX);
133         invites.push_back(i);
134 }
135
136 void userrec::RemoveInvite(char* channel)
137 {
138         log(DEBUG,"Removing invites");
139         if (channel)
140         {
141                 if (invites.size())
142                 {
143                         for (InvitedList::iterator i = invites.begin(); i != invites.end(); i++)
144                         {
145                                 if (i->channel)
146                                 {
147                                         if (!strcasecmp(i->channel,channel))
148                                         {
149                                                 invites.erase(i);
150                                                 return;
151                                         }
152                                 }
153                         }
154                 }
155         }
156 }
157
158 bool userrec::HasPermission(char* command)
159 {
160         char TypeName[MAXBUF],Classes[MAXBUF],ClassName[MAXBUF],CommandList[MAXBUF];
161         char* mycmd;
162         char* savept;
163         char* savept2;
164         
165         // users on u-lined servers can completely bypass
166         // all permissions based checks.
167         //
168         // of course, if this is sent to a remote server and this
169         // server is not ulined there, then that other server
170         // silently drops the command.
171         if (is_uline(this->server))
172                 return true;
173         
174         // are they even an oper at all?
175         if (strchr(this->modes,'o'))
176         {
177                 log(DEBUG,"*** HasPermission: %s is an oper",this->nick);
178                 for (int j =0; j < ConfValueEnum("type",&config_f); j++)
179                 {
180                         ConfValue("type","name",j,TypeName,&config_f);
181                         if (!strcmp(TypeName,this->oper))
182                         {
183                                 log(DEBUG,"*** HasPermission: %s is an oper of type '%s'",this->nick,this->oper);
184                                 ConfValue("type","classes",j,Classes,&config_f);
185                                 char* myclass = strtok_r(Classes," ",&savept);
186                                 while (myclass)
187                                 {
188                                         log(DEBUG,"*** HasPermission: checking classtype '%s'",myclass);
189                                         for (int k =0; k < ConfValueEnum("class",&config_f); k++)
190                                         {
191                                                 ConfValue("class","name",k,ClassName,&config_f);
192                                                 if (!strcmp(ClassName,myclass))
193                                                 {
194                                                         ConfValue("class","commands",k,CommandList,&config_f);
195                                                         log(DEBUG,"*** HasPermission: found class named %s with commands: '%s'",ClassName,CommandList);
196                                                         
197                                                         
198                                                         mycmd = strtok_r(CommandList," ",&savept2);
199                                                         while (mycmd)
200                                                         {
201                                                                 if (!strcasecmp(mycmd,command))
202                                                                 {
203                                                                         log(DEBUG,"*** Command %s found, returning true",command);
204                                                                         return true;
205                                                                 }
206                                                                 mycmd = strtok_r(NULL," ",&savept2);
207                                                         }
208                                                 }
209                                         }
210                                         myclass = strtok_r(NULL," ",&savept);
211                                 }
212                         }
213                 }
214         }
215         return false;
216 }
217
218
219 bool userrec::AddBuffer(std::string a)
220 {
221         std::string b = "";
222         for (unsigned int i = 0; i < a.length(); i++)
223                 if ((a[i] != '\r') && (a[i] != '\0') && (a[i] != 7))
224                         b = b + a[i];
225         std::stringstream stream(recvq);
226         stream << b;
227         recvq = stream.str();
228         unsigned int i = 0;
229         // count the size of the first line in the buffer.
230         while (i < recvq.length())
231         {
232                 if (recvq[i++] == '\n')
233                         break;
234         }
235         if (recvq.length() > (unsigned)this->recvqmax)
236         {
237                 this->SetWriteError("RecvQ exceeded");
238                 WriteOpers("*** User %s RecvQ of %d exceeds connect class maximum of %d",this->nick,recvq.length(),this->recvqmax);
239         }
240         // return false if we've had more than 600 characters WITHOUT
241         // a carriage return (this is BAD, drop the socket)
242         return (i < 600);
243 }
244
245 bool userrec::BufferIsReady()
246 {
247         for (unsigned int i = 0; i < recvq.length(); i++)
248                 if (recvq[i] == '\n')
249                         return true;
250         return false;
251 }
252
253 void userrec::ClearBuffer()
254 {
255         recvq = "";
256 }
257
258 std::string userrec::GetBuffer()
259 {
260         if (recvq == "")
261                 return "";
262         char* line = (char*)recvq.c_str();
263         std::string ret = "";
264         while ((*line != '\n') && (strlen(line)))
265         {
266                 ret = ret + *line;
267                 line++;
268         }
269         if ((*line == '\n') || (*line == '\r'))
270                 line++;
271         recvq = line;
272         return ret;
273 }
274
275 void userrec::AddWriteBuf(std::string data)
276 {
277         if (this->GetWriteError() != "")
278                 return;
279         if (sendq.length() + data.length() > (unsigned)this->sendqmax)
280         {
281                 WriteOpers("*** User %s SendQ of %d exceeds connect class maximum of %d",this->nick,sendq.length() + data.length(),this->sendqmax);
282                 this->SetWriteError("SendQ exceeded");
283                 return;
284         }
285         std::stringstream stream;
286         stream << sendq << data;
287         sendq = stream.str();
288 }
289
290 // send AS MUCH OF THE USERS SENDQ as we are able to (might not be all of it)
291 void userrec::FlushWriteBuf()
292 {
293         if (sendq.length())
294         {
295                 char* tb = (char*)this->sendq.c_str();
296                 int n_sent = write(this->fd,tb,this->sendq.length());
297                 if (n_sent == -1)
298                 {
299                         this->SetWriteError(strerror(errno));
300                 }
301                 else
302                 {
303                         // advance the queue
304                         tb += n_sent;
305                         this->sendq = tb;
306                         // update the user's stats counters
307                         this->bytes_out += n_sent;
308                         this->cmds_out++;
309                 }
310         }
311 }
312
313 void userrec::SetWriteError(std::string error)
314 {
315         log(DEBUG,"Setting error string for %s to '%s'",this->nick,error.c_str());
316         // don't try to set the error twice, its already set take the first string.
317         if (this->WriteError == "")
318                 this->WriteError = error;
319 }
320
321 std::string userrec::GetWriteError()
322 {
323         return this->WriteError;
324 }