Split user list from Channel into ChannelUserList
[clinton/bobotpp.git] / source / Utils.C
1 // Utils.C -*- C++ -*-
2 // Copyright (c) 1997, 1998 Etienne BERNARD
3 // Copyright (c) 2005,2008 Clinton Ebadi
4
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // any later version.
9
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 #include "Utils.H"
24
25 #include <string>
26 #include <sstream>
27
28 #include <cctype>
29 #include <cstdlib>
30
31 #include "Bot.H"
32 #include "ChannelList.H"
33 #include "StringTokenizer.H"
34 #include "User.H"
35 #include "UserList.H"
36
37 std::string
38 Utils::get_nick (std::string nuh)
39 {
40 StringTokenizer st(nuh);
41 return st.next_token('!');
42 }
43
44 std::string
45 Utils::get_userhost (std::string nuh)
46 {
47 StringTokenizer st(nuh);
48 st.next_token('@');
49 return st.rest();
50 }
51
52 std::string
53 Utils::get_key()
54 {
55 return long2str (std::rand());
56 }
57
58 bool
59 Utils::IP_p (std::string host)
60 {
61 for (std::string::size_type i = 0; i < host.length(); i++)
62 if (!std::isdigit (host[i]) && host[i] !='.')
63 return false;
64 return true;
65 }
66
67 std::string
68 Utils::make_wildcard (std::string mask)
69 {
70 StringTokenizer st (mask);
71 std::cerr << "make_wildcard: \"" << mask << "\"\n";
72
73 st.next_token('!', true);
74 std::string nick = "*";
75
76 std::string user = st.next_token('@', true);
77 if (user[0] == '~' || user[0] == '^' ||
78 user[0] == '+' || user[0] == '-' ||
79 user[0] == '*')
80 user = user.substr (1);
81 if (user.length() < 10)
82 user = std::string("*") + user;
83
84 std::string host = st.rest();
85 StringTokenizer st2(host);
86
87 if (!wildcard_p (host))
88 {
89 if (IP_p (host))
90 {
91 host = st2.next_token('.') + ".";
92 host = host + st2.next_token('.') + ".";
93 host = host + st2.next_token('.') + ".*";
94 }
95 else
96 {
97 st2.next_token('.', true);
98 if (st2.count_tokens('.') > 1)
99 host = std::string("*.") + st2.rest();
100 }
101 }
102 else
103 {
104 if (host == "") host = "*";
105 }
106
107 return nick + "!" + user + "@" + host;
108 }
109
110 bool
111 Utils::channel_p(std::string c)
112 {
113 return (c[0] == '#' || c[0] == '&');
114 }
115
116 bool
117 Utils::wildcard_p (std::string c)
118 {
119 return (c.find('*') != std::string::npos);
120 }
121
122 bool
123 Utils::valid_channel_name_p (std::string c)
124 {
125 return channel_p (c) && c.find(',') == std::string::npos;
126 }
127
128 #define isvalid(c) (((c) >= 'A' && (c) <= '~') || std::isdigit(c) || (c) == '-')
129
130 bool
131 Utils::valid_nickname_p (const Bot *b, std::string n)
132 {
133 // FIXME: make max nick length configurable
134 if (n[0] == '-' || std::isdigit(n[0]) || n.length() > b->MAX_NICKLENGTH)
135 return false;
136
137 for (std::string::size_type i = 0; i < n.length(); i++)
138 if (!isvalid(n[i]) || std::isspace (n[i]))
139 return false;
140
141 return true;
142 }
143
144 int
145 Utils::get_level (Bot * b, std::string nuh)
146 {
147 return b->userList->getMaxLevel(nuh);
148 }
149
150 int
151 Utils::get_level (Bot * b, std::string nuh, std::string channel)
152 {
153 if (!channel_p (channel))
154 return get_level(b, nuh);
155
156 Channel * c = b->channelList->getChannel(channel);
157 if (c && c->hasNick (get_nick (nuh)))
158 return c->getUser(get_nick (nuh)).getLevel ();
159 else
160 return -1;
161
162 return b->userList->getLevel(nuh, channel);
163 }
164
165 std::string
166 Utils::level2str(int l)
167 {
168 switch (l)
169 {
170 case User::USER: return "User";
171 case User::TRUSTED_USER: return "Trusted User";
172 case User::FRIEND: return "Friend";
173 case User::MASTER: return "Master";
174 }
175
176 return "None";
177 }
178
179 std::string
180 Utils::prot2str(int p)
181 {
182 switch (p)
183 {
184 case User::NO_BAN: return "No ban";
185 case User::NO_KICK: return "No kick";
186 case User::NO_DEOP: return "No deop";
187 }
188
189 return "None";
190 }
191
192 std::string
193 Utils::bool2str(bool b)
194 {
195 // FIXME: should these be lowercase?
196 return b ? "True" : "False";
197 }
198
199 std::string
200 Utils::long2str (long l)
201 {
202 std::ostringstream temp;
203 temp << l;
204
205 return temp.str ();
206 }
207
208 time_t
209 Utils::str2time(std::string str)
210 {
211 std::string num;
212 time_t ans = 0;
213
214 // Make sure that str is nominally valid before allocating a buffer
215 if (to_lower (str) == "inf")
216 return -1;
217
218 if (!std::isdigit (str[0]))
219 return 0;
220
221 num.reserve (64); // reserve a buffer to speed things up
222
223 for (std::string::size_type i = 0; i < str.length(); i++)
224 {
225 switch (str[i])
226 {
227 case 'y':
228 case 'Y':
229 ans += (std::atoi (num.c_str ()) * 31557600);
230 num.clear ();
231 break;
232 case 'M':
233 ans += (std::atoi (num.c_str ()) * 2629800);
234 num.clear ();
235 break;
236 case 'd':
237 case 'D':
238 ans += (std::atoi (num.c_str ()) * 86400);
239 num.clear ();
240 break;
241 case 'h':
242 case 'H':
243 ans += (std::atoi (num.c_str ()) * 3600);
244 num.clear ();
245 break;
246 case 'm':
247 ans += (std::atoi (num.c_str ()) * 60);
248 num.clear ();
249 break;
250 case 's':
251 case 'S':
252 ans += std::atoi (num.c_str ());
253 num.clear ();
254 default:
255 if (std::isdigit(str[i]))
256 num += str[i];
257 else
258 return 0;
259 }
260 }
261
262 if (!num.empty ())
263 ans += std::atoi (num.c_str ());
264
265 return std::time (0) + ans;
266 }
267
268 std::string
269 Utils::to_lower (std::string s)
270 {
271 std::string::iterator it;
272
273 for (it = s.begin (); it != s.end (); ++it)
274 *it = std::tolower (*it);
275
276 return s;
277 }
278
279 std::string
280 Utils::to_upper (std::string s)
281 {
282 std::string::iterator it;
283
284 for (it = s.begin (); it != s.end (); it++)
285 *it = std::toupper (*it);
286
287 return s;
288 }
289
290 std::string
291 Utils::trim_str (std::string s)
292 {
293 int i = 0, j = s.length () - 1;
294
295 while (i < j && std::isspace (s[i]))
296 i++;
297
298 while (j > 0 && std::isspace (s[j]))
299 j--;
300
301 return s.substr (i, j - i + 1);
302 }
303
304 #ifdef USESCRIPTS
305 // Returns a std::string from an SCM argument
306 std::string
307 Utils::scm2str (SCM s)
308 {
309 // FIXME: uses gh_, replace with scm_
310 char *tmp = scm_to_locale_string (s);
311 std::string temp (tmp);
312
313 free(tmp);
314
315 return temp;
316 }
317
318 // Returns a SCM from an std::string argument
319 SCM
320 Utils::str2scm (std::string s)
321 {
322 return scm_from_locale_string (s.c_str ());
323 }
324 #endif