2018-08-30 18:04:56 +02:00
|
|
|
/* This file is part of whyblocked.
|
|
|
|
* Copyright © 2018 tastytea <tastytea@tastytea.de>
|
|
|
|
*
|
|
|
|
* 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, version 3.
|
|
|
|
*
|
|
|
|
* 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
2018-10-18 21:31:05 +02:00
|
|
|
#include <QCoreApplication>
|
|
|
|
#include <QTranslator>
|
|
|
|
#include <QLocale>
|
|
|
|
#include <QLibraryInfo>
|
2018-08-30 18:04:56 +02:00
|
|
|
#include "version.hpp"
|
|
|
|
#include "whyblocked.hpp"
|
2018-10-18 21:31:05 +02:00
|
|
|
#include "interface_text.hpp"
|
2018-08-30 18:04:56 +02:00
|
|
|
|
|
|
|
using std::cout;
|
|
|
|
using std::cerr;
|
|
|
|
using std::cin;
|
|
|
|
|
2018-10-18 21:31:05 +02:00
|
|
|
// Allow cout to output QStrings
|
|
|
|
std::ostream &operator <<(std::ostream &stream, const QString &str)
|
|
|
|
{
|
|
|
|
stream << str.toStdString();
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
const string Text::get_answer(const QString &question)
|
2018-08-30 18:04:56 +02:00
|
|
|
{
|
2018-10-17 18:30:41 +02:00
|
|
|
string answer;
|
|
|
|
|
|
|
|
cout << question << ": ";
|
|
|
|
std::getline(cin, answer, '\n');
|
|
|
|
|
|
|
|
return answer;
|
|
|
|
}
|
|
|
|
|
2018-10-18 21:31:05 +02:00
|
|
|
const bool Text::askblocked()
|
2018-10-17 18:30:41 +02:00
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
2018-10-23 08:29:07 +02:00
|
|
|
const string blocked = get_answer(tr("Blocked or silenced?") +
|
|
|
|
" [b/s]");
|
2018-10-18 22:42:05 +02:00
|
|
|
if (blocked[0] == 'b' || blocked[0] == 'B')
|
2018-10-17 18:30:41 +02:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2018-10-18 22:42:05 +02:00
|
|
|
else if (blocked[0] == 's' || blocked[0] == 'S')
|
2018-10-17 18:30:41 +02:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-18 21:31:05 +02:00
|
|
|
const void Text::askrecipes(const string &user)
|
2018-10-17 18:30:41 +02:00
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
2018-10-18 21:31:05 +02:00
|
|
|
const string receipt_yn = get_answer(tr("Add receipt? [y/n]"));
|
|
|
|
if (receipt_yn[0] == tr("y")[0] || receipt_yn[0] == tr("Y")[0])
|
2018-10-17 18:30:41 +02:00
|
|
|
{
|
2018-10-18 21:31:05 +02:00
|
|
|
const string receipt = get_answer(tr("Receipt"));
|
2018-10-17 18:30:41 +02:00
|
|
|
|
|
|
|
if (database::add_receipt(user, receipt))
|
|
|
|
{
|
2018-10-18 21:31:05 +02:00
|
|
|
cout << tr("Receipt added.") << '\n';
|
2018-10-17 18:30:41 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-18 21:31:05 +02:00
|
|
|
else if (receipt_yn[0] == tr("n")[0] || receipt_yn[0] == tr("N")[0])
|
2018-10-17 18:30:41 +02:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-18 21:31:05 +02:00
|
|
|
const void Text::add()
|
2018-10-17 18:30:41 +02:00
|
|
|
{
|
2018-10-18 21:31:05 +02:00
|
|
|
const string user = get_answer(tr("User or instance"));
|
2018-10-17 18:30:41 +02:00
|
|
|
int blocked;
|
|
|
|
|
|
|
|
if (askblocked())
|
|
|
|
{
|
|
|
|
blocked = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
blocked = 0;
|
|
|
|
}
|
2018-10-18 21:31:05 +02:00
|
|
|
const string reason = get_answer(tr("Reason"));
|
2018-10-17 18:30:41 +02:00
|
|
|
|
|
|
|
if (database::add_block(user, blocked, reason))
|
|
|
|
{
|
2018-10-18 21:31:05 +02:00
|
|
|
cout << user << " " << tr("added.") << '\n';
|
2018-10-17 18:30:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
askrecipes(user);
|
|
|
|
}
|
|
|
|
|
2018-10-18 21:31:05 +02:00
|
|
|
const void Text::edit()
|
2018-10-17 18:30:41 +02:00
|
|
|
{
|
|
|
|
result_details olddata;
|
2018-10-18 21:31:05 +02:00
|
|
|
const string olduser = get_answer(tr("User or instance"));
|
2018-10-17 18:30:41 +02:00
|
|
|
if (database::details(olduser, olddata))
|
|
|
|
{
|
2018-10-18 21:31:05 +02:00
|
|
|
cout << tr("A blank line keeps the former value.") << '\n';
|
|
|
|
string newuser = get_answer(tr("Change user or instance to"));
|
2018-10-17 18:30:41 +02:00
|
|
|
if (newuser.empty())
|
|
|
|
{
|
|
|
|
newuser = olduser;
|
|
|
|
}
|
|
|
|
|
|
|
|
int blocked;
|
|
|
|
if (askblocked())
|
|
|
|
{
|
|
|
|
blocked = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
blocked = 0;
|
|
|
|
}
|
|
|
|
|
2018-10-18 21:31:05 +02:00
|
|
|
cout << tr("Old reason was:") << " " << std::get<1>(olddata) << '\n';
|
|
|
|
string newreason = get_answer(tr("Change reason to"));
|
2018-10-17 18:30:41 +02:00
|
|
|
if (newreason.empty())
|
|
|
|
{
|
|
|
|
newreason = std::get<1>(olddata);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<string> newreceipts;
|
|
|
|
for (const string &oldreceipt : std::get<2>(olddata))
|
|
|
|
{
|
2018-10-18 21:31:05 +02:00
|
|
|
cout << tr("Old receipt was:") << " " << oldreceipt << '\n';
|
|
|
|
string newreceipt = get_answer(tr("Change receipt to"));
|
2018-10-17 18:30:41 +02:00
|
|
|
if (newreceipt.empty())
|
|
|
|
{
|
|
|
|
newreceipt = oldreceipt;
|
|
|
|
}
|
|
|
|
newreceipts.push_back(newreceipt);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (database::remove(olduser))
|
|
|
|
{
|
|
|
|
database::add_block(newuser, blocked, newreason);
|
|
|
|
if (!newreceipts.empty())
|
|
|
|
{
|
|
|
|
for (const string &newreceipt : newreceipts)
|
|
|
|
{
|
|
|
|
database::add_receipt(newuser, newreceipt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
askrecipes(newuser);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-10-18 21:31:05 +02:00
|
|
|
cerr << tr("Could not remove") << " " << olduser << ".\n";
|
2018-10-17 18:30:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-18 21:31:05 +02:00
|
|
|
const void Text::remove()
|
2018-10-17 18:30:41 +02:00
|
|
|
{
|
2018-10-18 21:31:05 +02:00
|
|
|
const string user = get_answer(tr("User or instance"));
|
2018-10-17 18:30:41 +02:00
|
|
|
|
|
|
|
if (database::remove(user))
|
|
|
|
{
|
2018-10-18 21:31:05 +02:00
|
|
|
cout << user << " " << tr("removed.") << '\n';
|
2018-10-17 18:30:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-18 21:31:05 +02:00
|
|
|
const void Text::view()
|
2018-10-17 18:30:41 +02:00
|
|
|
{
|
|
|
|
result_view result;
|
|
|
|
if (database::view(result))
|
|
|
|
{
|
|
|
|
for (const std::tuple<string, int, string> &line : result)
|
|
|
|
{
|
|
|
|
if (std::get<1>(line) == 1)
|
|
|
|
{
|
2018-10-18 21:31:05 +02:00
|
|
|
cout << tr(" Blocked:") << " ";
|
2018-10-17 18:30:41 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-10-18 21:31:05 +02:00
|
|
|
cout << tr("Silenced:") << " ";
|
2018-10-17 18:30:41 +02:00
|
|
|
}
|
2018-10-18 21:31:05 +02:00
|
|
|
cout << std::get<0>(line) << " " << tr("because:") << " ";
|
2018-10-17 18:30:41 +02:00
|
|
|
cout << std::get<2>(line) << '\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-18 21:31:05 +02:00
|
|
|
const void Text::details()
|
2018-10-17 18:30:41 +02:00
|
|
|
{
|
2018-10-18 21:31:05 +02:00
|
|
|
const string user = get_answer(tr("User or instance"));
|
2018-10-17 18:30:41 +02:00
|
|
|
{
|
|
|
|
result_details result;
|
|
|
|
if (database::details(user, result))
|
|
|
|
{
|
2018-10-18 21:31:05 +02:00
|
|
|
cout << user << " " << tr("is") << " ";
|
2018-10-17 18:30:41 +02:00
|
|
|
if (std::get<0>(result) == 1)
|
|
|
|
{
|
2018-10-18 21:31:05 +02:00
|
|
|
cout << tr("blocked, because:") << " ";
|
2018-10-17 18:30:41 +02:00
|
|
|
}
|
|
|
|
else if (std::get<0>(result) == 0)
|
|
|
|
{
|
2018-10-18 21:31:05 +02:00
|
|
|
cout << tr("silenced, because:") << " ";
|
2018-10-17 18:30:41 +02:00
|
|
|
}
|
|
|
|
cout << std::get<1>(result) << '\n';
|
|
|
|
|
|
|
|
if (!std::get<2>(result).empty())
|
|
|
|
{
|
2018-10-18 21:31:05 +02:00
|
|
|
cout << tr("Receipts:") << '\n';
|
2018-10-17 18:30:41 +02:00
|
|
|
for (const string &url : std::get<2>(result))
|
|
|
|
{
|
|
|
|
cout << " " << url << '\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-18 21:31:05 +02:00
|
|
|
const void Text::help()
|
2018-10-17 18:30:41 +02:00
|
|
|
{
|
2018-10-18 21:31:05 +02:00
|
|
|
cout << tr(
|
|
|
|
"Type add, edit, remove, view or details. Or just the first letter.")
|
|
|
|
<< '\n';
|
|
|
|
cout << tr(
|
|
|
|
"Type help or h to show this help. Type quit or q to quit the program.")
|
|
|
|
<< '\n';
|
2018-08-30 18:04:56 +02:00
|
|
|
}
|
|
|
|
|
2018-10-18 21:31:05 +02:00
|
|
|
Text::Text(QObject *parent) : QObject(parent)
|
2018-08-30 18:04:56 +02:00
|
|
|
{
|
|
|
|
bool keeprunning = true;
|
2018-10-18 21:31:05 +02:00
|
|
|
|
|
|
|
cout << tr("This is Whyblocked") << " " << global::version << ".\n";
|
2018-10-17 18:30:41 +02:00
|
|
|
help();
|
2018-08-30 18:04:56 +02:00
|
|
|
while (keeprunning)
|
|
|
|
{
|
2018-10-17 18:30:41 +02:00
|
|
|
string answer = get_answer("");
|
2018-10-18 22:42:05 +02:00
|
|
|
switch (answer[0])
|
2018-08-30 18:04:56 +02:00
|
|
|
{
|
2018-10-18 22:42:05 +02:00
|
|
|
case 'a':
|
|
|
|
case 'A':
|
|
|
|
{
|
|
|
|
add();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'e':
|
|
|
|
case 'E':
|
|
|
|
{
|
|
|
|
edit();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'r':
|
|
|
|
case 'R':
|
|
|
|
{
|
|
|
|
remove();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'v':
|
|
|
|
case 'V':
|
|
|
|
{
|
|
|
|
view();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'd':
|
|
|
|
case 'D':
|
|
|
|
{
|
|
|
|
details();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'h':
|
|
|
|
case 'H':
|
|
|
|
{
|
|
|
|
help();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'q':
|
|
|
|
case 'Q':
|
|
|
|
{
|
|
|
|
keeprunning = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
cout << tr("Response not understood.") << '\n';
|
|
|
|
}
|
2018-08-30 18:04:56 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-18 21:31:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
QCoreApplication app(argc, argv);
|
|
|
|
QCoreApplication::setApplicationName("Whyblocked");
|
|
|
|
|
|
|
|
QTranslator qtTranslator;
|
|
|
|
qtTranslator.load("qt_" + QLocale::system().name(),
|
|
|
|
QLibraryInfo::location(QLibraryInfo::TranslationsPath));
|
|
|
|
app.installTranslator(&qtTranslator);
|
|
|
|
QTranslator appTranslator;
|
|
|
|
appTranslator.load("whyblocked_" + QLocale::system().name(),
|
|
|
|
QLibraryInfo::location(QLibraryInfo::TranslationsPath));
|
|
|
|
app.installTranslator(&appTranslator);
|
|
|
|
|
|
|
|
Text t(&app);
|
2018-08-30 18:04:56 +02:00
|
|
|
|
2018-10-06 21:33:02 +02:00
|
|
|
return 0;
|
2018-08-30 18:04:56 +02:00
|
|
|
}
|