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