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