| 1 |
pippijn |
1.1 |
/* |
| 2 |
pippijn |
1.4 |
* Copyright © 2005-2007 Atheme Development Group |
| 3 |
pippijn |
1.2 |
* Rights to this code are as documented in doc/pod/license.pod. |
| 4 |
pippijn |
1.1 |
* |
| 5 |
|
|
* This file contains code for the Memoserv READ function |
| 6 |
|
|
* |
| 7 |
pippijn |
1.4 |
* $Id: read.C,v 1.3 2007-07-21 13:23:20 pippijn Exp $ |
| 8 |
pippijn |
1.1 |
*/ |
| 9 |
|
|
|
| 10 |
|
|
#include "atheme.h" |
| 11 |
pippijn |
1.4 |
#include <ermyth/module.h> |
| 12 |
pippijn |
1.1 |
#include <account/myuser.h> |
| 13 |
|
|
#include <account/mymemo.h> |
| 14 |
|
|
|
| 15 |
pippijn |
1.4 |
static char const rcsid[] = "$Id: read.C,v 1.3 2007-07-21 13:23:20 pippijn Exp $"; |
| 16 |
pippijn |
1.1 |
|
| 17 |
pippijn |
1.4 |
REGISTER_MODULE ("memoserv/read", false, "The Ermyth Team <http://ermyth.schmorp.de>"); |
| 18 |
pippijn |
1.1 |
|
| 19 |
|
|
#define MAX_READ_AT_ONCE 5 |
| 20 |
|
|
|
| 21 |
|
|
static void ms_cmd_read (sourceinfo_t *si, int parc, char *parv[]); |
| 22 |
|
|
|
| 23 |
pippijn |
1.4 |
command_t const ms_read = { "READ", N_("Reads a memo."), AC_NONE, 2, ms_cmd_read }; |
| 24 |
pippijn |
1.1 |
|
| 25 |
pippijn |
1.4 |
E cmdvec ms_cmdtree; |
| 26 |
|
|
E helpvec ms_helptree; |
| 27 |
pippijn |
1.1 |
|
| 28 |
pippijn |
1.4 |
bool |
| 29 |
|
|
_modinit (module *m) |
| 30 |
pippijn |
1.1 |
{ |
| 31 |
|
|
ms_cmdtree << ms_read; |
| 32 |
|
|
help_addentry (ms_helptree, "READ", "help/memoserv/read", NULL); |
| 33 |
pippijn |
1.4 |
|
| 34 |
|
|
return true; |
| 35 |
pippijn |
1.1 |
} |
| 36 |
|
|
|
| 37 |
|
|
void |
| 38 |
|
|
_moddeinit () |
| 39 |
|
|
{ |
| 40 |
|
|
ms_cmdtree >> ms_read; |
| 41 |
|
|
help_delentry (ms_helptree, "READ"); |
| 42 |
|
|
} |
| 43 |
|
|
|
| 44 |
|
|
static void |
| 45 |
|
|
ms_cmd_read (sourceinfo_t *si, int parc, char *parv[]) |
| 46 |
|
|
{ |
| 47 |
|
|
/* Misc structs etc */ |
| 48 |
|
|
myuser_t *tmu; |
| 49 |
|
|
mymemo_t *memo, *receipt; |
| 50 |
|
|
unsigned int i = 1, memonum = 0, numread = 0; |
| 51 |
|
|
char strfbuf[32]; |
| 52 |
|
|
struct tm tm; |
| 53 |
|
|
bool readnew; |
| 54 |
|
|
myuser_t::memo_vector::iterator mzit, mzit_end; |
| 55 |
|
|
|
| 56 |
|
|
/* Grab arg */ |
| 57 |
|
|
char *arg1 = parv[0]; |
| 58 |
|
|
|
| 59 |
|
|
if (!arg1) |
| 60 |
|
|
{ |
| 61 |
|
|
command_fail (si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "READ"); |
| 62 |
|
|
|
| 63 |
|
|
command_fail (si, fault_needmoreparams, _("Syntax: READ <memo number>")); |
| 64 |
|
|
return; |
| 65 |
|
|
} |
| 66 |
|
|
|
| 67 |
|
|
/* user logged in? */ |
| 68 |
|
|
if (si->smu == NULL) |
| 69 |
|
|
{ |
| 70 |
|
|
command_fail (si, fault_noprivs, _("You are not logged in.")); |
| 71 |
|
|
return; |
| 72 |
|
|
} |
| 73 |
|
|
|
| 74 |
|
|
/* Check to see if any memos */ |
| 75 |
|
|
if (si->smu->memos.empty ()) |
| 76 |
|
|
{ |
| 77 |
|
|
command_fail (si, fault_nosuch_key, _("You have no memos.")); |
| 78 |
|
|
return; |
| 79 |
|
|
} |
| 80 |
|
|
|
| 81 |
|
|
memonum = atoi (arg1); |
| 82 |
|
|
readnew = !strcasecmp (arg1, "NEW"); |
| 83 |
|
|
if (!readnew && !memonum) |
| 84 |
|
|
{ |
| 85 |
|
|
command_fail (si, fault_badparams, _("Invalid message index.")); |
| 86 |
|
|
return; |
| 87 |
|
|
} |
| 88 |
|
|
|
| 89 |
|
|
/* Check to see if memonum is greater than memocount */ |
| 90 |
|
|
if (memonum > si->smu->memos.size ()) |
| 91 |
|
|
{ |
| 92 |
|
|
command_fail (si, fault_nosuch_key, _("Invalid message index.")); |
| 93 |
|
|
return; |
| 94 |
|
|
} |
| 95 |
|
|
|
| 96 |
|
|
/* Go to reading memos */ |
| 97 |
|
|
for (mzit = si->smu->memos.begin (), mzit_end = si->smu->memos.end (); mzit != mzit_end; ++mzit) |
| 98 |
|
|
{ |
| 99 |
|
|
memo = *mzit; |
| 100 |
|
|
if (i == memonum || (readnew && !(memo->status & MEMO_READ))) |
| 101 |
|
|
{ |
| 102 |
|
|
tm = *localtime (&memo->sent); |
| 103 |
|
|
strftime (strfbuf, sizeof (strfbuf) - 1, "%b %d %H:%M:%S %Y", &tm); |
| 104 |
|
|
|
| 105 |
|
|
if (!(memo->status & MEMO_READ)) |
| 106 |
|
|
{ |
| 107 |
|
|
memo->status |= MEMO_READ; |
| 108 |
|
|
si->smu->memoct_new--; |
| 109 |
|
|
tmu = myuser_find (memo->sender); |
| 110 |
|
|
|
| 111 |
|
|
/* If the sender is logged in, tell them the memo's been read */ |
| 112 |
|
|
/* but not for channel memos */ |
| 113 |
|
|
if (memo->status & MEMO_CHANNEL) |
| 114 |
|
|
; |
| 115 |
|
|
else if (strcasecmp (memosvs.nick, memo->sender) && (tmu != NULL) && (!tmu->logins.empty())) |
| 116 |
pippijn |
1.4 |
tmu->notice (memosvs.nick, "%s has read your memo, which was sent at %s", si->smu->name, strfbuf); |
| 117 |
pippijn |
1.1 |
else |
| 118 |
|
|
{ |
| 119 |
|
|
/* If they have an account, their inbox is not full and they aren't memoserv */ |
| 120 |
|
|
if ((tmu != NULL) && (tmu->memos.size () < me.mdlimit) && strcasecmp (memosvs.nick, memo->sender)) |
| 121 |
|
|
{ |
| 122 |
|
|
/* Malloc and populate memo struct */ |
| 123 |
pippijn |
1.4 |
receipt = new mymemo_t; |
| 124 |
pippijn |
1.1 |
receipt->sent = NOW; |
| 125 |
|
|
receipt->status = 0; |
| 126 |
|
|
strlcpy (receipt->sender, memosvs.nick, NICKLEN); |
| 127 |
|
|
snprintf (receipt->text, MEMOLEN, "%s has read a memo from you sent at %s", si->smu->name, strfbuf); |
| 128 |
|
|
|
| 129 |
|
|
/* Attach to their memo list */ |
| 130 |
|
|
tmu->memos.insert (receipt); |
| 131 |
|
|
tmu->memoct_new++; |
| 132 |
|
|
} |
| 133 |
|
|
} |
| 134 |
|
|
} |
| 135 |
|
|
|
| 136 |
|
|
command_success_nodata (si, "\2Memo %d - Sent by %s, %s\2", i, memo->sender, strfbuf); |
| 137 |
|
|
|
| 138 |
|
|
command_success_nodata (si, "------------------------------------------"); |
| 139 |
|
|
|
| 140 |
|
|
command_success_nodata (si, "%s", memo->text); |
| 141 |
|
|
|
| 142 |
|
|
if (!readnew) |
| 143 |
|
|
return; |
| 144 |
|
|
if (++numread >= MAX_READ_AT_ONCE && si->smu->memoct_new > 0) |
| 145 |
|
|
{ |
| 146 |
|
|
command_success_nodata (si, _("Stopping command after %d memos."), numread); |
| 147 |
|
|
return; |
| 148 |
|
|
} |
| 149 |
|
|
} |
| 150 |
|
|
i++; |
| 151 |
|
|
} |
| 152 |
|
|
|
| 153 |
|
|
if (readnew && numread == 0) |
| 154 |
|
|
command_fail (si, fault_nosuch_key, _("You have no new memos.")); |
| 155 |
|
|
else if (readnew) |
| 156 |
|
|
command_success_nodata (si, _("Read %d memos."), numread); |
| 157 |
|
|
} |