2 // Copyright (c) 1997, 1998 Etienne BERNARD
3 // Copyright (c) 2005,2008 Clinton Ebadi
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
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.
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.
32 #include "ChannelList.H"
33 #include "StringTokenizer.H"
38 Utils::get_nick (std::string nuh)
40 StringTokenizer st(nuh);
41 return st.next_token('!');
45 Utils::get_userhost (std::string nuh)
47 StringTokenizer st(nuh);
55 return long2str (std::rand());
59 Utils::IP_p (std::string host)
61 for (std::string::size_type i = 0; i < host.length(); i++)
62 if (!std::isdigit (host[i]) && host[i] !='.')
68 Utils::make_wildcard (std::string mask)
70 StringTokenizer st (mask);
72 st.next_token('!', true);
73 std::string nick = "*";
75 std::string user = st.next_token('@', true);
76 if (user[0] == '~' || user[0] == '^' ||
77 user[0] == '+' || user[0] == '-' ||
79 user = user.substr (1);
80 if (user.length() < 10)
81 user = std::string("*") + user;
83 std::string host = st.rest();
84 StringTokenizer st2(host);
86 if (!wildcard_p (host))
90 host = st2.next_token('.') + ".";
91 host = host + st2.next_token('.') + ".";
92 host = host + st2.next_token('.') + ".*";
96 st2.next_token('.', true);
97 if (st2.count_tokens('.') > 1)
98 host = std::string("*.") + st2.rest();
103 if (host == "") host = "*";
106 std::cout << nick + "!" + user + "@" + host << std::endl;
108 return nick + "!" + user + "@" + host;
112 Utils::channel_p(std::string c)
114 return (c[0] == '#' || c[0] == '&');
118 Utils::wildcard_p (std::string c)
120 return (c.find('*') != std::string::npos);
124 Utils::valid_channel_name_p (std::string c)
126 return channel_p (c) && c.find(',') == std::string::npos;
129 #define isvalid(c) (((c) >= 'A' && (c) <= '~') || std::isdigit(c) || (c) == '-')
132 Utils::valid_nickname_p (const Bot *b, std::string n)
134 // FIXME: make max nick length configurable
135 if (n[0] == '-' || std::isdigit(n[0]) || n.length() > b->MAX_NICKLENGTH)
138 for (std::string::size_type i = 0; i < n.length(); i++)
139 if (!isvalid(n[i]) || std::isspace (n[i]))
146 Utils::get_level (Bot * b, std::string nuh)
148 return b->userList->getMaxLevel(nuh);
152 Utils::get_level (Bot * b, std::string nuh, std::string channel)
154 if (!channel_p (channel))
155 return get_level(b, nuh);
157 if (Channel * c = b->channelList->getChannel(channel))
159 User * u = c->getUser(get_nick (nuh));
161 return u->getLevel();
167 return b->userList->getLevel(nuh, channel);
171 Utils::level2str(int l)
175 case User::USER: return "User";
176 case User::TRUSTED_USER: return "Trusted User";
177 case User::FRIEND: return "Friend";
178 case User::MASTER: return "Master";
185 Utils::prot2str(int p)
189 case User::NO_BAN: return "No ban";
190 case User::NO_KICK: return "No kick";
191 case User::NO_DEOP: return "No deop";
198 Utils::bool2str(bool b)
200 // FIXME: should these be lowercase?
201 return b ? "True" : "False";
205 Utils::long2str (long l)
207 std::ostringstream temp;
214 Utils::str2time(std::string str)
219 // Make sure that str is nominally valid before allocating a buffer
220 if (to_lower (str) == "inf")
223 if (!std::isdigit (str[0]))
226 num.reserve (64); // reserve a buffer to speed things up
228 for (std::string::size_type i = 0; i < str.length(); i++)
234 ans += (std::atoi (num.c_str ()) * 31557600);
238 ans += (std::atoi (num.c_str ()) * 2629800);
243 ans += (std::atoi (num.c_str ()) * 86400);
248 ans += (std::atoi (num.c_str ()) * 3600);
252 ans += (std::atoi (num.c_str ()) * 60);
257 ans += std::atoi (num.c_str ());
260 if (std::isdigit(str[i]))
268 ans += std::atoi (num.c_str ());
270 return std::time (0) + ans;
274 Utils::to_lower (std::string s)
276 std::string::iterator it;
278 for (it = s.begin (); it != s.end (); ++it)
279 *it = std::tolower (*it);
285 Utils::to_upper (std::string s)
287 std::string::iterator it;
289 for (it = s.begin (); it != s.end (); it++)
290 *it = std::toupper (*it);
296 Utils::trim_str (std::string s)
298 int i = 0, j = s.length () - 1;
300 while (i < j && std::isspace (s[i]))
303 while (j > 0 && std::isspace (s[j]))
306 return s.substr (i, j - i + 1);
310 // Returns a std::string from an SCM argument
312 Utils::scm2str (SCM s)
314 // FIXME: uses gh_, replace with scm_
315 char *tmp = scm_to_locale_string (s);
316 std::string temp (tmp);
323 // Returns a SCM from an std::string argument
325 Utils::str2scm (std::string s)
327 return scm_from_locale_string (s.c_str ());