cb21075d |
1 | // ShitList.C -*- C++ -*- |
2 | // Copyright (c) 1998 Etienne BERNARD |
3 | // Copyright (C) 2002 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
18 | |
19 | #ifdef HAVE_CONFIG_H |
20 | #include "config.h" |
21 | #endif |
22 | |
23 | #include <fstream> |
24 | |
25 | #include "ShitList.H" |
26 | #include "StringTokenizer.H" |
27 | |
28 | ShitList::ShitList(String filename) |
29 | : listFileName(filename) |
30 | { |
31 | #ifdef HAVE_STL_CLEAR |
32 | l.clear(); |
33 | #endif |
34 | read(); |
35 | } |
36 | |
37 | ShitList::~ShitList() |
38 | { |
39 | clear(); |
40 | } |
41 | |
42 | void |
43 | ShitList::read() |
44 | { |
45 | std::ifstream file(listFileName); |
46 | String temp; |
47 | int line = 1; |
48 | |
49 | clear(); |
50 | |
51 | if (!file) { |
52 | std::cerr << "I cannot find the file " << listFileName << std::endl; |
53 | return; |
54 | } |
55 | |
56 | while (file >> temp, temp.length() != 0) { |
57 | StringTokenizer st(temp); |
58 | String mask = st.nextToken(':'); |
59 | String channelMask = st.nextToken(':'); |
60 | String level = st.nextToken(':'); |
61 | String expiration = st.nextToken(':'); |
62 | String reason = st.rest().trim(); |
63 | l.push_back (new ShitEntry(mask, channelMask, std::atoi(level), |
64 | std::atol(expiration), reason)); |
65 | line++; |
66 | } |
67 | file.close(); |
68 | } |
69 | |
70 | void |
71 | ShitList::save() |
72 | { |
73 | std::list<ShitEntry *>::iterator it = l.begin(); |
74 | std::ofstream file(listFileName); |
75 | |
76 | if (!file) |
77 | return; |
78 | |
79 | for ( ; it != l.end(); ++it) |
80 | if ((*it)->isStillValid()) { |
81 | file << (*it)->shitMask.getMask() << ":" |
82 | << (*it)->shitChannelMask.getMask() << ":" |
83 | << (*it)->shitLevel << ":" |
84 | << (*it)->expirationDate << ":" |
85 | << (*it)->shitReason << std::endl; |
86 | } |
87 | } |
88 | |
89 | void |
90 | ShitList::clear() |
91 | { |
92 | ShitEntry *se; |
93 | |
94 | while (!l.empty()) { |
95 | se = (*l.begin()); |
96 | l.erase(l.begin()); |
97 | delete se; |
98 | } |
99 | } |
100 | |
101 | void |
102 | ShitList::addShit(String m, String mc, int lev, time_t e, String r) |
103 | { |
104 | l.push_back (new ShitEntry(m, mc, lev, e, r)); |
105 | } |
106 | |
107 | |
108 | void |
109 | ShitList::delShit(String mask, String channelMask) |
110 | { |
111 | for (std::list<ShitEntry *>::iterator it = l.begin(); |
112 | it != l.end(); |
113 | ++it) |
114 | if ((*it)->shitMask.getMask() == mask && |
115 | (*it)->shitChannelMask.getMask() == channelMask) { |
116 | delete (*it); |
117 | l.erase(it); |
118 | return; |
119 | } |
120 | } |
121 | |
122 | ShitEntry * |
123 | ShitList::getShit(String nuh, String channel) |
124 | { |
125 | for (std::list<ShitEntry *>::iterator it = l.begin(); |
126 | it != l.end(); |
127 | ++it) |
128 | if ((*it)->matches(nuh, channel)) { |
129 | return (*it); |
130 | } |
131 | |
132 | return 0; |
133 | } |