Back to index

citadel  8.12
expire_policy.c
Go to the documentation of this file.
00001 /* 
00002  * Functions which manage expire policy for rooms
00003  * Copyright (c) 1987-2012 by the citadel.org team
00004  *
00005  * This program is open source software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License, version 3.
00007  *
00008  * This program is distributed in the hope that it will be useful,
00009  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  * GNU General Public License for more details.
00012  */
00013 
00014 #include "sysdep.h"
00015 #include <stdlib.h>
00016 #include <unistd.h>
00017 #include <stdio.h>
00018 #include <sys/stat.h>
00019 #include <string.h>
00020 
00021 #if TIME_WITH_SYS_TIME
00022 # include <sys/time.h>
00023 # include <time.h>
00024 #else
00025 # if HAVE_SYS_TIME_H
00026 #  include <sys/time.h>
00027 # else
00028 #  include <time.h>
00029 # endif
00030 #endif
00031 
00032 #include <limits.h>
00033 #include <libcitadel.h>
00034 #include "citadel.h"
00035 #include "server.h"
00036 #include "database.h"
00037 #include "config.h"
00038 #include "room_ops.h"
00039 #include "sysdep_decls.h"
00040 #include "support.h"
00041 #include "msgbase.h"
00042 #include "citserver.h"
00043 
00044 #include "ctdl_module.h"
00045 #include "user_ops.h"
00046 
00047 /*
00048  * Retrieve the applicable expire policy for a specific room
00049  */
00050 void GetExpirePolicy(struct ExpirePolicy *epbuf, struct ctdlroom *qrbuf) {
00051        struct floor *fl;
00052 
00053        /* If the room has its own policy, return it */  
00054        if (qrbuf->QRep.expire_mode != 0) {
00055               memcpy(epbuf, &qrbuf->QRep, sizeof(struct ExpirePolicy));
00056               return;
00057        }
00058 
00059        /* (non-mailbox rooms)
00060         * If the floor has its own policy, return it
00061         */
00062        if ( (qrbuf->QRflags & QR_MAILBOX) == 0) {
00063               fl = CtdlGetCachedFloor(qrbuf->QRfloor);
00064               if (fl->f_ep.expire_mode != 0) {
00065                      memcpy(epbuf, &fl->f_ep, sizeof(struct ExpirePolicy));
00066                      return;
00067               }
00068        }
00069 
00070        /* (Mailbox rooms)
00071         * If there is a default policy for mailbox rooms, return it
00072         */
00073        if (qrbuf->QRflags & QR_MAILBOX) {
00074               if (config.c_mbxep.expire_mode != 0) {
00075                      memcpy(epbuf, &config.c_mbxep,
00076                             sizeof(struct ExpirePolicy));
00077                      return;
00078               }
00079        }
00080 
00081        /* Otherwise, fall back on the system default */
00082        memcpy(epbuf, &config.c_ep, sizeof(struct ExpirePolicy));
00083 }
00084 
00085 
00086 /*
00087  * Get Policy EXpire
00088  */
00089 void cmd_gpex(char *argbuf) {
00090        struct ExpirePolicy exp;
00091        struct floor *fl;
00092        char which[128];
00093 
00094        extract_token(which, argbuf, 0, '|', sizeof which);
00095        if (!strcasecmp(which, strof(roompolicy))||
00096            !strcasecmp(which, "room")) { /* Deprecated version */
00097               memcpy(&exp, &CC->room.QRep, sizeof(struct ExpirePolicy));
00098        }
00099        else if (!strcasecmp(which, strof(floorpolicy))||
00100                !strcasecmp(which, "floor")) { /* Deprecated version */
00101               fl = CtdlGetCachedFloor(CC->room.QRfloor);
00102               memcpy(&exp, &fl->f_ep, sizeof(struct ExpirePolicy));
00103        }
00104        else if (!strcasecmp(which, strof(mailboxespolicy))||
00105                !strcasecmp(which, "mailboxes")) {/* Deprecated version */
00106               memcpy(&exp, &config.c_mbxep, sizeof(struct ExpirePolicy));
00107        }
00108        else if (!strcasecmp(which, strof(sitepolicy))||
00109                !strcasecmp(which, "site")) {/* Deprecated version */
00110               memcpy(&exp, &config.c_ep, sizeof(struct ExpirePolicy));
00111        }
00112        else {
00113               cprintf("%d Invalid keyword \"%s\"\n", ERROR + ILLEGAL_VALUE, which);
00114               return;
00115        }
00116 
00117        cprintf("%d %d|%d\n", CIT_OK, exp.expire_mode, exp.expire_value);
00118 }
00119 
00120 
00121 /*
00122  * Set Policy EXpire
00123  */
00124 void cmd_spex(char *argbuf) {
00125        struct ExpirePolicy exp;
00126        struct floor flbuf;
00127        char which[128];
00128 
00129        memset(&exp, 0, sizeof(struct ExpirePolicy));
00130        extract_token(which, argbuf, 0, '|', sizeof which);
00131        exp.expire_mode = extract_int(argbuf, 1);
00132        exp.expire_value = extract_int(argbuf, 2);
00133 
00134        if ((exp.expire_mode < 0) || (exp.expire_mode > 3)) {
00135               cprintf("%d Invalid policy.\n", ERROR + ILLEGAL_VALUE);
00136               return;
00137        }
00138 
00139        if (   (!strcasecmp(which, strof(roompolicy)))
00140               || (!strcasecmp(which, "room"))
00141        ) {
00142               if (!is_room_aide()) {
00143                      cprintf("%d Higher access required.\n", ERROR + HIGHER_ACCESS_REQUIRED);
00144                      return;
00145               }
00146               CtdlGetRoomLock(&CC->room, CC->room.QRname);
00147               memcpy(&CC->room.QRep, &exp, sizeof(struct ExpirePolicy));
00148               CtdlPutRoomLock(&CC->room);
00149               cprintf("%d Room expire policy for '%s' has been updated.\n", CIT_OK, CC->room.QRname);
00150               syslog(LOG_DEBUG, "Room: %s , Policy: %d , Value: %d",
00151                      CC->room.QRname,
00152                      exp.expire_mode,
00153                      exp.expire_value
00154               );
00155               return;
00156        }
00157 
00158        if (CC->user.axlevel < AxAideU) {
00159               cprintf("%d Higher access required.\n", ERROR + HIGHER_ACCESS_REQUIRED);
00160               return;
00161        }
00162 
00163        if (   (!strcasecmp(which, strof(floorpolicy)))
00164               || (!strcasecmp(which, "floor"))
00165        ) {
00166               lgetfloor(&flbuf, CC->room.QRfloor);
00167               memcpy(&flbuf.f_ep, &exp, sizeof(struct ExpirePolicy));
00168               lputfloor(&flbuf, CC->room.QRfloor);
00169               cprintf("%d Floor expire policy has been updated.\n", CIT_OK);
00170               return;
00171        }
00172 
00173        else if (     (!strcasecmp(which, strof(mailboxespolicy)))
00174                      || (!strcasecmp(which, "mailboxes"))
00175               ) {
00176               memcpy(&config.c_mbxep, &exp, sizeof(struct ExpirePolicy));
00177               put_config();
00178               cprintf("%d Default expire policy for mailboxes set.\n", CIT_OK);
00179               return;
00180        }
00181 
00182        else if (     (!strcasecmp(which, strof(sitepolicy)))
00183                      || (!strcasecmp(which, "site"))
00184               ) {
00185               if (exp.expire_mode == EXPIRE_NEXTLEVEL) {
00186                      cprintf("%d Invalid policy (no higher level)\n",
00187                             ERROR + ILLEGAL_VALUE);
00188                      return;
00189               }
00190               memcpy(&config.c_ep, &exp, sizeof(struct ExpirePolicy));
00191               put_config();
00192               cprintf("%d Site expire policy has been updated.\n", CIT_OK);
00193               return;
00194        }
00195 
00196        cprintf("%d Invalid keyword '%s'\n", ERROR + ILLEGAL_VALUE, which);
00197 }