9b2faf1a52999e85f80c5b4a865161723b2832e3
[citadel.git] / citadel / modules / expire / expire_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 "msgbase.h"
35 #include "citserver.h"
36
37 #include "ctdl_module.h"
38 #include "user_ops.h"
39
40 /*
41  * Retrieve the applicable expire policy for a specific room
42  */
43 void GetExpirePolicy(struct ExpirePolicy *epbuf, struct ctdlroom *qrbuf) {
44         struct floor *fl;
45
46         /* If the room has its own policy, return it */ 
47         if (qrbuf->QRep.expire_mode != 0) {
48                 memcpy(epbuf, &qrbuf->QRep, sizeof(struct ExpirePolicy));
49                 return;
50         }
51
52         /* (non-mailbox rooms)
53          * If the floor has its own policy, return it
54          */
55         if ( (qrbuf->QRflags & QR_MAILBOX) == 0) {
56                 fl = CtdlGetCachedFloor(qrbuf->QRfloor);
57                 if (fl->f_ep.expire_mode != 0) {
58                         memcpy(epbuf, &fl->f_ep, sizeof(struct ExpirePolicy));
59                         return;
60                 }
61         }
62
63         /* (Mailbox rooms)
64          * If there is a default policy for mailbox rooms, return it
65          */
66         if (qrbuf->QRflags & QR_MAILBOX) {
67                 if (config.c_mbxep.expire_mode != 0) {
68                         memcpy(epbuf, &config.c_mbxep,
69                                 sizeof(struct ExpirePolicy));
70                         return;
71                 }
72         }
73
74         /* Otherwise, fall back on the system default */
75         memcpy(epbuf, &config.c_ep, sizeof(struct ExpirePolicy));
76 }
77
78
79 /*
80  * Get Policy EXpire
81  */
82 void cmd_gpex(char *argbuf) {
83         struct ExpirePolicy exp;
84         struct floor *fl;
85         char which[128];
86
87         extract_token(which, argbuf, 0, '|', sizeof which);
88         if (!strcasecmp(which, strof(room))) {
89                 memcpy(&exp, &CC->room.QRep, sizeof(struct ExpirePolicy));
90         }
91         else if (!strcasecmp(which, strof(floorpolicy))||
92                  !strcasecmp(which, "floor")) { /* Deprecated version */
93                 fl = CtdlGetCachedFloor(CC->room.QRfloor);
94                 memcpy(&exp, &fl->f_ep, sizeof(struct ExpirePolicy));
95         }
96         else if (!strcasecmp(which, strof(mailboxespolicy))||
97                  !strcasecmp(which, "mailboxes")) {/* Deprecated version */
98                 memcpy(&exp, &config.c_mbxep, sizeof(struct ExpirePolicy));
99         }
100         else if (!strcasecmp(which, strof(sitepolicy))||
101                  !strcasecmp(which, "site")) {/* Deprecated version */
102                 memcpy(&exp, &config.c_ep, sizeof(struct ExpirePolicy));
103         }
104         else {
105                 cprintf("%d Invalid keyword \"%s\"\n", ERROR + ILLEGAL_VALUE, which);
106                 return;
107         }
108
109         cprintf("%d %d|%d\n", CIT_OK, exp.expire_mode, exp.expire_value);
110 }
111
112
113 /*
114  * Set Policy EXpire
115  */
116 void cmd_spex(char *argbuf) {
117         struct ExpirePolicy exp;
118         struct floor flbuf;
119         char which[128];
120
121         memset(&exp, 0, sizeof(struct ExpirePolicy));
122         extract_token(which, argbuf, 0, '|', sizeof which);
123         exp.expire_mode = extract_int(argbuf, 1);
124         exp.expire_value = extract_int(argbuf, 2);
125
126         if ((exp.expire_mode < 0) || (exp.expire_mode > 3)) {
127                 cprintf("%d Invalid policy.\n", ERROR + ILLEGAL_VALUE);
128                 return;
129         }
130
131         if (!strcasecmp(which, "room")) {
132                 if (!is_room_aide()) {
133                         cprintf("%d Higher access required.\n",
134                                 ERROR + HIGHER_ACCESS_REQUIRED);
135                         return;
136                 }
137                 CtdlGetRoomLock(&CC->room, CC->room.QRname);
138                 memcpy(&CC->room.QRep, &exp, sizeof(struct ExpirePolicy));
139                 CtdlPutRoomLock(&CC->room);
140                 cprintf("%d Room expire policy has been updated.\n", CIT_OK);
141                 return;
142         }
143
144         if (CC->user.axlevel < AxAideU) {
145                 cprintf("%d Higher access required.\n",
146                         ERROR + HIGHER_ACCESS_REQUIRED);
147                 return;
148         }
149
150         if (!strcasecmp(which, strof(floorpolicy))||
151             !strcasecmp(which, "floor")) { /* deprecated version */
152                 lgetfloor(&flbuf, CC->room.QRfloor);
153                 memcpy(&flbuf.f_ep, &exp, sizeof(struct ExpirePolicy));
154                 lputfloor(&flbuf, CC->room.QRfloor);
155                 cprintf("%d Floor expire policy has been updated.\n", CIT_OK);
156                 return;
157         }
158
159         else if (!strcasecmp(which, strof(mailboxespolicy))||
160                  !strcasecmp(which, "mailboxes")) {
161                 memcpy(&config.c_mbxep, &exp, sizeof(struct ExpirePolicy));
162                 put_config();
163                 cprintf("%d Default expire policy for mailboxes set.\n",
164                         CIT_OK);
165                 return;
166         }
167
168         else if (!strcasecmp(which, strof(sitepolicy))||
169                  !strcasecmp(which, "site")) {/* deprecated version */
170                 if (exp.expire_mode == EXPIRE_NEXTLEVEL) {
171                         cprintf("%d Invalid policy (no higher level)\n",
172                                 ERROR + ILLEGAL_VALUE);
173                         return;
174                 }
175                 memcpy(&config.c_ep, &exp, sizeof(struct ExpirePolicy));
176                 put_config();
177                 cprintf("%d Site expire policy has been updated.\n", CIT_OK);
178                 return;
179         }
180
181         else {
182                 cprintf("%d Invalid keyword \"%s\"\n", ERROR + ILLEGAL_VALUE, which);
183                 return;
184         }
185
186 }
187