Operator
C++ library for plugging into matrix.org
All Classes Namespaces Files Functions Typedefs Pages
event.hh
Go to the documentation of this file.
1 
20 #ifndef __OPERATOR_EVENT_HH_
21 #define __OPERATOR_EVENT_HH_
22 
23 #include "error.hh"
24 
25 #include <functional>
26 #include <memory>
27 #include <string>
28 #include <vector>
29 #include <json.hpp>
30 
31 namespace Operator {
32  namespace Event {
35  class Event {
36  public:
37  Event(nlohmann::json d, const std::string &_room_id = "") throw (Error::Error);
38  virtual ~Event() {}
39 
40  const std::string &getType() const throw () { return data.at("type").get_ref<const std::string &>(); }
41  const nlohmann::json &getContent() const throw () { return data.at("content"); }
42  const nlohmann::json &getRawData() const throw () { return data; }
43  const std::string &getRoomID() const throw () { return room_id; }
44 
45  typedef std::function<std::unique_ptr<Event>(nlohmann::json data, const std::string &)> event_builder_t;
46  static std::unique_ptr<Event> newEvent(nlohmann::json data, const std::string & = "") throw (Error::Error);
47  static void registerEventClass(const std::string &type, event_builder_t);
48 
49  template<typename T> static std::unique_ptr<Event> _new_helper(nlohmann::json data, const std::string &room_id) {
50  return std::unique_ptr<Event>(new T(std::move(data), room_id));
51  }
52 
53  static const std::string TYPE;
54  protected:
55  nlohmann::json data;
56  std::string room_id;
57  const std::string &get_room_name(const nlohmann::json &data, const std::string &room_id) {
58  static std::string emptystring;
59  if (!room_id.empty())
60  {
61  return room_id;
62  }
63  auto it = data.find("room_id");
64  if (it == data.end() || !it->is_string())
65  {
66  return emptystring;
67  }
68  return it->get_ref<const std::string &>();
69  }
70  };
71 
74  class RoomEvent : public Event {
75  public:
76  RoomEvent(nlohmann::json d, const std::string &room_id = "") throw (Error::Error);
77 
78  const std::string &getEventID() const throw () { return data.at("event_id").get_ref<const std::string &>(); }
79  const std::string &getSender() const throw () { return data.at("sender").get_ref<const std::string &>(); }
80  const nlohmann::json &getUnsigned() const throw (std::out_of_range) { return data.at("unsigned"); }
81  // TODO: getOriginServerTs, getAge, ...
82 
83  static std::unique_ptr<Event> newEvent(nlohmann::json data, const std::string & = "") throw (Error::Error);
84  static void registerEventClass(const std::string &type, event_builder_t);
85  };
86 
89  class StateEvent : public RoomEvent {
90  public:
91  StateEvent(nlohmann::json d, const std::string &room_id = "") throw (Error::Error);
92 
93  const std::string &getStateKey() const throw() { return data.at("state_key").get_ref<const std::string &>(); }
94  const nlohmann::json &getPrevContent() const throw (std::out_of_range) {return data.at("prev_content"); }
95 
96  static std::unique_ptr<Event> newEvent(nlohmann::json data, const std::string & = "") throw (Error::Error);
97  static void registerEventClass(const std::string &type, event_builder_t);
98  };
99 
102  class RoomAliases : public RoomEvent {
103  public:
104  RoomAliases(nlohmann::json d, const std::string &room_id = "") throw (Error::Error);
105 
106  const std::vector<std::string> getAliases() const throw () { return aliases; }
107 
108  static const std::string TYPE;
109  private:
110  std::vector<std::string> aliases;
111  };
112 
115  class MessageEvent : public RoomEvent {
116  public:
117  MessageEvent(nlohmann::json d, const std::string &room_id = "") throw (Error::Error) : RoomEvent(std::move(d), room_id) {}
118  MessageEvent(const MessageEvent &other) throw ()
119  : RoomEvent(other) {}
120 
121  static std::unique_ptr<Event> newEvent(nlohmann::json data, const std::string & = "") throw (Error::Error);
122  static void registerEventClass(const std::string &type, event_builder_t);
123  };
124 
125  namespace Message {
128  class Message : public MessageEvent {
129  public:
130  Message(nlohmann::json d, const std::string &room_id = "") throw (Error::Error);
131  Message(const Message &other) throw ()
132  : MessageEvent(other) {}
133  Message(const std::string &msgtype, const std::string &body, const std::string &room_id) throw ();
134 
135  const std::string &getBody() const throw () { return getContent().at("body").get_ref<const std::string &>(); }
136  const std::string &getMessageType() const throw () { return getContent().at("msgtype").get_ref<const std::string &>(); }
137 
138  static const std::string TYPE;
139 
140  static void registerMessageClass(const std::string &type, event_builder_t);
141  static void registerMessageClass(const std::string &type, event_builder_t, event_builder_t);
142  static std::unique_ptr<Event> newEvent(nlohmann::json data, const std::string & = "") throw (Error::Error);
143  };
144 
147  template <class T> class _HTMLMessage : public T {
148  public:
149  _HTMLMessage<T>(nlohmann::json d, const std::string &room_id = "") throw (Error::Error)
150  : T(std::move(d), room_id) {}
151  _HTMLMessage<T>(const _HTMLMessage<T> &other) throw ()
152  : T(other) {}
153  _HTMLMessage<T>(const std::string &body, const std::string &html, const std::string &room_id)
154  : T({
155  {"type", Message::Message::TYPE},
156  {"room_id", room_id},
157  {"content", {
158  {"msgtype", T::MSGTYPE},
159  {"body", body},
160  {"format", "org.matrix.custom.html"},
161  {"formatted_body", html}
162  }}
163  }) {}
164  };
165 
168  class Text : public Message {
169  public:
170  Text(nlohmann::json d, const std::string &room_id = "") throw (Error::Error)
171  : Message(std::move(d), room_id) {
172  data["content"]["msgtype"] = MSGTYPE;
173  }
174  Text(const Text &other) throw ()
175  : Message(other) {}
176  Text(const std::string &body, const std::string &room)
177  : Message(body, MSGTYPE, room) {}
178 
179  static const std::string MSGTYPE;
180  };
181 
186  using HTML = _HTMLMessage<Text>;
187 
190  class Emote : public Message {
191  public:
192  Emote(nlohmann::json d, const std::string &room_id = "") throw (Error::Error)
193  : Message(std::move(d), room_id) {
194  data["content"]["msgtype"] = MSGTYPE;
195  }
196  Emote(const Text &other) throw ()
197  : Message(other) {}
198  Emote(const std::string &body, const std::string &room)
199  : Message(body, MSGTYPE, room) {}
200 
201  static const std::string MSGTYPE;
202  };
203 
209 
212  class Notice : public Message {
213  public:
214  Notice(nlohmann::json d, const std::string &room_id = "") throw (Error::Error)
215  : Message(std::move(d), room_id) {
216  data["content"]["msgtype"] = MSGTYPE;
217  }
218  Notice(const Text &other) throw ()
219  : Message(other) {}
220  Notice(const std::string &body, const std::string &room)
221  : Message(body, MSGTYPE, room) {}
222 
223  static const std::string MSGTYPE;
224  };
225 
231 
232  }
233 
236  class Login : public Event {
237  public:
238  Login(const std::string &user_id, const std::string &home_server) throw (Error::Error)
239  : Event({
240  {"type", Login::TYPE},
241  {"content", {
242  {"user_id", user_id},
243  {"home_server", home_server}
244  }}
245  }, "") {}
246 
247  const std::string &getUserID() const throw () { return getContent().at("user_id").get_ref<const std::string &>(); }
248  const std::string &getHomeServer() const throw () { return getContent().at("home_server").get_ref<const std::string &>(); }
249 
250  static const std::string TYPE;
251  };
252 
255  class Logout : public Event {
256  public:
257  Logout() throw (Error::Error)
258  : Event({
259  {"type", Logout::TYPE},
260  {"content", nlohmann::json::object()}
261  }, "") {}
262 
263  static const std::string TYPE;
264  };
265 
268  class RoomMembership : public Event {
269  public:
270  enum membership_t {NONE, INVITE, JOIN, KNOCK, LEAVE, BAN};
271  RoomMembership(membership_t membership, const std::string &room_id) throw (Error::Error)
272  : Event({
273  {"type", RoomMembership::TYPE},
274  {"room_id", room_id},
275  {"content", {
276  {"membership", membership}
277  }}
278  }, room_id) {}
279 
280  membership_t getMembership() const throw () { return static_cast<membership_t>(getContent().at("membership").get<int>()); }
281 
282  static const std::string TYPE;
283  };
284 
289  class Limited : public MessageEvent {
290  public:
291  Limited(const std::string &room_id) throw (Error::Error)
292  : MessageEvent({
293  {"type", Limited::TYPE},
294  {"room_id", room_id},
295  {"content", nlohmann::json::object()}
296  }, room_id) {}
297 
298  static const std::string TYPE;
299  };
300  }
301 }
302 
303 #endif // __OPERATOR_EVENT_HH_
general "message" events (a.k.a. timeline events)
Definition: event.hh:115
event relating to the state of a room.
Definition: event.hh:89
Definition: client.hh:32
the client has logged in
Definition: event.hh:236
the client has logged out
Definition: event.hh:255
HTML version of a message class.
Definition: event.hh:147
normal HTML messages
indicates that the sync result has been limited.
Definition: event.hh:289
normal text messages (msgtype m.text)
Definition: event.hh:168
base class for messages (events of type m.room.message)
Definition: event.hh:128
Error handling.
base error class.
Definition: error.hh:37
aliases for a room.
Definition: event.hh:102
event that is associated with a room.
Definition: event.hh:74
the user&#39;s membership in a room has changed
Definition: event.hh:268
emote messages (msgtype m.emote)
Definition: event.hh:190
notice messages (msgtype m.notice)
Definition: event.hh:212
base event class.
Definition: event.hh:35