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