Operator
C++ library for plugging into matrix.org
All Classes Namespaces Files Functions Typedefs Pages
client.hh
Go to the documentation of this file.
1 
20 #ifndef __OPERATOR_CLIENT_HH_
21 #define __OPERATOR_CLIENT_HH_
22 
23 #include "structs.hh"
24 #include "event.hh"
25 
26 #include <memory>
27 #include <string>
28 #include <map>
29 #include <list>
30 #include <set>
31 
32 namespace Operator {
33  namespace Client {
41  class Client {
42  public:
43  Client();
44 
45  class Room {
46  public:
47  Room(Client &c, const std::string &_id, Event::RoomMembership::membership_t _membership)
48  : client(c), id(_id), membership(_membership) {}
49 
50  const std::string &getID() const { return id; }
51  Event::RoomMembership::membership_t getMembership() const { return membership; }
52 
53  void on(const std::string &type, const callback_t<Room &, const Event::Event &> &);
54 
55  protected:
56  Client &client;
57  std::string id;
58  Event::RoomMembership::membership_t membership;
59  std::map<std::string, nlohmann::json> state;
60  std::map<std::string, std::list<callback_t<Room &, const Event::Event &> > > listeners;
61 
62  friend class ::Operator::Client::Client;
63  };
64 
65  virtual ~Client() {}
66 
74  virtual void login(const std::string &username, const std::string &password, const callback_t<const std::string &,const std::string &> & = noop_callback<const std::string &,const std::string &>) = 0;
75 
79  virtual void loginWith3PID(const std::string &medium, const std::string &address, const std::string &password, const callback_t<const std::string &,const std::string &> & = noop_callback<const std::string &,const std::string &>) = 0;
80 
84  virtual void logout(const callback_t<> & = noop_callback<>) = 0;
85 
88  virtual bool isLoggedIn() const = 0;
89 
92  virtual void startClient() = 0;
95  virtual void stopClient() = 0;
96 
102  virtual void sendMessage(const Event::MessageEvent &message, const callback_t<const std::string &> &callback = noop_callback<const std::string &>) = 0;
103 
108  Room &getRoom(const std::string &room_id) {
109  auto room_it = rooms.find(room_id);
110  if (room_it != rooms.end())
111  {
112  return room_it->second;
113  }
114  else
115  {
116  rooms.emplace(room_id, Room(*this, room_id, Event::RoomMembership::NONE));
117  }
118  }
119  const Room &getRoom(const std::string &room_id) const throw (std::out_of_range) {
120  return rooms.at(room_id);
121  }
122 
123  Room &operator[](const std::string &room_id) {
124  return getRoom(room_id);
125  }
126  const Room &operator[](const std::string &room_id) const throw (std::out_of_range) {
127  return getRoom(room_id);
128  }
129 
139  void on(const std::string &type, const callback_t<Room &, const Event::Event &> &callback);
155  template <class T> inline void on(const callback_t<Room &, const Event::Event &> &callback) { on(T::TYPE, callback); }
156  protected:
159  void trigger(const Event::Event &event);
160  void triggerError(const std::string &event_type, std::shared_ptr<Error::Error> error, const std::string &room_id = "");
161  std::map<std::string, Room> rooms;
162 
163  void _setRoomMembership(Room &room, Event::RoomMembership::membership_t membership) {
164  room.membership = membership;
165  }
166  private:
167  std::map<std::string, std::list<callback_t<Room &, const Event::Event &> > > listeners;
168  };
169 
175  inline Client &operator<<(Client &client, const Event::MessageEvent &message) {
176  client.sendMessage(message, noop_callback<const std::string &>);
177  return client;
178  }
179  }
180 }
181 
182 #endif // __OPERATOR_CLIENT_HH_
general "message" events (a.k.a. timeline events)
Definition: event.hh:115
Client & operator<<(Client &client, const Event::MessageEvent &message)
send a message event.
Definition: client.hh:175
Matrix events.
Definition: client.hh:32
virtual void loginWith3PID(const std::string &medium, const std::string &address, const std::string &password, const callback_t< const std::string &, const std::string &> &=noop_callback< const std::string &, const std::string &>)=0
log in using third party ID and pasword.
virtual void sendMessage(const Event::MessageEvent &message, const callback_t< const std::string &> &callback=noop_callback< const std::string &>)=0
send a message event.
Matrix client interface for high-level API.This interface is implemented by the high-level API classe...
Definition: client.hh:41
virtual void startClient()=0
start listening for new events.
Room & getRoom(const std::string &room_id)
return the room with the given ID.
Definition: client.hh:108
virtual void stopClient()=0
stop listening for new events.
void on(const callback_t< Room &, const Event::Event &> &callback)
register an event handler.
Definition: client.hh:155
virtual void logout(const callback_t<> &=noop_callback<>)=0
log out.
void trigger(const Event::Event &event)
call event handlers for the given event.
Definition: client.cc:39
Definition: client.hh:45
virtual void login(const std::string &username, const std::string &password, const callback_t< const std::string &, const std::string &> &=noop_callback< const std::string &, const std::string &>)=0
log in using username and password
virtual bool isLoggedIn() const =0
determine if the client is currently logged in.
typename std::function< void(std::shared_ptr< Error::Error > err, T...)> callback_t
general type for callbacks
Definition: structs.hh:43
base event class.
Definition: event.hh:35
data types for Matrix