* added server command line option "-f" to defrag databases on startup
[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 FILE *control_fp = NULL;
38
39 /*
40  * get_control  -  read the control record into memory.
41  */
42 void get_control(void) {
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         if (control_fp == NULL)
50                 control_fp = fopen("citadel.control", "rb+");
51         if (control_fp == NULL) return;
52
53         rewind(control_fp);
54         fread(&CitControl, sizeof(struct CitControl), 1, control_fp);
55         }
56
57 /*
58  * put_control  -  write the control record to disk.
59  */
60 void put_control(void) {
61
62         if (control_fp != NULL) {
63                 rewind(control_fp);
64                 fwrite(&CitControl, sizeof(struct CitControl), 1, control_fp);
65                 fflush(control_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("%d\n", config.c_createax);
153                 cprintf("000\n");
154                 }
155
156         else if (!strcasecmp(cmd, "SET")) {
157                 cprintf("%d Send configuration...\n", SEND_LISTING);
158                 a = 0;
159                 while (client_gets(buf), strcmp(buf, "000")) {
160                     switch(a) {
161                         case 0: strncpy(config.c_nodename, buf, 16);
162                                 break;
163                         case 1: strncpy(config.c_fqdn, buf, 64);
164                                 break;
165                         case 2: strncpy(config.c_humannode, buf, 21);
166                                 break;
167                         case 3: strncpy(config.c_phonenum, buf, 16);
168                                 break;
169                         case 4: config.c_creataide = atoi(buf);
170                                 break;
171                         case 5: config.c_sleeping = atoi(buf);
172                                 break;
173                         case 6: config.c_initax = atoi(buf);
174                                 if (config.c_initax < 1) config.c_initax = 1;
175                                 if (config.c_initax > 6) config.c_initax = 6;
176                                 break;
177                         case 7: config.c_regiscall = atoi(buf);
178                                 if (config.c_regiscall != 0)
179                                         config.c_regiscall = 1;
180                                 break;
181                         case 8: config.c_twitdetect = atoi(buf);
182                                 if (config.c_twitdetect != 0)
183                                         config.c_twitdetect = 1;
184                                 break;
185                         case 9: strncpy(config.c_twitroom,
186                                         buf, ROOMNAMELEN);
187                                 break;
188                         case 10: strncpy(config.c_moreprompt, buf, 80);
189                                 break;
190                         case 11: config.c_restrict = atoi(buf);
191                                 if (config.c_restrict != 0)
192                                         config.c_restrict = 1;
193                                 break;
194                         case 12: strncpy(config.c_bbs_city, buf, 32);
195                                 break;
196                         case 13: strncpy(config.c_sysadm, buf, 26);
197                                 break;
198                         case 14: config.c_maxsessions = atoi(buf);
199                                 if (config.c_maxsessions < 1)
200                                         config.c_maxsessions = 1;
201                                 break;
202                         case 15: strncpy(config.c_net_password, buf, 20);
203                                 break;
204                         case 16: config.c_userpurge = atoi(buf);
205                                 break;
206                         case 17: config.c_roompurge = atoi(buf);
207                                 break;
208                         case 18: strncpy(config.c_logpages,
209                                         buf, ROOMNAMELEN);
210                                 break;
211                         case 19: config.c_createax = atoi(buf);
212                                 if (config.c_createax < 1)
213                                         config.c_createax = 1;
214                                 if (config.c_createax > 6)
215                                         config.c_createax = 6;
216                                 break;
217                                 }
218                     ++a;
219                     }
220                 put_config();
221                 snprintf(buf,sizeof buf,
222                          "Global system configuration edited by %s",
223                          CC->curr_user);
224                 aide_message(buf);
225
226                 if (strlen(config.c_logpages) > 0)
227                         create_room(config.c_logpages, 4, "", 0);
228                 }
229
230         else {
231                 cprintf("%d The only valid options are GET and SET.\n",
232                         ERROR+ILLEGAL_VALUE);
233                 }
234         }