]> git.ipfire.org Git - thirdparty/squid.git/blame - src/acl/TimeData.cc
Renamed squid.h to squid-old.h and config.h to squid.h
[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
f7f3304a 37#include "squid-old.h"
3ad63615
AR
38#include "acl/TimeData.h"
39#include "acl/Checklist.h"
d295d770 40#include "wordlist.h"
5dee515e 41
5dee515e 42ACLTimeData::ACLTimeData () : weekbits (0), start (0), stop (0), next (NULL) {}
62e76326 43
44ACLTimeData::ACLTimeData(ACLTimeData const &old) : weekbits(old.weekbits), start (old.start), stop (old.stop), next (NULL)
5dee515e 45{
46 if (old.next)
62e76326 47 next = (ACLTimeData *)old.next->clone();
5dee515e 48}
49
50ACLTimeData&
51ACLTimeData::operator=(ACLTimeData const &old)
52{
53 weekbits = old.weekbits;
54 start = old.start;
55 stop = old.stop;
56 next = NULL;
62e76326 57
5dee515e 58 if (old.next)
62e76326 59 next = (ACLTimeData *)old.next->clone();
60
5dee515e 61 return *this;
62}
63
64ACLTimeData::~ACLTimeData()
65{
66 if (next)
00d77d6b 67 delete next;
62e76326 68}
5dee515e 69
70bool
71ACLTimeData::match(time_t when)
72{
73 static time_t last_when = 0;
62e76326 74
5dee515e 75 static struct tm tm;
76 time_t t;
62e76326 77
5dee515e 78 if (when != last_when) {
62e76326 79 last_when = when;
41d00cd3 80 memcpy(&tm, localtime(&when), sizeof(struct tm));
5dee515e 81 }
62e76326 82
5dee515e 83 t = (time_t) (tm.tm_hour * 60 + tm.tm_min);
84 ACLTimeData *data = this;
62e76326 85
5dee515e 86 while (data) {
4a7a3d56 87 debugs(28, 3, "aclMatchTime: checking " << t << " in " <<
88 data->start << "-" << data->stop << ", weekbits=" <<
bf8fe701 89 std::hex << data->weekbits);
5dee515e 90
62e76326 91 if (t >= data->start && t <= data->stop && (data->weekbits & (1 << tm.tm_wday)))
92 return 1;
93
94 data = data->next;
5dee515e 95 }
62e76326 96
5dee515e 97 return 0;
98}
99
100wordlist *
101ACLTimeData::dump()
102{
103 wordlist *W = NULL;
104 char buf[128];
105 ACLTimeData *t = this;
62e76326 106
5dee515e 107 while (t != NULL) {
62e76326 108 snprintf(buf, sizeof(buf), "%c%c%c%c%c%c%c %02d:%02d-%02d:%02d",
109 t->weekbits & ACL_SUNDAY ? 'S' : '-',
110 t->weekbits & ACL_MONDAY ? 'M' : '-',
111 t->weekbits & ACL_TUESDAY ? 'T' : '-',
112 t->weekbits & ACL_WEDNESDAY ? 'W' : '-',
113 t->weekbits & ACL_THURSDAY ? 'H' : '-',
114 t->weekbits & ACL_FRIDAY ? 'F' : '-',
115 t->weekbits & ACL_SATURDAY ? 'A' : '-',
116 t->start / 60, t->start % 60, t->stop / 60, t->stop % 60);
117 wordlistAdd(&W, buf);
118 t = t->next;
5dee515e 119 }
62e76326 120
5dee515e 121 return W;
122}
123
124void
125ACLTimeData::parse()
126{
127 ACLTimeData **Tail;
3c98d5a8 128 long parsed_weekbits = 0;
62e76326 129
3d0ac046 130 for (Tail = &next; *Tail; Tail = &((*Tail)->next));
5dee515e 131 ACLTimeData *q = NULL;
62e76326 132
5dee515e 133 int h1, m1, h2, m2;
62e76326 134
5dee515e 135 char *t = NULL;
62e76326 136
5dee515e 137 while ((t = strtokFile())) {
62e76326 138 if (*t < '0' || *t > '9') {
139 /* assume its day-of-week spec */
140
141 while (*t) {
142 switch (*t++) {
143
144 case 'S':
3c98d5a8 145 parsed_weekbits |= ACL_SUNDAY;
62e76326 146 break;
147
148 case 'M':
3c98d5a8 149 parsed_weekbits |= ACL_MONDAY;
62e76326 150 break;
151
152 case 'T':
3c98d5a8 153 parsed_weekbits |= ACL_TUESDAY;
62e76326 154 break;
155
156 case 'W':
3c98d5a8 157 parsed_weekbits |= ACL_WEDNESDAY;
62e76326 158 break;
159
160 case 'H':
3c98d5a8 161 parsed_weekbits |= ACL_THURSDAY;
62e76326 162 break;
163
164 case 'F':
3c98d5a8 165 parsed_weekbits |= ACL_FRIDAY;
62e76326 166 break;
167
168 case 'A':
3c98d5a8 169 parsed_weekbits |= ACL_SATURDAY;
62e76326 170 break;
171
172 case 'D':
3c98d5a8 173 parsed_weekbits |= ACL_WEEKDAYS;
62e76326 174 break;
175
176 case '-':
177 /* ignore placeholder */
178 break;
179
180 default:
bf8fe701 181 debugs(28, 0, "" << cfg_filename << " line " << config_lineno <<
182 ": " << config_input_line);
183 debugs(28, 0, "aclParseTimeSpec: Bad Day '" << *t << "'" );
62e76326 184 break;
185 }
186 }
187 } else {
188 /* assume its time-of-day spec */
189
6494b68a 190 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)))) {
bf8fe701 191 debugs(28, 0, "aclParseTimeSpec: Bad time range '" << t << "'");
4b0f5de8 192 self_destruct();
62e76326 193
194 if (q != this)
00d77d6b 195 delete q;
62e76326 196
197 return;
198 }
199
3c98d5a8 200 if ((parsed_weekbits == 0) && (start == 0) && (stop == 0))
3f7580f9 201 q = this;
202 else
203 q = new ACLTimeData;
204
62e76326 205 q->start = h1 * 60 + m1;
3f7580f9 206
62e76326 207 q->stop = h2 * 60 + m2;
208
3c98d5a8 209 q->weekbits = parsed_weekbits;
3f7580f9 210
3c98d5a8 211 parsed_weekbits = 0;
3f7580f9 212
62e76326 213 if (q->start > q->stop) {
bf8fe701 214 debugs(28, 0, "aclParseTimeSpec: Reversed time range");
4b0f5de8 215 self_destruct();
62e76326 216
217 if (q != this)
00d77d6b 218 delete q;
62e76326 219
220 return;
221 }
3f7580f9 222
223 if (q->weekbits == 0)
224 q->weekbits = ACL_ALLWEEK;
225
226 if (q != this) {
227 *(Tail) = q;
228 Tail = &q->next;
229 }
62e76326 230 }
5dee515e 231 }
62e76326 232
3c98d5a8 233 if (parsed_weekbits) {
3f7580f9 234
3c98d5a8 235 q = new ACLTimeData;
3f7580f9 236
237 q->start = 0 * 60 + 0;
62e76326 238
3f7580f9 239 q->stop = 24 * 60 + 0;
62e76326 240
3c98d5a8 241 q->weekbits = parsed_weekbits;
3f7580f9 242
3c98d5a8
FC
243 *(Tail) = q;
244 Tail = &q->next;
3f7580f9 245 }
5dee515e 246}
247
65092baf 248bool
249ACLTimeData::empty() const
250{
251 return false;
252}
5dee515e 253
254ACLData<time_t> *
255ACLTimeData::clone() const
256{
257 return new ACLTimeData(*this);
258}