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