More removal of $Id$ tags
[citadel.git] / citadel / modules / expire / expire_policy.c
1 /* 
2  * Functions which manage policy for rooms (such as message expiry)
3  */
4
5 #include "sysdep.h"
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <stdio.h>
9 #include <sys/stat.h>
10 #include <string.h>
11
12 #if TIME_WITH_SYS_TIME
13 # include <sys/time.h>
14 # include <time.h>
15 #else
16 # if HAVE_SYS_TIME_H
17 #  include <sys/time.h>
18 # else
19 #  include <time.h>
20 # endif
21 #endif
22
23 #include <limits.h>
24 #include <libcitadel.h>
25 #include "citadel.h"
26 #include "server.h"
27 #include "database.h"
28 #include "config.h"
29 #include "room_ops.h"
30 #include "sysdep_decls.h"
31 #include "support.h"
32 #include "msgbase.h"
33 #include "citserver.h"
34
35 #include "ctdl_module.h"
36 #include "user_ops.h"
37
38 /*
39  * Retrieve the applicable expire policy for a specific room
40  */
41 void GetExpirePolicy(struct ExpirePolicy *epbuf, struct ctdlroom *qrbuf) {
42         struct floor *fl;
43
44         /* If the room has its own policy, return it */ 
45         if (qrbuf->QRep.expire_mode != 0) {
46                 memcpy(epbuf, &qrbuf->QRep, sizeof(struct ExpirePolicy));
47                 return;
48         }
49
50         /* (non-mailbox rooms)
51          * If the floor has its own policy, return it
52          */
53         if ( (qrbuf->QRflags & QR_MAILBOX) == 0) {
54                 fl = CtdlGetCachedFloor(qrbuf->QRfloor);
55                 if (fl->f_ep.expire_mode != 0) {
56                         memcpy(epbuf, &fl->f_ep, sizeof(struct ExpirePolicy));
57                         return;
58                 }
59         }
60
61         /* (Mailbox rooms)
62          * If there is a default policy for mailbox rooms, return it
63          */
64         if (qrbuf->QRflags & QR_MAILBOX) {
65                 if (config.c_mbxep.expire_mode != 0) {
66                         memcpy(epbuf, &config.c_mbxep,
67                                 sizeof(struct ExpirePolicy));
68                         return;
69                 }
70         }
71
72         /* Otherwise, fall back on the system default */
73         memcpy(epbuf, &config.c_ep, sizeof(struct ExpirePolicy));
74 }
75
76
77 /*
78  * Get Policy EXpire
79  */
80 void cmd_gpex(char *argbuf) {
81         struct ExpirePolicy exp;
82         struct floor *fl;
83         char which[128];
84
85         extract_token(which, argbuf, 0, '|', sizeof which);
86         if (!strcasecmp(which, strof(roompolicy))||
87             !strcasecmp(which, "room")) { /* Deprecated version */
88                 memcpy(&exp, &CC->room.QRep, sizeof(struct ExpirePolicy));
89         }
90         else if (!strcasecmp(which, strof(floorpolicy))||
91                  !strcasecmp(which, "floor")) { /* Deprecated version */
92                 fl = CtdlGetCachedFloor(CC->room.QRfloor);
93                 memcpy(&exp, &fl->f_ep, sizeof(struct ExpirePolicy));
94         }
95         else if (!strcasecmp(which, strof(mailboxespolicy))||
96                  !strcasecmp(which, "mailboxes")) {/* Deprecated version */
97                 memcpy(&exp, &config.c_mbxep, sizeof(struct ExpirePolicy));
98         }
99         else if (!strcasecmp(which, strof(sitepolicy))||
100                  !strcasecmp(which, "site")) {/* Deprecated version */
101                 memcpy(&exp, &config.c_ep, sizeof(struct ExpirePolicy));
102         }
103         else {
104                 cprintf("%d Invalid keyword \"%s\"\n", ERROR + ILLEGAL_VALUE, which);
105                 return;
106         }
107
108         cprintf("%d %d|%d\n", CIT_OK, exp.expire_mode, exp.expire_value);
109 }
110
111
112 /*
113  * Set Policy EXpire
114  */
115 void cmd_spex(char *argbuf) {
116         struct ExpirePolicy exp;
117         struct floor flbuf;
118         char which[128];
119
120         memset(&exp, 0, sizeof(struct ExpirePolicy));
121         extract_token(which, argbuf, 0, '|', sizeof which);
122         exp.expire_mode = extract_int(argbuf, 1);
123         exp.expire_value = extract_int(argbuf, 2);
124
125         if ((exp.expire_mode < 0) || (exp.expire_mode > 3)) {
126                 cprintf("%d Invalid policy.\n", ERROR + ILLEGAL_VALUE);
127                 return;
128         }
129
130         if (!strcasecmp(which, strof(roompolicy))||
131             !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