Release bumped to "gts4".
[tac_plus.git] / time_limit.c
1 /*
2         These functions writen by Devrim SERAL <devrim@gazi.edu.tr>
3
4 Applicable format is  : <day str><time str> [,|] <day str><time str> [,|] and so on
5
6 The accept parameter for day str is:
7 SU = Sunday
8 MO = Monday
9 TU = Tuesday
10 WE = Wendsday
11 TH = Thursday
12 FR = Friday
13 SA = Saturday
14 WK = For week days
15 WD = For Week and
16 AL = For All days
17
18 And time str must be:
19 Hourminute-Hourminute
20 For example it's to be -> 0000-1200 or 1600-1700 or 1600-0800
21
22 License: This code is free software; you can redistribute it and/or modify it
23 under the terms of the GNU General Public License as published by the Free
24 Software Foundation; either version 2, or (at your option) any later version.
25
26 */
27
28
29 #include "tac_plus.h"
30
31 #include <stdlib.h>
32 #include <string.h>
33 #include <ctype.h>
34 #include <time.h>
35
36 #include "time_limit.h"
37 #include "report.h"
38 #include "main.h"
39 #include "utils.h"
40
41
42 static int str_token_proc TAC_ARGS((char *str));
43 static int process TAC_ARGS((char *str));
44 static int time_calc TAC_ARGS((char *str, int lct));
45 static int antoi TAC_ARGS((char *str, int n));
46
47
48 static char* week_days[] = {"SU","MO","TU","WE","TH","FR","SA","WK","WD","AL"};
49 static long week_day_val[] = {1, 2, 4, 8, 16, 32, 64, 62, 65, 127};
50
51 static int problem = 0;
52
53
54 int time_limit_process TAC_ARGS((const char *str));
55
56 int
57 time_limit_process(str)
58 const char *str;
59 {
60     int ret=0;
61     char *tmp_str, *str_copy;
62
63     str_copy = tac_strdup(str);
64     tmp_str = (char *) strtok(str_copy,",|");
65
66     while ( tmp_str != NULL) {
67         ret |= str_token_proc(tmp_str);
68         tmp_str = (char *) strtok(NULL,",");
69     }
70     free(str_copy);
71
72     return (ret);
73 }
74
75 static int str_token_proc TAC_ARGS((char *str));
76
77 static int
78 str_token_proc(str)
79 char *str;
80 {
81     int inv = 0, ret;
82
83     /* Pass space characters */
84     while (isspace((int) *str))
85         str++;
86
87     if (*str=='!') {
88         inv=1;
89         str++;
90     }
91
92     ret=process(str);
93
94     if (problem) {
95         if ( debug & DEBUG_AUTHEN_FLAG )
96             report(LOG_DEBUG,"Timestamp format incorrect");
97         problem=0;
98         return(0);
99     }
100
101     if (inv)
102         ret=!ret;
103
104     return(ret);
105 }
106
107
108 static void str_up TAC_ARGS((char *str));
109
110 static void
111 str_up(str)
112 char *str;
113 {
114     while (*str) {
115         if (islower((int) *str))
116             *str = toupper((int) *str);
117         str++;
118     }
119 }
120
121
122 static int process TAC_ARGS((char *str));
123
124 static int
125 process(str)
126 char *str;
127 {
128     int count = 0, ret = 0, i, j, localtm;
129     char *head, *buf, *gec;
130     time_t sec;
131     struct tm *tms;
132
133     /* Pass space characters  */
134     while (isspace((int) *str))
135         str++;
136
137     head=str;
138
139     /* Count alphanumeric char */
140     while (isalpha((int) *str)) {
141         count++;
142         str++;
143     }
144
145     if ( count==0 || count%2 ) {
146         problem++;
147         return 0;
148     }
149
150     buf = (char *) tac_malloc(count+1);
151     strncpy(buf, head, count);
152     gec = buf;
153     str_up(buf);
154
155     for (i=1; i<=(count/2); i++) {
156         for (j=0; j<NUM; j++)
157             if(!strncmp(gec,week_days[j],2))
158                 ret ^= week_day_val[j];
159         gec += 2;
160     }
161
162     /* We finished to use buffer so free it */
163     free(buf);
164
165     sec = time(NULL);
166     tms = localtime(&sec);
167     localtm = (tms->tm_hour)*60 + tms->tm_min;
168     ret = ( week_day_val[tms->tm_wday] & ret ) && time_calc(str,localtm);
169
170     if (ret>0)
171         return (1);
172     else
173         return (0);
174 }
175
176
177 static int time_calc TAC_ARGS((char *str, int lct));
178
179 static int
180 time_calc(str, lct)
181 char *str;
182 int lct;
183 {
184     char *t1, *t2, *head;
185     int say1, say2, count=0;
186
187     head = str;
188
189     while (isdigit((int) *head) || *head=='-') {
190         count++;
191         head++;
192     }
193
194     if ( *str=='\0' || count!= TPL ) {
195         problem++;
196         return (0);
197     }
198
199     t1 = (char *) tac_malloc(count);
200     strncpy(t1, str, count);                    /* Put str value to t1 */
201
202     t2 = (char *) strstr(t1,"-");               /* Find next time part */
203
204     if (t2==NULL) {
205         free(t1);
206         problem++;
207         return(0);
208     }
209
210     *t2++ = '\0';
211
212     if ( strlen(t1)<4 || strlen(t2)<4 ) {
213         free(t1);
214         problem++;
215         return(0);
216     }
217     say1 = antoi(t1,2)*60 + antoi(t1+2,2);
218     say2 = antoi(t2,2)*60 + antoi(t2+2,2);
219
220     free(t1);
221
222     if (say1 <= say2) {
223         if( (lct>=say1) && (lct<=say2) )
224             return(1);
225     } else {
226         if( (lct>=say1) || (lct<=say2) )
227             return(1);
228     }
229     return(0);
230 }
231
232 static int antoi TAC_ARGS((char *str, int n));
233
234 static int
235 antoi(str, n)
236 char *str;
237 int n;
238 {
239     char *buf;
240     int ret;
241
242     buf = (char *) tac_malloc(n);
243     strncpy(buf, str, n);
244     ret = atoi(buf);
245     free(buf);
246
247     return(ret);
248 }