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