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