fix possible crash on user deletion
[srvx.git] / src / mod-memoserv.c
1 /* mod-memoserv.c - MemoServ module for srvx
2  * Copyright 2003-2004 Martijn Smit and srvx Development Team
3  *
4  * This file is part of srvx.
5  *
6  * srvx is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with srvx; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
19  */
20
21 /* Adds new section to srvx.conf:
22  * "modules" {
23  *     "memoserv" {
24  *         "bot" "NickServ";
25  *         "message_expiry" "30d"; // age when messages are deleted; set
26  *                                 // to 0 to disable message expiration
27  *     };
28  *  };
29  *
30  * After that, to make the module active on an existing bot:
31  * /msg opserv bind nickserv * *memoserv.*
32  *
33  * If you want a dedicated MemoServ bot, make sure the service control
34  * commands are bound to OpServ:
35  * /msg opserv bind opserv service *modcmd.joiner
36  * /msg opserv bind opserv service\ add *modcmd.service\ add
37  * /msg opserv bind opserv service\ rename *modcmd.service\ rename
38  * /msg opserv bind opserv service\ trigger *modcmd.service\ trigger
39  * /msg opserv bind opserv service\ remove *modcmd.service\ remove
40  * Add the bot:
41  * /msg opserv service add MemoServ User-to-user Memorandum Service
42  * /msg opserv bind memoserv help *modcmd.help
43  * Restart srvx with the updated conf file (as above, butwith "bot"
44  * "MemoServ"), and bind the commands to it:
45  * /msg opserv bind memoserv * *memoserv.*
46  * /msg opserv bind memoserv set *modcmd.joiner
47  */
48
49 #include "chanserv.h"
50 #include "conf.h"
51 #include "modcmd.h"
52 #include "saxdb.h"
53 #include "timeq.h"
54
55 #define KEY_SENT "sent"
56 #define KEY_RECIPIENT "to"
57 #define KEY_FROM "from"
58 #define KEY_MESSAGE "msg"
59 #undef KEY_READ /* thanks microsoft! */
60 #define KEY_READ "read"
61 #define KEY_ACCOUNTS "accounts"
62 #define KEY_MESSAGES "messages"
63
64 static const struct message_entry msgtab[] = {
65     { "MSMSG_CANNOT_SEND_SELF", "You cannot send to yourself." },
66     { "MSMSG_CANNOT_SEND", "You cannot send to account $b%s$b." },
67     { "MSMSG_MEMO_SENT", "Message sent to $b%s$b." },
68     { "MSMSG_NO_MESSAGES", "You have no messages." },
69     { "MSMSG_MEMOS_FOUND", "Found $b%d$b matches.\nUse /msg $S READ <ID> to read a message." },
70     { "MSMSG_CLEAN_INBOX", "You have $b%d$b or more messages, please clean out your inbox.\nUse /msg $S READ <ID> to read a message." },
71     { "MSMSG_LIST_HEAD", "$bID$b   $bFrom$b       $bTime Sent$b" },
72     { "MSMSG_LIST_FORMAT", "%-2u     %s           %s" },
73     { "MSMSG_MEMO_HEAD", "Memo %u From $b%s$b, received on %s:" },
74     { "MSMSG_BAD_MESSAGE_ID", "$b%s$b is not a valid message ID (it should be a number between 0 and %u)." },
75     { "MSMSG_NO_SUCH_MEMO", "You have no memo with that ID." },
76     { "MSMSG_MEMO_DELETED", "Memo $b%d$b deleted." },
77     { "MSMSG_EXPIRY_OFF", "I am currently not expiring messages. (turned off)" },
78     { "MSMSG_EXPIRY", "Messages will be expired when they are %s old (%d seconds)." },
79     { "MSMSG_MESSAGES_EXPIRED", "$b%lu$b message(s) expired." },
80     { "MSMSG_MEMOS_INBOX", "You have $b%d$b new message(s) in your inbox and %d old messages.  Use /msg $S LIST to list them." },
81     { "MSMSG_NEW_MESSAGE", "You have a new message from $b%s$b (ID $b%d$b)." },
82     { "MSMSG_DELETED_ALL", "Deleted all of your messages." },
83     { "MSMSG_USE_CONFIRM", "Please use /msg $S DELETE * $bCONFIRM$b to delete $uall$u of your messages." },
84     { "MSMSG_STATUS_TOTAL", "I have $b%u$b memos in my database." },
85     { "MSMSG_STATUS_EXPIRED", "$b%ld$b memos expired during the time I am awake." },
86     { "MSMSG_STATUS_SENT", "$b%ld$b memos have been sent." },
87     { "MSMSG_SET_NOTIFY",     "$bNotify:       $b %s" },
88     { "MSMSG_SET_AUTHNOTIFY", "$bAuthNotify:   $b %s" },
89     { "MSMSG_SET_PRIVATE",    "$bPrivate:      $b %s" },
90     { NULL, NULL }
91 };
92
93 struct memo {
94     struct memo_account *recipient;
95     struct memo_account *sender;
96     char *message;
97     unsigned long sent;
98     unsigned int is_read : 1;
99 };
100
101 DECLARE_LIST(memoList, struct memo*);
102 DEFINE_LIST(memoList, struct memo*)
103
104 /* memo_account.flags fields */
105 #define MEMO_NOTIFY_NEW   1
106 #define MEMO_NOTIFY_LOGIN 2
107 #define MEMO_DENY_NONCHANNEL 4
108
109 struct memo_account {
110     struct handle_info *handle;
111     unsigned int flags;
112     struct memoList sent;
113     struct memoList recvd;
114 };
115
116 static struct {
117     struct userNode *bot;
118     unsigned long message_expiry;
119 } memoserv_conf;
120
121 const char *memoserv_module_deps[] = { NULL };
122 static struct module *memoserv_module;
123 static struct log_type *MS_LOG;
124 static unsigned long memoCount;
125 static unsigned long memosSent;
126 static unsigned long memosExpired;
127 static struct dict *memos; /* memo_account->handle->handle -> memo_account */
128
129 static struct memo_account *
130 memoserv_get_account(struct handle_info *hi)
131 {
132     struct memo_account *ma;
133     if (!hi)
134         return NULL;
135     ma = dict_find(memos, hi->handle, NULL);
136     if (ma)
137         return ma;
138     ma = calloc(1, sizeof(*ma));
139     if (!ma)
140         return ma;
141     ma->handle = hi;
142     ma->flags = MEMO_NOTIFY_NEW | MEMO_NOTIFY_LOGIN;
143     dict_insert(memos, ma->handle->handle, ma);
144     return ma;
145 }
146
147 static void
148 delete_memo(struct memo *memo)
149 {
150     memoList_remove(&memo->recipient->recvd, memo);
151     memoList_remove(&memo->sender->sent, memo);
152     free(memo->message);
153     free(memo);
154     memoCount--;
155 }
156
157 static void
158 delete_memo_account(void *data)
159 {
160     struct memo_account *ma = data;
161
162     while (ma->recvd.used)
163         delete_memo(ma->recvd.list[0]);
164     while (ma->sent.used)
165         delete_memo(ma->sent.list[0]);
166     memoList_clean(&ma->recvd);
167     memoList_clean(&ma->sent);
168     free(ma);
169 }
170
171 void
172 do_expire(void)
173 {
174     dict_iterator_t it;
175     for (it = dict_first(memos); it; it = iter_next(it)) {
176         struct memo_account *account = iter_data(it);
177         unsigned int ii;
178         for (ii = 0; ii < account->sent.used; ++ii) {
179             struct memo *memo = account->sent.list[ii];
180             if ((now - memo->sent) > memoserv_conf.message_expiry) {
181                 delete_memo(memo);
182                 memosExpired++;
183                 ii--;
184             }
185         }
186     }
187 }
188
189 static void
190 expire_memos(UNUSED_ARG(void *data))
191 {
192     if (memoserv_conf.message_expiry) {
193         do_expire();
194         timeq_add(now + memoserv_conf.message_expiry, expire_memos, NULL);
195     }
196 }
197
198 static struct memo*
199 add_memo(unsigned long sent, struct memo_account *recipient, struct memo_account *sender, char *message)
200 {
201     struct memo *memo;
202
203     memo = calloc(1, sizeof(*memo));
204     if (!memo)
205         return NULL;
206
207     memo->recipient = recipient;
208     memoList_append(&recipient->recvd, memo);
209     memo->sender = sender;
210     memoList_append(&sender->sent, memo);
211     memo->sent = sent;
212     memo->message = strdup(message);
213     memosSent++;
214     memoCount++;
215     return memo;
216 }
217
218 static int
219 memoserv_can_send(struct userNode *bot, struct userNode *user, struct memo_account *account)
220 {
221     extern struct userData *_GetChannelUser(struct chanData *channel, struct handle_info *handle, int override, int allow_suspended);
222     struct userData *dest;
223
224     if (!user->handle_info)
225         return 0;
226     if (user->handle_info == account->handle) {
227         send_message(user, bot, "MSMSG_CANNOT_SEND_SELF");
228         return 0;
229     }
230     if (!(account->flags & MEMO_DENY_NONCHANNEL))
231         return 1;
232     for (dest = account->handle->channels; dest; dest = dest->u_next) {
233         if (dest->seen && _GetChannelUser(dest->channel, user->handle_info, 1, 0))
234             return 1;
235     }
236     send_message(user, bot, "MSMSG_CANNOT_SEND", account->handle->handle);
237     return 0;
238 }
239
240 static struct memo *find_memo(struct userNode *user, struct svccmd *cmd, struct memo_account *ma, const char *msgid, unsigned int *id)
241 {
242     unsigned int memoid;
243     if (!isdigit(msgid[0])) {
244         if (ma->recvd.used)
245             reply("MSMSG_BAD_MESSAGE_ID", msgid, ma->recvd.used - 1);
246         else
247             reply("MSMSG_NO_MESSAGES");
248         return NULL;
249     }
250     memoid = atoi(msgid);
251     if (memoid >= ma->recvd.used) {
252         reply("MSMSG_NO_SUCH_MEMO");
253         return NULL;
254     }
255     return ma->recvd.list[*id = memoid];
256 }
257
258 static MODCMD_FUNC(cmd_send)
259 {
260     char *message;
261     struct handle_info *hi;
262     struct memo_account *ma, *sender;
263
264     if (!(hi = modcmd_get_handle_info(user, argv[1])))
265         return 0;
266     if (!(sender = memoserv_get_account(user->handle_info))
267         || !(ma = memoserv_get_account(hi))) {
268         reply("MSG_INTERNAL_FAILURE");
269         return 0;
270     }
271     if (!(memoserv_can_send(cmd->parent->bot, user, ma)))
272         return 0;
273     message = unsplit_string(argv + 2, argc - 2, NULL);
274     add_memo(now, ma, sender, message);
275     if (ma->flags & MEMO_NOTIFY_NEW) {
276         struct userNode *other;
277         for (other = ma->handle->users; other; other = other->next_authed)
278             send_message(other, cmd->parent->bot, "MSMSG_NEW_MESSAGE", user->nick, ma->recvd.used - 1);
279     }
280     reply("MSMSG_MEMO_SENT", ma->handle->handle);
281     return 1;
282 }
283
284 static MODCMD_FUNC(cmd_list)
285 {
286     struct memo_account *ma;
287     struct memo *memo;
288     unsigned int ii;
289     char posted[24];
290     time_t feh;
291
292     if (!(ma = memoserv_get_account(user->handle_info)))
293         return 0;
294     reply("MSMSG_LIST_HEAD");
295     for (ii = 0; (ii < ma->recvd.used) && (ii < 15); ++ii) {
296         memo = ma->recvd.list[ii];
297         feh = memo->sent;
298         strftime(posted, sizeof(posted), "%I:%M %p, %m/%d/%Y", localtime(&feh));
299         reply("MSMSG_LIST_FORMAT", ii, memo->sender->handle->handle, posted);
300     }
301     if (ii == 0)
302         reply("MSG_NONE");
303     else if (ii == 15)
304         reply("MSMSG_CLEAN_INBOX", ii);
305     else
306         reply("MSMSG_MEMOS_FOUND", ii);
307     return 1;
308 }
309
310 static MODCMD_FUNC(cmd_read)
311 {
312     struct memo_account *ma;
313     unsigned int memoid;
314     struct memo *memo;
315     char posted[24];
316     time_t memo_sent;
317
318     if (!(ma = memoserv_get_account(user->handle_info)))
319         return 0;
320     if (!(memo = find_memo(user, cmd, ma, argv[1], &memoid)))
321         return 0;
322     memo_sent = memo->sent;
323     strftime(posted, sizeof(posted), "%I:%M %p, %m/%d/%Y", localtime(&memo_sent));
324     reply("MSMSG_MEMO_HEAD", memoid, memo->sender->handle->handle, posted);
325     send_message_type(4, user, cmd->parent->bot, "%s", memo->message);
326     memo->is_read = 1;
327     return 1;
328 }
329
330 static MODCMD_FUNC(cmd_delete)
331 {
332     struct memo_account *ma;
333     struct memo *memo;
334     unsigned int memoid;
335
336     if (!(ma = memoserv_get_account(user->handle_info)))
337         return 0;
338     if (!irccasecmp(argv[1], "*") || !irccasecmp(argv[1], "all")) {
339         if ((argc < 3) || irccasecmp(argv[2], "confirm")) {
340             reply("MSMSG_USE_CONFIRM");
341             return 0;
342         }
343         while (ma->recvd.used)
344             delete_memo(ma->recvd.list[0]);
345         reply("MSMSG_DELETED_ALL");
346         return 1;
347     }
348
349     if (!(memo = find_memo(user, cmd, ma, argv[1], &memoid)))
350         return 0;
351     delete_memo(memo);
352     reply("MSMSG_MEMO_DELETED", memoid);
353     return 1;
354 }
355
356 static MODCMD_FUNC(cmd_expire)
357 {
358     unsigned long old_expired = memosExpired;
359     do_expire();
360     reply("MSMSG_MESSAGES_EXPIRED", memosExpired - old_expired);
361     return 1;
362 }
363
364 static MODCMD_FUNC(cmd_expiry)
365 {
366     char interval[INTERVALLEN];
367
368     if (!memoserv_conf.message_expiry) {
369         reply("MSMSG_EXPIRY_OFF");
370         return 1;
371     }
372
373     intervalString(interval, memoserv_conf.message_expiry, user->handle_info);
374     reply("MSMSG_EXPIRY", interval, memoserv_conf.message_expiry);
375     return 1;
376 }
377
378 static MODCMD_FUNC(cmd_set_notify)
379 {
380     struct memo_account *ma;
381     char *choice;
382
383     if (!(ma = memoserv_get_account(user->handle_info)))
384         return 0;
385     if (argc > 1) {
386         choice = argv[1];
387         if (enabled_string(choice)) {
388             ma->flags |= MEMO_NOTIFY_NEW;
389         } else if (disabled_string(choice)) {
390             ma->flags &= ~MEMO_NOTIFY_NEW;
391         } else {
392             reply("MSG_INVALID_BINARY", choice);
393             return 0;
394         }
395     }
396
397     choice = (ma->flags & MEMO_NOTIFY_NEW) ? "on" : "off";
398     reply("MSMSG_SET_NOTIFY", choice);
399     return 1;
400 }
401
402 static MODCMD_FUNC(cmd_set_authnotify)
403 {
404     struct memo_account *ma;
405     char *choice;
406
407     if (!(ma = memoserv_get_account(user->handle_info)))
408         return 0;
409     if (argc > 1) {
410         choice = argv[1];
411         if (enabled_string(choice)) {
412             ma->flags |= MEMO_NOTIFY_LOGIN;
413         } else if (disabled_string(choice)) {
414             ma->flags &= ~MEMO_NOTIFY_LOGIN;
415         } else {
416             reply("MSG_INVALID_BINARY", choice);
417             return 0;
418         }
419     }
420
421     choice = (ma->flags & MEMO_NOTIFY_LOGIN) ? "on" : "off";
422     reply("MSMSG_SET_AUTHNOTIFY", choice);
423     return 1;
424 }
425
426 static MODCMD_FUNC(cmd_set_private)
427 {
428     struct memo_account *ma;
429     char *choice;
430
431     if (!(ma = memoserv_get_account(user->handle_info)))
432         return 0;
433     if (argc > 1) {
434         choice = argv[1];
435         if (enabled_string(choice)) {
436             ma->flags |= MEMO_DENY_NONCHANNEL;
437         } else if (disabled_string(choice)) {
438             ma->flags &= ~MEMO_DENY_NONCHANNEL;
439         } else {
440             reply("MSG_INVALID_BINARY", choice);
441             return 0;
442         }
443     }
444
445     choice = (ma->flags & MEMO_DENY_NONCHANNEL) ? "on" : "off";
446     reply("MSMSG_SET_PRIVATE", choice);
447     return 1;
448 }
449
450 static MODCMD_FUNC(cmd_status)
451 {
452     reply("MSMSG_STATUS_TOTAL", memoCount);
453     reply("MSMSG_STATUS_EXPIRED", memosExpired);
454     reply("MSMSG_STATUS_SENT", memosSent);
455     return 1;
456 }
457
458 static void
459 memoserv_conf_read(void)
460 {
461     dict_t conf_node;
462     const char *str;
463
464     str = "modules/memoserv";
465     if (!(conf_node = conf_get_data(str, RECDB_OBJECT))) {
466         log_module(MS_LOG, LOG_ERROR, "config node `%s' is missing or has wrong type.", str);
467         return;
468     }
469
470     str = database_get_data(conf_node, "message_expiry", RECDB_QSTRING);
471     memoserv_conf.message_expiry = str ? ParseInterval(str) : 60*24*30;
472 }
473
474 static int
475 memoserv_saxdb_read_messages(struct dict *db)
476 {
477     char *str;
478     struct handle_info *sender, *recipient;
479     struct record_data *hir;
480     struct memo *memo;
481     dict_iterator_t it;
482     unsigned long sent;
483
484     for (it = dict_first(db); it; it = iter_next(it)) {
485         hir = iter_data(it);
486         if (hir->type != RECDB_OBJECT) {
487             log_module(MS_LOG, LOG_WARNING, "Unexpected rectype %d for %s.", hir->type, iter_key(it));
488             continue;
489         }
490
491         if (!(str = database_get_data(hir->d.object, KEY_SENT, RECDB_QSTRING))) {
492             log_module(MS_LOG, LOG_ERROR, "Date sent not present in memo %s; skipping", iter_key(it));
493             continue;
494         }
495         sent = strtoul(str, NULL, 0);
496
497         if (!(str = database_get_data(hir->d.object, KEY_RECIPIENT, RECDB_QSTRING))) {
498             log_module(MS_LOG, LOG_ERROR, "Recipient not present in memo %s; skipping", iter_key(it));
499             continue;
500         } else if (!(recipient = get_handle_info(str))) {
501             log_module(MS_LOG, LOG_ERROR, "Invalid recipient %s in memo %s; skipping", str, iter_key(it));
502             continue;
503         }
504
505         if (!(str = database_get_data(hir->d.object, KEY_FROM, RECDB_QSTRING))) {
506             log_module(MS_LOG, LOG_ERROR, "Sender not present in memo %s; skipping", iter_key(it));
507             continue;
508         } else if (!(sender = get_handle_info(str))) {
509             log_module(MS_LOG, LOG_ERROR, "Invalid sender %s in memo %s; skipping", str, iter_key(it));
510             continue;
511         }
512
513         if (!(str = database_get_data(hir->d.object, KEY_MESSAGE, RECDB_QSTRING))) {
514             log_module(MS_LOG, LOG_ERROR, "Message not present in memo %s; skipping", iter_key(it));
515             continue;
516         }
517
518         memo = add_memo(sent, memoserv_get_account(recipient), memoserv_get_account(sender), str);
519         if ((str = database_get_data(hir->d.object, KEY_READ, RECDB_QSTRING)))
520             memo->is_read = 1;
521     }
522     return 0;
523 }
524
525 static void
526 memoserv_saxdb_read_accounts(struct dict *db)
527 {
528     struct memo_account *ma;
529     struct handle_info *hi;
530     struct record_data *rd;
531     dict_iterator_t it;
532     const char *str;
533
534     for (it = dict_first(db); it; it = iter_next(it)) {
535         hi = get_handle_info(iter_key(it));
536         if (hi == NULL) {
537             log_module(MS_LOG, LOG_WARNING, "No account known for %s.", iter_key(it));
538             continue;
539         }
540
541         ma = memoserv_get_account(hi);
542         if (ma == NULL) {
543             log_module(MS_LOG, LOG_WARNING, "Unable to allocate memory for account %s.", iter_key(it));
544             continue;
545         }
546
547         rd = iter_data(it);
548         if (rd->type == RECDB_QSTRING) {
549             str = rd->d.qstring;
550         } else {
551             log_module(MS_LOG, LOG_WARNING, "Unexpected rectype %d for accounts/%s.", rd->type, iter_key(it));
552             continue;
553         }
554
555         if (str != NULL)
556             ma->flags = strtol(str, NULL, 0);
557     }
558 }
559
560 static int
561 memoserv_saxdb_read(struct dict *db)
562 {
563     struct dict *obj;
564
565     obj = database_get_data(db, KEY_ACCOUNTS, RECDB_OBJECT);
566     if (obj == NULL) {
567         return memoserv_saxdb_read_messages(db);
568     } else {
569         memoserv_saxdb_read_accounts(obj);
570         obj = database_get_data(db, KEY_MESSAGES, RECDB_OBJECT);
571         return memoserv_saxdb_read_messages(obj);
572     }
573 }
574
575 static int
576 memoserv_saxdb_write(struct saxdb_context *ctx)
577 {
578     dict_iterator_t it;
579     struct memo_account *ma;
580     struct memo *memo;
581     char str[17];
582     unsigned int id = 0, ii;
583
584     saxdb_start_record(ctx, "accounts", 1);
585     for (it = dict_first(memos); it; it = iter_next(it)) {
586         ma = iter_data(it);
587         saxdb_write_int(ctx, ma->handle->handle, ma->flags);
588     }
589     saxdb_end_record(ctx);
590
591     saxdb_start_record(ctx, "messages", 1);
592     for (it = dict_first(memos); it; it = iter_next(it)) {
593         ma = iter_data(it);
594         for (ii = 0; ii < ma->recvd.used; ++ii) {
595             memo = ma->recvd.list[ii];
596             snprintf(str, sizeof(str), "%x", id++);
597             saxdb_start_record(ctx, str, 0);
598             saxdb_write_int(ctx, KEY_SENT, memo->sent);
599             saxdb_write_string(ctx, KEY_RECIPIENT, memo->recipient->handle->handle);
600             saxdb_write_string(ctx, KEY_FROM, memo->sender->handle->handle);
601             saxdb_write_string(ctx, KEY_MESSAGE, memo->message);
602             if (memo->is_read)
603                 saxdb_write_int(ctx, KEY_READ, 1);
604             saxdb_end_record(ctx);
605         }
606     }
607     saxdb_end_record(ctx);
608
609     return 0;
610 }
611
612 static void
613 memoserv_cleanup(void)
614 {
615     dict_delete(memos);
616 }
617
618 static void
619 memoserv_check_messages(struct userNode *user, UNUSED_ARG(struct handle_info *old_handle))
620 {
621     unsigned int ii, unseen;
622     struct memo_account *ma;
623     struct memo *memo;
624
625     if (!(ma = memoserv_get_account(user->handle_info))
626         || !(ma->flags & MEMO_NOTIFY_LOGIN))
627         return;
628     for (ii = unseen = 0; ii < ma->recvd.used; ++ii) {
629         memo = ma->recvd.list[ii];
630         if (!memo->is_read)
631             unseen++;
632     }
633     if (ma->recvd.used && memoserv_conf.bot)
634         send_message(user, memoserv_conf.bot, "MSMSG_MEMOS_INBOX", unseen, ma->recvd.used - unseen);
635 }
636
637 static void
638 memoserv_rename_account(struct handle_info *hi, const char *old_handle)
639 {
640     struct memo_account *ma;
641     if (!(ma = dict_find(memos, old_handle, NULL)))
642         return;
643     dict_remove2(memos, old_handle, 1);
644     dict_insert(memos, hi->handle, ma);
645 }
646
647 static void
648 memoserv_unreg_account(UNUSED_ARG(struct userNode *user), struct handle_info *handle)
649 {
650     dict_remove(memos, handle->handle);
651 }
652
653 int
654 memoserv_init(void)
655 {
656     MS_LOG = log_register_type("MemoServ", "file:memoserv.log");
657     memos = dict_new();
658     dict_set_free_data(memos, delete_memo_account);
659     reg_auth_func(memoserv_check_messages);
660     reg_handle_rename_func(memoserv_rename_account);
661     reg_unreg_func(memoserv_unreg_account);
662     conf_register_reload(memoserv_conf_read);
663     reg_exit_func(memoserv_cleanup);
664     saxdb_register("MemoServ", memoserv_saxdb_read, memoserv_saxdb_write);
665
666     memoserv_module = module_register("MemoServ", MS_LOG, "mod-memoserv.help", NULL);
667     modcmd_register(memoserv_module, "send", cmd_send, 3, MODCMD_REQUIRE_AUTHED, NULL);
668     modcmd_register(memoserv_module, "list", cmd_list, 1, MODCMD_REQUIRE_AUTHED, NULL);
669     modcmd_register(memoserv_module, "read", cmd_read, 2, MODCMD_REQUIRE_AUTHED, NULL);
670     modcmd_register(memoserv_module, "delete", cmd_delete, 2, MODCMD_REQUIRE_AUTHED, NULL);
671     modcmd_register(memoserv_module, "expire", cmd_expire, 1, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL);
672     modcmd_register(memoserv_module, "expiry", cmd_expiry, 1, 0, NULL);
673     modcmd_register(memoserv_module, "status", cmd_status, 1, 0, NULL);
674     modcmd_register(memoserv_module, "set notify", cmd_set_notify, 1, 0, NULL);
675     modcmd_register(memoserv_module, "set authnotify", cmd_set_authnotify, 1, 0, NULL);
676     modcmd_register(memoserv_module, "set private", cmd_set_private, 1, 0, NULL);
677     message_register_table(msgtab);
678
679     if (memoserv_conf.message_expiry)
680         timeq_add(now + memoserv_conf.message_expiry, expire_memos, NULL);
681     return 1;
682 }
683
684 int
685 memoserv_finalize(void) {
686     dict_t conf_node;
687     const char *str;
688
689     str = "modules/memoserv";
690     if (!(conf_node = conf_get_data(str, RECDB_OBJECT))) {
691         log_module(MS_LOG, LOG_ERROR, "config node `%s' is missing or has wrong type.", str);
692         return 0;
693     }
694
695     str = database_get_data(conf_node, "bot", RECDB_QSTRING);
696     if (str)
697         memoserv_conf.bot = GetUserH(str);
698     return 1;
699 }