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