]> git.ipfire.org Git - thirdparty/squid.git/blame - src/acl/TimeData.cc
protos.h refactoring, part one.
[thirdparty/squid.git] / src / acl / TimeData.cc
CommitLineData
5dee515e 1/*
262a0e14 2 * $Id$
5dee515e 3 *
4 * DEBUG: section 28 Access Control
5 * AUTHOR: Duane Wessels
6 *
7 * SQUID Web Proxy Cache http://www.squid-cache.org/
8 * ----------------------------------------------------------
9 *
10 * Squid is the result of efforts by numerous individuals from
11 * the Internet community; see the CONTRIBUTORS file for full
12 * details. Many organizations have provided support for Squid's
13 * development; see the SPONSORS file for full details. Squid is
14 * Copyrighted (C) 2001 by the Regents of the University of
15 * California; see the COPYRIGHT file for full details. Squid
16 * incorporates software developed and/or copyrighted by other
17 * sources; see the CREDITS file for full details.
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
26ac0430 23 *
5dee515e 24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
26ac0430 28 *
5dee515e 29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
32 *
33 *
34 * Copyright (c) 2003, Robert Collins <robertc@squid-cache.org>
35 */
36
582c2af2 37#include "squid.h"
3ad63615
AR
38#include "acl/TimeData.h"
39#include "acl/Checklist.h"
fc54b8d2 40#include "cache_cf.h"
582c2af2
FC
41#include "Debug.h"
42#include "protos.h"
d295d770 43#include "wordlist.h"
5dee515e 44
5dee515e 45ACLTimeData::ACLTimeData () : weekbits (0), start (0), stop (0), next (NULL) {}
62e76326 46
47ACLTimeData::ACLTimeData(ACLTimeData const &old) : weekbits(old.weekbits), start (old.start), stop (old.stop), next (NULL)
5dee515e 48{
49 if (old.next)
62e76326 50 next = (ACLTimeData *)old.next->clone();
5dee515e 51}
52
53ACLTimeData&
54ACLTimeData::operator=(ACLTimeData const &old)
55{
56 weekbits = old.weekbits;
57 start = old.start;
58 stop = old.stop;
59 next = NULL;
62e76326 60
5dee515e 61 if (old.next)
62e76326 62 next = (ACLTimeData *)old.next->clone();
63
5dee515e 64 return *this;
65}
66
67ACLTimeData::~ACLTimeData()
68{
69 if (next)
00d77d6b 70 delete next;
62e76326 71}
5dee515e 72
73bool
74ACLTimeData::match(time_t when)
75{
76 static time_t last_when = 0;
62e76326 77
5dee515e 78 static struct tm tm;
79 time_t t;
62e76326 80
5dee515e 81 if (when != last_when) {
62e76326 82 last_when = when;
41d00cd3 83 memcpy(&tm, localtime(&when), sizeof(struct tm));
5dee515e 84 }
62e76326 85
5dee515e 86 t = (time_t) (tm.tm_hour * 60 + tm.tm_min);
87 ACLTimeData *data = this;
62e76326 88
5dee515e 89 while (data) {
4a7a3d56 90 debugs(28, 3, "aclMatchTime: checking " << t << " in " <<
91 data->start << "-" << data->stop << ", weekbits=" <<
bf8fe701 92 std::hex << data->weekbits);
5dee515e 93
62e76326 94 if (t >= data->start && t <= data->stop && (data->weekbits & (1 << tm.tm_wday)))
95 return 1;
96
97 data = data->next;
5dee515e 98 }
62e76326 99
5dee515e 100 return 0;
101}
102
103wordlist *
104ACLTimeData::dump()
105{
106 wordlist *W = NULL;
107 char buf[128];
108 ACLTimeData *t = this;
62e76326 109
5dee515e 110 while (t != NULL) {
62e76326 111 snprintf(buf, sizeof(buf), "%c%c%c%c%c%c%c %02d:%02d-%02d:%02d",
112 t->weekbits & ACL_SUNDAY ? 'S' : '-',
113 t->weekbits & ACL_MONDAY ? 'M' : '-',
114 t->weekbits & ACL_TUESDAY ? 'T' : '-',
115 t->weekbits & ACL_WEDNESDAY ? 'W' : '-',
116 t->weekbits & ACL_THURSDAY ? 'H' : '-',
117 t->weekbits & ACL_FRIDAY ? 'F' : '-',
118 t->weekbits & ACL_SATURDAY ? 'A' : '-',
119 t->start / 60, t->start % 60, t->stop / 60, t->stop % 60);
120 wordlistAdd(&W, buf);
121 t = t->next;
5dee515e 122 }
62e76326 123
5dee515e 124 return W;
125}
126
127void
128ACLTimeData::parse()
129{
130 ACLTimeData **Tail;
3c98d5a8 131 long parsed_weekbits = 0;
62e76326 132
3d0ac046 133 for (Tail = &next; *Tail; Tail = &((*Tail)->next));
5dee515e 134 ACLTimeData *q = NULL;
62e76326 135
5dee515e 136 int h1, m1, h2, m2;
62e76326 137
5dee515e 138 char *t = NULL;
62e76326 139
5dee515e 140 while ((t = strtokFile())) {
62e76326 141 if (*t < '0' || *t > '9') {
142 /* assume its day-of-week spec */
143
144 while (*t) {
145 switch (*t++) {
146
147 case 'S':
3c98d5a8 148 parsed_weekbits |= ACL_SUNDAY;
62e76326 149 break;
150
151 case 'M':
3c98d5a8 152 parsed_weekbits |= ACL_MONDAY;
62e76326 153 break;
154
155 case 'T':
3c98d5a8 156 parsed_weekbits |= ACL_TUESDAY;
62e76326 157 break;
158
159 case 'W':
3c98d5a8 160 parsed_weekbits |= ACL_WEDNESDAY;
62e76326 161 break;
162
163 case 'H':
3c98d5a8 164 parsed_weekbits |= ACL_THURSDAY;
62e76326 165 break;
166
167 case 'F':
3c98d5a8 168 parsed_weekbits |= ACL_FRIDAY;
62e76326 169 break;
170
171 case 'A':
3c98d5a8 172 parsed_weekbits |= ACL_SATURDAY;
62e76326 173 break;
174
175 case 'D':
3c98d5a8 176 parsed_weekbits |= ACL_WEEKDAYS;
62e76326 177 break;
178
179 case '-':
180 /* ignore placeholder */
181 break;
182
183 default:
fa84c01d 184 debugs(28, DBG_CRITICAL, "" << cfg_filename << " line " << config_lineno <<
bf8fe701 185 ": " << config_input_line);
fa84c01d 186 debugs(28, DBG_CRITICAL, "aclParseTimeSpec: Bad Day '" << *t << "'" );
62e76326 187 break;
188 }
189 }
190 } else {
191 /* assume its time-of-day spec */
192
6494b68a 193 if ((sscanf(t, "%d:%d-%d:%d", &h1, &m1, &h2, &m2) < 4) || (!((h1 >= 0 && h1 < 24) && ((h2 >= 0 && h2 < 24) || (h2 == 24 && m2 == 0)) && (m1 >= 0 && m1 < 60) && (m2 >= 0 && m2 < 60)))) {
fa84c01d 194 debugs(28, DBG_CRITICAL, "aclParseTimeSpec: Bad time range '" << t << "'");
4b0f5de8 195 self_destruct();
62e76326 196
197 if (q != this)
00d77d6b 198 delete q;
62e76326 199
200 return;
201 }
202
3c98d5a8 203 if ((parsed_weekbits == 0) && (start == 0) && (stop == 0))
3f7580f9 204 q = this;
205 else
206 q = new ACLTimeData;
207
62e76326 208 q->start = h1 * 60 + m1;
3f7580f9 209
62e76326 210 q->stop = h2 * 60 + m2;
211
3c98d5a8 212 q->weekbits = parsed_weekbits;
3f7580f9 213
3c98d5a8 214 parsed_weekbits = 0;
3f7580f9 215
62e76326 216 if (q->start > q->stop) {
fa84c01d 217 debugs(28, DBG_CRITICAL, "aclParseTimeSpec: Reversed time range");
4b0f5de8 218 self_destruct();
62e76326 219
220 if (q != this)
00d77d6b 221 delete q;
62e76326 222
223 return;
224 }
3f7580f9 225
226 if (q->weekbits == 0)
227 q->weekbits = ACL_ALLWEEK;
228
229 if (q != this) {
230 *(Tail) = q;
231 Tail = &q->next;
232 }
62e76326 233 }
5dee515e 234 }
62e76326 235
3c98d5a8 236 if (parsed_weekbits) {
3f7580f9 237
3c98d5a8 238 q = new ACLTimeData;
3f7580f9 239
240 q->start = 0 * 60 + 0;
62e76326 241
3f7580f9 242 q->stop = 24 * 60 + 0;
62e76326 243
3c98d5a8 244 q->weekbits = parsed_weekbits;
3f7580f9 245
3c98d5a8
FC
246 *(Tail) = q;
247 Tail = &q->next;
3f7580f9 248 }
5dee515e 249}
250
65092baf 251bool
252ACLTimeData::empty() const
253{
254 return false;
255}
5dee515e 256
257ACLData<time_t> *
258ACLTimeData::clone() const
259{
260 return new ACLTimeData(*this);
261}