]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/users.cpp
Fixed bug #85
[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                 for (int j =0; j < ConfValueEnum("type",&config_f); j++)
171                 {
172                         ConfValue("type","name",j,TypeName,&config_f);
173                         if (!strcmp(TypeName,this->oper))
174                         {
175                                 ConfValue("type","classes",j,Classes,&config_f);
176                                 char* myclass = strtok_r(Classes," ",&savept);
177                                 while (myclass)
178                                 {
179                                         for (int k =0; k < ConfValueEnum("class",&config_f); k++)
180                                         {
181                                                 ConfValue("class","name",k,ClassName,&config_f);
182                                                 if (!strcmp(ClassName,myclass))
183                                                 {
184                                                         ConfValue("class","commands",k,CommandList,&config_f);
185                                                         mycmd = strtok_r(CommandList," ",&savept2);
186                                                         while (mycmd)
187                                                         {
188                                                                 if ((!strcasecmp(mycmd,command)) || (*mycmd == '*'))
189                                                                 {
190                                                                         return true;
191                                                                 }
192                                                                 mycmd = strtok_r(NULL," ",&savept2);
193                                                         }
194                                                 }
195                                         }
196                                         myclass = strtok_r(NULL," ",&savept);
197                                 }
198                         }
199                 }
200         }
201         return false;
202 }
203
204
205 bool userrec::AddBuffer(std::string a)
206 {
207         std::string b = "";
208         for (unsigned int i = 0; i < a.length(); i++)
209                 if ((a[i] != '\r') && (a[i] != '\0') && (a[i] != 7))
210                         b = b + a[i];
211         std::stringstream stream(recvq);
212         stream << b;
213         recvq = stream.str();
214         unsigned int i = 0;
215         // count the size of the first line in the buffer.
216         while (i < recvq.length())
217         {
218                 if (recvq[i++] == '\n')
219                         break;
220         }
221         if (recvq.length() > (unsigned)this->recvqmax)
222         {
223                 this->SetWriteError("RecvQ exceeded");
224                 WriteOpers("*** User %s RecvQ of %d exceeds connect class maximum of %d",this->nick,recvq.length(),this->recvqmax);
225         }
226         // return false if we've had more than 600 characters WITHOUT
227         // a carriage return (this is BAD, drop the socket)
228         return (i < 600);
229 }
230
231 bool userrec::BufferIsReady()
232 {
233         for (unsigned int i = 0; i < recvq.length(); i++)
234                 if (recvq[i] == '\n')
235                         return true;
236         return false;
237 }
238
239 void userrec::ClearBuffer()
240 {
241         recvq = "";
242 }
243
244 std::string userrec::GetBuffer()
245 {
246         if (recvq == "")
247                 return "";
248         char* line = (char*)recvq.c_str();
249         std::string ret = "";
250         while ((*line != '\n') && (strlen(line)))
251         {
252                 ret = ret + *line;
253                 line++;
254         }
255         if ((*line == '\n') || (*line == '\r'))
256                 line++;
257         recvq = line;
258         return ret;
259 }
260
261 void userrec::AddWriteBuf(std::string data)
262 {
263         if (this->GetWriteError() != "")
264                 return;
265         if (sendq.length() + data.length() > (unsigned)this->sendqmax)
266         {
267                 WriteOpers("*** User %s SendQ of %d exceeds connect class maximum of %d",this->nick,sendq.length() + data.length(),this->sendqmax);
268                 this->SetWriteError("SendQ exceeded");
269                 return;
270         }
271         std::stringstream stream;
272         stream << sendq << data;
273         sendq = stream.str();
274 }
275
276 // send AS MUCH OF THE USERS SENDQ as we are able to (might not be all of it)
277 void userrec::FlushWriteBuf()
278 {
279         if (sendq.length())
280         {
281                 char* tb = (char*)this->sendq.c_str();
282                 int n_sent = write(this->fd,tb,this->sendq.length());
283                 if (n_sent == -1)
284                 {
285                         this->SetWriteError(strerror(errno));
286                 }
287                 else
288                 {
289                         // advance the queue
290                         tb += n_sent;
291                         this->sendq = tb;
292                         // update the user's stats counters
293                         this->bytes_out += n_sent;
294                         this->cmds_out++;
295                 }
296         }
297 }
298
299 void userrec::SetWriteError(std::string error)
300 {
301         log(DEBUG,"Setting error string for %s to '%s'",this->nick,error.c_str());
302         // don't try to set the error twice, its already set take the first string.
303         if (this->WriteError == "")
304                 this->WriteError = error;
305 }
306
307 std::string userrec::GetWriteError()
308 {
309         return this->WriteError;
310 }