/** * read.C: This file contains code for the Memoserv READ function * * Copyright © 2007 Pippijn van Steenhoven / The Ermyth Team * Rights to this code are as documented in COPYING. * * * Portions of this file were derived from sources bearing the following license: * Copyright © 2005-2007 Atheme Development Group * Rights to this code are as documented in doc/pod/license.pod. * * $Id: read.C,v 1.8 2007/09/22 14:27:27 pippijn Exp $ */ #include "atheme.h" #include #include #include #include static char const rcsid[] = "$Id: read.C,v 1.8 2007/09/22 14:27:27 pippijn Exp $"; REGISTER_MODULE ("memoserv/read", false, "The Ermyth Team "); #define MAX_READ_AT_ONCE 5 static void ms_cmd_read (sourceinfo_t *si, int parc, char *parv[]); command_t const ms_read = { "READ", N_("Reads a memo."), AC_NONE, 2, ms_cmd_read }; E cmdvec ms_cmdtree; E helpvec ms_helptree; bool _modinit (module *m) { ms_cmdtree << ms_read; help_addentry (ms_helptree, "READ", "help/memoserv/read", NULL); return true; } void _moddeinit () { ms_cmdtree >> ms_read; help_delentry (ms_helptree, "READ"); } static void ms_cmd_read (sourceinfo_t *si, int parc, char *parv[]) { /* Misc structs etc */ myuser_t *tmu; mymemo_t *memo, *receipt; unsigned int i = 1, memonum = 0, numread = 0; char strfbuf[32]; struct tm tm; bool readnew; myuser_t::memo_vector::iterator mzit, mzit_end; /* Grab arg */ char *arg1 = parv[0]; if (!arg1) { command_fail (si, fault::needmoreparams, STR_INSUFFICIENT_PARAMS, "READ"); command_fail (si, fault::needmoreparams, _("Syntax: READ ")); return; } /* user logged in? */ if (si->smu == NULL) { command_fail (si, fault::noprivs, _("You are not logged in.")); return; } /* Check to see if any memos */ if (si->smu->memos.empty ()) { command_fail (si, fault::nosuch_key, _("You have no memos.")); return; } memonum = atoi (arg1); readnew = !strcasecmp (arg1, "NEW"); if (!readnew && !memonum) { command_fail (si, fault::badparams, _("Invalid message index.")); return; } /* Check to see if memonum is greater than memocount */ if (memonum > si->smu->memos.size ()) { command_fail (si, fault::nosuch_key, _("Invalid message index.")); return; } /* Go to reading memos */ for (mzit = si->smu->memos.begin (), mzit_end = si->smu->memos.end (); mzit != mzit_end; ++mzit) { memo = *mzit; if (i == memonum || (readnew && !(memo->status & MEMO_READ))) { tm = *localtime (&memo->sent); strftime (strfbuf, sizeof (strfbuf) - 1, "%b %d %H:%M:%S %Y", &tm); if (!(memo->status & MEMO_READ)) { memo->status |= MEMO_READ; si->smu->memoct_new--; tmu = myuser_t::find (memo->sender); /* If the sender is logged in, tell them the memo's been read */ /* but not for channel memos */ if (memo->status & MEMO_CHANNEL) ; else if (strcasecmp (memosvs.nick, memo->sender) && (tmu != NULL) && (!tmu->logins.empty())) tmu->notice (memosvs.nick, "%s has read your memo, which was sent at %s", si->smu->name, strfbuf); else { /* If they have an account, their inbox is not full and they aren't memoserv */ if ((tmu != NULL) && (tmu->memos.size () < me.mdlimit) && strcasecmp (memosvs.nick, memo->sender)) { /* Malloc and populate memo struct */ receipt = new mymemo_t; receipt->sent = NOW; receipt->status = 0; strlcpy (receipt->sender, memosvs.nick, NICKLEN); snprintf (receipt->text, MEMOLEN, "%s has read a memo from you sent at %s", si->smu->name, strfbuf); /* Attach to their memo list */ tmu->memos.insert (receipt); tmu->memoct_new++; } } } command_success_nodata (si, "\2Memo %d - Sent by %s, %s\2", i, memo->sender, strfbuf); command_success_nodata (si, "------------------------------------------"); command_success_nodata (si, "%s", memo->text); if (!readnew) return; if (++numread >= MAX_READ_AT_ONCE && si->smu->memoct_new > 0) { command_success_nodata (si, _("Stopping command after %d memos."), numread); return; } } i++; } if (readnew && numread == 0) command_fail (si, fault::nosuch_key, _("You have no new memos.")); else if (readnew) command_success_nodata (si, _("Read %d memos."), numread); }