]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/users.cpp
c78eb4793978a83e03d6315873cd4c041feb3096
[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         // for local clients, clean up their dns thread
68         if (!strcmp(this->server,ServerName))
69         {
70                 pthread_kill(this->dnsthread,9);
71         }
72 }
73
74 void userrec::CloseSocket()
75 {
76         shutdown(this->fd,2);
77         close(this->fd);
78 }
79  
80 char* userrec::GetFullHost()
81 {
82         static char result[MAXBUF];
83         snprintf(result,MAXBUF,"%s!%s@%s",nick,ident,dhost);
84         return result;
85 }
86
87 int userrec::ReadData(void* buffer, size_t size)
88 {
89         if (this->fd > -1)
90         {
91                 log(DEBUG,"userrec::ReadData on fd %d",this->fd);
92                 return read(this->fd, buffer, size);
93         }
94         else return 0;
95 }
96
97
98 char* userrec::GetFullRealHost()
99 {
100         static char fresult[MAXBUF];
101         snprintf(fresult,MAXBUF,"%s!%s@%s",nick,ident,host);
102         return fresult;
103 }
104
105 bool userrec::IsInvited(char* channel)
106 {
107         for (InvitedList::iterator i = invites.begin(); i != invites.end(); i++)
108         {
109                 if (i->channel) {
110                         if (!strcasecmp(i->channel,channel))
111                         {
112                                 return true;
113                         }
114                 }
115         }
116         return false;
117 }
118
119 InvitedList* userrec::GetInviteList()
120 {
121         return &invites;
122 }
123
124 void userrec::InviteTo(char* channel)
125 {
126         Invited i;
127         strlcpy(i.channel,channel,CHANMAX);
128         invites.push_back(i);
129 }
130
131 void userrec::RemoveInvite(char* channel)
132 {
133         log(DEBUG,"Removing invites");
134         if (channel)
135         {
136                 if (invites.size())
137                 {
138                         for (InvitedList::iterator i = invites.begin(); i != invites.end(); i++)
139                         {
140                                 if (i->channel)
141                                 {
142                                         if (!strcasecmp(i->channel,channel))
143                                         {
144                                                 invites.erase(i);
145                                                 return;
146                                         }
147                                 }
148                         }
149                 }
150         }
151 }
152
153 bool userrec::HasPermission(char* command)
154 {
155         char TypeName[MAXBUF],Classes[MAXBUF],ClassName[MAXBUF],CommandList[MAXBUF];
156         char* mycmd;
157         char* savept;
158         char* savept2;
159         
160         // are they even an oper at all?
161         if (strchr(this->modes,'o'))
162         {
163                 log(DEBUG,"*** HasPermission: %s is an oper",this->nick);
164                 for (int j =0; j < ConfValueEnum("type",&config_f); j++)
165                 {
166                         ConfValue("type","name",j,TypeName,&config_f);
167                         if (!strcmp(TypeName,this->oper))
168                         {
169                                 log(DEBUG,"*** HasPermission: %s is an oper of type '%s'",this->nick,this->oper);
170                                 ConfValue("type","classes",j,Classes,&config_f);
171                                 char* myclass = strtok_r(Classes," ",&savept);
172                                 while (myclass)
173                                 {
174                                         log(DEBUG,"*** HasPermission: checking classtype '%s'",myclass);
175                                         for (int k =0; k < ConfValueEnum("class",&config_f); k++)
176                                         {
177                                                 ConfValue("class","name",k,ClassName,&config_f);
178                                                 if (!strcmp(ClassName,myclass))
179                                                 {
180                                                         ConfValue("class","commands",k,CommandList,&config_f);
181                                                         log(DEBUG,"*** HasPermission: found class named %s with commands: '%s'",ClassName,CommandList);
182                                                         
183                                                         
184                                                         mycmd = strtok_r(CommandList," ",&savept2);
185                                                         while (mycmd)
186                                                         {
187                                                                 if (!strcasecmp(mycmd,command))
188                                                                 {
189                                                                         log(DEBUG,"*** Command %s found, returning true",command);
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 }