bc4b772a72c1c28626a7b743d864d82ca8dcdbcb
[citadel.git] / citadel / modules / expire / expire_policy.c
1 /* 
2  * Functions which manage expire policy for rooms
3  * Copyright (c) 1987-2012 by the citadel.org team
4  *
5  * This program is open source software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License, version 3.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13
14 #include "sysdep.h"
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <stdio.h>
18 #include <sys/stat.h>
19 #include <string.h>
20
21 #if TIME_WITH_SYS_TIME
22 # include <sys/time.h>
23 # include <time.h>
24 #else
25 # if HAVE_SYS_TIME_H
26 #  include <sys/time.h>
27 # else
28 #  include <time.h>
29 # endif
30 #endif
31
32 #include <limits.h>
33 #include <libcitadel.h>
34 #include "citadel.h"
35 #include "server.h"
36 #include "database.h"
37 #include "config.h"
38 #include "room_ops.h"
39 #include "sysdep_decls.h"
40 #include "support.h"
41 #include "msgbase.h"
42 #include "citserver.h"
43
44 #include "ctdl_module.h"
45 #include "user_ops.h"
46
47 /*
48  * Retrieve the applicable expire policy for a specific room
49  */
50 void GetExpirePolicy(struct ExpirePolicy *epbuf, struct ctdlroom *qrbuf) {
51         struct floor *fl;
52
53         /* If the room has its own policy, return it */ 
54         if (qrbuf->QRep.expire_mode != 0) {
55                 memcpy(epbuf, &qrbuf->QRep, sizeof(struct ExpirePolicy));
56                 return;
57         }
58
59         /* (non-mailbox rooms)
60          * If the floor has its own policy, return it
61          */
62         if ( (qrbuf->QRflags & QR_MAILBOX) == 0) {
63                 fl = CtdlGetCachedFloor(qrbuf->QRfloor);
64                 if (fl->f_ep.expire_mode != 0) {
65                         memcpy(epbuf, &fl->f_ep, sizeof(struct ExpirePolicy));
66                         return;
67                 }
68         }
69
70         /* (Mailbox rooms)
71          * If there is a default policy for mailbox rooms, return it
72          */
73         if (qrbuf->QRflags & QR_MAILBOX) {
74                 if (config.c_mbxep.expire_mode != 0) {
75                         memcpy(epbuf, &config.c_mbxep,
76                                 sizeof(struct ExpirePolicy));
77                         return;
78                 }
79         }
80
81         /* Otherwise, fall back on the system default */
82         memcpy(epbuf, &config.c_ep, sizeof(struct ExpirePolicy));
83 }
84
85
86 /*
87  * Get Policy EXpire
88  */
89 void cmd_gpex(char *argbuf) {
90         struct ExpirePolicy exp;
91         struct floor *fl;
92         char which[128];
93
94         extract_token(which, argbuf, 0, '|', sizeof which);
95         if (!strcasecmp(which, strof(roompolicy))||
96             !strcasecmp(which, "room")) { /* Deprecated version */
97                 memcpy(&exp, &CC->room.QRep, sizeof(struct ExpirePolicy));
98         }
99         else if (!strcasecmp(which, strof(floorpolicy))||
100                  !strcasecmp(which, "floor")) { /* Deprecated version */
101                 fl = CtdlGetCachedFloor(CC->room.QRfloor);
102                 memcpy(&exp, &fl->f_ep, sizeof(struct ExpirePolicy));
103         }
104         else if (!strcasecmp(which, strof(mailboxespolicy))||
105                  !strcasecmp(which, "mailboxes")) {/* Deprecated version */
106                 memcpy(&exp, &config.c_mbxep, sizeof(struct ExpirePolicy));
107         }
108         else if (!strcasecmp(which, strof(sitepolicy))||
109                  !strcasecmp(which, "site")) {/* Deprecated version */
110                 memcpy(&exp, &config.c_ep, sizeof(struct ExpirePolicy));
111         }
112         else {
113                 cprintf("%d Invalid keyword \"%s\"\n", ERROR + ILLEGAL_VALUE, which);
114                 return;
115         }
116
117         cprintf("%d %d|%d\n", CIT_OK, exp.expire_mode, exp.expire_value);
118 }
119
120
121 /*
122  * Set Policy EXpire
123  */
124 void cmd_spex(char *argbuf) {
125         struct ExpirePolicy exp;
126         struct floor flbuf;
127         char which[128];
128
129         memset(&exp, 0, sizeof(struct ExpirePolicy));
130         extract_token(which, argbuf, 0, '|', sizeof which);
131         exp.expire_mode = extract_int(argbuf, 1);
132         exp.expire_value = extract_int(argbuf, 2);
133
134         if ((exp.expire_mode < 0) || (exp.expire_mode > 3)) {
135                 cprintf("%d Invalid policy.\n", ERROR + ILLEGAL_VALUE);
136                 return;
137         }
138
139         if (    (!strcasecmp(which, strof(roompolicy)))
140                 || (!strcasecmp(which, "room"))
141         ) {
142                 if (!is_room_aide()) {
143                         cprintf("%d Higher access required.\n", ERROR + HIGHER_ACCESS_REQUIRED);
144                         return;
145                 }
146                 CtdlGetRoomLock(&CC->room, CC->room.QRname);
147                 memcpy(&CC->room.QRep, &exp, sizeof(struct ExpirePolicy));
148                 CtdlPutRoomLock(&CC->room);
149                 cprintf("%d Room expire policy for '%s' has been updated.\n", CIT_OK, CC->room.QRname);
150                 syslog(LOG_DEBUG, "Room: %s , Policy: %d , Value: %d",
151                         CC->room.QRname,
152                         exp.expire_mode,
153                         exp.expire_value
154                 );
155                 return;
156         }
157
158         if (CC->user.axlevel < AxAideU) {
159                 cprintf("%d Higher access required.\n", ERROR + HIGHER_ACCESS_REQUIRED);
160                 return;
161         }
162
163         if (    (!strcasecmp(which, strof(floorpolicy)))
164                 || (!strcasecmp(which, "floor"))
165         ) {
166                 lgetfloor(&flbuf, CC->room.QRfloor);
167                 memcpy(&flbuf.f_ep, &exp, sizeof(struct ExpirePolicy));
168                 lputfloor(&flbuf, CC->room.QRfloor);
169                 cprintf("%d Floor expire policy has been updated.\n", CIT_OK);
170                 return;
171         }
172
173         else if (       (!strcasecmp(which, strof(mailboxespolicy)))
174                         || (!strcasecmp(which, "mailboxes"))
175                 ) {
176                 memcpy(&config.c_mbxep, &exp, sizeof(struct ExpirePolicy));
177                 put_config();
178                 cprintf("%d Default expire policy for mailboxes set.\n", CIT_OK);
179                 return;
180         }
181
182         else if (       (!strcasecmp(which, strof(sitepolicy)))
183                         || (!strcasecmp(which, "site"))
184                 ) {
185                 if (exp.expire_mode == EXPIRE_NEXTLEVEL) {
186                         cprintf("%d Invalid policy (no higher level)\n",
187                                 ERROR + ILLEGAL_VALUE);
188                         return;
189                 }
190                 memcpy(&config.c_ep, &exp, sizeof(struct ExpirePolicy));
191                 put_config();
192                 cprintf("%d Site expire policy has been updated.\n", CIT_OK);
193                 return;
194         }
195
196         cprintf("%d Invalid keyword '%s'\n", ERROR + ILLEGAL_VALUE, which);
197 }