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