comparison src/timecheck.c @ 8:0836fb919dfa

First entry of Paradise Server 2.9 patch 10 Beta
author darius
date Sat, 06 Dec 1997 04:37:05 +0000
parents
children
comparison
equal deleted inserted replaced
7:814de70c9f67 8:0836fb919dfa
1 /*--------------------------------------------------------------------------
2 NETREK II -- Paradise
3
4 Permission to use, copy, modify, and distribute this software and its
5 documentation, or any derivative works thereof, for any NON-COMMERCIAL
6 purpose and without fee is hereby granted, provided that this copyright
7 notice appear in all copies. No representations are made about the
8 suitability of this software for any purpose. This software is provided
9 "as is" without express or implied warranty.
10
11 Xtrek Copyright 1986 Chris Guthrie
12 Netrek (Xtrek II) Copyright 1989 Kevin P. Smith
13 Scott Silvey
14 Paradise II (Netrek II) Copyright 1993 Larry Denys
15 Kurt Olsen
16 Brandon Gillespie
17 --------------------------------------------------------------------------*/
18
19
20 #include "config.h"
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <time.h>
25
26 #include "path.h"
27 #include "config.h"
28
29
30
31 /*--------------------------------DESCRIPTION-------------------------------
32 This module contains the code to see if players are allowed to play
33 depending on the time of day. This is done with a matrix that contains
34 an element for each hour in each day of the week. If the element is zero,
35 access is not allow. If it is not, then access is allowed. The time_access
36 function is called to check the time and look up the time in the access
37 matrix.
38 Before the time_access function is called, the load_time_access function
39 needs to be called. This function loads the access matrix from a file.
40 The format of the file is:
41 udfgaergfhggdfhg --These are three garbage lines
42 sdjkhgfsadhghsdghdgh --They are ignored
43 fjishgfsdhgdfhjghdahgdtu
44 SUN 0 0 0 0 1 1 1 ... --name of day followed by 24 0's or 1's
45 MON 0 1 1 1 1 ... -- 0 = closed 1 = open
46 The time_access function checks the time access matrix to see if access
47 should be allowed. The function makes a provision for overriding the
48 access matrix. If the file OVERRIDE exists, then access is automaticly
49 allowed. If the file DENY exists then access is disallowed. If they both
50 exists then access is allowed.
51 ---------------------------------------------------------------------------*/
52
53
54
55
56
57
58
59 /*-------------------------------NUMBER DEFINES----------------------------*/
60 #define HOURS "etc/conf.hours"
61 #define OVERRIDE "etc/ALLOW"
62 #define DENY "etc/DENY"
63 /*------------------------------------------------------------------------*/
64
65
66
67
68
69
70
71 /*-------------------------------MODULE VARIABLES--------------------------*/
72
73 /*
74 * The access matrix that is looked up to see if the server is open. 0 =
75 * closed 1 = open
76 */
77 int timematrix[7][24];
78
79 /*-------------------------------------------------------------------------*/
80
81
82
83
84
85
86 /*------------------------------VISIBLE FUNCTIONS--------------------------*/
87
88 /*---------------------------------LOAD_TIME_ACCESS------------------------*/
89 /*
90 * This function loads the time access matrix from the time access file. If
91 * the file cannot be opened, a warning is printed out and the access matrix
92 * is filled with 1's, allowing access at all times.
93 */
94
95
96 extern int fclose();
97 #ifndef IRIX
98 extern int fscanf();
99 extern int fprintf();
100 #endif
101 extern time_t time();
102
103 void
104 load_time_access()
105 {
106 FILE *f; /* to open time access file with */
107 char *filename; /* to hold full path plus filename */
108 int i, j; /* looping vars */
109
110 filename = build_path(HOURS);
111
112 if ((f = fopen(filename, "r")) != NULL)
113 { /* file opened correctly? */
114 fgets(filename, 256, f); /* get rid of first three lines */
115 fgets(filename, 256, f);
116 fgets(filename, 256, f);
117 for (i = 0; i < 7; i++)
118 { /* go through each day */
119 fscanf(f, "%s", filename);/* get day name */
120 for (j = 0; j < 24; j++) /* go through each hour */
121 fscanf(f, "%d", &(timematrix[i][j])); /* get access for that hour */
122 }
123 fclose(f); /* clsoe time access file */
124 }
125 else
126 { /* else no timecheck file */
127 fprintf(stderr, "Warning, no .hours file found.");
128 fprintf(stderr, " Allowing access at all hours, all days.\n");
129 for (i = 0; i < 7; i++) /* go through all days */
130 for (j = 0; j < 24; j++) /* go through all hours */
131 timematrix[i][j] = 1; /* set access okay at this day,hour */
132 }
133 }
134
135
136
137
138 /*---------------------------------TIME_ACCESS-----------------------------*/
139 /*
140 * This function checks to see if the server is open. It returns a 1 if the
141 * server is open at the current time of day and day of the week. It returns
142 * a 0 otherwise.
143 */
144
145 int
146 time_access()
147 {
148 struct tm *tm; /* points to structure containing local time */
149 time_t secs; /* to get number of seconds */
150 FILE *fd; /* to open override file with */
151 char *filename; /* to hold full path and filename */
152
153 filename = build_path(OVERRIDE);
154 if ((fd = fopen(filename, "r")) != NULL)
155 { /* if override open file */
156 fclose(fd); /* exists then */
157 return 1; /* return server is open */
158 }
159 filename = build_path(DENY);
160 if ((fd = fopen(filename, "r")) != NULL)
161 { /* if override close file */
162 fclose(fd); /* exists then */
163 return 0; /* return server is closed */
164 }
165 time(&secs); /* get calendar time */
166 tm = localtime(&secs); /* convert it to local time */
167 return (timematrix[tm->tm_wday][tm->tm_hour]); /* check time access
168 * matrix */
169 }
170
171 /*------------------------------------------------------------------------*/
172
173
174
175
176
177 /*--------END OF FILE----------*/