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