2 // Copyright (c) 1997, 1998 Etienne BERNARD
3 // Copyright (c) 2005 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
29 #include "StringTokenizer.H"
32 Utils::get_nick (std::string nuh)
34 StringTokenizer st(nuh);
35 return st.next_token('!');
39 Utils::get_userhost (std::string nuh)
41 StringTokenizer st(nuh);
49 return long2str (std::rand());
53 Utils::IP_p (std::string host)
55 for (int i = 0; i < host.length(); i++)
56 if (!std::isdigit (host[i]) && host[i] !='.')
62 Utils::make_wildcard (std::string mask)
64 StringTokenizer st (mask);
66 st.next_token('!', true);
67 std::string nick = "*";
69 std::string user = st.next_token('@', true);
70 if (user[0] == '~' || user[0] == '^' ||
71 user[0] == '+' || user[0] == '-' ||
73 user = user.substr (1);
74 if (user.length() < 10)
75 user = std::string("*") + user;
77 std::string host = st.rest();
78 StringTokenizer st2(host);
80 if (!wildcard_p (host))
84 host = st2.next_token('.') + ".";
85 host = host + st2.next_token('.') + ".";
86 host = host + st2.next_token('.') + ".*";
90 st2.next_token('.', true);
91 if (st2.count_tokens('.') > 1)
92 host = std::string("*.") + st2.rest();
97 if (host == "") host = "*";
100 std::cout << nick + "!" + user + "@" + host << std::endl;
102 return nick + "!" + user + "@" + host;
106 Utils::channel_p(std::string c)
108 return (c[0] == '#' || c[0] == '&');
112 Utils::wildcard_p (std::string c)
114 return (c.find('*') != std::string::npos);
118 Utils::valid_channel_name_p (std::string c)
120 return channel_p (c) && c.find(',') == std::string::npos;
123 #define isvalid(c) (((c) >= 'A' && (c) <= '~') || std::isdigit(c) || (c) == '-')
126 Utils::valid_nickname_p (std::string n)
128 // FIXME: make max nick length configurable
129 if (n[0] == '-' || std::isdigit(n[0]) || n.length() > 9)
132 for (int i = 0; i < n.length(); i++)
133 if (!isvalid(n[i]) || std::isspace (n[i]))
140 Utils::get_level (Bot * b, std::string nuh)
142 return b->userList->getMaxLevel(nuh);
146 Utils::get_level (Bot * b, std::string nuh, std::string channel)
148 if (!channel_p (channel))
149 return get_level(b, nuh);
151 if (Channel * c = b->channelList->getChannel(channel))
153 User * u = c->getUser(get_nick (nuh));
155 return u->getLevel();
161 return b->userList->getLevel(nuh, channel);
165 Utils::level2str(int l)
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";
179 Utils::prot2str(int p)
183 case User::NO_BAN: return "No ban";
184 case User::NO_KICK: return "No kick";
185 case User::NO_DEOP: return "No deop";
192 Utils::bool2str(bool b)
194 // FIXME: should these be lowercase?
195 return b ? "True" : "False";
199 Utils::long2str (long l)
201 std::ostringstream temp;
208 Utils::str2time(std::string str)
213 // Make sure that str is nominally valid before allocating a buffer
214 if (to_lower (str) == "inf")
217 if (!std::isdigit (str[0]))
220 num.reserve (512); // reverse a buffer to speed things up
222 for (int i = 0; i < str.length(); i++)
228 ans += (std::atoi (num.c_str ()) * 31557600);
232 ans += (std::atoi (num.c_str ()) * 2629800);
237 ans += (std::atoi (num.c_str ()) * 86400);
242 ans += (std::atoi (num.c_str ()) * 3600);
246 ans += (std::atoi (num.c_str ()) * 60);
251 ans += std::atoi (num.c_str ());
254 if (std::isdigit(str[i]))
262 ans += std::atoi (num.c_str ());
264 return std::time (0) + ans;
268 Utils::to_lower (std::string s)
270 std::string::iterator it;
272 for (it = s.begin (); it != s.end (); ++it)
273 *it = std::tolower (*it);
279 Utils::to_upper (std::string s)
281 std::string::iterator it;
283 for (it = s.begin (); it != s.end (); ++it)
284 *it = std::toupper (*it);
290 Utils::trim_str (std::string s)
292 int i = 0, j = s.length () - 1;
294 while (i < j && std::isspace (s[i]))
297 while (j > 0 && std::isspace (s[j]))
300 return s.substr (i, j);
304 // Returns a std::string from an SCM argument
306 Utils::scm2str (SCM s)
308 // FIXME: uses gh_, replace with scm_
309 char *tmp = gh_scm2newstr(s, 0);
310 std::string temp (tmp);
317 // Returns a SCM from an std::string argument
319 Utils::str2scm (std::string s)
321 return gh_str02scm (s.c_str ());