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