bridge.hpp 12.4 KB
Newer Older
1 2
#pragma once

3

4
#include <irc/irc_message.hpp>
5
#include <irc/irc_client.hpp>
6
#include <bridge/colors.hpp>
7
#include <irc/irc_user.hpp>
8 9 10
#include <irc/iid.hpp>

#include <unordered_map>
11
#include <functional>
12
#include <exception>
13 14 15
#include <string>
#include <memory>

16 17
#include <biboumi.h>

18
class BiboumiComponent;
19 20
class Poller;

21 22
/**
 * A callback called for each IrcMessage we receive. If the message triggers
23 24
 * a response, it must send ore or more iq and return true (in that case it
 * is removed from the list), otherwise it must do nothing and just return
25 26
 * false.
 */
27
using irc_responder_callback_t = std::function<bool(const std::string& irc_hostname, const IrcMessage& message)>;
28

29 30 31 32 33 34 35 36 37
/**
 * One bridge is spawned for each XMPP user that uses the component.  The
 * bridge spawns IrcClients when needed (when the user wants to join a
 * channel on a new server) and does the translation between the two
 * protocols.
 */
class Bridge
{
public:
38
  explicit Bridge(const std::string& user_jid, BiboumiComponent& xmpp, std::shared_ptr<Poller> poller);
louiz’'s avatar
louiz’ committed
39 40 41 42 43 44
  ~Bridge() = default;

  Bridge(const Bridge&) = delete;
  Bridge(Bridge&& other) = delete;
  Bridge& operator=(const Bridge&) = delete;
  Bridge& operator=(Bridge&&) = delete;
louiz’'s avatar
louiz’ committed
45 46 47
  /**
   * QUIT all connected IRC servers.
   */
48
  void shutdown(const std::string& exit_message);
49 50 51 52
  /**
   * PART the given resource from all the channels
   */
  void remove_resource(const std::string& resource, const std::string& part_message);
louiz’'s avatar
louiz’ committed
53 54 55 56
  /**
   * Remove all inactive IrcClients
   */
  void clean();
57 58 59 60
  /**
   * Return the jid of the XMPP user using this bridge
   */
  const std::string& get_jid() const;
louiz’'s avatar
louiz’ committed
61
  std::string get_bare_jid() const;
62

Link Mauve's avatar
Link Mauve committed
63
  static Xmpp::body make_xmpp_body(const std::string& str, const std::string& encoding = "ISO-8859-1");
64 65 66 67 68 69
  /***
   **
   ** From XMPP to IRC.
   **
   **/

louiz’'s avatar
louiz’ committed
70 71 72 73
  /**
   * Try to join an irc_channel, does nothing and return true if the channel
   * was already joined.
   */
74
  bool join_irc_channel(const Iid& iid, const std::string& nickname, const std::string& password, const std::string& resource);
75

louiz’'s avatar
louiz’ committed
76
  void send_channel_message(const Iid& iid, const std::string& body);
louiz’'s avatar
louiz’ committed
77
  void send_private_message(const Iid& iid, const std::string& body, const std::string& type="PRIVMSG");
louiz’'s avatar
louiz’ committed
78
  void send_raw_message(const std::string& hostname, const std::string& body);
79
  void leave_irc_channel(Iid&& iid, const std::string& status_message, const std::string& resource);
louiz’'s avatar
louiz’ committed
80
  void send_irc_nick_change(const Iid& iid, const std::string& new_nick);
81 82
  void send_irc_kick(const Iid& iid, const std::string& target, const std::string& reason,
                     const std::string& iq_id, const std::string& to_jid);
83
  void set_channel_topic(const Iid& iid, const std::string& subject);
84 85
  void send_xmpp_version_to_irc(const Iid& iid, const std::string& name, const std::string& version,
                                const std::string& os);
86
  void send_irc_ping_result(const Iid& iid, const std::string& id);
87 88 89
  void send_irc_version_request(const std::string& irc_hostname, const std::string& target,
                                const std::string& iq_id, const std::string& to_jid,
                                const std::string& from_jid);
90 91
  void send_irc_channel_list_request(const Iid& iid, const std::string& iq_id,
                                     const std::string& to_jid);
92 93
  void forward_affiliation_role_change(const Iid& iid, const std::string& nick,
                                       const std::string& affiliation, const std::string& role);
94 95 96 97 98 99
  /**
   * Directly send a CTCP PING request to the IRC user
   */
  void send_irc_user_ping_request(const std::string& irc_hostname, const std::string& nick,
                                  const std::string& iq_id, const std::string& to_jid,
                                  const std::string& from_jid);
100 101 102 103 104 105 106
  /**
   * First check if the participant is in the room, before sending a direct
   * CTCP PING request to the IRC user
   */
  void send_irc_participant_ping_request(const Iid& iid, const std::string& nick,
                                         const std::string& iq_id, const std::string& to_jid,
                                         const std::string& from_jid);
107 108 109 110 111 112
  /**
   * Directly send back a result if it's a gateway ping or if we are
   * connected to the given IRC server, an error otherwise.
   */
  void on_gateway_ping(const std::string& irc_hostname, const std::string& iq_id, const std::string& to_jid,
                       const std::string& from_jid);
113

114
  void send_irc_invitation(const Iid& iid, const std::string& to);
115

116 117 118 119 120 121 122 123
  /***
   **
   ** From IRC to XMPP.
   **
   **/

  /**
   * Send a message corresponding to a server NOTICE, the from attribute
124
   * should be juste the server hostname.
125 126 127 128 129
   */
  void send_xmpp_message(const std::string& from, const std::string& author, const std::string& msg);
  /**
   * Send the presence of a new user in the MUC.
   */
130 131 132 133 134
  void send_user_join(const std::string& hostname, const std::string& chan_name,
                      const IrcUser* user, const char user_mode,
                      const bool self, const std::string& resource);
  void send_user_join(const std::string& hostname, const std::string& chan_name,
                      const IrcUser* user, const char user_mode,
135
                      const bool self);
136

137 138 139
  /**
   * Send the topic of the MUC to the user
   */
140
  void send_topic(const std::string& hostname, const std::string& chan_name, const std::string& topic, const std::string& who);
141
  void send_topic(const std::string& hostname, const std::string& chan_name, const std::string& topic, const std::string& who, const std::string& resource);
142 143 144 145 146
  /**
   * Send the MUC history to the user
   */
  void send_room_history(const std::string& hostname, const std::string& chan_name);
  void send_room_history(const std::string& hostname, const std::string& chan_name, const std::string& resource);
louiz’'s avatar
louiz’ committed
147 148 149
  /**
   * Send a MUC message from some participant
   */
louiz’'s avatar
louiz’ committed
150
  void send_message(const Iid& iid, const std::string& nick, const std::string& body, const bool muc);
151
  /**
152
   * Send a presence of type error, from a room.
153
   */
154
  void send_presence_error(const Iid& iid, const std::string& nick, const std::string& type, const std::string& condition, const std::string& error_code, const std::string& text);
louiz’'s avatar
louiz’ committed
155 156 157
  /**
   * Send an unavailable presence from this participant
   */
158
  void send_muc_leave(Iid&& iid, std::string&& nick, const std::string& message, const bool self, const std::string& resource="");
louiz’'s avatar
louiz’ committed
159 160
  /**
   * Send presences to indicate that an user old_nick (ourself if self ==
161 162 163
   * true) changed his nick to new_nick.  The user_mode is needed because
   * the xmpp presence needs ton contain the role and affiliation of the
   * user.
louiz’'s avatar
louiz’ committed
164
   */
165 166 167 168 169
  void send_nick_change(Iid&& iid,
                        const std::string& old_nick,
                        const std::string& new_nick,
                        const char user_mode,
                        const bool self);
170
  void kick_muc_user(Iid&& iid, const std::string& target, const std::string& reason, const std::string& author);
171
  void send_nickname_conflict_error(const Iid& iid, const std::string& nickname);
172 173 174 175
  /**
   * Send a role/affiliation change, matching the change of mode for that user
   */
  void send_affiliation_role_change(const Iid& iid, const std::string& target, const char mode);
176
  /**
louiz’'s avatar
louiz’ committed
177
   * Send an iq version request coming from nick!hostname@
178 179
   */
  void send_iq_version_request(const std::string& nick, const std::string& hostname);
180 181 182 183 184
  /**
   * Send an iq ping request coming from nick!hostname@
   */
  void send_xmpp_ping_request(const std::string& nick, const std::string& hostname,
                              const std::string& id);
louiz’'s avatar
louiz’ committed
185 186
  void send_xmpp_invitation(const Iid& iid, const std::string& author);

louiz’'s avatar
louiz’ committed
187 188 189 190
  /**
   * Misc
   */
  std::string get_own_nick(const Iid& iid);
louiz’'s avatar
louiz’ committed
191
  /**
192
   * Get the number of server to which this bridge is connected or connecting.
louiz’'s avatar
louiz’ committed
193
   */
194
  size_t active_clients() const;
195 196 197 198 199 200 201 202
  /**
   * Add (or replace the existing) <nick, jid> into the preferred_user_from map
   */
  void set_preferred_from_jid(const std::string& nick, const std::string& full_jid);
  /**
   * Remove the preferred jid for the given IRC nick
   */
  void remove_preferred_from_jid(const std::string& nick);
203 204 205 206 207
  /**
   * Given a channel_name, remove all preferred from_jid that come
   * from this chan.
   */
  void remove_all_preferred_from_jid_of_room(const std::string& channel_name);
208
  /**
209
   * Add a callback to the waiting list of irc callbacks.
210
   */
211
  void add_waiting_irc(irc_responder_callback_t&& callback);
212 213 214 215 216
  /**
   * Iter over all the waiting_iq, call the iq_responder_filter_t for each,
   * whenever one of them returns true: call the corresponding
   * iq_responder_callback_t and remove the callback from the list.
   */
217
  void trigger_on_irc_message(const std::string& irc_hostname, const IrcMessage& message);
218
  std::unordered_map<std::string, std::shared_ptr<IrcClient>>& get_irc_clients();
219
  std::set<char> get_chantypes(const std::string& hostname) const;
220 221 222
#ifdef USE_DATABASE
  void set_record_history(const bool val);
#endif
louiz’'s avatar
louiz’ committed
223

224 225 226 227 228 229
private:
  /**
   * Returns the client for the given hostname, create one (and use the
   * username in this case) if none is found, and connect that newly-created
   * client immediately.
   */
230
  IrcClient* make_irc_client(const std::string& hostname, const std::string& nickname);
louiz’'s avatar
louiz’ committed
231
  /**
232 233
   * This version does not create the IrcClient if it does not exist, throws
   * a IRCServerNotConnected error in that case.
louiz’'s avatar
louiz’ committed
234 235
   */
  IrcClient* get_irc_client(const std::string& hostname);
236 237 238
  /**
   * Idem, but returns nullptr if the server does not exist.
   */
239
  IrcClient* find_irc_client(const std::string& hostname) const;
240
  /**
241
   * The bare JID of the user associated with this bridge. Messages from/to this
242 243
   * JID are only managed by this bridge.
   */
244
  const std::string user_jid;
245 246 247 248 249 250
  /**
   * One IrcClient for each IRC server we need to be connected to.
   * The pointer is shared by the bridge and the poller.
   */
  std::unordered_map<std::string, std::shared_ptr<IrcClient>> irc_clients;
  /**
251
   * To communicate back with the XMPP component
252
   */
253
  BiboumiComponent& xmpp;
254 255 256 257
  /**
   * Poller, to give it the IrcClients that we spawn, to make it manage
   * their sockets.
   */
258
  std::shared_ptr<Poller> poller;
259 260 261 262 263 264 265 266
  /**
   * A map of <nick, full_jid>. For example if this map contains <"toto",
   * "#somechan%server@biboumi/ToTo">, whenever a private message is
   * received from the user "toto", instead of forwarding it to XMPP with
   * from='toto!server@biboumi', we use instead
   * from='#somechan%server@biboumi/ToTo'
   */
  std::unordered_map<std::string, std::string> preferred_user_from;
267 268 269 270 271 272
  /**
   * A list of callbacks that are waiting for some IrcMessage to trigger a
   * response.  We add callbacks in this list whenever we received an IQ
   * request and we need a response from IRC to be able to provide the
   * response iq.
   */
louiz’'s avatar
louiz’ committed
273
  std::vector<irc_responder_callback_t> waiting_irc;
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289

  /**
   * Resources to IRC channel/server mapping:
   */
  using Resource = std::string;
  using ChannelName = std::string;
  using IrcHostname = std::string;
  using ChannelKey = std::tuple<ChannelName, IrcHostname>;
  std::map<ChannelKey, std::set<Resource>> resources_in_chan;
  std::map<IrcHostname, std::set<Resource>> resources_in_server;
  /**
   * Manage which resource is in which channel
   */
  void add_resource_to_chan(const ChannelKey& channel_key, const std::string& resource);
  void remove_resource_from_chan(const ChannelKey& channel_key, const std::string& resource);
  bool is_resource_in_chan(const ChannelKey& channel_key, const std::string& resource) const;
290
  std::size_t number_of_resources_in_chan(const ChannelKey& channel_key) const;
291 292 293 294

  void add_resource_to_server(const IrcHostname& irc_hostname, const std::string& resource);
  void remove_resource_from_server(const IrcHostname& irc_hostname, const std::string& resource);
  bool is_resource_in_server(const IrcHostname& irc_hostname, const std::string& resource) const;
295
  size_t number_of_channels_the_resource_is_in(const std::string& irc_hostname, const std::string& resource) const;
296

297
  /**
298 299 300
   * Generate all the stanzas to be sent to this resource, simulating a join on this channel.
   * This means sending the whole user list, the topic, etc
   * TODO: send message history
301
   */
302
  void generate_channel_join_for_resource(const Iid& iid, const std::string& resource);
303 304 305
#ifdef USE_DATABASE
  bool record_history { true };
#endif
306 307
};

308 309 310 311 312 313 314
struct IRCNotConnected: public std::exception
{
  IRCNotConnected(const std::string& hostname):
    hostname(hostname) {}
  const std::string hostname;
};

315