1 // Userlist.C -*- C++ -*-
2 // Copyright (c) 1997, 1998 Etienne BERNARD
3 // Copyright (C) 2002,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., 51 Franklin Street, Fifth Floor, Boston, MA
25 #include "StringTokenizer.H"
28 UserList::UserList(String filename)
29 : listFilename(filename)
42 std::ifstream file(listFilename.c_str ());
43 String temp, empty = "";
50 std::cerr << "I cannot find the file " << listFilename << std::endl;
54 while (file >> temp, temp.length() != 0)
56 StringTokenizer st(temp);
58 if (st.count_tokens(':') != 7)
60 std::cerr << "Error when reading userlist (" << listFilename <<
61 ") line " << line << "...\n";
65 String mask = st.next_token(':');
66 String maskChannel = st.next_token(':');
67 String level = st.next_token(':');
68 String prot = st.next_token(':');
69 String aop = st.next_token(':');
70 String expiration = st.next_token(':');
71 String password = Utils::trim_str (st.rest());
73 if (password == "*NONE*")
78 l.push_back(new UserListItem(mask, maskChannel, std::atoi(level.c_str ()),
79 std::atoi(prot.c_str ()), std::atoi(aop.c_str ()),
80 std::atol(expiration.c_str ()), password));
90 std::list<UserListItem *>::iterator it = l.begin();
91 std::ofstream file(listFilename.c_str ());
96 ++it; // Skip the bot's entry
98 for ( ; it != l.end(); ++it)
100 if ((*it)->isStillValid())
102 file << (*it)->mask.getMask() << ":"
103 << (*it)->channelMask.getMask() << ":"
104 << (*it)->level << ":"
105 << (*it)->prot << ":"
107 << (*it)->expirationDate << ":";
109 if ((*it)->passwd == "")
115 file << (*it)->passwd;
137 UserList::addUser(String m, String mc, int lev, int p, bool a,
138 std::time_t e, String pa)
140 l.push_back (new UserListItem(m, mc, lev, p, a, e, pa));
144 UserList::addUserFirst(String m, String mc, int lev, int p,
145 bool a, std::time_t e, String pa)
147 l.push_front (new UserListItem(m, mc, lev, p, a, e, pa, true));
151 UserList::getUserListItem(String nuh, String channel)
153 for (std::list<UserListItem *>::iterator it = l.begin();
155 if ((*it)->matches(nuh, channel)) {
163 UserList::getMaxLevel(String nuh)
166 std::time_t current_time = std::time (0);
168 for (std::list<UserListItem *>::iterator it = l.begin();
171 if ((*it)->matches(nuh) && level < (*it)->level
172 && ((*it)->expirationDate == -1
173 || (*it)->expirationDate > current_time)
174 && ((*it)->passwd == ""
175 || (*it)->identified > 0))
177 level = (*it)->level;
185 UserList::getLevel(String nuh, String channel)
187 if (UserListItem *uli = getUserListItem(nuh, channel))
194 UserList::getMaxProt(String nuh, String channel)
197 std::time_t current_time = std::time (0);
199 for (std::list<UserListItem *>::iterator it = l.begin();
200 it != l.end(); it++) {
201 Mask m(nuh), mc(channel), msc((*it)->channelMask.getMask());
202 if (m.matches((*it)->mask) &&
203 (mc.matches((*it)->channelMask) || msc.matches(channel)) &&
204 prot < (*it)->prot &&
205 ((*it)->expirationDate == -1 ||
206 (*it)->expirationDate > current_time)) {
214 UserList::isInUserList(String nuh, String maskChannel)
216 for (std::list<UserListItem *>::iterator it = l.begin();
219 if ((*it)->matches(nuh, maskChannel))
226 UserList::removeFirst()
228 UserListItem * uli = *(l.begin());
229 if (uli->autoEntry) {
236 UserList::removeUser(String mask, String maskChannel)
238 for (std::list<UserListItem *>::iterator it = l.begin();
241 if ((*it)->mask.getMask() == mask &&
242 (*it)->channelMask.getMask() == maskChannel) {