bc5d155d8342785cbaf2508a5a43383960dacb4a
[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(roompolicy))||
89             !strcasecmp(which, "room")) { /* Deprecated version */
90                 memcpy(&exp, &CC->room.QRep, sizeof(struct ExpirePolicy));
91         }
92         else if (!strcasecmp(which, strof(floorpolicy))||
93                  !strcasecmp(which, "floor")) { /* Deprecated version */
94                 fl = CtdlGetCachedFloor(CC->room.QRfloor);
95                 memcpy(&exp, &fl->f_ep, sizeof(struct ExpirePolicy));
96         }
97         else if (!strcasecmp(which, strof(mailboxespolicy))||
98                  !strcasecmp(which, "mailboxes")) {/* Deprecated version */
99                 memcpy(&exp, &config.c_mbxep, sizeof(struct ExpirePolicy));
100         }
101         else if (!strcasecmp(which, strof(sitepolicy))||
102                  !strcasecmp(which, "site")) {/* Deprecated version */
103                 memcpy(&exp, &config.c_ep, sizeof(struct ExpirePolicy));
104         }
105         else {
106                 cprintf("%d Invalid keyword \"%s\"\n", ERROR + ILLEGAL_VALUE, which);
107                 return;
108         }
109
110         cprintf("%d %d|%d\n", CIT_OK, exp.expire_mode, exp.expire_value);
111 }
112
113
114 /*
115  * Set Policy EXpire
116  */
117 void cmd_spex(char *argbuf) {
118         struct ExpirePolicy exp;
119         struct floor flbuf;
120         char which[128];
121
122         memset(&exp, 0, sizeof(struct ExpirePolicy));
123         extract_token(which, argbuf, 0, '|', sizeof which);
124         exp.expire_mode = extract_int(argbuf, 1);
125         exp.expire_value = extract_int(argbuf, 2);
126
127         if ((exp.expire_mode < 0) || (exp.expire_mode > 3)) {
128                 cprintf("%d Invalid policy.\n", ERROR + ILLEGAL_VALUE);
129                 return;
130         }
131
132         if (!strcasecmp(which, strof(roompolicy))||
133             !strcasecmp(which, "room")) {
134                 if (!is_room_aide()) {
135                         cprintf("%d Higher access required.\n",
136                                 ERROR + HIGHER_ACCESS_REQUIRED);
137                         return;
138                 }
139                 CtdlGetRoomLock(&CC->room, CC->room.QRname);
140                 memcpy(&CC->room.QRep, &exp, sizeof(struct ExpirePolicy));
141                 CtdlPutRoomLock(&CC->room);
142                 cprintf("%d Room expire policy has been updated.\n", CIT_OK);
143                 return;
144         }
145
146         if (CC->user.axlevel < AxAideU) {
147                 cprintf("%d Higher access required.\n",
148                         ERROR + HIGHER_ACCESS_REQUIRED);
149                 return;
150         }
151
152         if (!strcasecmp(which, strof(floorpolicy))||
153             !strcasecmp(which, "floor")) { /* deprecated version */
154                 lgetfloor(&flbuf, CC->room.QRfloor);
155                 memcpy(&flbuf.f_ep, &exp, sizeof(struct ExpirePolicy));
156                 lputfloor(&flbuf, CC->room.QRfloor);
157                 cprintf("%d Floor expire policy has been updated.\n", CIT_OK);
158                 return;
159         }
160
161         else if (!strcasecmp(which, strof(mailboxespolicy))||
162                  !strcasecmp(which, "mailboxes")) {
163                 memcpy(&config.c_mbxep, &exp, sizeof(struct ExpirePolicy));
164                 put_config();
165                 cprintf("%d Default expire policy for mailboxes set.\n",
166                         CIT_OK);
167                 return;
168         }
169
170         else if (!strcasecmp(which, strof(sitepolicy))||
171                  !strcasecmp(which, "site")) {/* deprecated version */
172                 if (exp.expire_mode == EXPIRE_NEXTLEVEL) {
173                         cprintf("%d Invalid policy (no higher level)\n",
174                                 ERROR + ILLEGAL_VALUE);
175                         return;
176                 }
177                 memcpy(&config.c_ep, &exp, sizeof(struct ExpirePolicy));
178                 put_config();
179                 cprintf("%d Site expire policy has been updated.\n", CIT_OK);
180                 return;
181         }
182
183         else {
184                 cprintf("%d Invalid keyword \"%s\"\n", ERROR + ILLEGAL_VALUE, which);
185                 return;
186         }
187
188 }
189