database.hpp 6.25 KB
Newer Older
1 2
#pragma once

3 4 5
#include <biboumi.h>
#ifdef USE_DATABASE

6 7 8
#include <database/table.hpp>
#include <database/column.hpp>
#include <database/count_query.hpp>
9

10 11
#include <utils/optional_bool.hpp>

12
#include <chrono>
13 14 15
#include <string>

#include <memory>
16

17 18 19

class Database
{
20
 public:
21
  using time_point = std::chrono::system_clock::time_point;
22

23
  struct Uuid: Column<std::string> { static constexpr auto name = "uuid_"; };
24

25
  struct Owner: Column<std::string> { static constexpr auto name = "owner_"; };
26

27
  struct IrcChanName: Column<std::string> { static constexpr auto name = "ircChanName_"; };
28

29
  struct Channel: Column<std::string> { static constexpr auto name = "channel_"; };
30

31
  struct IrcServerName: Column<std::string> { static constexpr auto name = "ircServerName_"; };
32

33
  struct Server: Column<std::string> { static constexpr auto name = "server_"; };
34

35
  struct Date: Column<time_point::rep> { static constexpr auto name = "date_"; };
36

37
  struct Body: Column<std::string> { static constexpr auto name = "body_"; };
38

39
  struct Nick: Column<std::string> { static constexpr auto name = "nick_"; };
40

41
  struct Pass: Column<std::string> { static constexpr auto name = "pass_"; };
42

43 44
  struct Ports: Column<std::string> { static constexpr auto name = "ports_";
    Ports(): Column<std::string>("6667") {} };
45

46
  struct TlsPorts: Column<std::string> { static constexpr auto name = "tlsPorts_";
47
    TlsPorts(): Column<std::string>("6697;6670") {} };
48

49
  struct Username: Column<std::string> { static constexpr auto name = "username_"; };
50

51
  struct Realname: Column<std::string> { static constexpr auto name = "realname_"; };
52

53
  struct AfterConnectionCommand: Column<std::string> { static constexpr auto name = "afterConnectionCommand_"; };
54

55
  struct TrustedFingerprint: Column<std::string> { static constexpr auto name = "trustedFingerprint_"; };
56

57
  struct EncodingOut: Column<std::string> { static constexpr auto name = "encodingOut_"; };
58

59
  struct EncodingIn: Column<std::string> { static constexpr auto name = "encodingIn_"; };
60 61

  struct MaxHistoryLength: Column<int> { static constexpr auto name = "maxHistoryLength_";
62
    MaxHistoryLength(): Column<int>(20) {} };
63 64

  struct RecordHistory: Column<bool> { static constexpr auto name = "recordHistory_";
65
    RecordHistory(): Column<bool>(true) {}};
66

67
  struct RecordHistoryOptional: Column<OptionalBool> { static constexpr auto name = "recordHistory_"; };
68

69
  struct VerifyCert: Column<bool> { static constexpr auto name = "verifyCert_";
70
    VerifyCert(): Column<bool>(true) {} };
71 72

  struct Persistent: Column<bool> { static constexpr auto name = "persistent_";
73
    Persistent(): Column<bool>(false) {} };
74

75 76 77 78 79
  struct LocalJid: Column<std::string> { static constexpr auto name = "local"; };

  struct RemoteJid: Column<std::string> { static constexpr auto name = "remote"; };


80 81 82
  using MucLogLineTable = Table<Id, Uuid, Owner, IrcChanName, IrcServerName, Date, Body, Nick>;
  using MucLogLine = MucLogLineTable::RowType;

83
  using GlobalOptionsTable = Table<Id, Owner, MaxHistoryLength, RecordHistory, Persistent>;
84 85 86 87 88
  using GlobalOptions = GlobalOptionsTable::RowType;

  using IrcServerOptionsTable = Table<Id, Owner, Server, Pass, AfterConnectionCommand, TlsPorts, Ports, Username, Realname, VerifyCert, TrustedFingerprint, EncodingOut, EncodingIn, MaxHistoryLength>;
  using IrcServerOptions = IrcServerOptionsTable::RowType;

89
  using IrcChannelOptionsTable = Table<Id, Owner, Server, Channel, EncodingOut, EncodingIn, MaxHistoryLength, Persistent, RecordHistoryOptional>;
90 91
  using IrcChannelOptions = IrcChannelOptionsTable::RowType;

92 93 94
  using RosterTable = Table<LocalJid, RemoteJid>;
  using RosterItem = RosterTable::RowType;

95 96 97
  Database() = default;
  ~Database() = default;

louiz’'s avatar
louiz’ committed
98 99 100 101 102
  Database(const Database&) = delete;
  Database(Database&&) = delete;
  Database& operator=(const Database&) = delete;
  Database& operator=(Database&&) = delete;

103 104
  static GlobalOptions get_global_options(const std::string& owner);
  static IrcServerOptions get_irc_server_options(const std::string& owner,
105
                                                     const std::string& server);
106 107 108 109 110 111 112 113 114 115 116 117 118
  static IrcChannelOptions get_irc_channel_options(const std::string& owner,
                                                   const std::string& server,
                                                   const std::string& channel);
  static IrcChannelOptions get_irc_channel_options_with_server_default(const std::string& owner,
                                                                       const std::string& server,
                                                                       const std::string& channel);
  static IrcChannelOptions get_irc_channel_options_with_server_and_global_default(const std::string& owner,
                                                                                  const std::string& server,
                                                                                  const std::string& channel);
  static std::vector<MucLogLine> get_muc_logs(const std::string& owner, const std::string& chan_name, const std::string& server,
                                              int limit=-1, const std::string& start="", const std::string& end="");
  static std::string store_muc_message(const std::string& owner, const std::string& chan_name, const std::string& server_name,
                                       time_point date, const std::string& body, const std::string& nick);
119

120 121 122 123 124 125
  static void add_roster_item(const std::string& local, const std::string& remote);
  static bool has_roster_item(const std::string& local, const std::string& remote);
  static void delete_roster_item(const std::string& local, const std::string& remote);
  static std::vector<Database::RosterItem> get_contact_list(const std::string& local);
  static std::vector<Database::RosterItem> get_full_roster();

126
  static void close();
127
  static void open(const std::string& filename);
128

129
  template <typename TableType>
130
  static int64_t count(const TableType& table)
131 132 133 134 135 136 137 138 139
  {
    CountQuery query{table.get_name()};
    return query.execute(Database::db);
  }

  static MucLogLineTable muc_log_lines;
  static GlobalOptionsTable global_options;
  static IrcServerOptionsTable irc_server_options;
  static IrcChannelOptionsTable irc_channel_options;
140
  static RosterTable roster;
141
  static sqlite3* db;
142

143
 private:
144
  static std::string gen_uuid();
145 146
};
#endif /* USE_DATABASE */