bridge.hpp 5.56 KB
Newer Older
1 2 3 4
#ifndef BRIDGE_INCLUDED
# define BRIDGE_INCLUDED

#include <irc/irc_client.hpp>
5
#include <bridge/colors.hpp>
6
#include <irc/irc_user.hpp>
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#include <irc/iid.hpp>

#include <unordered_map>
#include <string>
#include <memory>

class XmppComponent;
class Poller;

/**
 * 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:
25
  explicit Bridge(const std::string& user_jid, XmppComponent* xmpp, std::shared_ptr<Poller> poller);
26
  ~Bridge();
louiz’'s avatar
louiz’ committed
27 28 29
  /**
   * QUIT all connected IRC servers.
   */
30
  void shutdown(const std::string& exit_message);
louiz’'s avatar
louiz’ committed
31 32 33 34
  /**
   * Remove all inactive IrcClients
   */
  void clean();
35 36 37 38
  /**
   * Return the jid of the XMPP user using this bridge
   */
  const std::string& get_jid() const;
39

40
  static Xmpp::body make_xmpp_body(const std::string& str);
41 42 43 44 45 46
  /***
   **
   ** From XMPP to IRC.
   **
   **/

louiz’'s avatar
louiz’ committed
47 48 49 50 51
  /**
   * Try to join an irc_channel, does nothing and return true if the channel
   * was already joined.
   */
  bool join_irc_channel(const Iid& iid, const std::string& username);
louiz’'s avatar
louiz’ committed
52
  void send_channel_message(const Iid& iid, const std::string& body);
louiz’'s avatar
louiz’ committed
53
  void send_private_message(const Iid& iid, const std::string& body, const std::string& type="PRIVMSG");
louiz’'s avatar
louiz’ committed
54
  void leave_irc_channel(Iid&& iid, std::string&& status_message);
louiz’'s avatar
louiz’ committed
55
  void send_irc_nick_change(const Iid& iid, const std::string& new_nick);
56
  void send_irc_kick(const Iid& iid, const std::string& target, const std::string& reason);
57
  void set_channel_topic(const Iid& iid, const std::string& subject);
louiz’'s avatar
louiz’ committed
58
  void send_xmpp_version_to_irc(const Iid& iid, const std::string& name, const std::string& version, const std::string& os);
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

  /***
   **
   ** From IRC to XMPP.
   **
   **/

  /**
   * Send a message corresponding to a server NOTICE, the from attribute
   * should be juste the server hostname@irc.component.
   */
  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.
   */
74 75
  void send_user_join(const std::string& hostname,
                      const std::string& chan_name,
76
                      const IrcUser* user,
77
                      const char user_mode,
78
                      const bool self);
79 80 81 82
  /**
   * Send the topic of the MUC to the user
   */
  void send_topic(const std::string& hostname, const std::string& chan_name, const std::string topic);
louiz’'s avatar
louiz’ committed
83 84 85
  /**
   * Send a MUC message from some participant
   */
louiz’'s avatar
louiz’ committed
86
  void send_message(const Iid& iid, const std::string& nick, const std::string& body, const bool muc);
87 88 89 90 91
  /**
   * Send a presence of type error, from a room. This is used to indicate
   * why joining a room failed.
   */
  void send_join_failed(const Iid& iid, const std::string& nick, const std::string& type, const std::string& condition, const std::string& text);
louiz’'s avatar
louiz’ committed
92 93 94
  /**
   * Send an unavailable presence from this participant
   */
louiz’'s avatar
louiz’ committed
95 96 97
  void send_muc_leave(Iid&& iid, std::string&& nick, const std::string& message, const bool self);
  /**
   * Send presences to indicate that an user old_nick (ourself if self ==
98 99 100
   * 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
101
   */
102 103 104 105 106
  void send_nick_change(Iid&& iid,
                        const std::string& old_nick,
                        const std::string& new_nick,
                        const char user_mode,
                        const bool self);
107
  void kick_muc_user(Iid&& iid, const std::string& target, const std::string& reason, const std::string& author);
108
  void send_nickname_conflict_error(const Iid& iid, const std::string& nickname);
109 110 111 112
  /**
   * 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);
113 114 115 116
  /**
   * Send an iq version request coming from nick%hostname@
   */
  void send_iq_version_request(const std::string& nick, const std::string& hostname);
louiz’'s avatar
louiz’ committed
117 118 119 120 121

  /**
   * Misc
   */
  std::string get_own_nick(const Iid& iid);
louiz’'s avatar
louiz’ committed
122
  /**
123
   * Get the number of server to which this bridge is connected or connecting.
louiz’'s avatar
louiz’ committed
124
   */
125
  size_t active_clients() const;
louiz’'s avatar
louiz’ committed
126

127 128 129 130 131 132 133
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.
   */
  IrcClient* get_irc_client(const std::string& hostname, const std::string& username);
louiz’'s avatar
louiz’ committed
134 135 136 137 138
  /**
   * This version does not create the IrcClient if it does not exist, and
   * returns nullptr in that case
   */
  IrcClient* get_irc_client(const std::string& hostname);
139 140 141 142
  /**
   * The JID of the user associated with this bridge. Messages from/to this
   * JID are only managed by this bridge.
   */
143
  const std::string user_jid;
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
  /**
   * 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;
  /**
   * A raw pointer, because we do not own it, the XMPP component owns us,
   * but we still need to communicate with it, when sending messages from
   * IRC to XMPP.
   */
  XmppComponent* xmpp;
  /**
   * Poller, to give it the IrcClients that we spawn, to make it manage
   * their sockets.
   */
159
  std::shared_ptr<Poller> poller;
160 161 162 163 164 165 166 167

  Bridge(const Bridge&) = delete;
  Bridge(Bridge&& other) = delete;
  Bridge& operator=(const Bridge&) = delete;
  Bridge& operator=(Bridge&&) = delete;
};

#endif // BRIDGE_INCLUDED