File: mail.cpp

package info (click to toggle)
bibledit-cloud 5.1.036-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 250,636 kB
  • sloc: xml: 915,934; ansic: 261,349; cpp: 92,628; javascript: 32,542; sh: 4,915; makefile: 586; php: 69
file content (220 lines) | stat: -rw-r--r-- 6,076 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
Copyright (©) 2003-2025 Teus Benschop.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/


#include <database/mail.h>
#include <filter/string.h>
#include <database/sqlite.h>
#include <webserver/request.h>
#include <database/logs.h>
#include <filter/date.h>


// Handles mail sent from Bibledit to the users.
// Database resilience: It stores the emails to be sent shortly after.
// The database is not very often written to.
// The risk of corruption is low and acceptable.


constexpr const auto mail {"mail"};


Database_Mail::Database_Mail (Webserver_Request& webserver_request):
m_webserver_request (webserver_request)
{
}


Database_Mail::~Database_Mail ()
{
}


void Database_Mail::create ()
{
  SqliteDatabase sql (mail);
  sql.add ("CREATE TABLE IF NOT EXISTS mail ("
           " username text,"
           " timestamp integer,"
           " subject text,"
           " body text,"
           " retry integer"
           ");");
  sql.execute ();
}


void Database_Mail::optimize ()
{
  SqliteDatabase sql (mail);
  sql.add ("VACUUM;");
  sql.execute ();
}


void Database_Mail::trim ()
{
  const int time = filter::date::seconds_since_epoch () - 2592000; // Remove entries after 30 days.
  SqliteDatabase sql (mail);
  sql.add ("DELETE FROM mail WHERE timestamp <");
  sql.add (time);
  sql.add (";");
  sql.execute ();
  sql.set_sql ("DELETE FROM mail WHERE retry > 10;");
  sql.execute();
}


// Send email.
// to: The email address.
// subject: The subject.
// body: The body.
// time: Normally not given, but if given, it indicates the time stamp for sending this email.
void Database_Mail::send (std::string to, std::string subject, std::string body, int time)
{
  if (time == 0) time = filter::date::seconds_since_epoch ();
  SqliteDatabase sql (mail);
  sql.add ("INSERT INTO mail VALUES (");
  sql.add (to);
  sql.add (",");
  sql.add (time);
  sql.add (",");
  sql.add (subject);
  sql.add (",");
  sql.add (body);
  sql.add (", 0);");
  sql.execute ();
}


// Get number of mails for the current user.
int Database_Mail::getMailCount ()
{
  const std::string& user = m_webserver_request.session_logic ()->get_username();
  SqliteDatabase sql (mail);
  sql.add ("SELECT count(*) FROM mail WHERE username =");
  sql.add (user);
  sql.add (";");
  const std::vector <std::string> result = sql.query () ["count(*)"];
  if (!result.empty ()) {
    return filter::strings::convert_to_int (result.at(0));
  }
  return 0;
}


// Get the mails of the current user.
std::vector <Database_Mail_User> Database_Mail::getMails ()
{
  std::vector <Database_Mail_User> mails;
  const std::string& user = m_webserver_request.session_logic ()->get_username();
  SqliteDatabase sql (mail);
  sql.add ("SELECT rowid, timestamp, subject FROM mail WHERE username =");
  sql.add (user);
  sql.add ("ORDER BY timestamp DESC;");
  std::map <std::string, std::vector <std::string> > result = sql.query ();
  const std::vector <std::string> rowids = result ["rowid"];
  const std::vector <std::string> timestamps = result ["timestamp"];
  const std::vector <std::string> subjects = result ["subject"];
  for (unsigned int i = 0; i < rowids.size(); i++) {
    Database_Mail_User db_mail = Database_Mail_User ();
    db_mail.rowid = filter::strings::convert_to_int (rowids [i]);
    db_mail.timestamp = filter::strings::convert_to_int (timestamps [i]);
    db_mail.subject = subjects [i];
    mails.push_back (db_mail);
  }
  return mails;
}


// Delete a mail.
void Database_Mail::erase (int id)
{
  SqliteDatabase sql (mail);
  sql.add ("DELETE FROM mail WHERE rowid =");
  sql.add (id);
  sql.add (";");
  sql.execute ();
}


// Get a mail.
Database_Mail_Item Database_Mail::get (int id)
{
  SqliteDatabase sql (mail);
  sql.add ("SELECT username, subject, body FROM mail WHERE rowid =");
  sql.add (id);
  sql.add (";");
  std::map <std::string, std::vector <std::string> > result = sql.query ();
  Database_Mail_Item item = Database_Mail_Item ();
  if (!result.empty ()) {
    item.username = result ["username"] [0];
    item.subject = result ["subject"] [0];
    item.body = result ["body"] [0];
  }
  return item;
}


// Get ids of all mails ready for sending.
std::vector <int> Database_Mail::getMailsToSend ()
{
  std::vector <int> ids;
  const int timestamp = filter::date::seconds_since_epoch ();
  SqliteDatabase sql (mail);
  sql.add ("SELECT rowid FROM mail WHERE timestamp <=");
  sql.add (timestamp);
  sql.add (";");
  const std::vector <std::string> result = sql.query () ["rowid"];
  for (const auto& id : result) {
    ids.push_back (filter::strings::convert_to_int (id));
  }
  return ids;
}


// Postpones a mail till later.
// Used for retrying after failure to send the mail.  
void Database_Mail::postpone (int id)
{
  SqliteDatabase sql (mail);
  sql.add ("UPDATE mail SET retry = retry + 1 WHERE rowid =");
  sql.add (id);
  sql.add (";");
  sql.execute();
  sql.clear();
  sql.add ("UPDATE mail SET timestamp = timestamp + retry * 900 WHERE rowid =");
  sql.add (id);
  sql.add (";");
  sql.execute ();
}


// Get the row IDs of all mails in the database.
std::vector <int> Database_Mail::getAllMails ()
{
  std::vector <int> rowids {};
  SqliteDatabase sql (mail);
  sql.add ("SELECT rowid FROM mail;");
  const std::vector <std::string> result = sql.query () ["rowid"];
  for (const auto& rowid : result) {
    const int id = filter::strings::convert_to_int (rowid);
    rowids.push_back (id);
  }
  return rowids;
}