]>
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 *memoserv.set
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." },
70 { "MSMSG_HOWTO_READ", "Use READ <ID> to read a message." },
71 { "MSMSG_CLEAN_INBOX", "You have $b%d$b or more messages, please clean out your inbox.\nUse READ <ID> to read a message." },
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" },
76 { "MSMSG_MEMO_HEAD", "Memo %u From $b%s$b, received on %s:" },
77 { "MSMSG_MEMO_RECIEPT", "$bRead Reciept$b requested, %s." },
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." },
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" },
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 LIST to list them." },
89 { "MSMSG_NEW_MESSAGE", "You have a new message from $b%s$b. Use LIST to see your messages." },
90 { "MSMSG_FULL_INBOX", "$b%s$b cannot recieve anymore memos as their inbox is full" },
91 { "MSMSG_DELETED_ALL", "Deleted all of your messages." },
92 { "MSMSG_USE_CONFIRM", "Please use DELETE * $bCONFIRM$b to delete $uall$u of your messages." },
94 { "MSMSG_STATUS_HIST_TOTAL", "I have $b%u$b history entries in my database." },
95 { "MSMSG_STATUS_TOTAL", "I have $b%u$b memos in my database." },
96 { "MSMSG_STATUS_EXPIRED", "$b%ld$b memos expired during the time I am awake." },
97 { "MSMSG_STATUS_SENT", "$b%ld$b memos have been sent." },
99 { "MSMSG_INVALID_OPTION", "$b%s$b is not a valid option." },
100 { "MSMSG_INVALID_BINARY", "$b%s$b is an invalid binary value." },
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" },
108 { "MSMSG_SET_OPTIONS", "$bMessaging Options$b" },
109 { "MSMSG_SET_OPTIONS_END", "-------------End of Options-------------" },
111 { "MSMSG_LIST_END", "--------------End of Memos--------------" },
112 { "MSMSG_BAR", "----------------------------------------"},
114 { "MSMSG_DEFCON_NO_NEW_MEMOS", "You cannot send new memos at this time, please try again soon." },
120 struct memo_account
*recipient
;
121 struct memo_account
*sender
;
125 unsigned int is_read
: 1;
126 unsigned int reciept
: 1;
130 struct memo_account
*recipient
;
131 struct memo_account
*sender
;
136 struct userNode
*memoserv
;
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]); \
145 DECLARE_LIST(memoList
, struct memo
*);
146 DEFINE_LIST(memoList
, struct memo
*);
147 DECLARE_LIST(historyList
, struct history
*);
148 DEFINE_LIST(historyList
, struct history
*);
150 /* memo_account.flags fields */
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
156 #define MEMO_USE_PRIVMSG 0x00000020
158 struct memo_account
{
159 struct handle_info
*handle
;
160 unsigned int flags
: 6;
162 struct memoList sent
;
163 struct memoList recvd
;
164 struct historyList hsent
;
165 struct historyList hrecvd
;
169 struct userNode
*bot
;
174 #define MEMOSERV_FUNC(NAME) MODCMD_FUNC(NAME)
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[])
176 typedef OPTION_FUNC(option_func_t
);
178 unsigned long memo_id
;
180 extern struct string_list
*autojoin_channels
;
181 const char *memoserv_module_deps
[] = { NULL
};
182 static struct module *memoserv_module
;
183 static struct log_type
*MS_LOG
;
184 static unsigned long memosSent
, memosExpired
;
185 static struct dict
*memos
; /* memo_account->handle->handle -> memo_account */
186 static struct dict
*historys
;
187 static dict_t memoserv_opt_dict
; /* contains option_func_t* */
189 static struct memo_account
*
190 memoserv_get_account(struct handle_info
*hi
)
192 struct memo_account
*ma
;
195 ma
= dict_find(memos
, hi
->handle
, NULL
);
198 ma
= calloc(1, sizeof(*ma
));
202 ma
->flags
= MEMO_NOTIFY_NEW
| MEMO_NOTIFY_LOGIN
| MEMO_USE_PRIVMSG
;
203 ma
->limit
= memoserv_conf
.limit
;
204 dict_insert(memos
, ma
->handle
->handle
, ma
);
205 dict_insert(historys
, ma
->handle
->handle
, ma
);
210 delete_memo(struct memo
*memo
)
212 memoList_remove(&memo
->recipient
->recvd
, memo
);
213 memoList_remove(&memo
->sender
->sent
, memo
);
219 delete_history(struct history
*history
)
221 historyList_remove(&history
->recipient
->hrecvd
, history
);
222 historyList_remove(&history
->sender
->hsent
, history
);
227 delete_memo_account(void *data
)
229 struct memo_account
*ma
= data
;
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
);
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
);
251 for (it
= dict_first(memos
); it
; it
= iter_next(it
)) {
252 struct memo_account
*acct
= iter_data(it
);
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
) {
264 for (it
= dict_first(historys
); it
; it
= iter_next(it
)) {
265 struct memo_account
*acct
= iter_data(it
);
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
);
279 expire_memos(UNUSED_ARG(void *data
))
281 if (memoserv_conf
.message_expiry
) {
283 timeq_add(now
+ memoserv_conf
.message_expiry
, expire_memos
, NULL
);
287 static struct history
*
288 add_history(time_t sent
, struct memo_account
*recipient
, struct memo_account
*sender
, unsigned long id
)
290 struct history
*history
;
292 history
= calloc(1, sizeof(*history
));
297 history
->recipient
= recipient
;
298 historyList_append(&recipient
->hrecvd
, history
);
299 history
->sender
= sender
;
300 historyList_append(&sender
->hsent
, history
);
301 history
->sent
= sent
;
308 add_memo(time_t sent
, struct memo_account
*recipient
, struct memo_account
*sender
, char *message
, int nfrom_read
)
311 struct history
*history
;
313 memo
= calloc(1, sizeof(*memo
));
322 memo
->recipient
= recipient
;
323 memoList_append(&recipient
->recvd
, memo
);
324 memo
->sender
= sender
;
325 memoList_append(&sender
->sent
, memo
);
327 memo
->message
= strdup(message
);
331 history
= add_history(sent
, recipient
, sender
, memo
->id
);
337 memoserv_can_send(struct userNode
*bot
, struct userNode
*user
, struct memo_account
*acct
)
339 extern struct userData
*_GetChannelUser(struct chanData
*channel
, struct handle_info
*handle
, int override
, int allow_suspended
);
340 struct userData
*dest
;
341 unsigned int i
= 0, match
= 0;
343 if (!user
->handle_info
)
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
;
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
);
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
)) {
366 send_message(user
, bot
, "MSMSG_CANNOT_SEND", acct
->handle
->handle
);
371 if (!(acct
->flags
& MEMO_DENY_NONCHANNEL
))
374 for (dest
= acct
->handle
->channels
; dest
; dest
= dest
->u_next
)
375 if (_GetChannelUser(dest
->channel
, user
->handle_info
, 1, 0))
378 send_message(user
, bot
, "MSMSG_CANNOT_SEND", acct
->handle
->handle
);
382 static struct memo
*find_memo(struct userNode
*user
, struct svccmd
*cmd
, struct memo_account
*ma
, const char *msgid
, unsigned int *id
)
385 if (!isdigit(msgid
[0])) {
387 reply("MSMSG_BAD_MESSAGE_ID", msgid
, ma
->recvd
.used
- 1);
389 reply("MSMSG_NO_MESSAGES");
392 memoid
= atoi(msgid
);
393 if (memoid
>= ma
->recvd
.used
) {
394 reply("MSMSG_NO_SUCH_MEMO");
397 return ma
->recvd
.list
[*id
= memoid
];
400 static MODCMD_FUNC(cmd_send
)
403 int reciept
= 0, inc
= 2;
404 struct handle_info
*hi
;
405 struct memo_account
*ma
, *sender
;
408 MEMOSERV_MIN_PARAMS(3);
410 if (checkDefCon(DEFCON_NO_NEW_MEMOS
) && !IsOper(user
)) {
411 reply("MSMSG_DEFCON_NO_NEW_MEMOS");
415 if (!(hi
= modcmd_get_handle_info(user
, argv
[1])))
418 if (!(sender
= memoserv_get_account(user
->handle_info
))
419 || !(ma
= memoserv_get_account(hi
))) {
420 reply("MSG_INTERNAL_FAILURE");
424 if (!(memoserv_can_send(cmd
->parent
->bot
, user
, ma
)))
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
++) {
438 /* Unknown mode. Give an error */
439 reply("MSMSG_UNKNOWN_SEND_FLAG", *flags
);
445 inc
= 2; /* Start of message is word 2 */
447 message
= unsplit_string(argv
+ inc
, argc
- inc
, NULL
);
448 memo
= add_memo(now
, ma
, sender
, message
, 1);
449 if ((reciept
== 1) || (ma
->flags
& MEMO_ALWAYS_RECIEPTS
))
452 if (ma
->flags
& MEMO_NOTIFY_NEW
) {
453 struct userNode
*other
;
455 for (other
= ma
->handle
->users
; other
; other
= other
->next_authed
)
456 send_message_type((ma
->flags
& MEMO_USE_PRIVMSG
)? MSG_TYPE_PRIVMSG
: MSG_TYPE_NOTICE
, other
, memoserv
? memoserv
: cmd
->parent
->bot
, "MSMSG_NEW_MESSAGE", user
->nick
);
459 reply("MSMSG_MEMO_SENT", ma
->handle
->handle
, memo_id
);
463 static MODCMD_FUNC(cmd_list
)
465 struct memo_account
*ma
;
471 if (!(ma
= memoserv_get_account(user
->handle_info
)))
474 reply("MSMSG_LIST_HEAD");
476 if(user
->handle_info
&& user
->handle_info
->userlist_style
!= HI_STYLE_CLEAN
)
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
);
483 reply("MSMSG_LIST_FORMAT", ii
, memo
->sender
->handle
->handle
, memo
->reciept
? "(r)" : "", posted
);
488 reply("MSMSG_CLEAN_INBOX", ii
);
490 reply("MSMSG_MEMOS_FOUND", ii
);
491 reply("MSMSG_HOWTO_READ");
494 reply("MSMSG_LIST_END");
499 static MODCMD_FUNC(cmd_history
)
501 struct memo_account
*ma
;
502 struct history
*history
;
509 if (!(ma
= memoserv_get_account(user
->handle_info
)))
512 reply("MSMSG_HISTORY_HEADER");
514 if(user
->handle_info
&& user
->handle_info
->userlist_style
!= HI_STYLE_CLEAN
)
517 for (it
= dict_first(historys
); it
; it
= iter_next(it
)) {
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
)) {
523 localtime_r(&history
->sent
, &tm
);
524 strftime(posted
, sizeof(posted
), "%I:%M %p, %m/%d/%Y", &tm
);
525 reply("MSMSG_HISTORY_FORMAT", history
->id
, history
->recipient
->handle
->handle
, posted
);
533 reply("MSMSG_MEMOS_FOUND", cc
);
535 reply("MSMSG_LIST_END");
540 static MODCMD_FUNC(cmd_read
)
542 struct memo_account
*ma
;
544 int rignore
= 0, brk
= 0, s
= 0;
550 if (!(ma
= memoserv_get_account(user
->handle_info
)))
552 if (!(memo
= find_memo(user
, cmd
, ma
, argv
[1], &memoid
)))
556 char *argtwo
= argv
[2];
581 localtime_r(&memo
->sent
, &tm
);
582 strftime(posted
, sizeof(posted
), "%I:%M %p, %m/%d/%Y", &tm
);
584 reply("MSMSG_MEMO_HEAD", memoid
, memo
->sender
->handle
->handle
, posted
);
585 send_message_type(4, user
, cmd
->parent
->bot
, "%s", memo
->message
);
589 if (ma
->flags
& MEMO_IGNORE_RECIEPTS
)
592 if (memo
->reciept
== 1) {
594 reply("MSMSG_MEMO_RECIEPT", rignore
? "ignoring" : "sending");
596 struct memo_account
*ma
;
597 struct memo_account
*sender
;
598 char content
[MAXLEN
];
600 ma
= memoserv_get_account(user
->handle_info
);
601 sender
= memoserv_get_account(memo
->sender
->handle
);
603 sprintf(content
, "%s has read your memo dated %s.", ma
->handle
->handle
, posted
);
605 memo
= add_memo(now
, sender
, ma
, content
, 1);
606 reply("MSMSG_MEMO_SENT", memob
->sender
->handle
->handle
, memo_id
);
608 if (sender
->flags
& MEMO_NOTIFY_NEW
) {
609 struct userNode
*other
;
611 for (other
= sender
->handle
->users
; other
; other
= other
->next_authed
)
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
);
621 static MODCMD_FUNC(cmd_delete
)
623 struct memo_account
*ma
;
627 MEMOSERV_MIN_PARAMS(2);
629 if (!(ma
= memoserv_get_account(user
->handle_info
)))
631 if (!irccasecmp(argv
[1], "*") || !irccasecmp(argv
[1], "all")) {
632 if ((argc
< 3) || irccasecmp(argv
[2], "confirm")) {
633 reply("MSMSG_USE_CONFIRM");
636 while (ma
->recvd
.used
)
637 delete_memo(ma
->recvd
.list
[0]);
638 reply("MSMSG_DELETED_ALL");
642 if (!(memo
= find_memo(user
, cmd
, ma
, argv
[1], &memoid
)))
645 reply("MSMSG_MEMO_DELETED", memoid
);
649 static MODCMD_FUNC(cmd_cancel
)
655 struct memo_account
*ma
;
657 MEMOSERV_MIN_PARAMS(2);
659 if (isdigit(argv
[1][0])) {
660 id
= strtoul(argv
[1], NULL
, 0);
662 reply("MSMSG_MEMO_CANCEL_NUMBER");
666 for (it
= dict_first(memos
); it
; it
= iter_next(it
)) {
668 for (ii
= 0; ii
< ma
->recvd
.used
; ++ii
) {
669 memo
= ma
->recvd
.list
[ii
];
671 if (id
== memo
->id
) {
672 if (!strcasecmp(memo
->sender
->handle
->handle
, user
->handle_info
->handle
)) {
674 reply("MSMSG_MEMO_READ", id
);
678 reply("MSMSG_MEMO_DELETED", id
);
682 reply("MSMSG_MEMO_DONT_OWN", id
);
689 reply("MSMSG_MEMO_CANT_LOCATE", id
);
693 static MODCMD_FUNC(cmd_expire
)
695 unsigned long old_expired
= memosExpired
;
697 reply("MSMSG_MESSAGES_EXPIRED", memosExpired
- old_expired
);
701 static MODCMD_FUNC(cmd_expiry
)
703 char interval
[INTERVALLEN
];
705 if (!memoserv_conf
.message_expiry
) {
706 reply("MSMSG_EXPIRY_OFF");
710 intervalString(interval
, memoserv_conf
.message_expiry
, user
->handle_info
);
711 reply("MSMSG_EXPIRY", interval
, memoserv_conf
.message_expiry
);
717 set_list(struct svccmd
*cmd
, struct userNode
*user
, struct handle_info
*hi
, int override
)
721 char *set_display
[] = {"AUTHNOTIFY", "NEWNOTIFY", "PRIVMSG", "PRIVATE", "LIMIT",
722 "IGNORERECIEPTS", "SENDRECIEPTS"};
724 reply("MSMSG_SET_OPTIONS");
727 /* Do this so options are presented in a consistent order. */
728 for (i
= 0; i
< ArrayLength(set_display
); ++i
)
729 if ((opt
= dict_find(memoserv_opt_dict
, set_display
[i
], NULL
)))
730 opt(cmd
, user
, hi
, override
, 0, NULL
);
731 reply("MSMSG_SET_OPTIONS_END");
734 static MODCMD_FUNC(cmd_set
)
736 struct handle_info
*hi
;
739 hi
= user
->handle_info
;
741 set_list(cmd
, user
, hi
, 0);
745 if (!(opt
= dict_find(memoserv_opt_dict
, argv
[1], NULL
))) {
746 reply("MSMSG_INVALID_OPTION", argv
[1]);
750 return opt(cmd
, user
, hi
, 0, argc
-1, argv
+1);
753 static MODCMD_FUNC(cmd_oset
)
755 struct handle_info
*hi
;
758 MEMOSERV_MIN_PARAMS(2);
760 if (!(hi
= get_victim_oper(cmd
, user
, argv
[1])))
764 set_list(cmd
, user
, hi
, 0);
768 if (!(opt
= dict_find(memoserv_opt_dict
, argv
[2], NULL
))) {
769 reply("MSMSG_INVALID_OPTION", argv
[2]);
773 return opt(cmd
, user
, hi
, 1, argc
-2, argv
+2);
776 static OPTION_FUNC(opt_newnotify
)
778 struct memo_account
*ma
;
781 if (!(ma
= memoserv_get_account(hi
)))
785 if (enabled_string(choice
)) {
786 ma
->flags
|= MEMO_NOTIFY_NEW
;
787 } else if (disabled_string(choice
)) {
788 ma
->flags
&= ~MEMO_NOTIFY_NEW
;
790 reply("MSMSG_INVALID_BINARY", choice
);
795 choice
= (ma
->flags
& MEMO_NOTIFY_NEW
) ? "on" : "off";
796 reply("MSMSG_SET_NEWNOTIFY", choice
);
800 static OPTION_FUNC(opt_privmsg
)
802 struct memo_account
*ma
;
805 if (!(ma
= memoserv_get_account(hi
)))
809 if (enabled_string(choice
)) {
810 ma
->flags
|= MEMO_USE_PRIVMSG
;
811 } else if (disabled_string(choice
)) {
812 ma
->flags
&= ~MEMO_USE_PRIVMSG
;
814 reply("MSMSG_INVALID_BINARY", choice
);
818 choice
= (ma
->flags
& MEMO_USE_PRIVMSG
) ? "on" : "off";
819 reply("MSMSG_SET_PRIVMSG", choice
);
823 static OPTION_FUNC(opt_authnotify
)
825 struct memo_account
*ma
;
828 if (!(ma
= memoserv_get_account(hi
)))
832 if (enabled_string(choice
)) {
833 ma
->flags
|= MEMO_NOTIFY_LOGIN
;
834 } else if (disabled_string(choice
)) {
835 ma
->flags
&= ~MEMO_NOTIFY_LOGIN
;
837 reply("MSMSG_INVALID_BINARY", choice
);
842 choice
= (ma
->flags
& MEMO_NOTIFY_LOGIN
) ? "on" : "off";
843 reply("MSMSG_SET_AUTHNOTIFY", choice
);
847 static OPTION_FUNC(opt_ignorereciepts
)
849 struct memo_account
*ma
;
852 if (!(ma
= memoserv_get_account(hi
)))
856 if (enabled_string(choice
)) {
857 ma
->flags
|= MEMO_IGNORE_RECIEPTS
;
858 } else if (disabled_string(choice
)) {
859 ma
->flags
&= ~MEMO_IGNORE_RECIEPTS
;
861 reply("MSMSG_INVALID_BINARY", choice
);
866 choice
= (ma
->flags
& MEMO_IGNORE_RECIEPTS
) ? "on" : "off";
867 reply("MSMSG_SET_IGNORERECIEPTS", choice
);
871 static OPTION_FUNC(opt_sendreciepts
)
873 struct memo_account
*ma
;
876 if (!(ma
= memoserv_get_account(hi
)))
880 if (enabled_string(choice
)) {
881 ma
->flags
|= MEMO_ALWAYS_RECIEPTS
;
882 } else if (disabled_string(choice
)) {
883 ma
->flags
&= ~MEMO_ALWAYS_RECIEPTS
;
885 reply("MSMSG_INVALID_BINARY", choice
);
890 choice
= (ma
->flags
& MEMO_ALWAYS_RECIEPTS
) ? "on" : "off";
891 reply("MSMSG_SET_SENDRECIEPTS", choice
);
895 static OPTION_FUNC(opt_private
)
897 struct memo_account
*ma
;
900 if (!(ma
= memoserv_get_account(hi
)))
904 if (enabled_string(choice
)) {
905 ma
->flags
|= MEMO_DENY_NONCHANNEL
;
906 } else if (disabled_string(choice
)) {
907 ma
->flags
&= ~MEMO_DENY_NONCHANNEL
;
909 reply("MSMSG_INVALID_BINARY", choice
);
914 choice
= (ma
->flags
& MEMO_DENY_NONCHANNEL
) ? "on" : "off";
915 reply("MSMSG_SET_PRIVATE", choice
);
919 static OPTION_FUNC(opt_limit
)
921 struct memo_account
*ma
;
924 if (!(ma
= memoserv_get_account(hi
)))
927 choice
= atoi(argv
[1]);
928 if (choice
> memoserv_conf
.limit
)
929 choice
= memoserv_conf
.limit
;
934 reply("MSMSG_SET_LIMIT", ma
->limit
);
938 static MODCMD_FUNC(cmd_status
)
940 struct memo_account
*ma
;
945 for (it
= dict_first(memos
); it
; it
= iter_next(it
)) {
947 for (ii
= 0; ii
< ma
->recvd
.used
; ++ii
)
951 for (it
= dict_first(historys
); it
; it
= iter_next(it
)) {
953 for (ii
= 0; ii
< ma
->hrecvd
.used
; ++ii
)
957 reply("MSMSG_STATUS_HIST_TOTAL", hc
);
958 reply("MSMSG_STATUS_TOTAL", mc
);
959 reply("MSMSG_STATUS_EXPIRED", memosExpired
);
960 reply("MSMSG_STATUS_SENT", memosSent
);
965 memoserv_conf_read(void)
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
);
976 str
= database_get_data(conf_node
, "limit", RECDB_QSTRING
);
977 memoserv_conf
.limit
= str
? atoi(str
) : 50;
979 str
= database_get_data(conf_node
, "message_expiry", RECDB_QSTRING
);
980 memoserv_conf
.message_expiry
= str
? ParseInterval(str
) : 60*24*30;
984 memoserv_user_read(const char *key
, struct record_data
*hir
)
987 struct memo_account
*ma
;
988 struct handle_info
*hi
;
990 if (!(hi
= get_handle_info(key
)))
993 ma
= dict_find(memos
, hi
->handle
, NULL
);
998 ma
= calloc(1, sizeof(*ma
));
1004 str
= database_get_data(hir
->d
.object
, KEY_FLAGS
, RECDB_QSTRING
);
1006 log_module(MS_LOG
, LOG_ERROR
, "Flags not present in memo %s; skipping", key
);
1009 ma
->flags
= strtoul(str
, NULL
, 0);
1011 str
= database_get_data(hir
->d
.object
, KEY_LIMIT
, RECDB_QSTRING
);
1013 log_module(MS_LOG
, LOG_ERROR
, "Limit not present in memo %s; skipping", key
);
1016 ma
->limit
= strtoul(str
, NULL
, 0);
1018 dict_insert(memos
, ma
->handle
->handle
, ma
);
1019 dict_insert(historys
, ma
->handle
->handle
, ma
);
1025 memoserv_memo_read(const char *key
, struct record_data
*hir
)
1028 struct handle_info
*sender
, *recipient
;
1033 if (hir
->type
!= RECDB_OBJECT
) {
1034 log_module(MS_LOG
, LOG_WARNING
, "Unexpected rectype %d for %s.", hir
->type
, key
);
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
);
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
);
1049 id
= strtoul(str
, NULL
, 0);
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
);
1056 } else if (!(recipient
= get_handle_info(str
))) {
1057 log_module(MS_LOG
, LOG_ERROR
, "Invalid recipient %s in memo %s; skipping", str
, key
);
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
);
1064 } else if (!(sender
= get_handle_info(str
))) {
1065 log_module(MS_LOG
, LOG_ERROR
, "Invalid sender %s in memo %s; skipping", str
, key
);
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
);
1074 memo
= add_memo(sent
, memoserv_get_account(recipient
), memoserv_get_account(sender
), str
, 0);
1075 if ((str
= database_get_data(hir
->d
.object
, KEY_READ
, RECDB_QSTRING
)))
1078 if ((str
= database_get_data(hir
->d
.object
, KEY_RECIEPT
, RECDB_QSTRING
)))
1087 memoserv_history_read(const char *key
, struct record_data
*hir
)
1090 struct handle_info
*sender
, *recipient
;
1091 struct history
*history
;
1095 if (hir
->type
!= RECDB_OBJECT
) {
1096 log_module(MS_LOG
, LOG_WARNING
, "Unexpected rectype %d for %s.", hir
->type
, key
);
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
);
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
);
1111 id
= strtoul(str
, NULL
, 0);
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
);
1116 } else if (!(recipient
= get_handle_info(str
))) {
1117 log_module(MS_LOG
, LOG_ERROR
, "Invalid recipient %s in history %s; skipping", str
, key
);
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
);
1124 } else if (!(sender
= get_handle_info(str
))) {
1125 log_module(MS_LOG
, LOG_ERROR
, "Invalid sender %s in history %s; skipping", str
, key
);
1129 history
= add_history(sent
, memoserv_get_account(recipient
), memoserv_get_account(sender
), id
);
1135 memoserv_saxdb_read(struct dict
*database
)
1137 struct dict
*section
;
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
));
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
));
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
));
1156 memoserv_write_users(struct saxdb_context
*ctx
, struct memo_account
*ma
)
1158 saxdb_start_record(ctx
, ma
->handle
->handle
, 0);
1160 saxdb_write_int(ctx
, KEY_FLAGS
, ma
->flags
);
1161 saxdb_write_int(ctx
, KEY_LIMIT
, ma
->limit
);
1163 saxdb_end_record(ctx
);
1168 memoserv_write_memos(struct saxdb_context
*ctx
, struct memo
*memo
)
1172 memset(str
, '\0', sizeof(str
));
1173 saxdb_start_record(ctx
, inttobase64(str
, memo
->id
, sizeof(str
)-1), 0);
1175 saxdb_write_int(ctx
, KEY_SENT
, memo
->sent
);
1176 saxdb_write_int(ctx
, KEY_ID
, memo
->id
);
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
);
1182 saxdb_write_int(ctx
, KEY_READ
, 1);
1185 saxdb_write_int(ctx
, KEY_RECIEPT
, 1);
1187 saxdb_end_record(ctx
);
1192 memoserv_write_history(struct saxdb_context
*ctx
, struct history
*history
)
1196 memset(str
, '\0', sizeof(str
));
1197 saxdb_start_record(ctx
, inttobase64(str
, history
->id
, sizeof(str
)-1), 0);
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
);
1204 saxdb_end_record(ctx
);
1209 memoserv_saxdb_write(struct saxdb_context
*ctx
)
1212 struct memo_account
*ma
;
1214 struct history
*history
;
1218 saxdb_start_record(ctx
, KEY_MAIN_ACCOUNTS
, 1);
1219 for (it
= dict_first(memos
); it
; it
= iter_next(it
)) {
1221 memoserv_write_users(ctx
, ma
);
1223 saxdb_end_record(ctx
);
1226 saxdb_start_record(ctx
, KEY_MAIN_MEMOS
, 1);
1227 for (it
= dict_first(memos
); it
; it
= iter_next(it
)) {
1229 for (ii
= 0; ii
< ma
->recvd
.used
; ++ii
) {
1230 memo
= ma
->recvd
.list
[ii
];
1231 memoserv_write_memos(ctx
, memo
);
1234 saxdb_end_record(ctx
);
1237 saxdb_start_record(ctx
, KEY_MAIN_HISTORY
, 1);
1238 for (it
= dict_first(historys
); it
; it
= iter_next(it
)) {
1240 for (ii
= 0; ii
< ma
->hrecvd
.used
; ++ii
) {
1241 history
= ma
->hrecvd
.list
[ii
];
1242 memoserv_write_history(ctx
, history
);
1245 saxdb_end_record(ctx
);
1251 memoserv_cleanup(void)
1254 dict_delete(historys
);
1258 memoserv_check_messages(struct userNode
*user
, UNUSED_ARG(struct handle_info
*old_handle
))
1260 unsigned int ii
, unseen
;
1261 struct memo_account
*ma
;
1264 if (!user
->uplink
->burst
) {
1265 if (!(ma
= memoserv_get_account(user
->handle_info
))
1266 || !(ma
->flags
& MEMO_NOTIFY_LOGIN
))
1268 for (ii
= unseen
= 0; ii
< ma
->recvd
.used
; ++ii
) {
1269 memo
= ma
->recvd
.list
[ii
];
1273 if (ma
->recvd
.used
&& memoserv
)
1274 if(unseen
) send_message_type((ma
->flags
& MEMO_USE_PRIVMSG
)? 1 : 0, user
, memoserv
, "MSMSG_MEMOS_INBOX", unseen
, ma
->recvd
.used
- unseen
);
1279 memoserv_rename_account(struct handle_info
*hi
, const char *old_handle
)
1281 struct memo_account
*ma
;
1282 if (!(ma
= dict_find(memos
, old_handle
, NULL
)))
1284 dict_remove2(memos
, old_handle
, 1);
1285 dict_insert(memos
, hi
->handle
, ma
);
1287 dict_remove2(historys
, old_handle
, 1);
1288 dict_insert(historys
, hi
->handle
, ma
);
1292 memoserv_unreg_account(UNUSED_ARG(struct userNode
*user
), struct handle_info
*handle
)
1294 dict_remove(memos
, handle
->handle
);
1295 dict_remove(historys
, handle
->handle
);
1301 MS_LOG
= log_register_type("MemoServ", "file:memoserv.log");
1303 historys
= dict_new();
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
);
1312 memoserv_module
= module_register("MemoServ", MS_LOG
, "mod-memoserv.help", NULL
);
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
);
1325 memoserv_opt_dict
= dict_new();
1326 dict_insert(memoserv_opt_dict
, "AUTHNOTIFY", opt_authnotify
);
1327 dict_insert(memoserv_opt_dict
, "NEWNOTIFY", opt_newnotify
);
1328 dict_insert(memoserv_opt_dict
, "PRIVMSG", opt_privmsg
);
1329 dict_insert(memoserv_opt_dict
, "PRIVATE", opt_private
);
1330 dict_insert(memoserv_opt_dict
, "IGNORERECIEPTS", opt_ignorereciepts
);
1331 dict_insert(memoserv_opt_dict
, "SENDRECIEPTS", opt_sendreciepts
);
1332 dict_insert(memoserv_opt_dict
, "LIMIT", opt_limit
);
1334 message_register_table(msgtab
);
1336 if (memoserv_conf
.message_expiry
)
1337 timeq_add(now
+ memoserv_conf
.message_expiry
, expire_memos
, NULL
);
1343 memoserv_finalize(void) {
1344 struct chanNode
*chan
;
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
);
1355 str
= database_get_data(conf_node
, "bot", RECDB_QSTRING
);
1357 memoserv
= memoserv_conf
.bot
;
1358 const char *modes
= conf_get_data("modules/memoserv/modes", RECDB_QSTRING
);
1359 memoserv
= AddService(str
, modes
? modes
: NULL
, "User-User Memorandum Services", NULL
);
1362 log_module(MS_LOG
, LOG_ERROR
, "database_get_data for memoserv_conf.bot failed!");
1366 if (autojoin_channels
&& memoserv
) {
1367 for (i
= 0; i
< autojoin_channels
->used
; i
++) {
1368 chan
= AddChannel(autojoin_channels
->list
[i
], now
, "+nt", NULL
, NULL
);
1369 AddChannelUser(memoserv
, chan
)->modes
|= MODE_CHANOP
;