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