]>
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
48 #define KEY_MAIN_ACCOUNTS "accounts"
49 #define KEY_FLAGS "flags"
50 #define KEY_LIMIT "limit"
52 #define KEY_MAIN_HISTORY "history"
53 #define KEY_MAIN_MEMOS "memos"
54 #define KEY_SENT "sent"
55 #define KEY_RECIPIENT "to"
56 #define KEY_FROM "from"
57 #define KEY_MESSAGE "msg"
58 #define KEY_READ "read"
59 #define KEY_RECIEPT "reciept"
63 static const struct message_entry msgtab
[] = {
64 { "MSMSG_CANNOT_SEND", "You cannot send to account $b%s$b." },
65 { "MSMSG_UNKNOWN_SEND_FLAG", "Unreccognised send flag '%c', message not sent." },
66 { "MSMSG_MEMO_SENT", "Message sent to $b%s$b (ID# %d)." },
67 { "MSMSG_NO_MESSAGES", "You have no messages." },
68 { "MSMSG_MEMOS_FOUND", "Found $b%d$b matches.\nUse /msg $S READ <ID> to read a message." },
69 { "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." },
70 { "MSMSG_LIST_HEAD", "$bID$b $bFrom$b $bTime Sent$b" },
71 { "MSMSG_LIST_FORMAT", "%-2u %s $b%s$b %s" },
72 { "MSMSG_HISTORY_HEADER", "$bID$b $bTo$b $bTime Sent$b" },
73 { "MSMSG_HISTORY_FORMAT", "%-2u %s %s" },
74 { "MSMSG_MEMO_HEAD", "Memo %u From $b%s$b, received on %s:" },
75 { "MSMSG_MEMO_RECIEPT", "$bRead Reciept$b requested, %s." },
76 { "MSMSG_BAD_MESSAGE_ID", "$b%s$b is not a valid message ID (it should be a number between 0 and %u)." },
77 { "MSMSG_NO_SUCH_MEMO", "You have no memo with that ID." },
78 { "MSMSG_MEMO_DELETED", "Memo $b%d$b deleted." },
79 { "MSMSG_MEMO_CANCEL_NUMBER", "You must specify a number id" },
80 { "MSMSG_MEMO_DONT_OWN", "You did not send memo# %d" },
81 { "MSMSG_MEMO_READ", "Memo# %d has already been read, you cannot cancel it." },
82 { "MSMSG_MEMO_CANT_LOCATE", "Could not locate memo# %d" },
83 { "MSMSG_EXPIRY_OFF", "I am currently not expiring messages. (turned off)" },
84 { "MSMSG_EXPIRY", "Messages will be expired when they are %s old (%d seconds)." },
85 { "MSMSG_MESSAGES_EXPIRED", "$b%lu$b message(s) expired." },
86 { "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." },
87 { "MSMSG_NEW_MESSAGE", "You have a new message from $b%s$b. /msg $S LIST" },
88 { "MSMSG_FULL_INBOX", "$b%s$b cannot recieve anymore memos as their inbox is full" },
89 { "MSMSG_DELETED_ALL", "Deleted all of your messages." },
90 { "MSMSG_USE_CONFIRM", "Please use /msg $S DELETE * $bCONFIRM$b to delete $uall$u of your messages." },
92 { "MSMSG_STATUS_HIST_TOTAL", "I have $b%u$b history entries in my database." },
93 { "MSMSG_STATUS_TOTAL", "I have $b%u$b memos in my database." },
94 { "MSMSG_STATUS_EXPIRED", "$b%ld$b memos expired during the time I am awake." },
95 { "MSMSG_STATUS_SENT", "$b%ld$b memos have been sent." },
97 { "MSMSG_INVALID_OPTION", "$b%s$b is not a valid option." },
98 { "MSMSG_INVALID_BINARY", "$b%s$b is an invalid binary value." },
99 { "MSMSG_SET_NOTIFY", "$bNotify: $b %s" },
100 { "MSMSG_SET_AUTHNOTIFY", "$bAuthNotify: $b %s" },
101 { "MSMSG_SET_PRIVATE", "$bPrivate: $b %s" },
102 { "MSMSG_SET_IGNORERECIEPTS", "$bIgnoreReciepts:$b %s" },
103 { "MSMSG_SET_SENDRECIEPTS", "$bSendReciepts: $b %s" },
104 { "MSMSG_SET_LIMIT", "$bLimit: $b %d" },
105 { "MSMSG_SET_OPTIONS", "$bMessaging Options$b" },
106 { "MSMSG_SET_OPTIONS_END", "-------------End of Options-------------" },
108 { "MSMSG_LIST_END", "--------------End of Memos--------------" },
109 { "MSMSG_BAR", "----------------------------------------"},
115 struct memo_account
*recipient
;
116 struct memo_account
*sender
;
120 unsigned int is_read
: 1;
121 unsigned int reciept
: 1;
125 struct memo_account
*recipient
;
126 struct memo_account
*sender
;
131 DECLARE_LIST(memoList
, struct memo
*);
132 DEFINE_LIST(memoList
, struct memo
*);
133 DECLARE_LIST(historyList
, struct history
*);
134 DEFINE_LIST(historyList
, struct history
*);
136 /* memo_account.flags fields */
137 #define MEMO_NOTIFY_NEW 0x00000001
138 #define MEMO_NOTIFY_LOGIN 0x00000002
139 #define MEMO_DENY_NONCHANNEL 0x00000004
140 #define MEMO_IGNORE_RECIEPTS 0x00000008
141 #define MEMO_ALWAYS_RECIEPTS 0x00000010
143 struct memo_account
{
144 struct handle_info
*handle
;
147 struct memoList sent
;
148 struct memoList recvd
;
149 struct historyList hsent
;
150 struct historyList hrecvd
;
154 struct userNode
*bot
;
159 #define MEMOSERV_FUNC(NAME) MODCMD_FUNC(NAME)
160 #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[])
161 typedef OPTION_FUNC(option_func_t
);
163 unsigned long memo_id
;
165 extern struct string_list
*autojoin_channels
;
166 const char *memoserv_module_deps
[] = { NULL
};
167 static struct module *memoserv_module
;
168 static struct log_type
*MS_LOG
;
169 static unsigned long memosSent
, memosExpired
;
170 static struct dict
*memos
; /* memo_account->handle->handle -> memo_account */
171 static struct dict
*historys
;
172 static dict_t memoserv_opt_dict
; /* contains option_func_t* */
174 static struct memo_account
*
175 memoserv_get_account(struct handle_info
*hi
)
177 struct memo_account
*ma
;
180 ma
= dict_find(memos
, hi
->handle
, NULL
);
183 ma
= calloc(1, sizeof(*ma
));
187 ma
->flags
= MEMO_NOTIFY_NEW
| MEMO_NOTIFY_LOGIN
;
188 ma
->limit
= memoserv_conf
.limit
;
189 dict_insert(memos
, ma
->handle
->handle
, ma
);
190 dict_insert(historys
, ma
->handle
->handle
, ma
);
195 delete_memo(struct memo
*memo
)
197 memoList_remove(&memo
->recipient
->recvd
, memo
);
198 memoList_remove(&memo
->sender
->sent
, memo
);
204 delete_history(struct history
*history
)
206 historyList_remove(&history
->recipient
->hrecvd
, history
);
207 historyList_remove(&history
->sender
->hsent
, history
);
212 delete_memo_account(void *data
)
214 struct memo_account
*ma
= data
;
216 while (ma
->recvd
.used
)
217 delete_memo(ma
->recvd
.list
[0]);
218 while (ma
->sent
.used
)
219 delete_memo(ma
->sent
.list
[0]);
220 memoList_clean(&ma
->recvd
);
221 memoList_clean(&ma
->sent
);
223 while (ma
->hrecvd
.used
)
224 delete_history(ma
->hrecvd
.list
[0]);
225 while (ma
->hsent
.used
)
226 delete_history(ma
->hsent
.list
[0]);
227 historyList_clean(&ma
->hrecvd
);
228 historyList_clean(&ma
->hsent
);
236 for (it
= dict_first(memos
); it
; it
= iter_next(it
)) {
237 struct memo_account
*acct
= iter_data(it
);
239 for (ii
= 0; ii
< acct
->sent
.used
; ++ii
) {
240 struct memo
*memo
= acct
->sent
.list
[ii
];
241 if ((now
- memo
->sent
) > memoserv_conf
.message_expiry
) {
249 for (it
= dict_first(historys
); it
; it
= iter_next(it
)) {
250 struct memo_account
*acct
= iter_data(it
);
252 for (ii
= 0; ii
< acct
->hsent
.used
; ++ii
) {
253 struct history
*history
= acct
->hsent
.list
[ii
];
254 if ((now
- history
->sent
) > memoserv_conf
.message_expiry
) {
255 delete_history(history
);
264 expire_memos(UNUSED_ARG(void *data
))
266 if (memoserv_conf
.message_expiry
) {
268 timeq_add(now
+ memoserv_conf
.message_expiry
, expire_memos
, NULL
);
272 static struct history
*
273 add_history(time_t sent
, struct memo_account
*recipient
, struct memo_account
*sender
, unsigned long id
)
275 struct history
*history
;
277 history
= calloc(1, sizeof(*history
));
282 history
->recipient
= recipient
;
283 historyList_append(&recipient
->hrecvd
, history
);
284 history
->sender
= sender
;
285 historyList_append(&sender
->hsent
, history
);
286 history
->sent
= sent
;
293 add_memo(time_t sent
, struct memo_account
*recipient
, struct memo_account
*sender
, char *message
, int nfrom_read
)
296 struct history
*history
;
298 memo
= calloc(1, sizeof(*memo
));
307 memo
->recipient
= recipient
;
308 memoList_append(&recipient
->recvd
, memo
);
309 memo
->sender
= sender
;
310 memoList_append(&sender
->sent
, memo
);
312 memo
->message
= strdup(message
);
316 history
= add_history(sent
, recipient
, sender
, memo
->id
);
322 memoserv_can_send(struct userNode
*bot
, struct userNode
*user
, struct memo_account
*acct
)
324 extern struct userData
*_GetChannelUser(struct chanData
*channel
, struct handle_info
*handle
, int override
, int allow_suspended
);
325 struct userData
*dest
;
326 unsigned int i
= 0, match
= 0;
328 if (!user
->handle_info
)
331 /* Sanity checks here because if the user doesnt have a limit set
332 the limit comes out at like 21233242 if you try and use it. */
333 if (acct
->limit
> memoserv_conf
.limit
)
334 acct
->limit
= memoserv_conf
.limit
;
336 if (acct
->recvd
.used
> acct
->limit
) {
337 send_message(user
, bot
, "MSMSG_FULL_INBOX", acct
->handle
->handle
);
338 send_message(user
, bot
, "MSMSG_CANNOT_SEND", acct
->handle
->handle
);
342 if (acct
->handle
->ignores
->used
) {
343 for (i
=0; i
< acct
->handle
->ignores
->used
; i
++) {
344 if (user_matches_glob(user
, acct
->handle
->ignores
->list
[i
], MATCH_USENICK
)) {
351 send_message(user
, bot
, "MSMSG_CANNOT_SEND", acct
->handle
->handle
);
356 if (!(acct
->flags
& MEMO_DENY_NONCHANNEL
))
359 for (dest
= acct
->handle
->channels
; dest
; dest
= dest
->u_next
)
360 if (_GetChannelUser(dest
->channel
, user
->handle_info
, 1, 0))
363 send_message(user
, bot
, "MSMSG_CANNOT_SEND", acct
->handle
->handle
);
367 static struct memo
*find_memo(struct userNode
*user
, struct svccmd
*cmd
, struct memo_account
*ma
, const char *msgid
, unsigned int *id
)
370 if (!isdigit(msgid
[0])) {
372 reply("MSMSG_BAD_MESSAGE_ID", msgid
, ma
->recvd
.used
- 1);
374 reply("MSMSG_NO_MESSAGES");
377 memoid
= atoi(msgid
);
378 if (memoid
>= ma
->recvd
.used
) {
379 reply("MSMSG_NO_SUCH_MEMO");
382 return ma
->recvd
.list
[*id
= memoid
];
385 static MODCMD_FUNC(cmd_send
)
388 int reciept
= 0, inc
= 2;
389 struct handle_info
*hi
;
390 struct memo_account
*ma
, *sender
;
393 if (!(hi
= modcmd_get_handle_info(user
, argv
[1])))
396 if (!(sender
= memoserv_get_account(user
->handle_info
))
397 || !(ma
= memoserv_get_account(hi
))) {
398 reply("MSG_INTERNAL_FAILURE");
402 if (!(memoserv_can_send(cmd
->parent
->bot
, user
, ma
)))
405 inc
= 2; /* Start of message on 3rd ([2]) word */
406 if(argv
[2][0] == '-' && argv
[2][1] != '-') { /* first word is flags ('-r')*/
407 char *flags
= argv
[2];
408 inc
++; /* Start of message is now 1 word later */
409 for(flags
++;*flags
;flags
++) {
416 /* Unknown mode. Give an error */
417 reply("MSMSG_UNKNOWN_SEND_FLAG", *flags
);
423 inc
= 2; /* Start of message is word 2 */
425 message
= unsplit_string(argv
+ inc
, argc
- inc
, NULL
);
426 memo
= add_memo(now
, ma
, sender
, message
, 1);
427 if ((reciept
== 1) || (ma
->flags
& MEMO_ALWAYS_RECIEPTS
))
430 if (ma
->flags
& MEMO_NOTIFY_NEW
) {
431 struct userNode
*other
;
433 for (other
= ma
->handle
->users
; other
; other
= other
->next_authed
)
434 send_message(other
, cmd
->parent
->bot
, "MSMSG_NEW_MESSAGE", user
->nick
);
437 reply("MSMSG_MEMO_SENT", ma
->handle
->handle
, memo_id
);
441 static MODCMD_FUNC(cmd_list
)
443 struct memo_account
*ma
;
449 if (!(ma
= memoserv_get_account(user
->handle_info
)))
452 reply("MSMSG_LIST_HEAD");
454 if(user
->handle_info
&& user
->handle_info
->userlist_style
!= HI_STYLE_CLEAN
)
457 for (ii
= 0; (ii
< ma
->recvd
.used
) && (ii
< 15); ++ii
) {
458 memo
= ma
->recvd
.list
[ii
];
459 localtime_r(&memo
->sent
, &tm
);
460 strftime(posted
, sizeof(posted
), "%I:%M %p, %m/%d/%Y", &tm
);
461 reply("MSMSG_LIST_FORMAT", ii
, memo
->sender
->handle
->handle
, memo
->reciept
? "(r)" : "", posted
);
466 reply("MSMSG_CLEAN_INBOX", ii
);
468 reply("MSMSG_MEMOS_FOUND", ii
);
470 reply("MSMSG_LIST_END");
475 static MODCMD_FUNC(cmd_history
)
477 struct memo_account
*ma
;
478 struct history
*history
;
485 if (!(ma
= memoserv_get_account(user
->handle_info
)))
488 reply("MSMSG_HISTORY_HEADER");
490 if(user
->handle_info
&& user
->handle_info
->userlist_style
!= HI_STYLE_CLEAN
)
493 for (it
= dict_first(historys
); it
; it
= iter_next(it
)) {
495 for (ii
= 0; ii
< ma
->hrecvd
.used
; ++ii
) {
496 history
= ma
->hrecvd
.list
[ii
];
497 if (!strcasecmp(history
->sender
->handle
->handle
, user
->handle_info
->handle
)) {
499 localtime_r(&history
->sent
, &tm
);
500 strftime(posted
, sizeof(posted
), "%I:%M %p, %m/%d/%Y", &tm
);
501 reply("MSMSG_HISTORY_FORMAT", history
->id
, history
->recipient
->handle
->handle
, posted
);
509 reply("MSMSG_MEMOS_FOUND", cc
);
511 reply("MSMSG_LIST_END");
516 static MODCMD_FUNC(cmd_read
)
518 struct memo_account
*ma
;
520 int rignore
= 0, brk
= 0, s
= 0;
526 if (!(ma
= memoserv_get_account(user
->handle_info
)))
528 if (!(memo
= find_memo(user
, cmd
, ma
, argv
[1], &memoid
)))
532 char *argtwo
= argv
[2];
557 localtime_r(&memo
->sent
, &tm
);
558 strftime(posted
, sizeof(posted
), "%I:%M %p, %m/%d/%Y", &tm
);
560 reply("MSMSG_MEMO_HEAD", memoid
, memo
->sender
->handle
->handle
, posted
);
561 send_message_type(4, user
, cmd
->parent
->bot
, "%s", memo
->message
);
565 if (ma
->flags
& MEMO_IGNORE_RECIEPTS
)
568 if (memo
->reciept
== 1) {
570 reply("MSMSG_MEMO_RECIEPT", rignore
? "ignoring" : "sending");
572 struct memo_account
*ma
;
573 struct memo_account
*sender
;
574 char content
[MAXLEN
];
576 ma
= memoserv_get_account(user
->handle_info
);
577 sender
= memoserv_get_account(memo
->sender
->handle
);
579 sprintf(content
, "%s has read your memo dated %s.", ma
->handle
->handle
, posted
);
581 memo
= add_memo(now
, sender
, ma
, content
, 1);
582 reply("MSMSG_MEMO_SENT", memob
->sender
->handle
->handle
, memo_id
);
584 if (sender
->flags
& MEMO_NOTIFY_NEW
) {
585 struct userNode
*other
;
587 for (other
= sender
->handle
->users
; other
; other
= other
->next_authed
)
588 send_message(other
, cmd
->parent
->bot
, "MSMSG_NEW_MESSAGE", ma
->handle
->handle
);
597 static MODCMD_FUNC(cmd_delete
)
599 struct memo_account
*ma
;
603 if (!(ma
= memoserv_get_account(user
->handle_info
)))
605 if (!irccasecmp(argv
[1], "*") || !irccasecmp(argv
[1], "all")) {
606 if ((argc
< 3) || irccasecmp(argv
[2], "confirm")) {
607 reply("MSMSG_USE_CONFIRM");
610 while (ma
->recvd
.used
)
611 delete_memo(ma
->recvd
.list
[0]);
612 reply("MSMSG_DELETED_ALL");
616 if (!(memo
= find_memo(user
, cmd
, ma
, argv
[1], &memoid
)))
619 reply("MSMSG_MEMO_DELETED", memoid
);
623 static MODCMD_FUNC(cmd_cancel
)
629 struct memo_account
*ma
;
631 if (isdigit(argv
[1][0])) {
632 id
= strtoul(argv
[1], NULL
, 0);
634 reply("MSMSG_MEMO_CANCEL_NUMBER");
638 for (it
= dict_first(memos
); it
; it
= iter_next(it
)) {
640 for (ii
= 0; ii
< ma
->recvd
.used
; ++ii
) {
641 memo
= ma
->recvd
.list
[ii
];
643 if (id
== memo
->id
) {
644 if (!strcasecmp(memo
->sender
->handle
->handle
, user
->handle_info
->handle
)) {
646 reply("MSMSG_MEMO_READ", id
);
650 reply("MSMSG_MEMO_DELETED", id
);
654 reply("MSMSG_MEMO_DONT_OWN", id
);
661 reply("MSMSG_MEMO_CANT_LOCATE", id
);
665 static MODCMD_FUNC(cmd_expire
)
667 unsigned long old_expired
= memosExpired
;
669 reply("MSMSG_MESSAGES_EXPIRED", memosExpired
- old_expired
);
673 static MODCMD_FUNC(cmd_expiry
)
675 char interval
[INTERVALLEN
];
677 if (!memoserv_conf
.message_expiry
) {
678 reply("MSMSG_EXPIRY_OFF");
682 intervalString(interval
, memoserv_conf
.message_expiry
, user
->handle_info
);
683 reply("MSMSG_EXPIRY", interval
, memoserv_conf
.message_expiry
);
689 set_list(struct svccmd
*cmd
, struct userNode
*user
, struct handle_info
*hi
, int override
)
693 char *set_display
[] = {"AUTHNOTIFY", "NOTIFY", "PRIVATE", "LIMIT",
694 "IGNORERECIEPTS", "SENDRECIEPTS"};
696 reply("MSMSG_SET_OPTIONS");
699 /* Do this so options are presented in a consistent order. */
700 for (i
= 0; i
< ArrayLength(set_display
); ++i
)
701 if ((opt
= dict_find(memoserv_opt_dict
, set_display
[i
], NULL
)))
702 opt(cmd
, user
, hi
, override
, 0, NULL
);
703 reply("MSMSG_SET_OPTIONS_END");
706 static MODCMD_FUNC(cmd_set
)
708 struct handle_info
*hi
;
711 hi
= user
->handle_info
;
713 set_list(cmd
, user
, hi
, 0);
717 if (!(opt
= dict_find(memoserv_opt_dict
, argv
[1], NULL
))) {
718 reply("MSMSG_INVALID_OPTION", argv
[1]);
722 return opt(cmd
, user
, hi
, 0, argc
-1, argv
+1);
725 static MODCMD_FUNC(cmd_oset
)
727 struct handle_info
*hi
;
733 if (!(hi
= get_victim_oper(cmd
, user
, argv
[1])))
737 set_list(cmd
, user
, hi
, 0);
741 if (!(opt
= dict_find(memoserv_opt_dict
, argv
[2], NULL
))) {
742 reply("MSMSG_INVALID_OPTION", argv
[2]);
746 return opt(cmd
, user
, hi
, 1, argc
-2, argv
+2);
749 static OPTION_FUNC(opt_notify
)
751 struct memo_account
*ma
;
754 if (!(ma
= memoserv_get_account(hi
)))
758 if (enabled_string(choice
)) {
759 ma
->flags
|= MEMO_NOTIFY_NEW
;
760 } else if (disabled_string(choice
)) {
761 ma
->flags
&= ~MEMO_NOTIFY_NEW
;
763 reply("MSMSG_INVALID_BINARY", choice
);
768 choice
= (ma
->flags
& MEMO_NOTIFY_NEW
) ? "on" : "off";
769 reply("MSMSG_SET_NOTIFY", choice
);
773 static OPTION_FUNC(opt_authnotify
)
775 struct memo_account
*ma
;
778 if (!(ma
= memoserv_get_account(hi
)))
782 if (enabled_string(choice
)) {
783 ma
->flags
|= MEMO_NOTIFY_LOGIN
;
784 } else if (disabled_string(choice
)) {
785 ma
->flags
&= ~MEMO_NOTIFY_LOGIN
;
787 reply("MSMSG_INVALID_BINARY", choice
);
792 choice
= (ma
->flags
& MEMO_NOTIFY_LOGIN
) ? "on" : "off";
793 reply("MSMSG_SET_AUTHNOTIFY", choice
);
797 static OPTION_FUNC(opt_ignorereciepts
)
799 struct memo_account
*ma
;
802 if (!(ma
= memoserv_get_account(hi
)))
806 if (enabled_string(choice
)) {
807 ma
->flags
|= MEMO_IGNORE_RECIEPTS
;
808 } else if (disabled_string(choice
)) {
809 ma
->flags
&= ~MEMO_IGNORE_RECIEPTS
;
811 reply("MSMSG_INVALID_BINARY", choice
);
816 choice
= (ma
->flags
& MEMO_IGNORE_RECIEPTS
) ? "on" : "off";
817 reply("MSMSG_SET_IGNORERECIEPTS", choice
);
821 static OPTION_FUNC(opt_sendreciepts
)
823 struct memo_account
*ma
;
826 if (!(ma
= memoserv_get_account(hi
)))
830 if (enabled_string(choice
)) {
831 ma
->flags
|= MEMO_ALWAYS_RECIEPTS
;
832 } else if (disabled_string(choice
)) {
833 ma
->flags
&= ~MEMO_ALWAYS_RECIEPTS
;
835 reply("MSMSG_INVALID_BINARY", choice
);
840 choice
= (ma
->flags
& MEMO_ALWAYS_RECIEPTS
) ? "on" : "off";
841 reply("MSMSG_SET_SENDRECIEPTS", choice
);
845 static OPTION_FUNC(opt_private
)
847 struct memo_account
*ma
;
850 if (!(ma
= memoserv_get_account(hi
)))
854 if (enabled_string(choice
)) {
855 ma
->flags
|= MEMO_DENY_NONCHANNEL
;
856 } else if (disabled_string(choice
)) {
857 ma
->flags
&= ~MEMO_DENY_NONCHANNEL
;
859 reply("MSMSG_INVALID_BINARY", choice
);
864 choice
= (ma
->flags
& MEMO_DENY_NONCHANNEL
) ? "on" : "off";
865 reply("MSMSG_SET_PRIVATE", choice
);
869 static OPTION_FUNC(opt_limit
)
871 struct memo_account
*ma
;
874 if (!(ma
= memoserv_get_account(hi
)))
877 choice
= atoi(argv
[1]);
878 if (choice
> memoserv_conf
.limit
)
879 choice
= memoserv_conf
.limit
;
884 reply("MSMSG_SET_LIMIT", ma
->limit
);
888 static MODCMD_FUNC(cmd_status
)
890 struct memo_account
*ma
;
895 for (it
= dict_first(memos
); it
; it
= iter_next(it
)) {
897 for (ii
= 0; ii
< ma
->recvd
.used
; ++ii
)
901 for (it
= dict_first(historys
); it
; it
= iter_next(it
)) {
903 for (ii
= 0; ii
< ma
->hrecvd
.used
; ++ii
)
907 reply("MSMSG_STATUS_HIST_TOTAL", hc
);
908 reply("MSMSG_STATUS_TOTAL", mc
);
909 reply("MSMSG_STATUS_EXPIRED", memosExpired
);
910 reply("MSMSG_STATUS_SENT", memosSent
);
915 memoserv_conf_read(void)
920 str
= "modules/memoserv";
921 if (!(conf_node
= conf_get_data(str
, RECDB_OBJECT
))) {
922 log_module(MS_LOG
, LOG_ERROR
, "config node `%s' is missing or has wrong type.", str
);
926 str
= database_get_data(conf_node
, "limit", RECDB_QSTRING
);
927 memoserv_conf
.limit
= str
? atoi(str
) : 50;
929 str
= database_get_data(conf_node
, "message_expiry", RECDB_QSTRING
);
930 memoserv_conf
.message_expiry
= str
? ParseInterval(str
) : 60*24*30;
934 memoserv_user_read(const char *key
, struct record_data
*hir
)
937 struct memo_account
*ma
;
938 struct handle_info
*hi
;
940 if (!(hi
= get_handle_info(key
)))
943 ma
= dict_find(memos
, hi
->handle
, NULL
);
948 ma
= calloc(1, sizeof(*ma
));
954 str
= database_get_data(hir
->d
.object
, KEY_FLAGS
, RECDB_QSTRING
);
956 log_module(MS_LOG
, LOG_ERROR
, "Flags not present in memo %s; skipping", key
);
959 ma
->flags
= strtoul(str
, NULL
, 0);
961 str
= database_get_data(hir
->d
.object
, KEY_LIMIT
, RECDB_QSTRING
);
963 log_module(MS_LOG
, LOG_ERROR
, "Limit not present in memo %s; skipping", key
);
966 ma
->limit
= strtoul(str
, NULL
, 0);
968 dict_insert(memos
, ma
->handle
->handle
, ma
);
969 dict_insert(historys
, ma
->handle
->handle
, ma
);
975 memoserv_memo_read(const char *key
, struct record_data
*hir
)
978 struct handle_info
*sender
, *recipient
;
983 if (hir
->type
!= RECDB_OBJECT
) {
984 log_module(MS_LOG
, LOG_WARNING
, "Unexpected rectype %d for %s.", hir
->type
, key
);
988 if (!(str
= database_get_data(hir
->d
.object
, KEY_SENT
, RECDB_QSTRING
))) {
989 log_module(MS_LOG
, LOG_ERROR
, "Date sent not present in memo %s; skipping", key
);
995 if (!(str
= database_get_data(hir
->d
.object
, KEY_ID
, RECDB_QSTRING
))) {
996 log_module(MS_LOG
, LOG_ERROR
, "ID sent not present in memo %s; skipping", key
);
999 id
= strtoul(str
, NULL
, 0);
1003 if (!(str
= database_get_data(hir
->d
.object
, KEY_RECIPIENT
, RECDB_QSTRING
))) {
1004 log_module(MS_LOG
, LOG_ERROR
, "Recipient not present in memo %s; skipping", key
);
1006 } else if (!(recipient
= get_handle_info(str
))) {
1007 log_module(MS_LOG
, LOG_ERROR
, "Invalid recipient %s in memo %s; skipping", str
, key
);
1011 if (!(str
= database_get_data(hir
->d
.object
, KEY_FROM
, RECDB_QSTRING
))) {
1012 log_module(MS_LOG
, LOG_ERROR
, "Sender not present in memo %s; skipping", key
);
1014 } else if (!(sender
= get_handle_info(str
))) {
1015 log_module(MS_LOG
, LOG_ERROR
, "Invalid sender %s in memo %s; skipping", str
, key
);
1019 if (!(str
= database_get_data(hir
->d
.object
, KEY_MESSAGE
, RECDB_QSTRING
))) {
1020 log_module(MS_LOG
, LOG_ERROR
, "Message not present in memo %s; skipping", key
);
1024 memo
= add_memo(sent
, memoserv_get_account(recipient
), memoserv_get_account(sender
), str
, 0);
1025 if ((str
= database_get_data(hir
->d
.object
, KEY_READ
, RECDB_QSTRING
)))
1028 if ((str
= database_get_data(hir
->d
.object
, KEY_RECIEPT
, RECDB_QSTRING
)))
1037 memoserv_history_read(const char *key
, struct record_data
*hir
)
1040 struct handle_info
*sender
, *recipient
;
1041 struct history
*history
;
1045 if (hir
->type
!= RECDB_OBJECT
) {
1046 log_module(MS_LOG
, LOG_WARNING
, "Unexpected rectype %d for %s.", hir
->type
, key
);
1050 if (!(str
= database_get_data(hir
->d
.object
, KEY_SENT
, RECDB_QSTRING
))) {
1051 log_module(MS_LOG
, LOG_ERROR
, "Date sent not present in history %s; skipping", key
);
1057 if (!(str
= database_get_data(hir
->d
.object
, KEY_ID
, RECDB_QSTRING
))) {
1058 log_module(MS_LOG
, LOG_ERROR
, "ID sent not present in history %s; skipping", key
);
1061 id
= strtoul(str
, NULL
, 0);
1063 if (!(str
= database_get_data(hir
->d
.object
, KEY_RECIPIENT
, RECDB_QSTRING
))) {
1064 log_module(MS_LOG
, LOG_ERROR
, "Recipient not present in history %s; skipping", key
);
1066 } else if (!(recipient
= get_handle_info(str
))) {
1067 log_module(MS_LOG
, LOG_ERROR
, "Invalid recipient %s in history %s; skipping", str
, key
);
1071 if (!(str
= database_get_data(hir
->d
.object
, KEY_FROM
, RECDB_QSTRING
))) {
1072 log_module(MS_LOG
, LOG_ERROR
, "Sender not present in history %s; skipping", key
);
1074 } else if (!(sender
= get_handle_info(str
))) {
1075 log_module(MS_LOG
, LOG_ERROR
, "Invalid sender %s in history %s; skipping", str
, key
);
1079 history
= add_history(sent
, memoserv_get_account(recipient
), memoserv_get_account(sender
), id
);
1085 memoserv_saxdb_read(struct dict
*database
)
1087 struct dict
*section
;
1090 if((section
= database_get_data(database
, KEY_MAIN_ACCOUNTS
, RECDB_OBJECT
)))
1091 for(it
= dict_first(section
); it
; it
= iter_next(it
))
1092 memoserv_user_read(iter_key(it
), iter_data(it
));
1094 if((section
= database_get_data(database
, KEY_MAIN_MEMOS
, RECDB_OBJECT
)))
1095 for(it
= dict_first(section
); it
; it
= iter_next(it
))
1096 memoserv_memo_read(iter_key(it
), iter_data(it
));
1098 if((section
= database_get_data(database
, KEY_MAIN_HISTORY
, RECDB_OBJECT
)))
1099 for(it
= dict_first(section
); it
; it
= iter_next(it
))
1100 memoserv_history_read(iter_key(it
), iter_data(it
));
1106 memoserv_write_users(struct saxdb_context
*ctx
, struct memo_account
*ma
)
1108 saxdb_start_record(ctx
, ma
->handle
->handle
, 0);
1110 saxdb_write_int(ctx
, KEY_FLAGS
, ma
->flags
);
1111 saxdb_write_int(ctx
, KEY_LIMIT
, ma
->limit
);
1113 saxdb_end_record(ctx
);
1118 memoserv_write_memos(struct saxdb_context
*ctx
, struct memo
*memo
)
1122 saxdb_start_record(ctx
, inttobase64(str
, memo
->id
, sizeof(str
)), 0);
1124 saxdb_write_int(ctx
, KEY_SENT
, memo
->sent
);
1125 saxdb_write_int(ctx
, KEY_ID
, memo
->id
);
1126 saxdb_write_string(ctx
, KEY_RECIPIENT
, memo
->recipient
->handle
->handle
);
1127 saxdb_write_string(ctx
, KEY_FROM
, memo
->sender
->handle
->handle
);
1128 saxdb_write_string(ctx
, KEY_MESSAGE
, memo
->message
);
1131 saxdb_write_int(ctx
, KEY_READ
, 1);
1134 saxdb_write_int(ctx
, KEY_RECIEPT
, 1);
1136 saxdb_end_record(ctx
);
1141 memoserv_write_history(struct saxdb_context
*ctx
, struct history
*history
)
1145 saxdb_start_record(ctx
, inttobase64(str
, history
->id
, sizeof(str
)), 0);
1147 saxdb_write_int(ctx
, KEY_SENT
, history
->sent
);
1148 saxdb_write_int(ctx
, KEY_ID
, history
->id
);
1149 saxdb_write_string(ctx
, KEY_RECIPIENT
, history
->recipient
->handle
->handle
);
1150 saxdb_write_string(ctx
, KEY_FROM
, history
->sender
->handle
->handle
);
1152 saxdb_end_record(ctx
);
1157 memoserv_saxdb_write(struct saxdb_context
*ctx
)
1160 struct memo_account
*ma
;
1162 struct history
*history
;
1166 saxdb_start_record(ctx
, KEY_MAIN_ACCOUNTS
, 1);
1167 for (it
= dict_first(memos
); it
; it
= iter_next(it
)) {
1169 memoserv_write_users(ctx
, ma
);
1171 saxdb_end_record(ctx
);
1174 saxdb_start_record(ctx
, KEY_MAIN_MEMOS
, 1);
1175 for (it
= dict_first(memos
); it
; it
= iter_next(it
)) {
1177 for (ii
= 0; ii
< ma
->recvd
.used
; ++ii
) {
1178 memo
= ma
->recvd
.list
[ii
];
1179 memoserv_write_memos(ctx
, memo
);
1182 saxdb_end_record(ctx
);
1185 saxdb_start_record(ctx
, KEY_MAIN_HISTORY
, 1);
1186 for (it
= dict_first(historys
); it
; it
= iter_next(it
)) {
1188 for (ii
= 0; ii
< ma
->hrecvd
.used
; ++ii
) {
1189 history
= ma
->hrecvd
.list
[ii
];
1190 memoserv_write_history(ctx
, history
);
1193 saxdb_end_record(ctx
);
1199 memoserv_cleanup(void)
1202 dict_delete(historys
);
1206 memoserv_check_messages(struct userNode
*user
, UNUSED_ARG(struct handle_info
*old_handle
))
1208 unsigned int ii
, unseen
;
1209 struct memo_account
*ma
;
1212 if (!user
->uplink
->burst
) {
1213 if (!(ma
= memoserv_get_account(user
->handle_info
))
1214 || !(ma
->flags
& MEMO_NOTIFY_LOGIN
))
1216 for (ii
= unseen
= 0; ii
< ma
->recvd
.used
; ++ii
) {
1217 memo
= ma
->recvd
.list
[ii
];
1221 if (ma
->recvd
.used
&& memoserv_conf
.bot
)
1222 if(unseen
) send_message(user
, memoserv_conf
.bot
, "MSMSG_MEMOS_INBOX", unseen
, ma
->recvd
.used
- unseen
);
1227 memoserv_rename_account(struct handle_info
*hi
, const char *old_handle
)
1229 struct memo_account
*ma
;
1230 if (!(ma
= dict_find(memos
, old_handle
, NULL
)))
1232 dict_remove2(memos
, old_handle
, 1);
1233 dict_insert(memos
, hi
->handle
, ma
);
1235 dict_remove2(historys
, old_handle
, 1);
1236 dict_insert(historys
, hi
->handle
, ma
);
1240 memoserv_unreg_account(UNUSED_ARG(struct userNode
*user
), struct handle_info
*handle
)
1242 dict_remove(memos
, handle
->handle
);
1243 dict_remove(historys
, handle
->handle
);
1249 MS_LOG
= log_register_type("MemoServ", "file:memoserv.log");
1251 historys
= dict_new();
1252 dict_set_free_data(memos
, delete_memo_account
);
1253 reg_auth_func(memoserv_check_messages
);
1254 reg_handle_rename_func(memoserv_rename_account
);
1255 reg_unreg_func(memoserv_unreg_account
);
1256 conf_register_reload(memoserv_conf_read
);
1257 reg_exit_func(memoserv_cleanup
);
1258 saxdb_register("MemoServ", memoserv_saxdb_read
, memoserv_saxdb_write
);
1260 memoserv_module
= module_register("MemoServ", MS_LOG
, "mod-memoserv.help", NULL
);
1261 modcmd_register(memoserv_module
, "send", cmd_send
, 3, MODCMD_REQUIRE_AUTHED
, NULL
);
1262 modcmd_register(memoserv_module
, "list", cmd_list
, 1, MODCMD_REQUIRE_AUTHED
, NULL
);
1263 modcmd_register(memoserv_module
, "read", cmd_read
, 2, MODCMD_REQUIRE_AUTHED
, NULL
);
1264 modcmd_register(memoserv_module
, "delete", cmd_delete
, 2, MODCMD_REQUIRE_AUTHED
, NULL
);
1265 modcmd_register(memoserv_module
, "cancel", cmd_cancel
, 2, MODCMD_REQUIRE_AUTHED
, NULL
);
1266 modcmd_register(memoserv_module
, "history", cmd_history
, 1, MODCMD_REQUIRE_AUTHED
, NULL
);
1267 modcmd_register(memoserv_module
, "expire", cmd_expire
, 1, MODCMD_REQUIRE_AUTHED
, "flags", "+oper", NULL
);
1268 modcmd_register(memoserv_module
, "expiry", cmd_expiry
, 1, 0, NULL
);
1269 modcmd_register(memoserv_module
, "status", cmd_status
, 1, 0, NULL
);
1270 modcmd_register(memoserv_module
, "set", cmd_set
, 1, MODCMD_REQUIRE_AUTHED
, NULL
);
1271 modcmd_register(memoserv_module
, "oset", cmd_oset
, 1, MODCMD_REQUIRE_AUTHED
, "flags", "+helping", NULL
);
1273 memoserv_opt_dict
= dict_new();
1274 dict_insert(memoserv_opt_dict
, "AUTHNOTIFY", opt_authnotify
);
1275 dict_insert(memoserv_opt_dict
, "NOTIFY", opt_notify
);
1276 dict_insert(memoserv_opt_dict
, "PRIVATE", opt_private
);
1277 dict_insert(memoserv_opt_dict
, "IGNORERECIEPTS", opt_ignorereciepts
);
1278 dict_insert(memoserv_opt_dict
, "SENDRECIEPTS", opt_sendreciepts
);
1279 dict_insert(memoserv_opt_dict
, "LIMIT", opt_limit
);
1281 message_register_table(msgtab
);
1283 if (memoserv_conf
.message_expiry
)
1284 timeq_add(now
+ memoserv_conf
.message_expiry
, expire_memos
, NULL
);
1290 memoserv_finalize(void) {
1291 struct chanNode
*chan
;
1296 str
= "modules/memoserv";
1297 if (!(conf_node
= conf_get_data(str
, RECDB_OBJECT
))) {
1298 log_module(MS_LOG
, LOG_ERROR
, "config node `%s' is missing or has wrong type.", str
);
1302 str
= database_get_data(conf_node
, "bot", RECDB_QSTRING
);
1304 memoserv_conf
.bot
= GetUserH(str
);
1306 log_module(MS_LOG
, LOG_ERROR
, "database_get_data for memoserv_conf.bot failed!");
1308 if (autojoin_channels
&& memoserv_conf
.bot
) {
1309 for (i
= 0; i
< autojoin_channels
->used
; i
++) {
1310 chan
= AddChannel(autojoin_channels
->list
[i
], now
, "+nt", NULL
, NULL
);
1311 AddChannelUser(memoserv_conf
.bot
, chan
)->modes
|= MODE_CHANOP
;