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