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