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