database.hpp 7.63 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
#include <map>
17

18 19 20

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

84
  using GlobalOptionsTable = Table<Id, Owner, MaxHistoryLength, RecordHistory, Persistent>;
85 86 87 88 89
  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;

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

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

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

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

104 105
  static GlobalOptions get_global_options(const std::string& owner);
  static IrcServerOptions get_irc_server_options(const std::string& owner,
106
                                                     const std::string& server);
107 108 109 110 111 112 113 114 115 116 117 118 119
  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);
120

121 122 123 124 125 126
  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();

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

130
  template <typename TableType>
131
  static int64_t count(const TableType& table)
132 133 134 135 136 137 138 139 140
  {
    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;
141
  static RosterTable roster;
142
  static sqlite3* db;
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
  /**
   * Some caches, to avoid doing very frequent query requests for a few options.
   */
  using CacheKey = std::tuple<std::string, std::string, std::string>;

  static EncodingIn::real_type get_encoding_in(const std::string& owner,
                                        const std::string& server,
                                        const std::string& channel)
  {
    CacheKey channel_key{owner, server, channel};
    auto it = Database::encoding_in_cache.find(channel_key);
    if (it == Database::encoding_in_cache.end())
      {
        auto options = Database::get_irc_channel_options_with_server_default(owner, server, channel);
        EncodingIn::real_type result = options.col<Database::EncodingIn>();
        if (result.empty())
          result = "ISO-8859-1";
        it = Database::encoding_in_cache.insert(std::make_pair(channel_key, result)).first;
      }
    return it->second;
  }
  static void invalidate_encoding_in_cache(const std::string& owner,
                                           const std::string& server,
                                           const std::string& channel)
  {
    CacheKey channel_key{owner, server, channel};
    Database::encoding_in_cache.erase(channel_key);
  }
  static void invalidate_encoding_in_cache()
  {
    Database::encoding_in_cache.clear();
  }

177
 private:
178
  static std::string gen_uuid();
179
  static std::map<CacheKey, EncodingIn::real_type> encoding_in_cache;
180 181
};
#endif /* USE_DATABASE */