ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/ermyth/modules/memoserv/read.C
Revision: 1.8
Committed: Sat Sep 22 14:27:27 2007 UTC (19 years, 1 month ago) by pippijn
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.7: +4 -3 lines
Log Message:
split up ermyth into ermyth-modules, libermyth (currently just ermyth-util) and ermyth-core

File Contents

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