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