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