* added RCS Id keyword strings to sources
[citadel.git] / citadel / serv_test.c
1 /* $Id$ */
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <stdio.h>
5 #include <fcntl.h>
6 #include <signal.h>
7 #include <pwd.h>
8 #include <errno.h>
9 #include <sys/types.h>
10 #include <sys/time.h>
11 #include <sys/wait.h>
12 #include <string.h>
13 #include <limits.h>
14 #include <pthread.h>
15 #include "citadel.h"
16 #include "server.h"
17 #include <syslog.h>
18 #include <time.h>
19 #include "sysdep_decls.h"
20 #include "citserver.h"
21 #include "support.h"
22 #include "config.h"
23 #include "dynloader.h"
24 #include "room_ops.h"
25 #include "policy.h"
26 #include "database.h"
27 #include "msgbase.h"
28
29 extern struct CitContext *ContextList;
30
31 #define MODULE_NAME     "Dummy test module"
32 #define MODULE_AUTHOR   "Art Cancro"
33 #define MODULE_EMAIL    "ajc@uncnsrd.mt-kisco.ny.us"
34 #define MAJOR_VERSION   0
35 #define MINOR_VERSION   3
36
37 static struct DLModule_Info info = {
38         MODULE_NAME,
39         MODULE_AUTHOR,
40         MODULE_EMAIL,
41         MAJOR_VERSION,
42         MINOR_VERSION
43         };
44
45 void CleanupTest(void) {
46         lprintf(9, "--- test of adding an unload hook --- \n");
47         }
48
49 void NewRoomTest(void) {
50         lprintf(9, "--- test module was told we're now in %s ---\n",
51                 CC->cs_room);
52         }
53
54 void SessionStartTest(void) {
55         lprintf(9, "--- starting up session %d ---\n",
56                 CC->cs_pid);
57         }
58
59 void SessionStopTest(void) {
60         lprintf(9, "--- ending session %d ---\n", 
61                 CC->cs_pid);
62         }
63
64 void LoginTest(void) {
65         lprintf(9, "--- Hello, %s ---\n", CC->curr_user);
66         }
67
68
69 void Ygorl(char *username, long usernum) {
70         if (!strcasecmp(username, "Hexslinger")) {
71                 strcpy(username, "Flaming Asshole");
72                 }
73         }
74
75
76
77 void DoPurgeMessages(struct quickroom *qrbuf) {
78         struct ExpirePolicy epbuf;
79         long delnum;
80         time_t xtime, now;
81         char msgid[64];
82         int a;
83
84         time(&now);
85         GetExpirePolicy(&epbuf, qrbuf);
86         
87         /* lprintf(9, "ExpirePolicy for <%s> is <%d> <%d>\n",
88                 qrbuf->QRname, epbuf.expire_mode, epbuf.expire_value);
89          */
90
91         /* If the room is set to never expire messages ... do nothing */
92         if (epbuf.expire_mode == EXPIRE_NEXTLEVEL) return;
93         if (epbuf.expire_mode == EXPIRE_MANUAL) return;
94
95         get_msglist(qrbuf);
96         
97         /* Nothing to do if there aren't any messages */
98         if (CC->num_msgs == 0) return;
99
100         /* If the room is set to expire by count, do that */
101         if (epbuf.expire_mode == EXPIRE_NUMMSGS) {
102                 while (CC->num_msgs > epbuf.expire_value) {
103                         delnum = MessageFromList(0);
104                         lprintf(5, "Expiring message %ld\n", delnum);
105                         cdb_delete(CDB_MSGMAIN, &delnum, sizeof(long));
106                         memcpy(&CC->msglist[0], &CC->msglist[1],
107                                 (sizeof(long)*(CC->num_msgs - 1)));
108                         CC->num_msgs = CC->num_msgs - 1;
109                         }
110                 }
111
112         /* If the room is set to expire by age... */
113         if (epbuf.expire_mode == EXPIRE_AGE) {
114                 for (a=0; a<(CC->num_msgs); ++a) {
115                         delnum = MessageFromList(a);
116                         sprintf(msgid, "%ld", delnum);
117                         xtime = output_message(msgid, MT_DATE, 0, 0);
118
119                         if ((xtime > 0L)
120                            && (now - xtime > (time_t)(epbuf.expire_value * 86400L))) {
121                                 cdb_delete(CDB_MSGMAIN, &delnum, sizeof(long));
122                                 SetMessageInList(a, 0L);
123                                 lprintf(5, "Expiring message %ld\n", delnum);
124                                 }
125                         }
126                 }
127         CC->num_msgs = sort_msglist(CC->msglist, CC->num_msgs);
128         put_msglist(qrbuf);
129         }
130
131 void PurgeMessages(void) {
132         ForEachRoom(DoPurgeMessages);
133         }
134
135 struct DLModule_Info *Dynamic_Module_Init(void)
136 {
137    CtdlRegisterCleanupHook(CleanupTest);
138    CtdlRegisterCleanupHook(PurgeMessages);
139    CtdlRegisterSessionHook(NewRoomTest, EVT_NEWROOM);
140    CtdlRegisterSessionHook(SessionStartTest, EVT_START);
141    CtdlRegisterSessionHook(SessionStopTest, EVT_STOP);
142    CtdlRegisterSessionHook(LoginTest, EVT_LOGIN);
143    CtdlRegisterUserHook(Ygorl, EVT_OUTPUTMSG);
144    return &info;
145 }