]>
jfr.im git - irc/evilnet/x3.git/blob - src/mod-memoserv.c
1 /* mod-memoserv.c - MemoServ module for srvx
2 * Copyright 2003-2004 Martijn Smit and srvx Development Team
3 * Copyright 2005-2006 X3 Development Team
5 * This file is part of x3.
7 * x3 is free software; you can redistribute it and/or modify
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.
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.
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.
23 * /msg opserv bind nickserv * *memoserv.*
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
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.*
38 * /msg opserv bind memoserv set *modcmd.joiner
49 #define KEY_MAIN_ACCOUNTS "accounts"
50 #define KEY_FLAGS "flags"
51 #define KEY_LIMIT "limit"
53 #define KEY_MAIN_HISTORY "history"
54 #define KEY_MAIN_MEMOS "memos"
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"
60 #define KEY_RECIEPT "reciept"
64 static const struct message_entry msgtab
[] = {
65 { "MSMSG_CANNOT_SEND", "You cannot send to account $b%s$b." },
66 { "MSMSG_UNKNOWN_SEND_FLAG", "Unreccognised send flag '%c', message not sent." },
67 { "MSMSG_MEMO_SENT", "Message sent to $b%s$b (ID# %d)." },
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 $b%s$b %s" },
73 { "MSMSG_HISTORY_HEADER", "$bID$b $bTo$b $bTime Sent$b" },
74 { "MSMSG_HISTORY_FORMAT", "%-2u %s %s" },
75 { "MSMSG_MEMO_HEAD", "Memo %u From $b%s$b, received on %s:" },
76 { "MSMSG_MEMO_RECIEPT", "$bRead Reciept$b requested, %s." },
77 { "MSMSG_BAD_MESSAGE_ID", "$b%s$b is not a valid message ID (it should be a number between 0 and %u)." },
78 { "MSMSG_NO_SUCH_MEMO", "You have no memo with that ID." },
79 { "MSMSG_MEMO_DELETED", "Memo $b%d$b deleted." },
80 { "MSMSG_MEMO_CANCEL_NUMBER", "You must specify a number id" },
81 { "MSMSG_MEMO_DONT_OWN", "You did not send memo# %d" },
82 { "MSMSG_MEMO_READ", "Memo# %d has already been read, you cannot cancel it." },
83 { "MSMSG_MEMO_CANT_LOCATE", "Could not locate memo# %d" },
84 { "MSMSG_EXPIRY_OFF", "I am currently not expiring messages. (turned off)" },
85 { "MSMSG_EXPIRY", "Messages will be expired when they are %s old (%d seconds)." },
86 { "MSMSG_MESSAGES_EXPIRED", "$b%lu$b message(s) expired." },
87 { "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." },
88 { "MSMSG_NEW_MESSAGE", "You have a new message from $b%s$b. /msg $S LIST" },
89 { "MSMSG_FULL_INBOX", "$b%s$b cannot recieve anymore memos as their inbox is full" },
90 { "MSMSG_DELETED_ALL", "Deleted all of your messages." },
91 { "MSMSG_USE_CONFIRM", "Please use /msg $S DELETE * $bCONFIRM$b to delete $uall$u of your messages." },
93 { "MSMSG_STATUS_HIST_TOTAL", "I have $b%u$b history entries in my database." },
94 { "MSMSG_STATUS_TOTAL", "I have $b%u$b memos in my database." },
95 { "MSMSG_STATUS_EXPIRED", "$b%ld$b memos expired during the time I am awake." },
96 { "MSMSG_STATUS_SENT", "$b%ld$b memos have been sent." },
98 { "MSMSG_INVALID_OPTION", "$b%s$b is not a valid option." },
99 { "MSMSG_INVALID_BINARY", "$b%s$b is an invalid binary value." },
100 { "MSMSG_SET_NOTIFY", "$bNotify: $b %s" },
101 { "MSMSG_SET_AUTHNOTIFY", "$bAuthNotify: $b %s" },
102 { "MSMSG_SET_PRIVATE", "$bPrivate: $b %s" },
103 { "MSMSG_SET_IGNORERECIEPTS", "$bIgnoreReciepts:$b %s" },
104 { "MSMSG_SET_SENDRECIEPTS", "$bSendReciepts: $b %s" },
105 { "MSMSG_SET_LIMIT", "$bLimit: $b %d" },
106 { "MSMSG_SET_OPTIONS", "$bMessaging Options$b" },
107 { "MSMSG_SET_OPTIONS_END", "-------------End of Options-------------" },
109 { "MSMSG_LIST_END", "--------------End of Memos--------------" },
110 { "MSMSG_BAR", "----------------------------------------"},
112 { "MSMSG_DEFCON_NO_NEW_MEMOS", "You cannot send new memos at this time, please try again soon." },
118 struct memo_account
*recipient
;
119 struct memo_account
*sender
;
123 unsigned int is_read
: 1;
124 unsigned int reciept
: 1;
128 struct memo_account
*recipient
;
129 struct memo_account
*sender
;
134 struct userNode
*memoserv
;
136 #define MEMOSERV_FUNC(NAME) MODCMD_FUNC(NAME)
137 #define MEMOSERV_SYNTAX() svccmd_send_help_brief(user, memoserv, cmd)
138 #define MEMOSERV_MIN_PARAMS(N) if(argc < (N)) { \
139 reply("MSG_MISSING_PARAMS", argv[0]); \
143 DECLARE_LIST(memoList
, struct memo
*);
144 DEFINE_LIST(memoList
, struct memo
*);
145 DECLARE_LIST(historyList
, struct history
*);
146 DEFINE_LIST(historyList
, struct history
*);
148 /* memo_account.flags fields */
149 #define MEMO_NOTIFY_NEW 0x00000001
150 #define MEMO_NOTIFY_LOGIN 0x00000002
151 #define MEMO_DENY_NONCHANNEL 0x00000004
152 #define MEMO_IGNORE_RECIEPTS 0x00000008
153 #define MEMO_ALWAYS_RECIEPTS 0x00000010
155 struct memo_account
{
156 struct handle_info
*handle
;
159 struct memoList sent
;
160 struct memoList recvd
;
161 struct historyList hsent
;
162 struct historyList hrecvd
;
166 struct userNode
*bot
;
171 #define MEMOSERV_FUNC(NAME) MODCMD_FUNC(NAME)
172 #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[])
173 typedef OPTION_FUNC(option_func_t
);
175 unsigned long memo_id
;
177 extern struct string_list
*autojoin_channels
;
178 const char *memoserv_module_deps
[] = { NULL
};
179 static struct module *memoserv_module
;
180 static struct log_type
*MS_LOG
;
181 static unsigned long memosSent
, memosExpired
;
182 static struct dict
*memos
; /* memo_account->handle->handle -> memo_account */
183 static struct dict
*historys
;
184 static dict_t memoserv_opt_dict
; /* contains option_func_t* */
186 static struct memo_account
*
187 memoserv_get_account(struct handle_info
*hi
)
189 struct memo_account
*ma
;
192 ma
= dict_find(memos
, hi
->handle
, NULL
);
195 ma
= calloc(1, sizeof(*ma
));
199 ma
->flags
= MEMO_NOTIFY_NEW
| MEMO_NOTIFY_LOGIN
;
200 ma
->limit
= memoserv_conf
.limit
;
201 dict_insert(memos
, ma
->handle
->handle
, ma
);
202 dict_insert(historys
, ma
->handle
->handle
, ma
);
207 delete_memo(struct memo
*memo
)
209 memoList_remove(&memo
->recipient
->recvd
, memo
);
210 memoList_remove(&memo
->sender
->sent
, memo
);
216 delete_history(struct history
*history
)
218 historyList_remove(&history
->recipient
->hrecvd
, history
);
219 historyList_remove(&history
->sender
->hsent
, history
);
224 delete_memo_account(void *data
)
226 struct memo_account
*ma
= data
;
228 while (ma
->recvd
.used
)
229 delete_memo(ma
->recvd
.list
[0]);
230 while (ma
->sent
.used
)
231 delete_memo(ma
->sent
.list
[0]);
232 memoList_clean(&ma
->recvd
);
233 memoList_clean(&ma
->sent
);
235 while (ma
->hrecvd
.used
)
236 delete_history(ma
->hrecvd
.list
[0]);
237 while (ma
->hsent
.used
)
238 delete_history(ma
->hsent
.list
[0]);
239 historyList_clean(&ma
->hrecvd
);
240 historyList_clean(&ma
->hsent
);
248 for (it
= dict_first(memos
); it
; it
= iter_next(it
)) {
249 struct memo_account
*acct
= iter_data(it
);
251 for (ii
= 0; ii
< acct
->sent
.used
; ++ii
) {
252 struct memo
*memo
= acct
->sent
.list
[ii
];
253 if ((now
- memo
->sent
) > memoserv_conf
.message_expiry
) {
261 for (it
= dict_first(historys
); it
; it
= iter_next(it
)) {
262 struct memo_account
*acct
= iter_data(it
);
264 for (ii
= 0; ii
< acct
->hsent
.used
; ++ii
) {
265 struct history
*history
= acct
->hsent
.list
[ii
];
266 if ((now
- history
->sent
) > memoserv_conf
.message_expiry
) {
267 delete_history(history
);
276 expire_memos(UNUSED_ARG(void *data
))
278 if (memoserv_conf
.message_expiry
) {
280 timeq_add(now
+ memoserv_conf
.message_expiry
, expire_memos
, NULL
);
284 static struct history
*
285 add_history(time_t sent
, struct memo_account
*recipient
, struct memo_account
*sender
, unsigned long id
)
287 struct history
*history
;
289 history
= calloc(1, sizeof(*history
));
294 history
->recipient
= recipient
;
295 historyList_append(&recipient
->hrecvd
, history
);
296 history
->sender
= sender
;
297 historyList_append(&sender
->hsent
, history
);
298 history
->sent
= sent
;
305 add_memo(time_t sent
, struct memo_account
*recipient
, struct memo_account
*sender
, char *message
, int nfrom_read
)
308 struct history
*history
;
310 memo
= calloc(1, sizeof(*memo
));
319 memo
->recipient
= recipient
;
320 memoList_append(&recipient
->recvd
, memo
);
321 memo
->sender
= sender
;
322 memoList_append(&sender
->sent
, memo
);
324 memo
->message
= strdup(message
);
328 history
= add_history(sent
, recipient
, sender
, memo
->id
);
334 memoserv_can_send(struct userNode
*bot
, struct userNode
*user
, struct memo_account
*acct
)
336 extern struct userData
*_GetChannelUser(struct chanData
*channel
, struct handle_info
*handle
, int override
, int allow_suspended
);
337 struct userData
*dest
;
338 unsigned int i
= 0, match
= 0;
340 if (!user
->handle_info
)
343 /* Sanity checks here because if the user doesnt have a limit set
344 the limit comes out at like 21233242 if you try and use it. */
345 if (acct
->limit
> memoserv_conf
.limit
)
346 acct
->limit
= memoserv_conf
.limit
;
348 if (acct
->recvd
.used
> acct
->limit
) {
349 send_message(user
, bot
, "MSMSG_FULL_INBOX", acct
->handle
->handle
);
350 send_message(user
, bot
, "MSMSG_CANNOT_SEND", acct
->handle
->handle
);
354 if (acct
->handle
->ignores
->used
) {
355 for (i
=0; i
< acct
->handle
->ignores
->used
; i
++) {
356 if (user_matches_glob(user
, acct
->handle
->ignores
->list
[i
], MATCH_USENICK
)) {
363 send_message(user
, bot
, "MSMSG_CANNOT_SEND", acct
->handle
->handle
);
368 if (!(acct
->flags
& MEMO_DENY_NONCHANNEL
))
371 for (dest
= acct
->handle
->channels
; dest
; dest
= dest
->u_next
)
372 if (_GetChannelUser(dest
->channel
, user
->handle_info
, 1, 0))
375 send_message(user
, bot
, "MSMSG_CANNOT_SEND", acct
->handle
->handle
);
379 static struct memo
*find_memo(struct userNode
*user
, struct svccmd
*cmd
, struct memo_account
*ma
, const char *msgid
, unsigned int *id
)
382 if (!isdigit(msgid
[0])) {
384 reply("MSMSG_BAD_MESSAGE_ID", msgid
, ma
->recvd
.used
- 1);
386 reply("MSMSG_NO_MESSAGES");
389 memoid
= atoi(msgid
);
390 if (memoid
>= ma
->recvd
.used
) {
391 reply("MSMSG_NO_SUCH_MEMO");
394 return ma
->recvd
.list
[*id
= memoid
];
397 static MODCMD_FUNC(cmd_send
)
400 int reciept
= 0, inc
= 2;
401 struct handle_info
*hi
;
402 struct memo_account
*ma
, *sender
;
405 MEMOSERV_MIN_PARAMS(3);
407 if (checkDefCon(DEFCON_NO_NEW_MEMOS
) && !IsOper(user
)) {
408 reply("MSMSG_DEFCON_NO_NEW_MEMOS");
412 if (!(hi
= modcmd_get_handle_info(user
, argv
[1])))
415 if (!(sender
= memoserv_get_account(user
->handle_info
))
416 || !(ma
= memoserv_get_account(hi
))) {
417 reply("MSG_INTERNAL_FAILURE");
421 if (!(memoserv_can_send(cmd
->parent
->bot
, user
, ma
)))
424 inc
= 2; /* Start of message on 3rd ([2]) word */
425 if(argv
[2][0] == '-' && argv
[2][1] != '-') { /* first word is flags ('-r')*/
426 char *flags
= argv
[2];
427 inc
++; /* Start of message is now 1 word later */
428 for(flags
++;*flags
;flags
++) {
435 /* Unknown mode. Give an error */
436 reply("MSMSG_UNKNOWN_SEND_FLAG", *flags
);
442 inc
= 2; /* Start of message is word 2 */
444 message
= unsplit_string(argv
+ inc
, argc
- inc
, NULL
);
445 memo
= add_memo(now
, ma
, sender
, message
, 1);
446 if ((reciept
== 1) || (ma
->flags
& MEMO_ALWAYS_RECIEPTS
))
449 if (ma
->flags
& MEMO_NOTIFY_NEW
) {
450 struct userNode
*other
;
452 for (other
= ma
->handle
->users
; other
; other
= other
->next_authed
)
453 send_message(other
, cmd
->parent
->bot
, "MSMSG_NEW_MESSAGE", user
->nick
);
456 reply("MSMSG_MEMO_SENT", ma
->handle
->handle
, memo_id
);
460 static MODCMD_FUNC(cmd_list
)
462 struct memo_account
*ma
;
468 if (!(ma
= memoserv_get_account(user
->handle_info
)))
471 reply("MSMSG_LIST_HEAD");
473 if(user
->handle_info
&& user
->handle_info
->userlist_style
!= HI_STYLE_CLEAN
)
476 for (ii
= 0; (ii
< ma
->recvd
.used
) && (ii
< 15); ++ii
) {
477 memo
= ma
->recvd
.list
[ii
];
478 localtime_r(&memo
->sent
, &tm
);
479 strftime(posted
, sizeof(posted
), "%I:%M %p, %m/%d/%Y", &tm
);
480 reply("MSMSG_LIST_FORMAT", ii
, memo
->sender
->handle
->handle
, memo
->reciept
? "(r)" : "", posted
);
485 reply("MSMSG_CLEAN_INBOX", ii
);
487 reply("MSMSG_MEMOS_FOUND", ii
);
489 reply("MSMSG_LIST_END");
494 static MODCMD_FUNC(cmd_history
)
496 struct memo_account
*ma
;
497 struct history
*history
;
504 if (!(ma
= memoserv_get_account(user
->handle_info
)))
507 reply("MSMSG_HISTORY_HEADER");
509 if(user
->handle_info
&& user
->handle_info
->userlist_style
!= HI_STYLE_CLEAN
)
512 for (it
= dict_first(historys
); it
; it
= iter_next(it
)) {
514 for (ii
= 0; ii
< ma
->hrecvd
.used
; ++ii
) {
515 history
= ma
->hrecvd
.list
[ii
];
516 if (!strcasecmp(history
->sender
->handle
->handle
, user
->handle_info
->handle
)) {
518 localtime_r(&history
->sent
, &tm
);
519 strftime(posted
, sizeof(posted
), "%I:%M %p, %m/%d/%Y", &tm
);
520 reply("MSMSG_HISTORY_FORMAT", history
->id
, history
->recipient
->handle
->handle
, posted
);
528 reply("MSMSG_MEMOS_FOUND", cc
);
530 reply("MSMSG_LIST_END");
535 static MODCMD_FUNC(cmd_read
)
537 struct memo_account
*ma
;
539 int rignore
= 0, brk
= 0, s
= 0;
545 if (!(ma
= memoserv_get_account(user
->handle_info
)))
547 if (!(memo
= find_memo(user
, cmd
, ma
, argv
[1], &memoid
)))
551 char *argtwo
= argv
[2];
576 localtime_r(&memo
->sent
, &tm
);
577 strftime(posted
, sizeof(posted
), "%I:%M %p, %m/%d/%Y", &tm
);
579 reply("MSMSG_MEMO_HEAD", memoid
, memo
->sender
->handle
->handle
, posted
);
580 send_message_type(4, user
, cmd
->parent
->bot
, "%s", memo
->message
);
584 if (ma
->flags
& MEMO_IGNORE_RECIEPTS
)
587 if (memo
->reciept
== 1) {
589 reply("MSMSG_MEMO_RECIEPT", rignore
? "ignoring" : "sending");
591 struct memo_account
*ma
;
592 struct memo_account
*sender
;
593 char content
[MAXLEN
];
595 ma
= memoserv_get_account(user
->handle_info
);
596 sender
= memoserv_get_account(memo
->sender
->handle
);
598 sprintf(content
, "%s has read your memo dated %s.", ma
->handle
->handle
, posted
);
600 memo
= add_memo(now
, sender
, ma
, content
, 1);
601 reply("MSMSG_MEMO_SENT", memob
->sender
->handle
->handle
, memo_id
);
603 if (sender
->flags
& MEMO_NOTIFY_NEW
) {
604 struct userNode
*other
;
606 for (other
= sender
->handle
->users
; other
; other
= other
->next_authed
)
607 send_message(other
, cmd
->parent
->bot
, "MSMSG_NEW_MESSAGE", ma
->handle
->handle
);
616 static MODCMD_FUNC(cmd_delete
)
618 struct memo_account
*ma
;
622 MEMOSERV_MIN_PARAMS(2);
624 if (!(ma
= memoserv_get_account(user
->handle_info
)))
626 if (!irccasecmp(argv
[1], "*") || !irccasecmp(argv
[1], "all")) {
627 if ((argc
< 3) || irccasecmp(argv
[2], "confirm")) {
628 reply("MSMSG_USE_CONFIRM");
631 while (ma
->recvd
.used
)
632 delete_memo(ma
->recvd
.list
[0]);
633 reply("MSMSG_DELETED_ALL");
637 if (!(memo
= find_memo(user
, cmd
, ma
, argv
[1], &memoid
)))
640 reply("MSMSG_MEMO_DELETED", memoid
);
644 static MODCMD_FUNC(cmd_cancel
)
650 struct memo_account
*ma
;
652 MEMOSERV_MIN_PARAMS(2);
654 if (isdigit(argv
[1][0])) {
655 id
= strtoul(argv
[1], NULL
, 0);
657 reply("MSMSG_MEMO_CANCEL_NUMBER");
661 for (it
= dict_first(memos
); it
; it
= iter_next(it
)) {
663 for (ii
= 0; ii
< ma
->recvd
.used
; ++ii
) {
664 memo
= ma
->recvd
.list
[ii
];
666 if (id
== memo
->id
) {
667 if (!strcasecmp(memo
->sender
->handle
->handle
, user
->handle_info
->handle
)) {
669 reply("MSMSG_MEMO_READ", id
);
673 reply("MSMSG_MEMO_DELETED", id
);
677 reply("MSMSG_MEMO_DONT_OWN", id
);
684 reply("MSMSG_MEMO_CANT_LOCATE", id
);
688 static MODCMD_FUNC(cmd_expire
)
690 unsigned long old_expired
= memosExpired
;
692 reply("MSMSG_MESSAGES_EXPIRED", memosExpired
- old_expired
);
696 static MODCMD_FUNC(cmd_expiry
)
698 char interval
[INTERVALLEN
];
700 if (!memoserv_conf
.message_expiry
) {
701 reply("MSMSG_EXPIRY_OFF");
705 intervalString(interval
, memoserv_conf
.message_expiry
, user
->handle_info
);
706 reply("MSMSG_EXPIRY", interval
, memoserv_conf
.message_expiry
);
712 set_list(struct svccmd
*cmd
, struct userNode
*user
, struct handle_info
*hi
, int override
)
716 char *set_display
[] = {"AUTHNOTIFY", "NOTIFY", "PRIVATE", "LIMIT",
717 "IGNORERECIEPTS", "SENDRECIEPTS"};
719 reply("MSMSG_SET_OPTIONS");
722 /* Do this so options are presented in a consistent order. */
723 for (i
= 0; i
< ArrayLength(set_display
); ++i
)
724 if ((opt
= dict_find(memoserv_opt_dict
, set_display
[i
], NULL
)))
725 opt(cmd
, user
, hi
, override
, 0, NULL
);
726 reply("MSMSG_SET_OPTIONS_END");
729 static MODCMD_FUNC(cmd_set
)
731 struct handle_info
*hi
;
734 hi
= user
->handle_info
;
736 set_list(cmd
, user
, hi
, 0);
740 if (!(opt
= dict_find(memoserv_opt_dict
, argv
[1], NULL
))) {
741 reply("MSMSG_INVALID_OPTION", argv
[1]);
745 return opt(cmd
, user
, hi
, 0, argc
-1, argv
+1);
748 static MODCMD_FUNC(cmd_oset
)
750 struct handle_info
*hi
;
753 MEMOSERV_MIN_PARAMS(2);
755 if (!(hi
= get_victim_oper(cmd
, user
, argv
[1])))
759 set_list(cmd
, user
, hi
, 0);
763 if (!(opt
= dict_find(memoserv_opt_dict
, argv
[2], NULL
))) {
764 reply("MSMSG_INVALID_OPTION", argv
[2]);
768 return opt(cmd
, user
, hi
, 1, argc
-2, argv
+2);
771 static OPTION_FUNC(opt_notify
)
773 struct memo_account
*ma
;
776 if (!(ma
= memoserv_get_account(hi
)))
780 if (enabled_string(choice
)) {
781 ma
->flags
|= MEMO_NOTIFY_NEW
;
782 } else if (disabled_string(choice
)) {
783 ma
->flags
&= ~MEMO_NOTIFY_NEW
;
785 reply("MSMSG_INVALID_BINARY", choice
);
790 choice
= (ma
->flags
& MEMO_NOTIFY_NEW
) ? "on" : "off";
791 reply("MSMSG_SET_NOTIFY", choice
);
795 static OPTION_FUNC(opt_authnotify
)
797 struct memo_account
*ma
;
800 if (!(ma
= memoserv_get_account(hi
)))
804 if (enabled_string(choice
)) {
805 ma
->flags
|= MEMO_NOTIFY_LOGIN
;
806 } else if (disabled_string(choice
)) {
807 ma
->flags
&= ~MEMO_NOTIFY_LOGIN
;
809 reply("MSMSG_INVALID_BINARY", choice
);
814 choice
= (ma
->flags
& MEMO_NOTIFY_LOGIN
) ? "on" : "off";
815 reply("MSMSG_SET_AUTHNOTIFY", choice
);
819 static OPTION_FUNC(opt_ignorereciepts
)
821 struct memo_account
*ma
;
824 if (!(ma
= memoserv_get_account(hi
)))
828 if (enabled_string(choice
)) {
829 ma
->flags
|= MEMO_IGNORE_RECIEPTS
;
830 } else if (disabled_string(choice
)) {
831 ma
->flags
&= ~MEMO_IGNORE_RECIEPTS
;
833 reply("MSMSG_INVALID_BINARY", choice
);
838 choice
= (ma
->flags
& MEMO_IGNORE_RECIEPTS
) ? "on" : "off";
839 reply("MSMSG_SET_IGNORERECIEPTS", choice
);
843 static OPTION_FUNC(opt_sendreciepts
)
845 struct memo_account
*ma
;
848 if (!(ma
= memoserv_get_account(hi
)))
852 if (enabled_string(choice
)) {
853 ma
->flags
|= MEMO_ALWAYS_RECIEPTS
;
854 } else if (disabled_string(choice
)) {
855 ma
->flags
&= ~MEMO_ALWAYS_RECIEPTS
;
857 reply("MSMSG_INVALID_BINARY", choice
);
862 choice
= (ma
->flags
& MEMO_ALWAYS_RECIEPTS
) ? "on" : "off";
863 reply("MSMSG_SET_SENDRECIEPTS", choice
);
867 static OPTION_FUNC(opt_private
)
869 struct memo_account
*ma
;
872 if (!(ma
= memoserv_get_account(hi
)))
876 if (enabled_string(choice
)) {
877 ma
->flags
|= MEMO_DENY_NONCHANNEL
;
878 } else if (disabled_string(choice
)) {
879 ma
->flags
&= ~MEMO_DENY_NONCHANNEL
;
881 reply("MSMSG_INVALID_BINARY", choice
);
886 choice
= (ma
->flags
& MEMO_DENY_NONCHANNEL
) ? "on" : "off";
887 reply("MSMSG_SET_PRIVATE", choice
);
891 static OPTION_FUNC(opt_limit
)
893 struct memo_account
*ma
;
896 if (!(ma
= memoserv_get_account(hi
)))
899 choice
= atoi(argv
[1]);
900 if (choice
> memoserv_conf
.limit
)
901 choice
= memoserv_conf
.limit
;
906 reply("MSMSG_SET_LIMIT", ma
->limit
);
910 static MODCMD_FUNC(cmd_status
)
912 struct memo_account
*ma
;
917 for (it
= dict_first(memos
); it
; it
= iter_next(it
)) {
919 for (ii
= 0; ii
< ma
->recvd
.used
; ++ii
)
923 for (it
= dict_first(historys
); it
; it
= iter_next(it
)) {
925 for (ii
= 0; ii
< ma
->hrecvd
.used
; ++ii
)
929 reply("MSMSG_STATUS_HIST_TOTAL", hc
);
930 reply("MSMSG_STATUS_TOTAL", mc
);
931 reply("MSMSG_STATUS_EXPIRED", memosExpired
);
932 reply("MSMSG_STATUS_SENT", memosSent
);
937 memoserv_conf_read(void)
942 str
= "modules/memoserv";
943 if (!(conf_node
= conf_get_data(str
, RECDB_OBJECT
))) {
944 log_module(MS_LOG
, LOG_ERROR
, "config node `%s' is missing or has wrong type.", str
);
948 str
= database_get_data(conf_node
, "limit", RECDB_QSTRING
);
949 memoserv_conf
.limit
= str
? atoi(str
) : 50;
951 str
= database_get_data(conf_node
, "message_expiry", RECDB_QSTRING
);
952 memoserv_conf
.message_expiry
= str
? ParseInterval(str
) : 60*24*30;
956 memoserv_user_read(const char *key
, struct record_data
*hir
)
959 struct memo_account
*ma
;
960 struct handle_info
*hi
;
962 if (!(hi
= get_handle_info(key
)))
965 ma
= dict_find(memos
, hi
->handle
, NULL
);
970 ma
= calloc(1, sizeof(*ma
));
976 str
= database_get_data(hir
->d
.object
, KEY_FLAGS
, RECDB_QSTRING
);
978 log_module(MS_LOG
, LOG_ERROR
, "Flags not present in memo %s; skipping", key
);
981 ma
->flags
= strtoul(str
, NULL
, 0);
983 str
= database_get_data(hir
->d
.object
, KEY_LIMIT
, RECDB_QSTRING
);
985 log_module(MS_LOG
, LOG_ERROR
, "Limit not present in memo %s; skipping", key
);
988 ma
->limit
= strtoul(str
, NULL
, 0);
990 dict_insert(memos
, ma
->handle
->handle
, ma
);
991 dict_insert(historys
, ma
->handle
->handle
, ma
);
997 memoserv_memo_read(const char *key
, struct record_data
*hir
)
1000 struct handle_info
*sender
, *recipient
;
1005 if (hir
->type
!= RECDB_OBJECT
) {
1006 log_module(MS_LOG
, LOG_WARNING
, "Unexpected rectype %d for %s.", hir
->type
, key
);
1010 if (!(str
= database_get_data(hir
->d
.object
, KEY_SENT
, RECDB_QSTRING
))) {
1011 log_module(MS_LOG
, LOG_ERROR
, "Date sent not present in memo %s; skipping", key
);
1017 if (!(str
= database_get_data(hir
->d
.object
, KEY_ID
, RECDB_QSTRING
))) {
1018 log_module(MS_LOG
, LOG_ERROR
, "ID sent not present in memo %s; skipping", key
);
1021 id
= strtoul(str
, NULL
, 0);
1025 if (!(str
= database_get_data(hir
->d
.object
, KEY_RECIPIENT
, RECDB_QSTRING
))) {
1026 log_module(MS_LOG
, LOG_ERROR
, "Recipient not present in memo %s; skipping", key
);
1028 } else if (!(recipient
= get_handle_info(str
))) {
1029 log_module(MS_LOG
, LOG_ERROR
, "Invalid recipient %s in memo %s; skipping", str
, key
);
1033 if (!(str
= database_get_data(hir
->d
.object
, KEY_FROM
, RECDB_QSTRING
))) {
1034 log_module(MS_LOG
, LOG_ERROR
, "Sender not present in memo %s; skipping", key
);
1036 } else if (!(sender
= get_handle_info(str
))) {
1037 log_module(MS_LOG
, LOG_ERROR
, "Invalid sender %s in memo %s; skipping", str
, key
);
1041 if (!(str
= database_get_data(hir
->d
.object
, KEY_MESSAGE
, RECDB_QSTRING
))) {
1042 log_module(MS_LOG
, LOG_ERROR
, "Message not present in memo %s; skipping", key
);
1046 memo
= add_memo(sent
, memoserv_get_account(recipient
), memoserv_get_account(sender
), str
, 0);
1047 if ((str
= database_get_data(hir
->d
.object
, KEY_READ
, RECDB_QSTRING
)))
1050 if ((str
= database_get_data(hir
->d
.object
, KEY_RECIEPT
, RECDB_QSTRING
)))
1059 memoserv_history_read(const char *key
, struct record_data
*hir
)
1062 struct handle_info
*sender
, *recipient
;
1063 struct history
*history
;
1067 if (hir
->type
!= RECDB_OBJECT
) {
1068 log_module(MS_LOG
, LOG_WARNING
, "Unexpected rectype %d for %s.", hir
->type
, key
);
1072 if (!(str
= database_get_data(hir
->d
.object
, KEY_SENT
, RECDB_QSTRING
))) {
1073 log_module(MS_LOG
, LOG_ERROR
, "Date sent not present in history %s; skipping", key
);
1079 if (!(str
= database_get_data(hir
->d
.object
, KEY_ID
, RECDB_QSTRING
))) {
1080 log_module(MS_LOG
, LOG_ERROR
, "ID sent not present in history %s; skipping", key
);
1083 id
= strtoul(str
, NULL
, 0);
1085 if (!(str
= database_get_data(hir
->d
.object
, KEY_RECIPIENT
, RECDB_QSTRING
))) {
1086 log_module(MS_LOG
, LOG_ERROR
, "Recipient not present in history %s; skipping", key
);
1088 } else if (!(recipient
= get_handle_info(str
))) {
1089 log_module(MS_LOG
, LOG_ERROR
, "Invalid recipient %s in history %s; skipping", str
, key
);
1093 if (!(str
= database_get_data(hir
->d
.object
, KEY_FROM
, RECDB_QSTRING
))) {
1094 log_module(MS_LOG
, LOG_ERROR
, "Sender not present in history %s; skipping", key
);
1096 } else if (!(sender
= get_handle_info(str
))) {
1097 log_module(MS_LOG
, LOG_ERROR
, "Invalid sender %s in history %s; skipping", str
, key
);
1101 history
= add_history(sent
, memoserv_get_account(recipient
), memoserv_get_account(sender
), id
);
1107 memoserv_saxdb_read(struct dict
*database
)
1109 struct dict
*section
;
1112 if((section
= database_get_data(database
, KEY_MAIN_ACCOUNTS
, RECDB_OBJECT
)))
1113 for(it
= dict_first(section
); it
; it
= iter_next(it
))
1114 memoserv_user_read(iter_key(it
), iter_data(it
));
1116 if((section
= database_get_data(database
, KEY_MAIN_MEMOS
, RECDB_OBJECT
)))
1117 for(it
= dict_first(section
); it
; it
= iter_next(it
))
1118 memoserv_memo_read(iter_key(it
), iter_data(it
));
1120 if((section
= database_get_data(database
, KEY_MAIN_HISTORY
, RECDB_OBJECT
)))
1121 for(it
= dict_first(section
); it
; it
= iter_next(it
))
1122 memoserv_history_read(iter_key(it
), iter_data(it
));
1128 memoserv_write_users(struct saxdb_context
*ctx
, struct memo_account
*ma
)
1130 saxdb_start_record(ctx
, ma
->handle
->handle
, 0);
1132 saxdb_write_int(ctx
, KEY_FLAGS
, ma
->flags
);
1133 saxdb_write_int(ctx
, KEY_LIMIT
, ma
->limit
);
1135 saxdb_end_record(ctx
);
1140 memoserv_write_memos(struct saxdb_context
*ctx
, struct memo
*memo
)
1144 saxdb_start_record(ctx
, inttobase64(str
, memo
->id
, sizeof(str
)), 0);
1146 saxdb_write_int(ctx
, KEY_SENT
, memo
->sent
);
1147 saxdb_write_int(ctx
, KEY_ID
, memo
->id
);
1148 saxdb_write_string(ctx
, KEY_RECIPIENT
, memo
->recipient
->handle
->handle
);
1149 saxdb_write_string(ctx
, KEY_FROM
, memo
->sender
->handle
->handle
);
1150 saxdb_write_string(ctx
, KEY_MESSAGE
, memo
->message
);
1153 saxdb_write_int(ctx
, KEY_READ
, 1);
1156 saxdb_write_int(ctx
, KEY_RECIEPT
, 1);
1158 saxdb_end_record(ctx
);
1163 memoserv_write_history(struct saxdb_context
*ctx
, struct history
*history
)
1167 saxdb_start_record(ctx
, inttobase64(str
, history
->id
, sizeof(str
)), 0);
1169 saxdb_write_int(ctx
, KEY_SENT
, history
->sent
);
1170 saxdb_write_int(ctx
, KEY_ID
, history
->id
);
1171 saxdb_write_string(ctx
, KEY_RECIPIENT
, history
->recipient
->handle
->handle
);
1172 saxdb_write_string(ctx
, KEY_FROM
, history
->sender
->handle
->handle
);
1174 saxdb_end_record(ctx
);
1179 memoserv_saxdb_write(struct saxdb_context
*ctx
)
1182 struct memo_account
*ma
;
1184 struct history
*history
;
1188 saxdb_start_record(ctx
, KEY_MAIN_ACCOUNTS
, 1);
1189 for (it
= dict_first(memos
); it
; it
= iter_next(it
)) {
1191 memoserv_write_users(ctx
, ma
);
1193 saxdb_end_record(ctx
);
1196 saxdb_start_record(ctx
, KEY_MAIN_MEMOS
, 1);
1197 for (it
= dict_first(memos
); it
; it
= iter_next(it
)) {
1199 for (ii
= 0; ii
< ma
->recvd
.used
; ++ii
) {
1200 memo
= ma
->recvd
.list
[ii
];
1201 memoserv_write_memos(ctx
, memo
);
1204 saxdb_end_record(ctx
);
1207 saxdb_start_record(ctx
, KEY_MAIN_HISTORY
, 1);
1208 for (it
= dict_first(historys
); it
; it
= iter_next(it
)) {
1210 for (ii
= 0; ii
< ma
->hrecvd
.used
; ++ii
) {
1211 history
= ma
->hrecvd
.list
[ii
];
1212 memoserv_write_history(ctx
, history
);
1215 saxdb_end_record(ctx
);
1221 memoserv_cleanup(void)
1224 dict_delete(historys
);
1228 memoserv_check_messages(struct userNode
*user
, UNUSED_ARG(struct handle_info
*old_handle
))
1230 unsigned int ii
, unseen
;
1231 struct memo_account
*ma
;
1234 if (!user
->uplink
->burst
) {
1235 if (!(ma
= memoserv_get_account(user
->handle_info
))
1236 || !(ma
->flags
& MEMO_NOTIFY_LOGIN
))
1238 for (ii
= unseen
= 0; ii
< ma
->recvd
.used
; ++ii
) {
1239 memo
= ma
->recvd
.list
[ii
];
1243 if (ma
->recvd
.used
&& memoserv
)
1244 if(unseen
) send_message(user
, memoserv
, "MSMSG_MEMOS_INBOX", unseen
, ma
->recvd
.used
- unseen
);
1249 memoserv_rename_account(struct handle_info
*hi
, const char *old_handle
)
1251 struct memo_account
*ma
;
1252 if (!(ma
= dict_find(memos
, old_handle
, NULL
)))
1254 dict_remove2(memos
, old_handle
, 1);
1255 dict_insert(memos
, hi
->handle
, ma
);
1257 dict_remove2(historys
, old_handle
, 1);
1258 dict_insert(historys
, hi
->handle
, ma
);
1262 memoserv_unreg_account(UNUSED_ARG(struct userNode
*user
), struct handle_info
*handle
)
1264 dict_remove(memos
, handle
->handle
);
1265 dict_remove(historys
, handle
->handle
);
1271 MS_LOG
= log_register_type("MemoServ", "file:memoserv.log");
1273 historys
= dict_new();
1274 dict_set_free_data(memos
, delete_memo_account
);
1275 reg_auth_func(memoserv_check_messages
);
1276 reg_handle_rename_func(memoserv_rename_account
);
1277 reg_unreg_func(memoserv_unreg_account
);
1278 conf_register_reload(memoserv_conf_read
);
1279 reg_exit_func(memoserv_cleanup
);
1280 saxdb_register("MemoServ", memoserv_saxdb_read
, memoserv_saxdb_write
);
1282 memoserv_module
= module_register("MemoServ", MS_LOG
, "mod-memoserv.help", NULL
);
1283 modcmd_register(memoserv_module
, "send", cmd_send
, 3, MODCMD_REQUIRE_AUTHED
, NULL
);
1284 modcmd_register(memoserv_module
, "list", cmd_list
, 1, MODCMD_REQUIRE_AUTHED
, NULL
);
1285 modcmd_register(memoserv_module
, "read", cmd_read
, 2, MODCMD_REQUIRE_AUTHED
, NULL
);
1286 modcmd_register(memoserv_module
, "delete", cmd_delete
, 2, MODCMD_REQUIRE_AUTHED
, NULL
);
1287 modcmd_register(memoserv_module
, "cancel", cmd_cancel
, 2, MODCMD_REQUIRE_AUTHED
, NULL
);
1288 modcmd_register(memoserv_module
, "history", cmd_history
, 1, MODCMD_REQUIRE_AUTHED
, NULL
);
1289 modcmd_register(memoserv_module
, "expire", cmd_expire
, 1, MODCMD_REQUIRE_AUTHED
, "flags", "+oper", NULL
);
1290 modcmd_register(memoserv_module
, "expiry", cmd_expiry
, 1, 0, NULL
);
1291 modcmd_register(memoserv_module
, "status", cmd_status
, 1, 0, NULL
);
1292 modcmd_register(memoserv_module
, "set", cmd_set
, 1, MODCMD_REQUIRE_AUTHED
, NULL
);
1293 modcmd_register(memoserv_module
, "oset", cmd_oset
, 1, MODCMD_REQUIRE_AUTHED
, "flags", "+helping", NULL
);
1295 memoserv_opt_dict
= dict_new();
1296 dict_insert(memoserv_opt_dict
, "AUTHNOTIFY", opt_authnotify
);
1297 dict_insert(memoserv_opt_dict
, "NOTIFY", opt_notify
);
1298 dict_insert(memoserv_opt_dict
, "PRIVATE", opt_private
);
1299 dict_insert(memoserv_opt_dict
, "IGNORERECIEPTS", opt_ignorereciepts
);
1300 dict_insert(memoserv_opt_dict
, "SENDRECIEPTS", opt_sendreciepts
);
1301 dict_insert(memoserv_opt_dict
, "LIMIT", opt_limit
);
1303 message_register_table(msgtab
);
1305 if (memoserv_conf
.message_expiry
)
1306 timeq_add(now
+ memoserv_conf
.message_expiry
, expire_memos
, NULL
);
1312 memoserv_finalize(void) {
1313 struct chanNode
*chan
;
1318 str
= "modules/memoserv";
1319 if (!(conf_node
= conf_get_data(str
, RECDB_OBJECT
))) {
1320 log_module(MS_LOG
, LOG_ERROR
, "config node `%s' is missing or has wrong type.", str
);
1324 str
= database_get_data(conf_node
, "bot", RECDB_QSTRING
);
1326 memoserv
= memoserv_conf
.bot
;
1327 const char *modes
= conf_get_data("modules/memoserv/modes", RECDB_QSTRING
);
1328 memoserv
= AddService(str
, modes
? modes
: NULL
, "User-User Memorandum Services", NULL
);
1331 log_module(MS_LOG
, LOG_ERROR
, "database_get_data for memoserv_conf.bot failed!");
1335 if (autojoin_channels
&& memoserv
) {
1336 for (i
= 0; i
< autojoin_channels
->used
; i
++) {
1337 chan
= AddChannel(autojoin_channels
->list
[i
], now
, "+nt", NULL
, NULL
);
1338 AddChannelUser(memoserv
, chan
)->modes
|= MODE_CHANOP
;