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