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