Improve printing code, get user

FossilOrigin-Name: e80ec5a64546737f28a437f9f7a41b4caf5f8d19b98cc7876e62eefca087573f
This commit is contained in:
nekobit 2022-10-20 18:55:41 +00:00
parent e7a3fecc1d
commit 01bc54d865
5 changed files with 55 additions and 21 deletions

View file

@ -30,6 +30,10 @@
#include "database/database.h"
#include "user.h"
namespace {
constexpr int CMD_GAP = 27;
}
using CTLFunction_t = std::function<int(int argc, char** argv)>;
struct CTLOptions
@ -44,7 +48,6 @@ void print_usage(const std::string& command, std::ostream& buf)
buf << "Usage: " << command << " [command] [command-args]...\n";
}
namespace CTL {
int create_user(int argc, char** argv)
{
@ -79,10 +82,10 @@ namespace CTL {
if (i == 0)
{
print_usage(argv[0], std::cout);
std::cout << "\ncreate_user:\n"
" -n, --name" "\t\t\t" "Username (required)\n"
" -N, --nickname" "\t\t" "Display name\n"
" -e, --email" "\t\t\t" "Email" << std::endl;
std::cout << "\ncreate_user:\n" << std::left
<< std::setw(CMD_GAP) << " -n, --name" << std::setw(0) << "Username (required)\n"
<< std::setw(CMD_GAP) << " -N, --nickname" << std::setw(0) << "Display name\n"
<< std::setw(CMD_GAP) << " -e, --email" << std::setw(0) << "Email" << std::endl;
return EXIT_FAILURE;
}
// Fall?
@ -146,9 +149,9 @@ namespace CTL {
if (i == 0)
{
print_usage(argv[0], std::cout);
std::cout << "\nlist_user:\n"
" -a, --acct" "\t\t" "Lookup by account\n"
" -i, --id" "\t\t" "Lookup by id\n";
std::cout << "\nlist_user:\n" << std::left
<< std::setw(CMD_GAP) << " -a, --acct" << std::setw(0) << "Lookup by account\n" << std::setw(0)
<< std::setw(CMD_GAP) << " -i, --id" << std::setw(0) << "Lookup by id" << std::endl;
return EXIT_FAILURE;
}
// Fall?
@ -203,9 +206,8 @@ int control_mode_init(int argc, char** argv)
size_t l = commands.size();
for (const CTLOptions& cmd : commands)
{
// Long name tab wizardry
std::cout << " - " << cmd.name << (cmd.name.length() < 12 ? "\t" : "") << "\t\t" <<
cmd.desc << (--l != 0 ? "\n" : "");
std::cout << " - " << std::left << std::setw(24) << cmd.name
<< std::setw(0) << cmd.desc << (--l != 0 ? "\n" : "");
}
// Flush and leave

View file

@ -17,6 +17,21 @@
*/
#pragma once
/* See this!!!
* -------------
* OpenSSL has deprecated the RSA features (again) for some SSL
* replacement, but version 3.0 is still fairly new and hasn't even
* landed in Gentoo or Arch stable, only Debian Testing (lol) seems to
* have the latest OpenSSL that I know of, which is the system I am
* typing this on.
*
* For now, Wormhole remains compatible with version 1.1.1, and probably
* for a long while.
*/
#define OPENSSL_NO_DEPRECATED
#define OPENSSL_API_COMPAT 0x10100000L
#include <cstddef>
#include <openssl/rsa.h>
#include <memory>

View file

@ -149,7 +149,7 @@ User SQLite::get_user(decltype(User::id) id)
if (sqlite3_column_type(stmt, 1) != SQLITE_NULL)
luser.display_name = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1));
if (sqlite3_column_type(stmt, 2) != SQLITE_NULL)
luser.bio = reinterpret_cast<const char*>(sqlite3_column_int64(stmt, 2));
luser.bio = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 2));
if (sqlite3_column_type(stmt, 3) != SQLITE_NULL)
luser.key = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 3));
});
@ -179,7 +179,10 @@ void SQLite::create_user(User& p)
User SQLite::get_user(const decltype(User::acct)& acct)
{
User luser{0, {}, false, {}, acct, {}, {}, {}};
User luser{};
luser.id = 0;
luser.acct = acct;
luser.local = false;
// Setup function
sqlite_compile_to_cache(GET_USER_BY_ACCT, "SELECT id, display_name, bio, key FROM users WHERE acct=?1");
@ -188,8 +191,10 @@ User SQLite::get_user(const decltype(User::acct)& acct)
luser.id = sqlite3_column_int64(stmt, 0);
if (sqlite3_column_type(stmt, 1) != SQLITE_NULL)
luser.display_name = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1));
if (sqlite3_column_type(stmt, 2) != SQLITE_NULL)
luser.bio = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 2));
if (sqlite3_column_type(stmt, 3) != SQLITE_NULL)
luser.key = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 3));
});

View file

@ -16,8 +16,14 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <iomanip>
#include "user.h"
User::User()
: id(0), email(""), local(false), bio(""), acct(""),
birthday(0), display_name(""), key("")
{}
void User::generate_key(const Crypt::RsaFactory& keygen)
{
@ -25,14 +31,18 @@ void User::generate_key(const Crypt::RsaFactory& keygen)
std::ostream& operator<<(std::ostream& os, const User& user)
{
using namespace std;
using namespace std::string_literals;
constexpr int gap = 16;
constexpr const char* gap_sym = " | ";
os << " User \"" << user.acct << "\":\n";
os << right<<setw(gap) << "id" << gap_sym << left<<setw(0) << user.id << "\n";
os << right<<setw(gap) << "display name" << gap_sym << left<<setw(0) << user.display_name << "\n";
os << right<<setw(gap) << "email" << gap_sym << left<<setw(0) << user.email << "\n";
os << right<<setw(gap) << "local" << gap_sym << left<<setw(0) << user.local << "\n";
os << right<<setw(gap) << "bio" << gap_sym << left<<setw(0) << user.bio << "\n";
os << right<<setw(gap) << "key" << gap_sym << left<<setw(0) << user.get_key();
return os <<
"User \""s + user.acct + "\" info:\n"
" id:" "\t" + std::to_string(user.id) + "\n"
" display name:" "\t" + user.display_name + "\n"
" email:" "\t" + user.email + "\n"
" local:" "\t" + std::to_string(user.local) + "\n"
" bio:" "\t" + user.bio + "\n"
" key:" "\t" + user.get_key();
return os;
}

View file

@ -24,6 +24,8 @@
class User
{
public:
User();
~User() = default;
// Values read from databases, so public here
unsigned long id;
std::string email;