b1038d07978930358b69ed7556547ba04e161002
[clinton/bobotpp.git] / source / UserList.C
1 // Userlist.C -*- C++ -*-
2 // Copyright (c) 1997, 1998 Etienne BERNARD
3 // Copyright (C) 2002,2005 Clinton Ebadi
4
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
8 // any later version.
9
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.
14
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
18 // 02110-1301, USA.
19
20 #include <fstream>
21 #include <iostream>
22
23 #include "UserList.H"
24 #include "StringTokenizer.H"
25 #include "Utils.H"
26
27 UserList::UserList(String filename)
28 : listFilename(filename)
29 {
30 read();
31 }
32
33 UserList::~UserList()
34 {
35 clear();
36 }
37
38 void
39 UserList::read()
40 {
41 std::ifstream file(listFilename);
42 String temp, empty = "";
43 int line = 1;
44
45 clear();
46
47 if (!file)
48 {
49 std::cerr << "I cannot find the file " << listFilename << std::endl;
50 return;
51 }
52
53 while (file >> temp, temp.length() != 0)
54 {
55 StringTokenizer st(temp);
56
57 if (st.count_tokens(':') != 7)
58 {
59 std::cerr << "Error when reading userlist (" << listFilename <<
60 ") line " << line << "...\n";
61 return;
62 }
63
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());
71
72 if (password == "*NONE*")
73 {
74 password = "";
75 }
76
77 l.push_back(new UserListItem(mask, maskChannel, atoi(level),
78 atoi(prot), atoi(aop),
79 atol(expiration), password));
80 line++;
81 }
82
83 file.close();
84 }
85
86 void
87 UserList::save()
88 {
89 std::list<UserListItem *>::iterator it = l.begin();
90 std::ofstream file(listFilename);
91
92 if (!file)
93 return;
94
95 ++it; // Skip the bot's entry
96
97 for ( ; it != l.end(); ++it)
98 {
99 if ((*it)->isStillValid())
100 {
101 file << (*it)->mask.getMask() << ":"
102 << (*it)->channelMask.getMask() << ":"
103 << (*it)->level << ":"
104 << (*it)->prot << ":"
105 << (*it)->aop << ":"
106 << (*it)->expirationDate << ":";
107
108 if ((*it)->passwd == "")
109 {
110 file << "*NONE*";
111 }
112 else
113 {
114 file << (*it)->passwd;
115 }
116
117 file << std::endl;
118 }
119 }
120 }
121
122 void
123 UserList::clear()
124 {
125 UserListItem *uli;
126
127 while (!l.empty())
128 {
129 uli = (*l.begin());
130 l.erase(l.begin());
131 delete uli;
132 }
133 }
134
135 void
136 UserList::addUser(String m, String mc, int lev, int p, bool a,
137 std::time_t e, String pa)
138 {
139 l.push_back (new UserListItem(m, mc, lev, p, a, e, pa));
140 }
141
142 void
143 UserList::addUserFirst(String m, String mc, int lev, int p,
144 bool a, std::time_t e, String pa)
145 {
146 l.push_front (new UserListItem(m, mc, lev, p, a, e, pa, true));
147 }
148
149 UserListItem *
150 UserList::getUserListItem(String nuh, String channel)
151 {
152 for (std::list<UserListItem *>::iterator it = l.begin();
153 it != l.end(); ++it)
154 if ((*it)->matches(nuh, channel)) {
155 return (*it);
156 }
157
158 return 0;
159 }
160
161 int
162 UserList::getMaxLevel(String nuh)
163 {
164 int level = -1;
165 std::time_t current_time = std::time (0);
166
167 for (std::list<UserListItem *>::iterator it = l.begin();
168 it != l.end(); it++)
169 {
170 if ((*it)->matches(nuh) && level < (*it)->level
171 && ((*it)->expirationDate == -1
172 || (*it)->expirationDate > current_time)
173 && ((*it)->passwd == ""
174 || (*it)->identified > 0))
175 {
176 level = (*it)->level;
177 }
178 }
179
180 return level;
181 }
182
183 int
184 UserList::getLevel(String nuh, String channel)
185 {
186 if (UserListItem *uli = getUserListItem(nuh, channel))
187 return uli->level;
188
189 return -1;
190 }
191
192 int
193 UserList::getMaxProt(String nuh, String channel)
194 {
195 int prot = -1;
196 std::time_t current_time = std::time (0);
197
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)) {
206 prot = (*it)->prot;
207 }
208 }
209 return prot;
210 }
211
212 bool
213 UserList::isInUserList(String nuh, String maskChannel)
214 {
215 for (std::list<UserListItem *>::iterator it = l.begin();
216 it != l.end();
217 ++it)
218 if ((*it)->matches(nuh, maskChannel))
219 return true;
220
221 return false;
222 }
223
224 void
225 UserList::removeFirst()
226 {
227 UserListItem * uli = *(l.begin());
228 if (uli->autoEntry) {
229 l.erase(l.begin());
230 delete uli;
231 };
232 }
233
234 void
235 UserList::removeUser(String mask, String maskChannel)
236 {
237 for (std::list<UserListItem *>::iterator it = l.begin();
238 it != l.end();
239 ++it)
240 if ((*it)->mask.getMask() == mask &&
241 (*it)->channelMask.getMask() == maskChannel) {
242 delete (*it);
243 l.erase(it);
244 return;
245 }
246 }