Debian Codegraph @

src/bnetd/mail.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2001,2006 Dizzy
3 * Copyright (C) 2004 Donny Redmond ([email protected])
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19 #include "common/setup_before.h"
20 #include "mail.h"
21
22 #include <sstream>
23 #include <fstream>
24 #include <iomanip>
25 #include <cstdio>
26 #include <cstring>
27 #include <cstdlib>
28 #include <cerrno>
29 #include <algorithm>
30 #include <vector>
31
32 #include "compat/strcasecmp.h"
33 #include "compat/mkdir.h"
34 #include "compat/statmacros.h"
35 #include "common/eventlog.h"
36 #include "common/xalloc.h"
37 #include "common/xstring.h"
38 #include "account.h"
39 #include "message.h"
40 #include "prefs.h"
41 #include "connection.h"
42 #include "helpfile.h"
43 #include "command.h"
44 #include "i18n.h"
45 #include "common/setup_after.h"
46
47
48 namespace pvpgn
49 {
50
51 namespace bnetd
52 {
53
54 static int identify_mail_function(const std::string&);
55 static void mail_usage(t_connection*);
56 static void mail_func_send(t_connection*, char const *, char const *);
57 static void mail_func_read(t_connection*, std::string);
58 static void mail_func_delete(t_connection*, std::string);
59 static unsigned get_mail_quota(t_account *);
60
61 /* Mail API */
62 /* for now this functions are only for internal use */
63
64 Mail::Mail(const std::string& sender, const std::string& message, const std::time_t timestamp)
65 :sender_(sender), message_(message), timestamp_(timestamp)
66 {
67 }
68
69 Mail::~Mail() throw()
70 {
71 }
72
73 const std::string&
74 Mail::sender() const
75 {
76 return sender_;
77 }
78
79 const std::string&
80 Mail::message() const
81 {
82 return message_;
83 }
84
85 const std::time_t&
86 Mail::timestamp() const
87 {
88 return timestamp_;
89 }
90
91 Mailbox::Mailbox(unsigned uid_)
92 :uid(uid_), path(buildPath(prefs_get_maildir())), mdir(path, true)
93 {
94 }
95
96 std::string
97 Mailbox::buildPath(const std::string& root) const
98 {
99 std::ostringstream ostr;
100 ostr << root << "/" << std::setfill('0') << std::setw(6) << uid;
101 return ostr.str();
102 }
103
104 void
105 Mailbox::createOpenDir()
106 {
107 p_mkdir(prefs_get_maildir(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
108 p_mkdir(path.c_str(), S_IRWXU | S_IXGRP | S_IRGRP | S_IROTH | S_IXOTH);
109 mdir.open(path, false);
110 }
111
112 Mailbox::~Mailbox() throw()
113 {
114 }
115
116 unsigned
117 Mailbox::size() const
118 {
119 mdir.rewind();
120
121 unsigned count = 0;
122 while ((mdir.read())) count++;
123 return count;
124 }
125
126 bool
127 Mailbox::empty() const
128 {
129 mdir.rewind();
130
131 if (mdir.read()) return false;
132 return true;
133 }
134
135 void
136 Mailbox::deliver(const std::string& sender, const std::string& mess)
137 {
138 if (!mdir)
139 try {
140 createOpenDir();
141 }
142 catch (const Directory::OpenError&) {
143 ERROR1("could not (re)open directory: '{}'", path.c_str());
144 throw DeliverError("could not (re)open directory: " + path);
145 }
146
147 std::ostringstream ostr;
148 ostr << path << '/' << std::setfill('0') << std::setw(15) << static_cast<unsigned long>(std::time(0));
149
150 std::ofstream fd(ostr.str().c_str());
151 if (!fd) {
152 ERROR1("error opening mail file. check permissions: '{}'", path.c_str());
153 throw DeliverError("error opening mail file. check permissions: " + path);
154 }
155
156 fd << sender << std::endl << mess << std::endl;
157 }
158
159 Mail
160 Mailbox::read(const std::string& fname, const std::time_t& timestamp) const
161 {
162 std::ifstream fd(fname.c_str());
163 if (!fd) {
164 ERROR1("error opening mail file: {}", fname.c_str());
165 throw ReadError("error opening mail file: " + fname);
166 }
167
168 std::string sender;
169 std::getline(fd, sender);
170
171 std::string message;
172 std::getline(fd, message);
173
174 return Mail(sender, message, timestamp);
175 }
176
177 Mail
178 Mailbox::read(unsigned int idx) const
179 {
180 mdir.rewind();
181 const char * dentry = mdir.read();
182 for (unsigned i = 0; i < idx && (dentry = mdir.read());)
183 if (dentry[0] != '.') ++i;
184 if (!dentry) {
185 INFO0("mail not found");
186 throw ReadError("mail not found");
187 }
188
189 std::string fname(path);
190 fname += '/';
191 fname += dentry;
192
193 return read(fname, std::atoi(dentry));
194 }
195
196 void
197 Mailbox::readAll(MailList& dest) const
198 {
199 mdir.rewind();
200
201 std::string fname(path);
202 fname += '/';
203
204 const char* dentry;
205 while ((dentry = mdir.read())) {
206 if (dentry[0] == '.') continue;
207
208 try {
209 dest.push_back(read(fname + dentry, std::atoi(dentry)));
210 }
211 catch (const ReadError&) {
212 /* ignore ReadError in reading a specific message and try to read as much as we can */
213 }
214 }
215 }
216
217 void
218 Mailbox::erase(unsigned int idx)
219 {
220 mdir.rewind();
221 const char* dentry = mdir.read();
222 for (unsigned i = 0; i < idx && (dentry = mdir.read());)
223 if (dentry[0] != '.') ++i;
224
225 if (!dentry) {
226 WARN0("index out of range");
227 return;
228 }
229
230 std::string fname(path);
231 fname += '/';
232 fname += dentry;
233
234 if (std::remove(fname.c_str()) < 0)
235 INFO2("could not remove file \"{}\" (std::remove: {})", fname.c_str(), std::strerror(errno));
236 }
237
238 void
239 Mailbox::clear()
240 {
241 std::string fname(path);
242 fname += '/';
243
244 mdir.rewind();
245
246 const char* dentry;
247 while ((dentry = mdir.read())) {
248 std::remove((fname + dentry).c_str());
249 }
250 }
251
252 extern int handle_mail_command(t_connection * c, char const * text)
253 {
254 if (!prefs_get_mail_support()) {
255 message_send_text(c, message_type_error, c, localize(c, "This server has NO mail support."));
256 return -1;
257 }
258
259 char const *subcommand;
260
261 std::vector<std::string> args = split_command(text, 3);
262 if (args[1].empty())
263 {
264 describe_command(c, args[0].c_str());
265 return -1;
266 }
267 subcommand = args[1].c_str(); // subcommand
268
269 switch (identify_mail_function(subcommand)) {
270 case MAIL_FUNC_SEND:
271 if (args[3].empty())
272 {
273 describe_command(c, args[0].c_str());
274 return -1;
275 }
276 mail_func_send(c, args[2].c_str(), args[3].c_str());
277 break;
278 case MAIL_FUNC_READ:
279 mail_func_read(c, args[2]);
280 break;
281 case MAIL_FUNC_DELETE:
282 if (args[2].empty())
283 {
284 describe_command(c, args[0].c_str());
285 return -1;
286 }
287 mail_func_delete(c, args[2]);
288 break;
289 default:
290 describe_command(c, args[0].c_str());
291 }
292
293 return 0;
294 }
295
296 static int identify_mail_function(const std::string& funcstr)
297 {
298 if (funcstr.empty() || !strcasecmp(funcstr.c_str(), "read") || !strcasecmp(funcstr.c_str(), "r"))
299 return MAIL_FUNC_READ;
300 if (!strcasecmp(funcstr.c_str(), "send") || !strcasecmp(funcstr.c_str(), "s"))
301 return MAIL_FUNC_SEND;
302 if (!strcasecmp(funcstr.c_str(), "delete") || !strcasecmp(funcstr.c_str(), "del"))
303 return MAIL_FUNC_DELETE;
304
305 return MAIL_FUNC_UNKNOWN;
306 }
307
308 static unsigned get_mail_quota(t_account * user)
309 {
310 int quota;
311 char const * user_quota = account_get_strattr(user, "BNET\\auth\\mailquota");
312
313 if (!user_quota) quota = prefs_get_mail_quota();
314 else {
315 quota = std::atoi(user_quota);
316 if (quota < 1) quota = 1;
317 if (quota > MAX_MAIL_QUOTA) quota = MAX_MAIL_QUOTA;
318 }
319
320 return quota;
321 }
322
323 static void mail_func_send(t_connection * c, char const * receiver, char const * message)
324 {
325 if (!c) {
326 ERROR0("got NULL connection");
327 return;
328 }
329
330 t_account * recv = accountlist_find_account(receiver);
331 if (!recv) {
332 message_send_text(c, message_type_error, c, localize(c, "Receiver UNKNOWN!"));
333 return;
334 }
335
336 Mailbox mbox(account_get_uid(recv));
337 if (get_mail_quota(recv) <= mbox.size()) {
338 message_send_text(c, message_type_error, c, localize(c, "Receiver has reached his mail quota. Your message will NOT be sent."));
339 return;
340 }
341
342 try {
343 mbox.deliver(conn_get_username(c), message);
344 message_send_text(c, message_type_info, c, localize(c, "Your mail has been sent successfully."));
345 }
346 catch (const Mailbox::DeliverError&) {
347 message_send_text(c, message_type_error, c, localize(c, "There was an error completing your request!"));
348 }
349 }
350
351 bool NonNumericChar(const char ch)
352 {
353 if (ch < '0' || ch > '9') return true;
354 return false;
355 }
356
357 static void mail_func_read(t_connection * c, std::string token)
358 {
359 if (!c) {
360 ERROR0("got NULL connection");
361 return;
362 }
363
364 t_account * user = conn_get_account(c);
365 Mailbox mbox(account_get_uid(user));
366
367 if (token.empty()) { /* user wants to see the mail summary */
368 if (mbox.empty()) {
369 message_send_text(c, message_type_info, c, localize(c, "You have no mail."));
370 return;
371 }
372
373 MailList mlist;
374 mbox.readAll(mlist);
375
376 std::ostringstream ostr;
377 ostr << "You have " << mbox.size() << " messages. Your mail quote is set to " << get_mail_quota(user) << '.';
378 message_send_text(c, message_type_info, c, ostr.str().c_str());
379 message_send_text(c, message_type_info, c, localize(c, "ID Sender Date"));
380 message_send_text(c, message_type_info, c, "-------------------------------------");
381
382 for (MailList::const_iterator it(mlist.begin()); it != mlist.end(); ++it) {
383 ostr.str("");
384 ostr << std::setfill('0') << std::setw(2) << std::right << (it - mlist.begin()) << " "
385 << std::setfill(' ') << std::setw(14) << std::left << it->sender() << ' ';
386 char buff[128];
387 std::strftime(buff, sizeof(buff), "%a %b %d %H:%M:%S %Y", std::localtime(&it->timestamp()));
388 ostr << buff;
389 message_send_text(c, message_type_info, c, ostr.str().c_str());
390 }
391
392 message_send_text(c, message_type_info, c, localize(c, "Use /mail read <ID> to read the content of any message"));
393 }
394 else { /* user wants to read a message */
395 if (std::find_if(token.begin(), token.end(), NonNumericChar) != token.end()) {
396 message_send_text(c, message_type_error, c, localize(c, "Invalid index. Please use /mail read <index> where <index> is a number."));
397 return;
398 }
399
400 try {
401 unsigned idx = std::atoi(token.c_str());
402 Mail mail(mbox.read(idx));
403
404 std::ostringstream ostr;
405 ostr << "Message #" << idx << " from " << mail.sender() << " on ";
406 char buff[128];
407 std::strftime(buff, sizeof(buff), "%a %b %d %H:%M:%S %Y", std::localtime(&mail.timestamp()));
408 ostr << buff << ':';
409 message_send_text(c, message_type_info, c, ostr.str().c_str());
410 message_send_text(c, message_type_info, c, mail.message().c_str());
411 }
412 catch (const Mailbox::ReadError&) {
413 message_send_text(c, message_type_error, c, localize(c, "There was an error completing your request."));
414 }
415 }
416 }
417
418 static void mail_func_delete(t_connection * c, std::string token)
419 {
420 if (!c) {
421 ERROR0("got NULL connection");
422 return;
423 }
424
425 if (token.empty()) {
426 message_send_text(c, message_type_error, c, localize(c, "Please specify which message to delete. Use the following syntax: /mail delete {<index>|all} ."));
427 return;
428 }
429
430 t_account * user = conn_get_account(c);
431 Mailbox mbox(account_get_uid(user));
432
433 if (token == "all") {
434 mbox.clear();
435 message_send_text(c, message_type_info, c, localize(c, "Successfully deleted messages."));
436 }
437 else {
438 if (std::find_if(token.begin(), token.end(), NonNumericChar) != token.end()) {
439 message_send_text(c, message_type_error, c, localize(c, "Invalid index. Please use /mail delete {<index>|all} where <index> is a number."));
440 return;
441 }
442
443 mbox.erase(std::atoi(token.c_str()));
444 message_send_text(c, message_type_info, c, localize(c, "Succesfully deleted message."));
445 }
446 }
447
448
449 extern unsigned check_mail(t_connection const * c)
450 {
451 if (!c) {
452 ERROR0("got NULL connection");
453 return 0;
454 }
455
456 if (t_account * account = conn_get_account(c))
457 return Mailbox(account_get_uid(account)).size();
458
459 return 0;
460 }
461
462 }
463
464 }