HUGE PATCH. This moves all of mime_parser.c and all
[citadel.git] / citadel / policy.c
1 /* 
2  * $Id$
3  *
4  * Functions which manage policy for rooms (such as message expiry)
5  */
6
7 #include "sysdep.h"
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <stdio.h>
11 #include <sys/stat.h>
12 #include <string.h>
13
14 #if TIME_WITH_SYS_TIME
15 # include <sys/time.h>
16 # include <time.h>
17 #else
18 # if HAVE_SYS_TIME_H
19 #  include <sys/time.h>
20 # else
21 #  include <time.h>
22 # endif
23 #endif
24
25 #include <limits.h>
26 #include <libcitadel.h>
27 #include "citadel.h"
28 #include "server.h"
29 #include "database.h"
30 #include "config.h"
31 #include "room_ops.h"
32 #include "sysdep_decls.h"
33 #include "support.h"
34 #include "user_ops.h"
35 #include "msgbase.h"
36 #include "citserver.h"
37
38
39 /*
40  * Retrieve the applicable expire policy for a specific room
41  */
42 void GetExpirePolicy(struct ExpirePolicy *epbuf, struct ctdlroom *qrbuf) {
43         struct floor *fl;
44
45         /* If the room has its own policy, return it */ 
46         if (qrbuf->QRep.expire_mode != 0) {
47                 memcpy(epbuf, &qrbuf->QRep, sizeof(struct ExpirePolicy));
48                 return;
49         }
50
51         /* (non-mailbox rooms)
52          * If the floor has its own policy, return it
53          */
54         if ( (qrbuf->QRflags & QR_MAILBOX) == 0) {
55                 fl = cgetfloor(qrbuf->QRfloor);
56                 if (fl->f_ep.expire_mode != 0) {
57                         memcpy(epbuf, &fl->f_ep, sizeof(struct ExpirePolicy));
58                         return;
59                 }
60         }
61
62         /* (Mailbox rooms)
63          * If there is a default policy for mailbox rooms, return it
64          */
65         if (qrbuf->QRflags & QR_MAILBOX) {
66                 if (config.c_mbxep.expire_mode != 0) {
67                         memcpy(epbuf, &config.c_mbxep,
68                                 sizeof(struct ExpirePolicy));
69                         return;
70                 }
71         }
72
73         /* Otherwise, fall back on the system default */
74         memcpy(epbuf, &config.c_ep, sizeof(struct ExpirePolicy));
75 }
76
77
78 /*
79  * Get Policy EXpire
80  */
81 void cmd_gpex(char *argbuf) {
82         struct ExpirePolicy exp;
83         struct floor *fl;
84         char which[128];
85
86         extract_token(which, argbuf, 0, '|', sizeof which);
87         if (!strcasecmp(which, "room")) {
88                 memcpy(&exp, &CC->room.QRep, sizeof(struct ExpirePolicy));
89         }
90         else if (!strcasecmp(which, "floor")) {
91                 fl = cgetfloor(CC->room.QRfloor);
92                 memcpy(&exp, &fl->f_ep, sizeof(struct ExpirePolicy));
93         }
94         else if (!strcasecmp(which, "mailboxes")) {
95                 memcpy(&exp, &config.c_mbxep, sizeof(struct ExpirePolicy));
96         }
97         else if (!strcasecmp(which, "site")) {
98                 memcpy(&exp, &config.c_ep, sizeof(struct ExpirePolicy));
99         }
100         else {
101                 cprintf("%d Invalid keyword \"%s\"\n", ERROR + ILLEGAL_VALUE, which);
102                 return;
103         }
104
105         cprintf("%d %d|%d\n", CIT_OK, exp.expire_mode, exp.expire_value);
106 }
107
108
109 /*
110  * Set Policy EXpire
111  */
112 void cmd_spex(char *argbuf) {
113         struct ExpirePolicy exp;
114         struct floor flbuf;
115         char which[128];
116
117         memset(&exp, 0, sizeof(struct ExpirePolicy));
118         extract_token(which, argbuf, 0, '|', sizeof which);
119         exp.expire_mode = extract_int(argbuf, 1);
120         exp.expire_value = extract_int(argbuf, 2);
121
122         if ((exp.expire_mode < 0) || (exp.expire_mode > 3)) {
123                 cprintf("%d Invalid policy.\n", ERROR + ILLEGAL_VALUE);
124                 return;
125         }
126
127         if (!strcasecmp(which, "room")) {
128                 if (!is_room_aide()) {
129                         cprintf("%d Higher access required.\n",
130                                 ERROR + HIGHER_ACCESS_REQUIRED);
131                         return;
132                 }
133                 lgetroom(&CC->room, CC->room.QRname);
134                 memcpy(&CC->room.QRep, &exp, sizeof(struct ExpirePolicy));
135                 lputroom(&CC->room);
136                 cprintf("%d Room expire policy has been updated.\n", CIT_OK);
137                 return;
138         }
139
140         if (CC->user.axlevel < 6) {
141                 cprintf("%d Higher access required.\n",
142                         ERROR + HIGHER_ACCESS_REQUIRED);
143                 return;
144         }
145
146         if (!strcasecmp(which, "floor")) {
147                 lgetfloor(&flbuf, CC->room.QRfloor);
148                 memcpy(&flbuf.f_ep, &exp, sizeof(struct ExpirePolicy));
149                 lputfloor(&flbuf, CC->room.QRfloor);
150                 cprintf("%d Floor expire policy has been updated.\n", CIT_OK);
151                 return;
152         }
153
154         else if (!strcasecmp(which, "mailboxes")) {
155                 memcpy(&config.c_mbxep, &exp, sizeof(struct ExpirePolicy));
156                 put_config();
157                 cprintf("%d Default expire policy for mailboxes set.\n",
158                         CIT_OK);
159                 return;
160         }
161
162         else if (!strcasecmp(which, "site")) {
163                 if (exp.expire_mode == EXPIRE_NEXTLEVEL) {
164                         cprintf("%d Invalid policy (no higher level)\n",
165                                 ERROR + ILLEGAL_VALUE);
166                         return;
167                 }
168                 memcpy(&config.c_ep, &exp, sizeof(struct ExpirePolicy));
169                 put_config();
170                 cprintf("%d Site expire policy has been updated.\n", CIT_OK);
171                 return;
172         }
173
174         else {
175                 cprintf("%d Invalid keyword \"%s\"\n", ERROR + ILLEGAL_VALUE, which);
176                 return;
177         }
178
179 }
180
181