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