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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
|
/*
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/ipc.h>
#include <filter/url.h>
#include <filter/string.h>
#include <filter/date.h>
#include <database/sqlite.h>
#include <webserver/request.h>
#include <database/logic.h>
// Database resilience: Stored in plain file system.
Database_Ipc::Database_Ipc (Webserver_Request& webserver_request):
m_webserver_request (webserver_request)
{
}
void Database_Ipc::trim ()
{
std::vector <Database_Ipc_Item> data = readData ();
for (auto & record : data) {
if (record.user == "") {
deleteMessage (record.rowid);
}
}
int now = filter::date::seconds_since_epoch ();
std::vector <std::string> files = filter_url_scandir (folder ());
for (std::string item : files) {
std::string path = file(item);
int time = filter_url_file_modification_time (path);
int age_seconds = now - time;
if (age_seconds > 3600) {
filter_url_unlink (path);
}
}
}
void Database_Ipc::storeMessage (std::string user, std::string channel, std::string command, std::string message)
{
// Load entire database into memory.
std::vector <Database_Ipc_Item> data = readData ();
// Gather information about records to delete.
std::vector <int> deletes;
if (channel == "") {
for (auto & record : data) {
if ((record.user == user) && (record.channel == channel) && (record.command == command)) {
deletes.push_back (record.rowid);
}
}
}
// Write new information.
int rowid = getNextId (data);
writeRecord (rowid, user, channel, command, message);
// Actually delete the records.
// Do this after writing new data, to be sure there is always some data on disk.
for (auto& erase : deletes) {
deleteMessage (erase);
}
}
// Retrieves a message if there is any.
// Returns an object with the data.
// The rowid is 0 if there was nothing,
// Else the object's properties are set properly.
Database_Ipc_Message Database_Ipc::retrieveMessage (int id, std::string user, std::string channel, std::string command)
{
int highestId = 0;
std::string hitChannel = "";
std::string hitCommand = "";
std::string hitMessage = "";
std::vector <Database_Ipc_Item> data = readData ();
for (auto & record : data) {
// Selection condition 1: The database record has a message identifier younger than the calling identifier.
int recordid = record.rowid;
if (recordid > id) {
// Selection condition 2: Channel matches calling channel, or empty channel.
std::string recordchannel = record.channel;
if ((recordchannel == channel) || (recordchannel == "")) {
// Selection condition 3: Record user matches calling user, or empty user.
std::string recorduser = record.user;
if ((recorduser == user) || (recorduser == "")) {
// Selection condition 4: Matching command.
std::string recordcommand = record.command;
if (recordcommand == command) {
if (recordid > highestId) {
highestId = recordid;
hitChannel = recordchannel;
hitCommand = recordcommand;
hitMessage = filter_url_file_get_contents (file (record.file));
}
}
}
}
}
}
Database_Ipc_Message message = Database_Ipc_Message ();
if (highestId) {
message.id = highestId;
message.channel = hitChannel;
message.command = hitCommand;
message.message = hitMessage;
}
return message;
}
void Database_Ipc::deleteMessage (int id)
{
std::vector <Database_Ipc_Item> data = readData ();
for (auto & record : data) {
if (record.rowid == id) {
filter_url_unlink (file (record.file));
}
}
}
std::string Database_Ipc::getFocus ()
{
const std::string& user = m_webserver_request.session_logic ()->get_username ();
int highestId = 0;
std::string hitMessage = "";
std::vector <Database_Ipc_Item> data = readData ();
for (auto & record : data) {
int recordid = record.rowid;
// Conditions: Command is "focus", and matching user.
if (record.command == "focus") {
if (record.user == user) {
if (recordid > highestId) {
highestId = recordid;
hitMessage = filter_url_file_get_contents (file (record.file));
}
}
}
}
if (highestId) return hitMessage;
// No focus found: Return Genesis 1:1.
return "1.1.1";
}
Database_Ipc_Message Database_Ipc::getNote ()
{
const std::string& user = m_webserver_request.session_logic ()->get_username ();
int highestId = 0;
std::string hitMessage = "";
std::vector <Database_Ipc_Item> data = readData ();
for (auto & record : data) {
int recordid = record.rowid;
// Conditions: Command is "opennote", and matching user.
if (record.command == "opennote") {
if (record.user == user) {
if (recordid > highestId) {
highestId = recordid;
hitMessage = filter_url_file_get_contents (file (record.file));
}
}
}
}
Database_Ipc_Message note = Database_Ipc_Message ();
if (highestId) {
note.id = highestId;
note.message = hitMessage;
}
return note;
}
bool Database_Ipc::getNotesAlive ()
{
const std::string& user = m_webserver_request.session_logic ()->get_username ();
int highestId = 0;
std::string hitMessage = "";
std::vector <Database_Ipc_Item> data = readData ();
for (auto & record : data) {
int recordid = record.rowid;
// Conditions: Command is "notesalive", and matching user.
if (record.command == "notesalive") {
if (record.user == user) {
if (recordid > highestId) {
highestId = recordid;
hitMessage = filter_url_file_get_contents (file (record.file));
}
}
}
}
if (highestId) return filter::strings::convert_to_bool (hitMessage);
return false;
}
std::string Database_Ipc::folder ()
{
return filter_url_create_root_path ({database_logic_databases (), "ipc"});
}
std::string Database_Ipc::file (std::string file)
{
return filter_url_create_path ({folder (), file});
}
// Reads most fields of all data in this database.
// Returns an array containing the above.
// The fields are those that can be obtained from the name of the files.
// One file is one entry in the database.
// The filename looks like this: rowid__user__channel__command
std::vector <Database_Ipc_Item> Database_Ipc::readData ()
{
std::vector <Database_Ipc_Item> data;
std::vector <std::string> files = filter_url_scandir (folder ());
for (std::string file : files) {
std::vector <std::string> explosion = filter::strings::explode (file, '_');
if (explosion.size () == 7) {
Database_Ipc_Item item = Database_Ipc_Item ();
item.file = file;
item.rowid = filter::strings::convert_to_int (explosion [0]);
item.user = explosion [2];
item.channel = explosion [4];
item.command = explosion [6];
data.push_back (item);
}
}
return data;
}
void Database_Ipc::writeRecord (int rowid, std::string user, std::string channel, std::string command, std::string message)
{
std::string filename = std::to_string (rowid) + "__" + user + "__" + channel + "__" + command;
filename = file (filename);
filter_url_file_put_contents (filename, message);
}
// Returns the next available row identifier.
int Database_Ipc::getNextId (const std::vector <Database_Ipc_Item> & data)
{
int id = 0;
for (auto & record : data) {
if (record.rowid > id) id = record.rowid;
}
id++;
return id;
}
|