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