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);
71 std::cerr << "make_wildcard: \"" << mask << "\"\n";
73 st.next_token('!', true);
74 std::string nick = "*";
76 std::string user = st.next_token('@', true);
77 if (user[0] == '~' || user[0] == '^' ||
78 user[0] == '+' || user[0] == '-' ||
80 user = user.substr (1);
81 if (user.length() < 10)
82 user = std::string("*") + user;
84 std::string host = st.rest();
85 StringTokenizer st2(host);
87 if (!wildcard_p (host))
91 host = st2.next_token('.') + ".";
92 host = host + st2.next_token('.') + ".";
93 host = host + st2.next_token('.') + ".*";
97 st2.next_token('.', true);
98 if (st2.count_tokens('.') > 1)
99 host = std::string("*.") + st2.rest();
104 if (host == "") host = "*";
107 return nick + "!" + user + "@" + host;
111 Utils::channel_p(std::string c)
113 return (c[0] == '#' || c[0] == '&');
117 Utils::wildcard_p (std::string c)
119 return (c.find('*') != std::string::npos);
123 Utils::valid_channel_name_p (std::string c)
125 return channel_p (c) && c.find(',') == std::string::npos;
128 #define isvalid(c) (((c) >= 'A' && (c) <= '~') || std::isdigit(c) || (c) == '-')
131 Utils::valid_nickname_p (const Bot *b, std::string n)
133 // FIXME: make max nick length configurable
134 if (n[0] == '-' || std::isdigit(n[0]) || n.length() > b->MAX_NICKLENGTH)
137 for (std::string::size_type i = 0; i < n.length(); i++)
138 if (!isvalid(n[i]) || std::isspace (n[i]))
145 Utils::get_level (Bot * b, std::string nuh)
147 return b->userList->getMaxLevel(nuh);
151 Utils::get_level (Bot * b, std::string nuh, std::string channel)
153 if (!channel_p (channel))
154 return get_level(b, nuh);
156 Channel * c = b->channelList->getChannel(channel);
157 if (c && c->hasNick (get_nick (nuh)))
158 return c->getUser(get_nick (nuh)).getLevel ();
162 return b->userList->getLevel(nuh, channel);
166 Utils::level2str(int l)
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";
180 Utils::prot2str(int p)
184 case User::NO_BAN: return "No ban";
185 case User::NO_KICK: return "No kick";
186 case User::NO_DEOP: return "No deop";
193 Utils::bool2str(bool b)
195 // FIXME: should these be lowercase?
196 return b ? "True" : "False";
200 Utils::long2str (long l)
202 std::ostringstream temp;
209 Utils::str2time(std::string str)
214 // Make sure that str is nominally valid before allocating a buffer
215 if (to_lower (str) == "inf")
218 if (!std::isdigit (str[0]))
221 num.reserve (64); // reserve a buffer to speed things up
223 for (std::string::size_type i = 0; i < str.length(); i++)
229 ans += (std::atoi (num.c_str ()) * 31557600);
233 ans += (std::atoi (num.c_str ()) * 2629800);
238 ans += (std::atoi (num.c_str ()) * 86400);
243 ans += (std::atoi (num.c_str ()) * 3600);
247 ans += (std::atoi (num.c_str ()) * 60);
252 ans += std::atoi (num.c_str ());
255 if (std::isdigit(str[i]))
263 ans += std::atoi (num.c_str ());
265 return std::time (0) + ans;
269 Utils::to_lower (std::string s)
271 std::string::iterator it;
273 for (it = s.begin (); it != s.end (); ++it)
274 *it = std::tolower (*it);
280 Utils::to_upper (std::string s)
282 std::string::iterator it;
284 for (it = s.begin (); it != s.end (); it++)
285 *it = std::toupper (*it);
291 Utils::trim_str (std::string s)
293 int i = 0, j = s.length () - 1;
295 while (i < j && std::isspace (s[i]))
298 while (j > 0 && std::isspace (s[j]))
301 return s.substr (i, j - i + 1);
305 // Returns a std::string from an SCM argument
307 Utils::scm2str (SCM s)
309 // FIXME: uses gh_, replace with scm_
310 char *tmp = scm_to_locale_string (s);
311 std::string temp (tmp);
318 // Returns a SCM from an std::string argument
320 Utils::str2scm (std::string s)
322 return scm_from_locale_string (s.c_str ());