[project @ 2005-06-28 03:16:45 by unknown_lamer]
[clinton/bobotpp.git] / source / Utils.C
CommitLineData
cb21075d 1// Utils.C -*- C++ -*-
2// Copyright (c) 1997, 1998 Etienne BERNARD
a6339323 3// Copyright (c) 2005 Clinton Ebadi
cb21075d 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
39b022cb 17// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
cb21075d 18
19#ifdef HAVE_CONFIG_H
20#include "config.h"
21#endif
22
23#include <cctype>
24#include <cstdlib>
a6339323 25#include <string>
26#include <sstream>
cb21075d 27
28#include "Utils.H"
29#include "StringTokenizer.H"
30
a6339323 31std::string
32Utils::get_nick (std::string nuh)
cb21075d 33{
34 StringTokenizer st(nuh);
a6339323 35 return st.next_token('!');
cb21075d 36}
37
a6339323 38std::string
39Utils::get_userhost (std::string nuh)
cb21075d 40{
41 StringTokenizer st(nuh);
a6339323 42 st.next_token('@');
cb21075d 43 return st.rest();
44}
45
a6339323 46std::string
47Utils::get_key()
cb21075d 48{
a6339323 49 return long2str (std::rand());
cb21075d 50}
51
52bool
a6339323 53Utils::IP_p (std::string host)
cb21075d 54{
cf8ea873 55 for (std::string::size_type i = 0; i < host.length(); i++)
a6339323 56 if (!std::isdigit (host[i]) && host[i] !='.')
cb21075d 57 return false;
58 return true;
59}
60
a6339323 61std::string
62Utils::make_wildcard (std::string mask)
cb21075d 63{
a6339323 64 StringTokenizer st (mask);
cb21075d 65
a6339323 66 st.next_token('!', true);
67 std::string nick = "*";
cb21075d 68
a6339323 69 std::string user = st.next_token('@', true);
cb21075d 70 if (user[0] == '~' || user[0] == '^' ||
71 user[0] == '+' || user[0] == '-' ||
72 user[0] == '*')
a6339323 73 user = user.substr (1);
cb21075d 74 if (user.length() < 10)
a6339323 75 user = std::string("*") + user;
cb21075d 76
a6339323 77 std::string host = st.rest();
cb21075d 78 StringTokenizer st2(host);
a6339323 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 = "*";
cb21075d 98 }
cb21075d 99
100 std::cout << nick + "!" + user + "@" + host << std::endl;
a6339323 101
cb21075d 102 return nick + "!" + user + "@" + host;
103}
104
105bool
a6339323 106Utils::channel_p(std::string c)
cb21075d 107{
108 return (c[0] == '#' || c[0] == '&');
109}
110
111bool
a6339323 112Utils::wildcard_p (std::string c)
cb21075d 113{
a6339323 114 return (c.find('*') != std::string::npos);
cb21075d 115}
116
117bool
a6339323 118Utils::valid_channel_name_p (std::string c)
cb21075d 119{
a6339323 120 return channel_p (c) && c.find(',') == std::string::npos;
cb21075d 121}
122
a6339323 123#define isvalid(c) (((c) >= 'A' && (c) <= '~') || std::isdigit(c) || (c) == '-')
cb21075d 124
125bool
6b7614a8 126Utils::valid_nickname_p (const Bot *b, std::string n)
cb21075d 127{
a6339323 128 // FIXME: make max nick length configurable
6b7614a8 129 if (n[0] == '-' || std::isdigit(n[0]) || n.length() > b->MAX_NICKLENGTH)
cb21075d 130 return false;
131
cf8ea873 132 for (std::string::size_type i = 0; i < n.length(); i++)
a6339323 133 if (!isvalid(n[i]) || std::isspace (n[i]))
cb21075d 134 return false;
135
136 return true;
137}
138
139int
a6339323 140Utils::get_level (Bot * b, std::string nuh)
cb21075d 141{
142 return b->userList->getMaxLevel(nuh);
143}
144
145int
a6339323 146Utils::get_level (Bot * b, std::string nuh, std::string channel)
cb21075d 147{
a6339323 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 {
cb21075d 158 return -1;
159 }
160
161 return b->userList->getLevel(nuh, channel);
162}
163
a6339323 164std::string
165Utils::level2str(int l)
cb21075d 166{
a6339323 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
cb21075d 175 return "None";
176}
177
a6339323 178std::string
179Utils::prot2str(int p)
cb21075d 180{
a6339323 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
cb21075d 188 return "None";
189}
190
a6339323 191std::string
192Utils::bool2str(bool b)
cb21075d 193{
a6339323 194 // FIXME: should these be lowercase?
cb21075d 195 return b ? "True" : "False";
196}
197
a6339323 198std::string
199Utils::long2str (long l)
200{
201 std::ostringstream temp;
202 temp << l;
203
204 return temp.str ();
205}
206
cb21075d 207time_t
a6339323 208Utils::str2time(std::string str)
cb21075d 209{
a6339323 210 std::string num;
cb21075d 211 time_t ans = 0;
a6339323 212
213 // Make sure that str is nominally valid before allocating a buffer
214 if (to_lower (str) == "inf")
cb21075d 215 return -1;
a6339323 216
217 if (!std::isdigit (str[0]))
cb21075d 218 return 0;
219
cf8ea873 220 num.reserve (64); // reserve a buffer to speed things up
a6339323 221
cf8ea873 222 for (std::string::size_type i = 0; i < str.length(); i++)
a6339323 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 }
cb21075d 259 }
a6339323 260
261 if (!num.empty ())
262 ans += std::atoi (num.c_str ());
263
264 return std::time (0) + ans;
265}
266
267std::string
268Utils::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}
cb21075d 277
a6339323 278std::string
279Utils::to_upper (std::string s)
280{
281 std::string::iterator it;
282
0bca109b 283 for (it = s.begin (); it != s.end (); it++)
a6339323 284 *it = std::toupper (*it);
cb21075d 285
a6339323 286 return s;
287}
288
289std::string
290Utils::trim_str (std::string s)
291{
0bca109b 292 int i = 0, j = s.length () - 1;
a6339323 293
294 while (i < j && std::isspace (s[i]))
295 i++;
296
297 while (j > 0 && std::isspace (s[j]))
298 j--;
299
0bca109b 300 return s.substr (i, j - i + 1);
cb21075d 301}
302
303#ifdef USESCRIPTS
a6339323 304// Returns a std::string from an SCM argument
305std::string
306Utils::scm2str (SCM s)
cb21075d 307{
a6339323 308 // FIXME: uses gh_, replace with scm_
cb21075d 309 char *tmp = gh_scm2newstr(s, 0);
a6339323 310 std::string temp (tmp);
311
cb21075d 312 free(tmp);
a6339323 313
cb21075d 314 return temp;
315}
316
a6339323 317// Returns a SCM from an std::string argument
cb21075d 318SCM
a6339323 319Utils::str2scm (std::string s)
cb21075d 320{
a6339323 321 return gh_str02scm (s.c_str ());
cb21075d 322}
323#endif