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