Remove String->const char* conversion operator in favor of c_str method
[clinton/bobotpp.git] / source / UserList.C
CommitLineData
cb21075d 1// Userlist.C -*- C++ -*-
2// Copyright (c) 1997, 1998 Etienne BERNARD
a6339323 3// Copyright (C) 2002,2005 Clinton Ebadi
cb21075d 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
7b564711 17// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18// 02110-1301, USA.
cb21075d 19
4ce3a244 20#include <cstdlib>
cb21075d 21#include <fstream>
22#include <iostream>
23
24#include "UserList.H"
25#include "StringTokenizer.H"
a6339323 26#include "Utils.H"
cb21075d 27
28UserList::UserList(String filename)
29 : listFilename(filename)
30{
31 read();
32}
33
34UserList::~UserList()
35{
36 clear();
37}
38
39void
40UserList::read()
41{
815a1816 42 std::ifstream file(listFilename.c_str ());
cb21075d 43 String temp, empty = "";
44 int line = 1;
45
46 clear();
47
0316e2c1 48 if (!file)
49 {
50 std::cerr << "I cannot find the file " << listFilename << std::endl;
cb21075d 51 return;
52 }
0316e2c1 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
815a1816 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));
0316e2c1 81 line++;
82 }
83
cb21075d 84 file.close();
85}
86
87void
88UserList::save()
89{
90 std::list<UserListItem *>::iterator it = l.begin();
815a1816 91 std::ofstream file(listFilename.c_str ());
cb21075d 92
93 if (!file)
94 return;
95
0316e2c1 96 ++it; // Skip the bot's entry
97
cb21075d 98 for ( ; it != l.end(); ++it)
0316e2c1 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 }
cb21075d 120 }
121}
122
123void
124UserList::clear()
125{
126 UserListItem *uli;
127
0316e2c1 128 while (!l.empty())
129 {
130 uli = (*l.begin());
131 l.erase(l.begin());
132 delete uli;
133 }
cb21075d 134}
135
136void
137UserList::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
143void
144UserList::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
150UserListItem *
151UserList::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
162int
163UserList::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++)
0316e2c1 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 }
cb21075d 180
181 return level;
182}
183
184int
185UserList::getLevel(String nuh, String channel)
186{
187 if (UserListItem *uli = getUserListItem(nuh, channel))
188 return uli->level;
189
190 return -1;
191}
192
193int
194UserList::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
213bool
214UserList::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
225void
226UserList::removeFirst()
227{
228 UserListItem * uli = *(l.begin());
229 if (uli->autoEntry) {
230 l.erase(l.begin());
231 delete uli;
232 };
233}
234
235void
236UserList::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}