ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/ermyth/modules/memoserv/read.C
Revision: 1.2
Committed: Sat Jul 21 01:29:09 2007 UTC (19 years, 3 months ago) by pippijn
Content type: text/plain
Branch: MAIN
Changes since 1.1: +2 -2 lines
Log Message:
- moved to new documentation system
- fixed small build error

File Contents

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