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
24 #include "StringTokenizer.H"
27 UserList::UserList(String filename)
28 : listFilename(filename)
41 std::ifstream file(listFilename);
42 String temp, empty = "";
49 std::cerr << "I cannot find the file " << listFilename << std::endl;
53 while (file >> temp, temp.length() != 0)
55 StringTokenizer st(temp);
57 if (st.count_tokens(':') != 7)
59 std::cerr << "Error when reading userlist (" << listFilename <<
60 ") line " << line << "...\n";
64 String mask = st.next_token(':');
65 String maskChannel = st.next_token(':');
66 String level = st.next_token(':');
67 String prot = st.next_token(':');
68 String aop = st.next_token(':');
69 String expiration = st.next_token(':');
70 String password = Utils::trim_str (st.rest());
72 if (password == "*NONE*")
77 l.push_back(new UserListItem(mask, maskChannel, atoi(level),
78 atoi(prot), atoi(aop),
79 atol(expiration), password));
89 std::list<UserListItem *>::iterator it = l.begin();
90 std::ofstream file(listFilename);
95 ++it; // Skip the bot's entry
97 for ( ; it != l.end(); ++it)
99 if ((*it)->isStillValid())
101 file << (*it)->mask.getMask() << ":"
102 << (*it)->channelMask.getMask() << ":"
103 << (*it)->level << ":"
104 << (*it)->prot << ":"
106 << (*it)->expirationDate << ":";
108 if ((*it)->passwd == "")
114 file << (*it)->passwd;
136 UserList::addUser(String m, String mc, int lev, int p, bool a,
137 std::time_t e, String pa)
139 l.push_back (new UserListItem(m, mc, lev, p, a, e, pa));
143 UserList::addUserFirst(String m, String mc, int lev, int p,
144 bool a, std::time_t e, String pa)
146 l.push_front (new UserListItem(m, mc, lev, p, a, e, pa, true));
150 UserList::getUserListItem(String nuh, String channel)
152 for (std::list<UserListItem *>::iterator it = l.begin();
154 if ((*it)->matches(nuh, channel)) {
162 UserList::getMaxLevel(String nuh)
165 std::time_t current_time = std::time (0);
167 for (std::list<UserListItem *>::iterator it = l.begin();
170 if ((*it)->matches(nuh) && level < (*it)->level
171 && ((*it)->expirationDate == -1
172 || (*it)->expirationDate > current_time)
173 && ((*it)->passwd == ""
174 || (*it)->identified > 0))
176 level = (*it)->level;
184 UserList::getLevel(String nuh, String channel)
186 if (UserListItem *uli = getUserListItem(nuh, channel))
193 UserList::getMaxProt(String nuh, String channel)
196 std::time_t current_time = std::time (0);
198 for (std::list<UserListItem *>::iterator it = l.begin();
199 it != l.end(); it++) {
200 Mask m(nuh), mc(channel), msc((*it)->channelMask.getMask());
201 if (m.matches((*it)->mask) &&
202 (mc.matches((*it)->channelMask) || msc.matches(channel)) &&
203 prot < (*it)->prot &&
204 ((*it)->expirationDate == -1 ||
205 (*it)->expirationDate > current_time)) {
213 UserList::isInUserList(String nuh, String maskChannel)
215 for (std::list<UserListItem *>::iterator it = l.begin();
218 if ((*it)->matches(nuh, maskChannel))
225 UserList::removeFirst()
227 UserListItem * uli = *(l.begin());
228 if (uli->autoEntry) {
235 UserList::removeUser(String mask, String maskChannel)
237 for (std::list<UserListItem *>::iterator it = l.begin();
240 if ((*it)->mask.getMask() == mask &&
241 (*it)->channelMask.getMask() == maskChannel) {