]> jfr.im git - irc/evilnet/x3.git/blame - src/mod-memoserv.c
couple of tweaks so i can do testing with creating svn tarballs
[irc/evilnet/x3.git] / src / mod-memoserv.c
CommitLineData
d76ed9a9 1/* mod-memoserv.c - MemoServ module for srvx
2 * Copyright 2003-2004 Martijn Smit and srvx Development Team
34a9e19a 3 * Copyright 2005-2006 X3 Development Team
d76ed9a9 4 *
83ff05c3 5 * This file is part of x3.
d76ed9a9 6 *
d0f04f71 7 * x3 is free software; you can redistribute it and/or modify
d76ed9a9 8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with srvx; if not, write to the Free Software Foundation,
19 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20 */
21
acf3c6d5 22/*
d76ed9a9 23 * /msg opserv bind nickserv * *memoserv.*
24 *
25 * If you want a dedicated MemoServ bot, make sure the service control
26 * commands are bound to OpServ:
27 * /msg opserv bind opserv service *modcmd.joiner
28 * /msg opserv bind opserv service\ add *modcmd.service\ add
29 * /msg opserv bind opserv service\ rename *modcmd.service\ rename
30 * /msg opserv bind opserv service\ trigger *modcmd.service\ trigger
31 * /msg opserv bind opserv service\ remove *modcmd.service\ remove
32 * Add the bot:
33 * /msg opserv service add MemoServ User-to-user Memorandum Service
34 * /msg opserv bind memoserv help *modcmd.help
35 * Restart srvx with the updated conf file (as above, butwith "bot"
36 * "MemoServ"), and bind the commands to it:
37 * /msg opserv bind memoserv * *memoserv.*
fc4a50fc 38 * /msg opserv bind memoserv set *memoserv.set
d76ed9a9 39 */
40
41#include "chanserv.h"
42#include "conf.h"
43#include "modcmd.h"
e3e5ba49 44#include "nickserv.h"
08895577 45#include "opserv.h"
d76ed9a9 46#include "saxdb.h"
47#include "timeq.h"
48
34a9e19a 49#define KEY_MAIN_ACCOUNTS "accounts"
50#define KEY_FLAGS "flags"
51#define KEY_LIMIT "limit"
52
1b4a47ca 53#define KEY_MAIN_HISTORY "history"
34a9e19a 54#define KEY_MAIN_MEMOS "memos"
d76ed9a9 55#define KEY_SENT "sent"
56#define KEY_RECIPIENT "to"
57#define KEY_FROM "from"
58#define KEY_MESSAGE "msg"
59#define KEY_READ "read"
82794e1b 60#define KEY_RECIEPT "reciept"
f2e592d3 61#define KEY_ID "id"
d76ed9a9 62
1b4a47ca 63
d76ed9a9 64static const struct message_entry msgtab[] = {
65 { "MSMSG_CANNOT_SEND", "You cannot send to account $b%s$b." },
a76bcc6a 66 { "MSMSG_UNKNOWN_SEND_FLAG", "Unreccognised send flag '%c', message not sent." },
f2e592d3 67 { "MSMSG_MEMO_SENT", "Message sent to $b%s$b (ID# %d)." },
d76ed9a9 68 { "MSMSG_NO_MESSAGES", "You have no messages." },
fc4a50fc 69 { "MSMSG_MEMOS_FOUND", "Found $b%d$b matches." },
70 { "MSMSG_HOWTO_READ", "Use /msg $S READ <ID> to read a message." },
d76ed9a9 71 { "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." },
cbc5a1a4 72 { "MSMSG_LIST_HEAD", "$bID$b $bFrom$b $bTime Sent$b" },
73 { "MSMSG_LIST_FORMAT", "%-2u %s $b%s$b %s" },
74 { "MSMSG_HISTORY_HEADER", "$bID$b $bTo$b $bTime Sent$b" },
75 { "MSMSG_HISTORY_FORMAT", "%-2u %s %s" },
d76ed9a9 76 { "MSMSG_MEMO_HEAD", "Memo %u From $b%s$b, received on %s:" },
82794e1b 77 { "MSMSG_MEMO_RECIEPT", "$bRead Reciept$b requested, %s." },
d76ed9a9 78 { "MSMSG_BAD_MESSAGE_ID", "$b%s$b is not a valid message ID (it should be a number between 0 and %u)." },
79 { "MSMSG_NO_SUCH_MEMO", "You have no memo with that ID." },
80 { "MSMSG_MEMO_DELETED", "Memo $b%d$b deleted." },
f2e592d3 81 { "MSMSG_MEMO_CANCEL_NUMBER", "You must specify a number id" },
82 { "MSMSG_MEMO_DONT_OWN", "You did not send memo# %d" },
83 { "MSMSG_MEMO_READ", "Memo# %d has already been read, you cannot cancel it." },
84 { "MSMSG_MEMO_CANT_LOCATE", "Could not locate memo# %d" },
d76ed9a9 85 { "MSMSG_EXPIRY_OFF", "I am currently not expiring messages. (turned off)" },
86 { "MSMSG_EXPIRY", "Messages will be expired when they are %s old (%d seconds)." },
87 { "MSMSG_MESSAGES_EXPIRED", "$b%lu$b message(s) expired." },
88 { "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." },
850bf0cf 89 { "MSMSG_NEW_MESSAGE", "You have a new message from $b%s$b. /msg $S LIST" },
a8692672 90 { "MSMSG_FULL_INBOX", "$b%s$b cannot recieve anymore memos as their inbox is full" },
d76ed9a9 91 { "MSMSG_DELETED_ALL", "Deleted all of your messages." },
92 { "MSMSG_USE_CONFIRM", "Please use /msg $S DELETE * $bCONFIRM$b to delete $uall$u of your messages." },
e3e5ba49 93
1b4a47ca 94 { "MSMSG_STATUS_HIST_TOTAL", "I have $b%u$b history entries in my database." },
acf3c6d5 95 { "MSMSG_STATUS_TOTAL", "I have $b%u$b memos in my database." },
d76ed9a9 96 { "MSMSG_STATUS_EXPIRED", "$b%ld$b memos expired during the time I am awake." },
acf3c6d5 97 { "MSMSG_STATUS_SENT", "$b%ld$b memos have been sent." },
e3e5ba49 98
a8692672 99 { "MSMSG_INVALID_OPTION", "$b%s$b is not a valid option." },
e3e5ba49 100 { "MSMSG_INVALID_BINARY", "$b%s$b is an invalid binary value." },
fc4a50fc 101 { "MSMSG_SET_AUTHNOTIFY", "$bAuthNotify$b: %s" },
102 { "MSMSG_SET_NEWNOTIFY", "$bNewNotify$b: %s" },
103 { "MSMSG_SET_PRIVMSG", "$bPrivmsg$b: %s" },
104 { "MSMSG_SET_PRIVATE", "$bPrivate$b: %s" },
105 { "MSMSG_SET_IGNORERECIEPTS", "$bIgnoreReciepts$b: %s" },
106 { "MSMSG_SET_SENDRECIEPTS", "$bSendReciepts$b: %s" },
107 { "MSMSG_SET_LIMIT", "$bLimit$b: %d" },
2a951803 108 { "MSMSG_SET_OPTIONS", "$bMessaging Options$b" },
e3e5ba49 109 { "MSMSG_SET_OPTIONS_END", "-------------End of Options-------------" },
110
111 { "MSMSG_LIST_END", "--------------End of Memos--------------" },
112 { "MSMSG_BAR", "----------------------------------------"},
113
08895577 114 { "MSMSG_DEFCON_NO_NEW_MEMOS", "You cannot send new memos at this time, please try again soon." },
115
d76ed9a9 116 { NULL, NULL }
117};
118
119struct memo {
120 struct memo_account *recipient;
121 struct memo_account *sender;
122 char *message;
123 time_t sent;
f2e592d3 124 unsigned long id;
d76ed9a9 125 unsigned int is_read : 1;
82794e1b 126 unsigned int reciept : 1;
d76ed9a9 127};
128
1b4a47ca 129struct history {
130 struct memo_account *recipient;
131 struct memo_account *sender;
132 time_t sent;
133 unsigned long id;
134};
135
68cb9c2c 136struct userNode *memoserv;
137
138#define MEMOSERV_FUNC(NAME) MODCMD_FUNC(NAME)
139#define MEMOSERV_SYNTAX() svccmd_send_help_brief(user, memoserv, cmd)
140#define MEMOSERV_MIN_PARAMS(N) if(argc < (N)) { \
141 reply("MSG_MISSING_PARAMS", argv[0]); \
142 MEMOSERV_SYNTAX(); \
143 return 0; }
144
d76ed9a9 145DECLARE_LIST(memoList, struct memo*);
146DEFINE_LIST(memoList, struct memo*);
1b4a47ca 147DECLARE_LIST(historyList, struct history*);
148DEFINE_LIST(historyList, struct history*);
d76ed9a9 149
150/* memo_account.flags fields */
2a951803 151#define MEMO_NOTIFY_NEW 0x00000001
152#define MEMO_NOTIFY_LOGIN 0x00000002
153#define MEMO_DENY_NONCHANNEL 0x00000004
154#define MEMO_IGNORE_RECIEPTS 0x00000008
155#define MEMO_ALWAYS_RECIEPTS 0x00000010
fc4a50fc 156#define MEMO_USE_PRIVMSG 0x00000020
d76ed9a9 157
158struct memo_account {
159 struct handle_info *handle;
fc4a50fc 160 unsigned int flags : 6;
a8692672 161 unsigned int limit;
d76ed9a9 162 struct memoList sent;
163 struct memoList recvd;
1b4a47ca 164 struct historyList hsent;
165 struct historyList hrecvd;
d76ed9a9 166};
167
168static struct {
169 struct userNode *bot;
170 int message_expiry;
a8692672 171 unsigned int limit;
d76ed9a9 172} memoserv_conf;
173
d9abe201 174#define MEMOSERV_FUNC(NAME) MODCMD_FUNC(NAME)
c092fcad 175#define OPTION_FUNC(NAME) int NAME(struct svccmd *cmd, struct userNode *user, struct handle_info *hi, UNUSED_ARG(unsigned int override), unsigned int argc, char *argv[])
d9abe201 176typedef OPTION_FUNC(option_func_t);
177
f2e592d3 178unsigned long memo_id;
179
e3e5ba49 180extern struct string_list *autojoin_channels;
d76ed9a9 181const char *memoserv_module_deps[] = { NULL };
182static struct module *memoserv_module;
183static struct log_type *MS_LOG;
184static unsigned long memosSent, memosExpired;
185static struct dict *memos; /* memo_account->handle->handle -> memo_account */
1b4a47ca 186static struct dict *historys;
e3e5ba49 187static dict_t memoserv_opt_dict; /* contains option_func_t* */
188
d76ed9a9 189static struct memo_account *
190memoserv_get_account(struct handle_info *hi)
191{
192 struct memo_account *ma;
193 if (!hi)
194 return NULL;
195 ma = dict_find(memos, hi->handle, NULL);
196 if (ma)
197 return ma;
198 ma = calloc(1, sizeof(*ma));
199 if (!ma)
200 return ma;
201 ma->handle = hi;
fc4a50fc 202 ma->flags = MEMO_NOTIFY_NEW | MEMO_NOTIFY_LOGIN | MEMO_USE_PRIVMSG;
512d7958 203 ma->limit = memoserv_conf.limit;
d76ed9a9 204 dict_insert(memos, ma->handle->handle, ma);
1b4a47ca 205 dict_insert(historys, ma->handle->handle, ma);
d76ed9a9 206 return ma;
207}
208
209static void
210delete_memo(struct memo *memo)
211{
212 memoList_remove(&memo->recipient->recvd, memo);
213 memoList_remove(&memo->sender->sent, memo);
214 free(memo->message);
215 free(memo);
216}
217
1b4a47ca 218static void
219delete_history(struct history *history)
220{
221 historyList_remove(&history->recipient->hrecvd, history);
222 historyList_remove(&history->sender->hsent, history);
223 free(history);
224}
225
d76ed9a9 226static void
227delete_memo_account(void *data)
228{
229 struct memo_account *ma = data;
230
231 while (ma->recvd.used)
232 delete_memo(ma->recvd.list[0]);
233 while (ma->sent.used)
234 delete_memo(ma->sent.list[0]);
235 memoList_clean(&ma->recvd);
236 memoList_clean(&ma->sent);
1b4a47ca 237
238 while (ma->hrecvd.used)
239 delete_history(ma->hrecvd.list[0]);
240 while (ma->hsent.used)
241 delete_history(ma->hsent.list[0]);
242 historyList_clean(&ma->hrecvd);
243 historyList_clean(&ma->hsent);
d76ed9a9 244 free(ma);
245}
246
247void
248do_expire(void)
249{
250 dict_iterator_t it;
251 for (it = dict_first(memos); it; it = iter_next(it)) {
252 struct memo_account *acct = iter_data(it);
253 unsigned int ii;
254 for (ii = 0; ii < acct->sent.used; ++ii) {
255 struct memo *memo = acct->sent.list[ii];
256 if ((now - memo->sent) > memoserv_conf.message_expiry) {
257 delete_memo(memo);
258 memosExpired++;
259 ii--;
260 }
261 }
262 }
1b4a47ca 263
264 for (it = dict_first(historys); it; it = iter_next(it)) {
265 struct memo_account *acct = iter_data(it);
266 unsigned int ii;
267 for (ii = 0; ii < acct->hsent.used; ++ii) {
268 struct history *history = acct->hsent.list[ii];
269 if ((now - history->sent) > memoserv_conf.message_expiry) {
270 delete_history(history);
271 memosExpired++;
272 ii--;
273 }
274 }
275 }
d76ed9a9 276}
277
278static void
279expire_memos(UNUSED_ARG(void *data))
280{
281 if (memoserv_conf.message_expiry) {
282 do_expire();
283 timeq_add(now + memoserv_conf.message_expiry, expire_memos, NULL);
284 }
285}
286
1b4a47ca 287static struct history*
288add_history(time_t sent, struct memo_account *recipient, struct memo_account *sender, unsigned long id)
289{
290 struct history *history;
291
292 history = calloc(1, sizeof(*history));
293 if (!history)
294 return NULL;
295
296 history->id = id;
297 history->recipient = recipient;
298 historyList_append(&recipient->hrecvd, history);
299 history->sender = sender;
300 historyList_append(&sender->hsent, history);
301 history->sent = sent;
302
303 return history;
304}
305
306
d76ed9a9 307static struct memo*
f2e592d3 308add_memo(time_t sent, struct memo_account *recipient, struct memo_account *sender, char *message, int nfrom_read)
d76ed9a9 309{
310 struct memo *memo;
1b4a47ca 311 struct history *history;
d76ed9a9 312
313 memo = calloc(1, sizeof(*memo));
314 if (!memo)
315 return NULL;
316
f2e592d3 317 if (nfrom_read) {
318 memo_id++;
319 memo->id = memo_id;
320 }
321
d76ed9a9 322 memo->recipient = recipient;
323 memoList_append(&recipient->recvd, memo);
324 memo->sender = sender;
325 memoList_append(&sender->sent, memo);
326 memo->sent = sent;
327 memo->message = strdup(message);
328 memosSent++;
1b4a47ca 329
330 if (nfrom_read)
331 history = add_history(sent, recipient, sender, memo->id);
332
d76ed9a9 333 return memo;
334}
335
336static int
337memoserv_can_send(struct userNode *bot, struct userNode *user, struct memo_account *acct)
338{
339 extern struct userData *_GetChannelUser(struct chanData *channel, struct handle_info *handle, int override, int allow_suspended);
340 struct userData *dest;
35ca8140 341 unsigned int i = 0, match = 0;
d76ed9a9 342
343 if (!user->handle_info)
344 return 0;
a8692672 345
346 /* Sanity checks here because if the user doesnt have a limit set
347 the limit comes out at like 21233242 if you try and use it. */
348 if (acct->limit > memoserv_conf.limit)
349 acct->limit = memoserv_conf.limit;
350
351 if (acct->recvd.used > acct->limit) {
352 send_message(user, bot, "MSMSG_FULL_INBOX", acct->handle->handle);
353 send_message(user, bot, "MSMSG_CANNOT_SEND", acct->handle->handle);
354 return 0;
355 }
356
35ca8140 357 if (acct->handle->ignores->used) {
358 for (i=0; i < acct->handle->ignores->used; i++) {
359 if (user_matches_glob(user, acct->handle->ignores->list[i], MATCH_USENICK)) {
360 match = 1;
361 break;
362 }
363 }
364
365 if (match) {
366 send_message(user, bot, "MSMSG_CANNOT_SEND", acct->handle->handle);
367 return 0;
368 }
369 }
370
d76ed9a9 371 if (!(acct->flags & MEMO_DENY_NONCHANNEL))
372 return 1;
a8692672 373
d76ed9a9 374 for (dest = acct->handle->channels; dest; dest = dest->u_next)
375 if (_GetChannelUser(dest->channel, user->handle_info, 1, 0))
376 return 1;
a8692672 377
d76ed9a9 378 send_message(user, bot, "MSMSG_CANNOT_SEND", acct->handle->handle);
379 return 0;
380}
381
382static struct memo *find_memo(struct userNode *user, struct svccmd *cmd, struct memo_account *ma, const char *msgid, unsigned int *id)
383{
384 unsigned int memoid;
385 if (!isdigit(msgid[0])) {
386 if (ma->recvd.used)
387 reply("MSMSG_BAD_MESSAGE_ID", msgid, ma->recvd.used - 1);
388 else
389 reply("MSMSG_NO_MESSAGES");
390 return NULL;
391 }
392 memoid = atoi(msgid);
393 if (memoid >= ma->recvd.used) {
394 reply("MSMSG_NO_SUCH_MEMO");
395 return NULL;
396 }
397 return ma->recvd.list[*id = memoid];
398}
399
400static MODCMD_FUNC(cmd_send)
401{
402 char *message;
82794e1b 403 int reciept = 0, inc = 2;
d76ed9a9 404 struct handle_info *hi;
405 struct memo_account *ma, *sender;
82794e1b 406 struct memo *memo;
d76ed9a9 407
68cb9c2c 408 MEMOSERV_MIN_PARAMS(3);
409
08895577 410 if (checkDefCon(DEFCON_NO_NEW_MEMOS) && !IsOper(user)) {
411 reply("MSMSG_DEFCON_NO_NEW_MEMOS");
412 return 0;
413 }
414
d76ed9a9 415 if (!(hi = modcmd_get_handle_info(user, argv[1])))
416 return 0;
a8692672 417
d76ed9a9 418 if (!(sender = memoserv_get_account(user->handle_info))
419 || !(ma = memoserv_get_account(hi))) {
420 reply("MSG_INTERNAL_FAILURE");
421 return 0;
422 }
a8692672 423
d76ed9a9 424 if (!(memoserv_can_send(cmd->parent->bot, user, ma)))
425 return 0;
a8692672 426
a76bcc6a 427 inc = 2; /* Start of message on 3rd ([2]) word */
428 if(argv[2][0] == '-' && argv[2][1] != '-') { /* first word is flags ('-r')*/
429 char *flags = argv[2];
430 inc++; /* Start of message is now 1 word later */
431 for(flags++;*flags;flags++) {
432 switch (*flags) {
433 case 'r':
82794e1b 434 reciept = 1;
435 break;
436
437 default:
a76bcc6a 438 /* Unknown mode. Give an error */
439 reply("MSMSG_UNKNOWN_SEND_FLAG", *flags);
440 return 0;
441 }
82794e1b 442 }
443 }
a76bcc6a 444 else
445 inc = 2; /* Start of message is word 2 */
82794e1b 446
447 message = unsplit_string(argv + inc, argc - inc, NULL);
f2e592d3 448 memo = add_memo(now, ma, sender, message, 1);
2a951803 449 if ((reciept == 1) || (ma->flags & MEMO_ALWAYS_RECIEPTS))
82794e1b 450 memo->reciept = 1;
a8692672 451
d76ed9a9 452 if (ma->flags & MEMO_NOTIFY_NEW) {
453 struct userNode *other;
a8692672 454
d76ed9a9 455 for (other = ma->handle->users; other; other = other->next_authed)
fc4a50fc 456 send_message_type((ma->flags & MEMO_USE_PRIVMSG)? MSG_TYPE_PRIVMSG : MSG_TYPE_NOTICE, other, cmd->parent->bot, "MSMSG_NEW_MESSAGE", user->nick);
d76ed9a9 457 }
a8692672 458
f2e592d3 459 reply("MSMSG_MEMO_SENT", ma->handle->handle, memo_id);
d76ed9a9 460 return 1;
461}
462
463static MODCMD_FUNC(cmd_list)
464{
465 struct memo_account *ma;
466 struct memo *memo;
467 unsigned int ii;
468 char posted[24];
469 struct tm tm;
470
471 if (!(ma = memoserv_get_account(user->handle_info)))
472 return 0;
e3e5ba49 473
d76ed9a9 474 reply("MSMSG_LIST_HEAD");
e3e5ba49 475
476 if(user->handle_info && user->handle_info->userlist_style != HI_STYLE_CLEAN)
477 reply("MSMSG_BAR");
478
d76ed9a9 479 for (ii = 0; (ii < ma->recvd.used) && (ii < 15); ++ii) {
480 memo = ma->recvd.list[ii];
481 localtime_r(&memo->sent, &tm);
482 strftime(posted, sizeof(posted), "%I:%M %p, %m/%d/%Y", &tm);
82794e1b 483 reply("MSMSG_LIST_FORMAT", ii, memo->sender->handle->handle, memo->reciept ? "(r)" : "", posted);
d76ed9a9 484 }
485 if (ii == 0)
486 reply("MSG_NONE");
487 else if (ii == 15)
488 reply("MSMSG_CLEAN_INBOX", ii);
fc4a50fc 489 else {
d76ed9a9 490 reply("MSMSG_MEMOS_FOUND", ii);
fc4a50fc 491 reply("MSMSG_HOWTO_READ");
492 }
e3e5ba49 493
494 reply("MSMSG_LIST_END");
495
d76ed9a9 496 return 1;
497}
498
cbc5a1a4 499static MODCMD_FUNC(cmd_history)
500{
501 struct memo_account *ma;
1b4a47ca 502 struct history *history;
cbc5a1a4 503 dict_iterator_t it;
504 unsigned int ii = 0;
505 unsigned int cc = 0;
506 char posted[24];
507 struct tm tm;
508
509 if (!(ma = memoserv_get_account(user->handle_info)))
510 return 0;
511
512 reply("MSMSG_HISTORY_HEADER");
513
514 if(user->handle_info && user->handle_info->userlist_style != HI_STYLE_CLEAN)
515 reply("MSMSG_BAR");
516
1b4a47ca 517 for (it = dict_first(historys); it; it = iter_next(it)) {
cbc5a1a4 518 ma = iter_data(it);
1b4a47ca 519 for (ii = 0; ii < ma->hrecvd.used; ++ii) {
520 history = ma->hrecvd.list[ii];
521 if (!strcasecmp(history->sender->handle->handle, user->handle_info->handle)) {
cbc5a1a4 522 cc++;
1b4a47ca 523 localtime_r(&history->sent, &tm);
cbc5a1a4 524 strftime(posted, sizeof(posted), "%I:%M %p, %m/%d/%Y", &tm);
1b4a47ca 525 reply("MSMSG_HISTORY_FORMAT", history->id, history->recipient->handle->handle, posted);
cbc5a1a4 526 }
527 }
528 }
529
530 if (cc == 0)
531 reply("MSG_NONE");
532 else
533 reply("MSMSG_MEMOS_FOUND", cc);
534
535 reply("MSMSG_LIST_END");
536
537 return 1;
538}
539
d76ed9a9 540static MODCMD_FUNC(cmd_read)
541{
542 struct memo_account *ma;
543 unsigned int memoid;
82794e1b 544 int rignore = 0, brk = 0, s = 0;
d76ed9a9 545 struct memo *memo;
8cfd8013 546 struct memo *memob;
d76ed9a9 547 char posted[24];
548 struct tm tm;
549
550 if (!(ma = memoserv_get_account(user->handle_info)))
551 return 0;
552 if (!(memo = find_memo(user, cmd, ma, argv[1], &memoid)))
553 return 0;
82794e1b 554
8108185c 555 if (argc > 2) {
82794e1b 556 char *argtwo = argv[2];
557 while (*argtwo) {
558 switch (*argtwo) {
559 case '-':
560 if (s != 0)
561 brk = 1;
562 break;
563
564 case 'i':
565 if (s > 0)
566 rignore = 1;
567 break;
568
569 default: break;
570 }
571
572 if (brk == 1)
573 break;
574 else {
575 s++;
576 argtwo++;
577 }
578 }
579 }
580
d76ed9a9 581 localtime_r(&memo->sent, &tm);
582 strftime(posted, sizeof(posted), "%I:%M %p, %m/%d/%Y", &tm);
82794e1b 583
d76ed9a9 584 reply("MSMSG_MEMO_HEAD", memoid, memo->sender->handle->handle, posted);
585 send_message_type(4, user, cmd->parent->bot, "%s", memo->message);
586 memo->is_read = 1;
8cfd8013 587 memob = memo;
82794e1b 588
2a951803 589 if (ma->flags & MEMO_IGNORE_RECIEPTS)
590 rignore = 1;
591
82794e1b 592 if (memo->reciept == 1) {
593 memo->reciept = 0;
594 reply("MSMSG_MEMO_RECIEPT", rignore ? "ignoring" : "sending");
595 if (rignore == 0) {
596 struct memo_account *ma;
597 struct memo_account *sender;
598 char content[MAXLEN];
599
600 ma = memoserv_get_account(user->handle_info);
601 sender = memoserv_get_account(memo->sender->handle);
602
603 sprintf(content, "%s has read your memo dated %s.", ma->handle->handle, posted);
604
f2e592d3 605 memo = add_memo(now, sender, ma, content, 1);
8cfd8013 606 reply("MSMSG_MEMO_SENT", memob->sender->handle->handle, memo_id);
82794e1b 607
608 if (sender->flags & MEMO_NOTIFY_NEW) {
609 struct userNode *other;
610
611 for (other = sender->handle->users; other; other = other->next_authed)
fc4a50fc 612 send_message_type((ma->flags & MEMO_USE_PRIVMSG)? MSG_TYPE_PRIVMSG : MSG_TYPE_NOTICE, other, cmd->parent->bot, "MSMSG_NEW_MESSAGE", ma->handle->handle);
82794e1b 613 }
614
615
616 }
617 }
d76ed9a9 618 return 1;
619}
620
621static MODCMD_FUNC(cmd_delete)
622{
623 struct memo_account *ma;
624 struct memo *memo;
625 unsigned int memoid;
626
68cb9c2c 627 MEMOSERV_MIN_PARAMS(2);
628
d76ed9a9 629 if (!(ma = memoserv_get_account(user->handle_info)))
630 return 0;
631 if (!irccasecmp(argv[1], "*") || !irccasecmp(argv[1], "all")) {
632 if ((argc < 3) || irccasecmp(argv[2], "confirm")) {
633 reply("MSMSG_USE_CONFIRM");
634 return 0;
635 }
636 while (ma->recvd.used)
637 delete_memo(ma->recvd.list[0]);
638 reply("MSMSG_DELETED_ALL");
639 return 1;
640 }
641
642 if (!(memo = find_memo(user, cmd, ma, argv[1], &memoid)))
643 return 0;
644 delete_memo(memo);
645 reply("MSMSG_MEMO_DELETED", memoid);
646 return 1;
647}
648
f2e592d3 649static MODCMD_FUNC(cmd_cancel)
650{
651 unsigned long id;
652 unsigned int ii;
653 dict_iterator_t it;
654 struct memo *memo;
655 struct memo_account *ma;
656
68cb9c2c 657 MEMOSERV_MIN_PARAMS(2);
658
f2e592d3 659 if (isdigit(argv[1][0])) {
660 id = strtoul(argv[1], NULL, 0);
661 } else {
662 reply("MSMSG_MEMO_CANCEL_NUMBER");
663 return 0;
664 }
665
666 for (it = dict_first(memos); it; it = iter_next(it)) {
667 ma = iter_data(it);
668 for (ii = 0; ii < ma->recvd.used; ++ii) {
669 memo = ma->recvd.list[ii];
670
671 if (id == memo->id) {
672 if (!strcasecmp(memo->sender->handle->handle, user->handle_info->handle)) {
673 if (memo->is_read) {
674 reply("MSMSG_MEMO_READ", id);
675 return 0;
676 } else {
677 delete_memo(memo);
678 reply("MSMSG_MEMO_DELETED", id);
679 return 1;
680 }
681 } else {
682 reply("MSMSG_MEMO_DONT_OWN", id);
683 return 0;
684 }
685 }
686 }
687 }
688
689 reply("MSMSG_MEMO_CANT_LOCATE", id);
690 return 0;
691}
692
d76ed9a9 693static MODCMD_FUNC(cmd_expire)
694{
695 unsigned long old_expired = memosExpired;
696 do_expire();
697 reply("MSMSG_MESSAGES_EXPIRED", memosExpired - old_expired);
698 return 1;
699}
700
701static MODCMD_FUNC(cmd_expiry)
702{
703 char interval[INTERVALLEN];
704
705 if (!memoserv_conf.message_expiry) {
706 reply("MSMSG_EXPIRY_OFF");
707 return 1;
708 }
709
710 intervalString(interval, memoserv_conf.message_expiry, user->handle_info);
711 reply("MSMSG_EXPIRY", interval, memoserv_conf.message_expiry);
712 return 1;
713}
714
e3e5ba49 715
716static void
c092fcad 717set_list(struct svccmd *cmd, struct userNode *user, struct handle_info *hi, int override)
e3e5ba49 718{
719 option_func_t *opt;
d9abe201 720 unsigned int i;
fc4a50fc 721 char *set_display[] = {"AUTHNOTIFY", "NEWNOTIFY", "PRIVMSG", "PRIVATE", "LIMIT",
2a951803 722 "IGNORERECIEPTS", "SENDRECIEPTS"};
e3e5ba49 723
c092fcad 724 reply("MSMSG_SET_OPTIONS");
725 reply("MSMSG_BAR");
e3e5ba49 726
727 /* Do this so options are presented in a consistent order. */
d9abe201 728 for (i = 0; i < ArrayLength(set_display); ++i)
729 if ((opt = dict_find(memoserv_opt_dict, set_display[i], NULL)))
c092fcad 730 opt(cmd, user, hi, override, 0, NULL);
731 reply("MSMSG_SET_OPTIONS_END");
e3e5ba49 732}
733
734static MODCMD_FUNC(cmd_set)
735{
736 struct handle_info *hi;
737 option_func_t *opt;
738
739 hi = user->handle_info;
d9abe201 740 if (argc < 2) {
c092fcad 741 set_list(cmd, user, hi, 0);
e3e5ba49 742 return 1;
743 }
744
d9abe201 745 if (!(opt = dict_find(memoserv_opt_dict, argv[1], NULL))) {
e3e5ba49 746 reply("MSMSG_INVALID_OPTION", argv[1]);
747 return 0;
748 }
d9abe201 749
c092fcad 750 return opt(cmd, user, hi, 0, argc-1, argv+1);
e3e5ba49 751}
752
753static MODCMD_FUNC(cmd_oset)
754{
755 struct handle_info *hi;
756 option_func_t *opt;
757
68cb9c2c 758 MEMOSERV_MIN_PARAMS(2);
eb43ca8c 759
c092fcad 760 if (!(hi = get_victim_oper(cmd, user, argv[1])))
d9abe201 761 return 0;
e3e5ba49 762
d9abe201 763 if (argc < 3) {
c092fcad 764 set_list(cmd, user, hi, 0);
e3e5ba49 765 return 1;
766 }
767
d9abe201 768 if (!(opt = dict_find(memoserv_opt_dict, argv[2], NULL))) {
e3e5ba49 769 reply("MSMSG_INVALID_OPTION", argv[2]);
770 return 0;
771 }
772
c092fcad 773 return opt(cmd, user, hi, 1, argc-2, argv+2);
e3e5ba49 774}
775
fc4a50fc 776static OPTION_FUNC(opt_newnotify)
d76ed9a9 777{
778 struct memo_account *ma;
779 char *choice;
780
d9abe201 781 if (!(ma = memoserv_get_account(hi)))
d76ed9a9 782 return 0;
783 if (argc > 1) {
784 choice = argv[1];
785 if (enabled_string(choice)) {
786 ma->flags |= MEMO_NOTIFY_NEW;
787 } else if (disabled_string(choice)) {
788 ma->flags &= ~MEMO_NOTIFY_NEW;
789 } else {
c092fcad 790 reply("MSMSG_INVALID_BINARY", choice);
d76ed9a9 791 return 0;
792 }
793 }
794
795 choice = (ma->flags & MEMO_NOTIFY_NEW) ? "on" : "off";
fc4a50fc 796 reply("MSMSG_SET_NEWNOTIFY", choice);
797 return 1;
798}
799
800static OPTION_FUNC(opt_privmsg)
801{
802 struct memo_account *ma;
803 char *choice;
804
805 if (!(ma = memoserv_get_account(hi)))
806 return 0;
807 if (argc > 1) {
808 choice = argv[1];
809 if (enabled_string(choice)) {
810 ma->flags |= MEMO_USE_PRIVMSG;
811 } else if (disabled_string(choice)) {
812 ma->flags &= ~MEMO_USE_PRIVMSG;
813 } else {
814 reply("MSMSG_INVALID_BINARY", choice);
815 return 0;
816 }
817 }
818 choice = (ma->flags & MEMO_USE_PRIVMSG) ? "on" : "off";
819 reply("MSMSG_SET_PRIVMSG", choice);
d76ed9a9 820 return 1;
821}
822
d9abe201 823static OPTION_FUNC(opt_authnotify)
d76ed9a9 824{
825 struct memo_account *ma;
826 char *choice;
827
d9abe201 828 if (!(ma = memoserv_get_account(hi)))
d76ed9a9 829 return 0;
830 if (argc > 1) {
831 choice = argv[1];
832 if (enabled_string(choice)) {
833 ma->flags |= MEMO_NOTIFY_LOGIN;
834 } else if (disabled_string(choice)) {
835 ma->flags &= ~MEMO_NOTIFY_LOGIN;
836 } else {
c092fcad 837 reply("MSMSG_INVALID_BINARY", choice);
d76ed9a9 838 return 0;
839 }
840 }
841
842 choice = (ma->flags & MEMO_NOTIFY_LOGIN) ? "on" : "off";
c092fcad 843 reply("MSMSG_SET_AUTHNOTIFY", choice);
d76ed9a9 844 return 1;
845}
846
2a951803 847static OPTION_FUNC(opt_ignorereciepts)
848{
849 struct memo_account *ma;
850 char *choice;
851
852 if (!(ma = memoserv_get_account(hi)))
853 return 0;
854 if (argc > 1) {
855 choice = argv[1];
856 if (enabled_string(choice)) {
857 ma->flags |= MEMO_IGNORE_RECIEPTS;
858 } else if (disabled_string(choice)) {
859 ma->flags &= ~MEMO_IGNORE_RECIEPTS;
860 } else {
c092fcad 861 reply("MSMSG_INVALID_BINARY", choice);
2a951803 862 return 0;
863 }
864 }
865
866 choice = (ma->flags & MEMO_IGNORE_RECIEPTS) ? "on" : "off";
c092fcad 867 reply("MSMSG_SET_IGNORERECIEPTS", choice);
2a951803 868 return 1;
869}
870
871static OPTION_FUNC(opt_sendreciepts)
872{
873 struct memo_account *ma;
874 char *choice;
875
876 if (!(ma = memoserv_get_account(hi)))
877 return 0;
878 if (argc > 1) {
879 choice = argv[1];
880 if (enabled_string(choice)) {
881 ma->flags |= MEMO_ALWAYS_RECIEPTS;
882 } else if (disabled_string(choice)) {
883 ma->flags &= ~MEMO_ALWAYS_RECIEPTS;
884 } else {
c092fcad 885 reply("MSMSG_INVALID_BINARY", choice);
2a951803 886 return 0;
887 }
888 }
889
890 choice = (ma->flags & MEMO_ALWAYS_RECIEPTS) ? "on" : "off";
c092fcad 891 reply("MSMSG_SET_SENDRECIEPTS", choice);
2a951803 892 return 1;
893}
894
d9abe201 895static OPTION_FUNC(opt_private)
d76ed9a9 896{
897 struct memo_account *ma;
898 char *choice;
899
d9abe201 900 if (!(ma = memoserv_get_account(hi)))
d76ed9a9 901 return 0;
902 if (argc > 1) {
903 choice = argv[1];
904 if (enabled_string(choice)) {
905 ma->flags |= MEMO_DENY_NONCHANNEL;
906 } else if (disabled_string(choice)) {
907 ma->flags &= ~MEMO_DENY_NONCHANNEL;
908 } else {
c092fcad 909 reply("MSMSG_INVALID_BINARY", choice);
d76ed9a9 910 return 0;
911 }
912 }
913
914 choice = (ma->flags & MEMO_DENY_NONCHANNEL) ? "on" : "off";
c092fcad 915 reply("MSMSG_SET_PRIVATE", choice);
d76ed9a9 916 return 1;
917}
918
d9abe201 919static OPTION_FUNC(opt_limit)
acf3c6d5 920{
921 struct memo_account *ma;
a8692672 922 unsigned int choice;
acf3c6d5 923
d9abe201 924 if (!(ma = memoserv_get_account(hi)))
acf3c6d5 925 return 0;
926 if (argc > 1) {
927 choice = atoi(argv[1]);
928 if (choice > memoserv_conf.limit)
929 choice = memoserv_conf.limit;
930
931 ma->limit = choice;
932 }
933
c092fcad 934 reply("MSMSG_SET_LIMIT", ma->limit);
acf3c6d5 935 return 1;
936}
937
d76ed9a9 938static MODCMD_FUNC(cmd_status)
939{
1b4a47ca 940 struct memo_account *ma;
941 dict_iterator_t it;
942 int mc = 0, hc = 0;
943 unsigned int ii;
944
945 for (it = dict_first(memos); it; it = iter_next(it)) {
946 ma = iter_data(it);
947 for (ii = 0; ii < ma->recvd.used; ++ii)
948 mc++;
949 }
950
951 for (it = dict_first(historys); it; it = iter_next(it)) {
952 ma = iter_data(it);
953 for (ii = 0; ii < ma->hrecvd.used; ++ii)
954 hc++;
955 }
956
957 reply("MSMSG_STATUS_HIST_TOTAL", hc);
958 reply("MSMSG_STATUS_TOTAL", mc);
d76ed9a9 959 reply("MSMSG_STATUS_EXPIRED", memosExpired);
960 reply("MSMSG_STATUS_SENT", memosSent);
961 return 1;
962}
963
964static void
965memoserv_conf_read(void)
966{
967 dict_t conf_node;
968 const char *str;
969
970 str = "modules/memoserv";
971 if (!(conf_node = conf_get_data(str, RECDB_OBJECT))) {
972 log_module(MS_LOG, LOG_ERROR, "config node `%s' is missing or has wrong type.", str);
973 return;
974 }
975
d9abe201 976 str = database_get_data(conf_node, "limit", RECDB_QSTRING);
a8370a20 977 memoserv_conf.limit = str ? atoi(str) : 50;
d9abe201 978
d76ed9a9 979 str = database_get_data(conf_node, "message_expiry", RECDB_QSTRING);
980 memoserv_conf.message_expiry = str ? ParseInterval(str) : 60*24*30;
981}
982
983static int
34a9e19a 984memoserv_user_read(const char *key, struct record_data *hir)
985{
986 char *str;
987 struct memo_account *ma;
988 struct handle_info *hi;
989
990 if (!(hi = get_handle_info(key)))
991 return 0;
992
993 ma = dict_find(memos, hi->handle, NULL);
994 if (ma)
995 return 0;
996
997
998 ma = calloc(1, sizeof(*ma));
999 if (!ma)
1000 return 0;
1001
1002 ma->handle = hi;
1003
1004 str = database_get_data(hir->d.object, KEY_FLAGS, RECDB_QSTRING);
1005 if (!str) {
1006 log_module(MS_LOG, LOG_ERROR, "Flags not present in memo %s; skipping", key);
1007 return 0;
1008 }
1009 ma->flags = strtoul(str, NULL, 0);
1010
1011 str = database_get_data(hir->d.object, KEY_LIMIT, RECDB_QSTRING);
1012 if (!str) {
1013 log_module(MS_LOG, LOG_ERROR, "Limit not present in memo %s; skipping", key);
1014 return 0;
1015 }
1016 ma->limit = strtoul(str, NULL, 0);
1017
1018 dict_insert(memos, ma->handle->handle, ma);
1b4a47ca 1019 dict_insert(historys, ma->handle->handle, ma);
34a9e19a 1020
1021 return 0;
1022}
1023
1024static int
1025memoserv_memo_read(const char *key, struct record_data *hir)
d76ed9a9 1026{
1027 char *str;
1028 struct handle_info *sender, *recipient;
d76ed9a9 1029 struct memo *memo;
f2e592d3 1030 unsigned long id;
d76ed9a9 1031 time_t sent;
1032
34a9e19a 1033 if (hir->type != RECDB_OBJECT) {
1034 log_module(MS_LOG, LOG_WARNING, "Unexpected rectype %d for %s.", hir->type, key);
1035 return 0;
1036 }
d76ed9a9 1037
34a9e19a 1038 if (!(str = database_get_data(hir->d.object, KEY_SENT, RECDB_QSTRING))) {
1039 log_module(MS_LOG, LOG_ERROR, "Date sent not present in memo %s; skipping", key);
1040 return 0;
1041 }
d76ed9a9 1042
34a9e19a 1043 sent = atoi(str);
d76ed9a9 1044
f2e592d3 1045 if (!(str = database_get_data(hir->d.object, KEY_ID, RECDB_QSTRING))) {
1046 log_module(MS_LOG, LOG_ERROR, "ID sent not present in memo %s; skipping", key);
1047 return 0;
1048 }
1049 id = strtoul(str, NULL, 0);
1050 if (id > memo_id)
1051 memo_id = id;
1052
34a9e19a 1053 if (!(str = database_get_data(hir->d.object, KEY_RECIPIENT, RECDB_QSTRING))) {
1054 log_module(MS_LOG, LOG_ERROR, "Recipient not present in memo %s; skipping", key);
1055 return 0;
1056 } else if (!(recipient = get_handle_info(str))) {
1057 log_module(MS_LOG, LOG_ERROR, "Invalid recipient %s in memo %s; skipping", str, key);
1058 return 0;
1059 }
d76ed9a9 1060
34a9e19a 1061 if (!(str = database_get_data(hir->d.object, KEY_FROM, RECDB_QSTRING))) {
1062 log_module(MS_LOG, LOG_ERROR, "Sender not present in memo %s; skipping", key);
1063 return 0;
1064 } else if (!(sender = get_handle_info(str))) {
1065 log_module(MS_LOG, LOG_ERROR, "Invalid sender %s in memo %s; skipping", str, key);
1066 return 0;
1067 }
82794e1b 1068
34a9e19a 1069 if (!(str = database_get_data(hir->d.object, KEY_MESSAGE, RECDB_QSTRING))) {
1070 log_module(MS_LOG, LOG_ERROR, "Message not present in memo %s; skipping", key);
1071 return 0;
d76ed9a9 1072 }
34a9e19a 1073
f2e592d3 1074 memo = add_memo(sent, memoserv_get_account(recipient), memoserv_get_account(sender), str, 0);
34a9e19a 1075 if ((str = database_get_data(hir->d.object, KEY_READ, RECDB_QSTRING)))
1076 memo->is_read = 1;
1077
1078 if ((str = database_get_data(hir->d.object, KEY_RECIEPT, RECDB_QSTRING)))
1079 memo->reciept = 1;
1080
f2e592d3 1081 memo->id = id;
1082
34a9e19a 1083 return 0;
1084}
1085
1b4a47ca 1086static int
1087memoserv_history_read(const char *key, struct record_data *hir)
1088{
1089 char *str;
1090 struct handle_info *sender, *recipient;
1091 struct history *history;
1092 unsigned long id;
1093 time_t sent;
1094
1095 if (hir->type != RECDB_OBJECT) {
1096 log_module(MS_LOG, LOG_WARNING, "Unexpected rectype %d for %s.", hir->type, key);
1097 return 0;
1098 }
1099
1100 if (!(str = database_get_data(hir->d.object, KEY_SENT, RECDB_QSTRING))) {
1101 log_module(MS_LOG, LOG_ERROR, "Date sent not present in history %s; skipping", key);
1102 return 0;
1103 }
1104
1105 sent = atoi(str);
1106
1107 if (!(str = database_get_data(hir->d.object, KEY_ID, RECDB_QSTRING))) {
1108 log_module(MS_LOG, LOG_ERROR, "ID sent not present in history %s; skipping", key);
1109 return 0;
1110 }
1111 id = strtoul(str, NULL, 0);
1112
1113 if (!(str = database_get_data(hir->d.object, KEY_RECIPIENT, RECDB_QSTRING))) {
1114 log_module(MS_LOG, LOG_ERROR, "Recipient not present in history %s; skipping", key);
1115 return 0;
1116 } else if (!(recipient = get_handle_info(str))) {
1117 log_module(MS_LOG, LOG_ERROR, "Invalid recipient %s in history %s; skipping", str, key);
1118 return 0;
1119 }
1120
1121 if (!(str = database_get_data(hir->d.object, KEY_FROM, RECDB_QSTRING))) {
1122 log_module(MS_LOG, LOG_ERROR, "Sender not present in history %s; skipping", key);
1123 return 0;
1124 } else if (!(sender = get_handle_info(str))) {
1125 log_module(MS_LOG, LOG_ERROR, "Invalid sender %s in history %s; skipping", str, key);
1126 return 0;
1127 }
1128
1129 history = add_history(sent, memoserv_get_account(recipient), memoserv_get_account(sender), id);
1130
1131 return 0;
1132}
1133
34a9e19a 1134static int
1135memoserv_saxdb_read(struct dict *database)
1136{
1137 struct dict *section;
1138 dict_iterator_t it;
1139
1140 if((section = database_get_data(database, KEY_MAIN_ACCOUNTS, RECDB_OBJECT)))
1141 for(it = dict_first(section); it; it = iter_next(it))
1142 memoserv_user_read(iter_key(it), iter_data(it));
1143
1144 if((section = database_get_data(database, KEY_MAIN_MEMOS, RECDB_OBJECT)))
1145 for(it = dict_first(section); it; it = iter_next(it))
1146 memoserv_memo_read(iter_key(it), iter_data(it));
1147
1b4a47ca 1148 if((section = database_get_data(database, KEY_MAIN_HISTORY, RECDB_OBJECT)))
1149 for(it = dict_first(section); it; it = iter_next(it))
1150 memoserv_history_read(iter_key(it), iter_data(it));
1151
34a9e19a 1152 return 0;
1153}
1154
1155static int
1156memoserv_write_users(struct saxdb_context *ctx, struct memo_account *ma)
1157{
1158 saxdb_start_record(ctx, ma->handle->handle, 0);
1159
1160 saxdb_write_int(ctx, KEY_FLAGS, ma->flags);
1161 saxdb_write_int(ctx, KEY_LIMIT, ma->limit);
1162
1163 saxdb_end_record(ctx);
1164 return 0;
1165}
1166
1167static int
1168memoserv_write_memos(struct saxdb_context *ctx, struct memo *memo)
1169{
fc4a50fc 1170 char str[20];
34a9e19a 1171
fc4a50fc 1172 memset(str, '\0', sizeof(str));
1173 saxdb_start_record(ctx, inttobase64(str, memo->id, sizeof(str)-1), 0);
34a9e19a 1174
1175 saxdb_write_int(ctx, KEY_SENT, memo->sent);
f2e592d3 1176 saxdb_write_int(ctx, KEY_ID, memo->id);
34a9e19a 1177 saxdb_write_string(ctx, KEY_RECIPIENT, memo->recipient->handle->handle);
1178 saxdb_write_string(ctx, KEY_FROM, memo->sender->handle->handle);
1179 saxdb_write_string(ctx, KEY_MESSAGE, memo->message);
1180
1181 if (memo->is_read)
1182 saxdb_write_int(ctx, KEY_READ, 1);
1183
1184 if (memo->reciept)
1185 saxdb_write_int(ctx, KEY_RECIEPT, 1);
1186
1187 saxdb_end_record(ctx);
d76ed9a9 1188 return 0;
1189}
1190
1b4a47ca 1191static int
1192memoserv_write_history(struct saxdb_context *ctx, struct history *history)
1193{
fc4a50fc 1194 char str[20];
1b4a47ca 1195
fc4a50fc 1196 memset(str, '\0', sizeof(str));
1197 saxdb_start_record(ctx, inttobase64(str, history->id, sizeof(str)-1), 0);
1b4a47ca 1198
1199 saxdb_write_int(ctx, KEY_SENT, history->sent);
1200 saxdb_write_int(ctx, KEY_ID, history->id);
1201 saxdb_write_string(ctx, KEY_RECIPIENT, history->recipient->handle->handle);
1202 saxdb_write_string(ctx, KEY_FROM, history->sender->handle->handle);
1203
1204 saxdb_end_record(ctx);
1205 return 0;
1206}
1207
d76ed9a9 1208static int
1209memoserv_saxdb_write(struct saxdb_context *ctx)
1210{
1211 dict_iterator_t it;
1212 struct memo_account *ma;
1213 struct memo *memo;
1b4a47ca 1214 struct history *history;
34a9e19a 1215 unsigned int ii;
d76ed9a9 1216
34a9e19a 1217 /* Accounts */
1218 saxdb_start_record(ctx, KEY_MAIN_ACCOUNTS, 1);
1219 for (it = dict_first(memos); it; it = iter_next(it)) {
1220 ma = iter_data(it);
1221 memoserv_write_users(ctx, ma);
1222 }
1223 saxdb_end_record(ctx);
1224
1b4a47ca 1225 /* Memos */
34a9e19a 1226 saxdb_start_record(ctx, KEY_MAIN_MEMOS, 1);
d76ed9a9 1227 for (it = dict_first(memos); it; it = iter_next(it)) {
1228 ma = iter_data(it);
1229 for (ii = 0; ii < ma->recvd.used; ++ii) {
1230 memo = ma->recvd.list[ii];
34a9e19a 1231 memoserv_write_memos(ctx, memo);
d76ed9a9 1232 }
1233 }
34a9e19a 1234 saxdb_end_record(ctx);
1235
1b4a47ca 1236 /* History */
1237 saxdb_start_record(ctx, KEY_MAIN_HISTORY, 1);
1238 for (it = dict_first(historys); it; it = iter_next(it)) {
1239 ma = iter_data(it);
1240 for (ii = 0; ii < ma->hrecvd.used; ++ii) {
1241 history = ma->hrecvd.list[ii];
1242 memoserv_write_history(ctx, history);
1243 }
1244 }
1245 saxdb_end_record(ctx);
1246
d76ed9a9 1247 return 0;
1248}
1249
1250static void
1251memoserv_cleanup(void)
1252{
1253 dict_delete(memos);
1b4a47ca 1254 dict_delete(historys);
d76ed9a9 1255}
1256
1257static void
1258memoserv_check_messages(struct userNode *user, UNUSED_ARG(struct handle_info *old_handle))
1259{
1260 unsigned int ii, unseen;
1261 struct memo_account *ma;
1262 struct memo *memo;
1263
7637f48f 1264 if (!user->uplink->burst) {
1265 if (!(ma = memoserv_get_account(user->handle_info))
1266 || !(ma->flags & MEMO_NOTIFY_LOGIN))
1267 return;
1268 for (ii = unseen = 0; ii < ma->recvd.used; ++ii) {
1269 memo = ma->recvd.list[ii];
1270 if (!memo->is_read)
1271 unseen++;
1272 }
68cb9c2c 1273 if (ma->recvd.used && memoserv)
fc4a50fc 1274 if(unseen) send_message_type((ma->flags & MEMO_USE_PRIVMSG)? 1 : 0, user, memoserv, "MSMSG_MEMOS_INBOX", unseen, ma->recvd.used - unseen);
d76ed9a9 1275 }
d76ed9a9 1276}
1277
1278static void
1279memoserv_rename_account(struct handle_info *hi, const char *old_handle)
1280{
1281 struct memo_account *ma;
1282 if (!(ma = dict_find(memos, old_handle, NULL)))
1283 return;
1284 dict_remove2(memos, old_handle, 1);
1285 dict_insert(memos, hi->handle, ma);
1b4a47ca 1286
1287 dict_remove2(historys, old_handle, 1);
1288 dict_insert(historys, hi->handle, ma);
d76ed9a9 1289}
1290
1291static void
1292memoserv_unreg_account(UNUSED_ARG(struct userNode *user), struct handle_info *handle)
1293{
1294 dict_remove(memos, handle->handle);
1b4a47ca 1295 dict_remove(historys, handle->handle);
d76ed9a9 1296}
1297
1298int
1299memoserv_init(void)
1300{
1301 MS_LOG = log_register_type("MemoServ", "file:memoserv.log");
1302 memos = dict_new();
1b4a47ca 1303 historys = dict_new();
d76ed9a9 1304 dict_set_free_data(memos, delete_memo_account);
1305 reg_auth_func(memoserv_check_messages);
1306 reg_handle_rename_func(memoserv_rename_account);
1307 reg_unreg_func(memoserv_unreg_account);
1308 conf_register_reload(memoserv_conf_read);
1309 reg_exit_func(memoserv_cleanup);
1310 saxdb_register("MemoServ", memoserv_saxdb_read, memoserv_saxdb_write);
1311
1312 memoserv_module = module_register("MemoServ", MS_LOG, "mod-memoserv.help", NULL);
cbc5a1a4 1313 modcmd_register(memoserv_module, "send", cmd_send, 3, MODCMD_REQUIRE_AUTHED, NULL);
1314 modcmd_register(memoserv_module, "list", cmd_list, 1, MODCMD_REQUIRE_AUTHED, NULL);
1315 modcmd_register(memoserv_module, "read", cmd_read, 2, MODCMD_REQUIRE_AUTHED, NULL);
1316 modcmd_register(memoserv_module, "delete", cmd_delete, 2, MODCMD_REQUIRE_AUTHED, NULL);
1317 modcmd_register(memoserv_module, "cancel", cmd_cancel, 2, MODCMD_REQUIRE_AUTHED, NULL);
1318 modcmd_register(memoserv_module, "history", cmd_history, 1, MODCMD_REQUIRE_AUTHED, NULL);
1319 modcmd_register(memoserv_module, "expire", cmd_expire, 1, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL);
1320 modcmd_register(memoserv_module, "expiry", cmd_expiry, 1, 0, NULL);
1321 modcmd_register(memoserv_module, "status", cmd_status, 1, 0, NULL);
1322 modcmd_register(memoserv_module, "set", cmd_set, 1, MODCMD_REQUIRE_AUTHED, NULL);
1323 modcmd_register(memoserv_module, "oset", cmd_oset, 1, MODCMD_REQUIRE_AUTHED, "flags", "+helping", NULL);
e3e5ba49 1324
1325 memoserv_opt_dict = dict_new();
1326 dict_insert(memoserv_opt_dict, "AUTHNOTIFY", opt_authnotify);
fc4a50fc 1327 dict_insert(memoserv_opt_dict, "NEWNOTIFY", opt_newnotify);
1328 dict_insert(memoserv_opt_dict, "PRIVMSG", opt_privmsg);
e3e5ba49 1329 dict_insert(memoserv_opt_dict, "PRIVATE", opt_private);
2a951803 1330 dict_insert(memoserv_opt_dict, "IGNORERECIEPTS", opt_ignorereciepts);
1331 dict_insert(memoserv_opt_dict, "SENDRECIEPTS", opt_sendreciepts);
d9abe201 1332 dict_insert(memoserv_opt_dict, "LIMIT", opt_limit);
e3e5ba49 1333
d76ed9a9 1334 message_register_table(msgtab);
1335
1336 if (memoserv_conf.message_expiry)
1337 timeq_add(now + memoserv_conf.message_expiry, expire_memos, NULL);
e3e5ba49 1338
d76ed9a9 1339 return 1;
1340}
1341
1342int
1343memoserv_finalize(void) {
e3e5ba49 1344 struct chanNode *chan;
1345 unsigned int i;
d76ed9a9 1346 dict_t conf_node;
1347 const char *str;
1348
1349 str = "modules/memoserv";
1350 if (!(conf_node = conf_get_data(str, RECDB_OBJECT))) {
1351 log_module(MS_LOG, LOG_ERROR, "config node `%s' is missing or has wrong type.", str);
1352 return 0;
1353 }
1354
1355 str = database_get_data(conf_node, "bot", RECDB_QSTRING);
68cb9c2c 1356 if (str) {
1357 memoserv = memoserv_conf.bot;
14f0e274 1358 const char *modes = conf_get_data("modules/memoserv/modes", RECDB_QSTRING);
1359 memoserv = AddService(str, modes ? modes : NULL, "User-User Memorandum Services", NULL);
1360
6a64b9ce 1361 } else {
c092fcad 1362 log_module(MS_LOG, LOG_ERROR, "database_get_data for memoserv_conf.bot failed!");
6a64b9ce 1363 exit(1);
1364 }
e3e5ba49 1365
68cb9c2c 1366 if (autojoin_channels && memoserv) {
e3e5ba49 1367 for (i = 0; i < autojoin_channels->used; i++) {
1368 chan = AddChannel(autojoin_channels->list[i], now, "+nt", NULL, NULL);
68cb9c2c 1369 AddChannelUser(memoserv, chan)->modes |= MODE_CHANOP;
e3e5ba49 1370 }
1371 }
1372
d76ed9a9 1373 return 1;
1374}