Fixes for Cygwin (see ChangeLog)
[citadel.git] / citadel / control.c
1 /*
2  * control.c
3  *
4  * This module handles states which are global to the entire server.
5  *
6  * $Id$
7  *
8  */
9
10 #include "sysdep.h"
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <stdio.h>
14 #include <fcntl.h>
15 #include <signal.h>
16 #include <time.h>
17 #include <ctype.h>
18 #include <string.h>
19 #include <errno.h>
20 #include <limits.h>
21 #ifdef HAVE_PTHREAD_H
22 #include <pthread.h>
23 #endif
24 #include <syslog.h>
25 #include "citadel.h"
26 #include "server.h"
27 #include "control.h"
28 #include "sysdep_decls.h"
29 #include "support.h"
30 #include "config.h"
31 #include "msgbase.h"
32 #include "tools.h"
33 #include "room_ops.h"
34
35 struct CitControl CitControl;
36 struct config config;
37
38 /*
39  * get_control  -  read the control record into memory.
40  */
41 void get_control(void) {
42         FILE *fp;
43
44         /* Zero it out.  If the control record on disk is missing or short,
45          * the system functions with all control record fields initialized
46          * to zero.
47          */
48         memset(&CitControl, 0, sizeof(struct CitControl));
49         fp = fopen("citadel.control", "rb");
50         if (fp == NULL) return;
51
52         fread(&CitControl, sizeof(struct CitControl), 1, fp);
53         fclose(fp);
54         }
55
56 /*
57  * put_control  -  write the control record to disk.
58  */
59 void put_control(void) {
60         FILE *fp;
61
62         fp = fopen("citadel.control", "wb");
63         if (fp != NULL) {
64                 fwrite(&CitControl, sizeof(struct CitControl), 1, fp);
65                 fclose(fp);
66                 }
67         }
68
69
70 /*
71  * get_new_message_number()  -  Obtain a new, unique ID to be used for a message.
72  */
73 long get_new_message_number(void) {
74         begin_critical_section(S_CONTROL);
75         get_control();
76         ++CitControl.MMhighest;
77         put_control();
78         end_critical_section(S_CONTROL);
79         return(CitControl.MMhighest);
80         }
81
82
83 /*
84  * get_new_user_number()  -  Obtain a new, unique ID to be used for a user.
85  */
86 long get_new_user_number(void) {
87         begin_critical_section(S_CONTROL);
88         get_control();
89         ++CitControl.MMnextuser;
90         put_control();
91         end_critical_section(S_CONTROL);
92         return(CitControl.MMnextuser);
93         }
94
95
96
97 /*
98  * get_new_room_number()  -  Obtain a new, unique ID to be used for a room.
99  */
100 long get_new_room_number(void) {
101         begin_critical_section(S_CONTROL);
102         get_control();
103         ++CitControl.MMnextroom;
104         put_control();
105         end_critical_section(S_CONTROL);
106         return(CitControl.MMnextroom);
107         }
108
109
110
111 /* 
112  * Get or set global configuration options
113  */
114 void cmd_conf(char *argbuf) {
115         char cmd[256];
116         char buf[256];
117         int a;
118
119         if (!(CC->logged_in)) {
120                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
121                 return;
122                 }
123
124         if (CC->usersupp.axlevel < 6) {
125                 cprintf("%d Higher access required.\n",
126                         ERROR+HIGHER_ACCESS_REQUIRED);
127                 return;
128                 }
129
130         extract(cmd, argbuf, 0);
131         if (!strcasecmp(cmd, "GET")) {
132                 cprintf("%d Configuration...\n", LISTING_FOLLOWS);
133                 cprintf("%s\n", config.c_nodename);
134                 cprintf("%s\n", config.c_fqdn);
135                 cprintf("%s\n", config.c_humannode);
136                 cprintf("%s\n", config.c_phonenum);
137                 cprintf("%d\n", config.c_creataide);
138                 cprintf("%d\n", config.c_sleeping);
139                 cprintf("%d\n", config.c_initax);
140                 cprintf("%d\n", config.c_regiscall);
141                 cprintf("%d\n", config.c_twitdetect);
142                 cprintf("%s\n", config.c_twitroom);
143                 cprintf("%s\n", config.c_moreprompt);
144                 cprintf("%d\n", config.c_restrict);
145                 cprintf("%s\n", config.c_bbs_city);
146                 cprintf("%s\n", config.c_sysadm);
147                 cprintf("%d\n", config.c_maxsessions);
148                 cprintf("%s\n", config.c_net_password);
149                 cprintf("%d\n", config.c_userpurge);
150                 cprintf("%d\n", config.c_roompurge);
151                 cprintf("%s\n", config.c_logpages);
152                 cprintf("000\n");
153                 }
154
155         else if (!strcasecmp(cmd, "SET")) {
156                 cprintf("%d Send configuration...\n", SEND_LISTING);
157                 a = 0;
158                 while (client_gets(buf), strcmp(buf, "000")) {
159                     switch(a) {
160                         case 0: strncpy(config.c_nodename, buf, 16);
161                                 break;
162                         case 1: strncpy(config.c_fqdn, buf, 64);
163                                 break;
164                         case 2: strncpy(config.c_humannode, buf, 21);
165                                 break;
166                         case 3: strncpy(config.c_phonenum, buf, 16);
167                                 break;
168                         case 4: config.c_creataide = atoi(buf);
169                                 break;
170                         case 5: config.c_sleeping = atoi(buf);
171                                 break;
172                         case 6: config.c_initax = atoi(buf);
173                                 if (config.c_initax < 1) config.c_initax = 1;
174                                 if (config.c_initax > 6) config.c_initax = 6;
175                                 break;
176                         case 7: config.c_regiscall = atoi(buf);
177                                 if (config.c_regiscall != 0)
178                                         config.c_regiscall = 1;
179                                 break;
180                         case 8: config.c_twitdetect = atoi(buf);
181                                 if (config.c_twitdetect != 0)
182                                         config.c_twitdetect = 1;
183                                 break;
184                         case 9: strncpy(config.c_twitroom,
185                                         buf, ROOMNAMELEN);
186                                 break;
187                         case 10: strncpy(config.c_moreprompt, buf, 80);
188                                 break;
189                         case 11: config.c_restrict = atoi(buf);
190                                 if (config.c_restrict != 0)
191                                         config.c_restrict = 1;
192                                 break;
193                         case 12: strncpy(config.c_bbs_city, buf, 32);
194                                 break;
195                         case 13: strncpy(config.c_sysadm, buf, 26);
196                                 break;
197                         case 14: config.c_maxsessions = atoi(buf);
198                                 if (config.c_maxsessions < 1)
199                                         config.c_maxsessions = 1;
200                                 break;
201                         case 15: strncpy(config.c_net_password, buf, 20);
202                                 break;
203                         case 16: config.c_userpurge = atoi(buf);
204                                 break;
205                         case 17: config.c_roompurge = atoi(buf);
206                                 break;
207                         case 18: strncpy(config.c_logpages,
208                                         buf, ROOMNAMELEN);
209                                 break;
210                                 }
211                     ++a;
212                     }
213                 put_config();
214                 snprintf(buf,sizeof buf,
215                          "Global system configuration edited by %s",
216                          CC->curr_user);
217                 aide_message(buf);
218
219                 if (strlen(config.c_logpages) > 0)
220                         create_room(config.c_logpages, 4, "", 0);
221                 }
222
223         else {
224                 cprintf("%d The only valid options are GET and SET.\n",
225                         ERROR+ILLEGAL_VALUE);
226                 }
227         }