Blame view

libpurple/purpleline.hpp 7.13 KB
20733774   Matti Virkkunen   Initial commit - ...
1
2
  #pragma once
  
f7bd89a8   Matti Virkkunen   Implemented text ...
3
4
5
  #include <string>
  #include <deque>
  
82987920   Matti Virkkunen   Implemented sendi...
6
  #include <cmds.h>
93706fc9   Matti Virkkunen   Refactored Thrift...
7
8
9
10
  #include <debug.h>
  #include <plugin.h>
  #include <prpl.h>
  
0ac35486   Matti Virkkunen   Use libpurple HTT...
11
  #include "constants.hpp"
39cee5be   Matti Virkkunen   Refactoring: spli...
12
  #include "thriftclient.hpp"
0ac35486   Matti Virkkunen   Use libpurple HTT...
13
  #include "httpclient.hpp"
9d4cbc44   Matti Virkkunen   Split fetchOperat...
14
15
  #include "poller.hpp"
  #include "pinverifier.hpp"
dc8f9a72   Matti Virkkunen   Log in and get pr...
16
  
93706fc9   Matti Virkkunen   Refactored Thrift...
17
  class ThriftClient;
20733774   Matti Virkkunen   Initial commit - ...
18
  
0d8a5e28   Matti Virkkunen   Even more buddy l...
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
  template <typename T>
  struct _blist_node_type { };
  
  template <>
  struct _blist_node_type<PurpleBuddy> {
      static const PurpleBlistNodeType type = PURPLE_BLIST_BUDDY_NODE;
      static PurpleAccount *get_account(PurpleBuddy *buddy) {
          return purple_buddy_get_account(buddy);
      }
  };
  
  template <>
  struct _blist_node_type<PurpleChat> {
      static const PurpleBlistNodeType type = PURPLE_BLIST_CHAT_NODE;
      static PurpleAccount *get_account(PurpleChat *chat) {
          return purple_chat_get_account(chat);
      }
  };
  
9d4cbc44   Matti Virkkunen   Split fetchOperat...
38
39
40
41
  enum class ChatType {
      ANY = 0,
      GROUP = 1,
      ROOM = 2,
38a0988d   Matti Virkkunen   Implemented leavi...
42
      GROUP_INVITE = 3,
9d4cbc44   Matti Virkkunen   Split fetchOperat...
43
  };
98c73d7b   Matti Virkkunen   Added reconnectio...
44
  
61ed212d   Matti Virkkunen   Split purpleline....
45
46
47
48
49
50
  std::string markup_escape(std::string const &text);
  
  std::string markup_unescape(std::string const &markup);
  
  std::string url_encode(std::string const &str);
  
9d4cbc44   Matti Virkkunen   Split fetchOperat...
51
  class PurpleLine {
0d8a5e28   Matti Virkkunen   Even more buddy l...
52
  
5892b95c   Matti Virkkunen   Added /open comma...
53
54
55
56
57
58
59
60
61
62
63
      struct Attachment {
          line::ContentType::type type;
          std::string id;
          std::string path;
  
          Attachment(line::ContentType::type type, std::string id)
              : type(type), id(id)
          {
          }
      };
  
0d8a5e28   Matti Virkkunen   Even more buddy l...
64
65
      static std::map<ChatType, std::string> chat_type_to_string;
  
20733774   Matti Virkkunen   Initial commit - ...
66
67
68
      PurpleConnection *conn;
      PurpleAccount *acct;
  
9d4cbc44   Matti Virkkunen   Split fetchOperat...
69
      boost::shared_ptr<ThriftClient> c_out;
0ac35486   Matti Virkkunen   Use libpurple HTT...
70
71
  
      HTTPClient http;
9d4cbc44   Matti Virkkunen   Split fetchOperat...
72
  
0fa93df6   Matti Virkkunen   Added support for...
73
74
75
      // Remove if libpurple HTTP ever gets support for binary request bodies
      LineHttpTransport os_http;
  
9d4cbc44   Matti Virkkunen   Split fetchOperat...
76
77
      friend class Poller;
      Poller poller;
20733774   Matti Virkkunen   Initial commit - ...
78
  
9d4cbc44   Matti Virkkunen   Split fetchOperat...
79
80
      friend class PINVerifier;
      PINVerifier pin_verifier;
f7bd89a8   Matti Virkkunen   Implemented text ...
81
  
0d8a5e28   Matti Virkkunen   Even more buddy l...
82
      int next_purple_id;
fc604ce8   Matti Virkkunen   Added initial sup...
83
  
f7bd89a8   Matti Virkkunen   Implemented text ...
84
85
      std::deque<std::string> recent_messages;
  
5892b95c   Matti Virkkunen   Added /open comma...
86
87
      std::vector<std::string> temp_files;
  
0d8a5e28   Matti Virkkunen   Even more buddy l...
88
      line::Profile profile;
43975ee1   Matti Virkkunen   Implement accepti...
89
90
      line::Contact profile_contact; // contains some fields from profile
      line::Contact no_contact; // empty object
0d8a5e28   Matti Virkkunen   Even more buddy l...
91
92
93
      std::map<std::string, line::Group> groups;
      std::map<std::string, line::Room> rooms;
      std::map<std::string, line::Contact> contacts;
fc604ce8   Matti Virkkunen   Added initial sup...
94
  
64d14eab   Matti Virkkunen   Implement account...
95
96
97
      void *pin_ui_handle;
      guint pin_timeout;
  
20733774   Matti Virkkunen   Initial commit - ...
98
  public:
dc8f9a72   Matti Virkkunen   Log in and get pr...
99
  
20733774   Matti Virkkunen   Initial commit - ...
100
      PurpleLine(PurpleConnection *conn, PurpleAccount *acct);
b34f8916   Matti Virkkunen   Replaced wrappers...
101
      ~PurpleLine();
20733774   Matti Virkkunen   Initial commit - ...
102
  
4834479b   Matti Virkkunen   Add /history comm...
103
      static void register_commands();
b34f8916   Matti Virkkunen   Replaced wrappers...
104
105
      static const char *list_icon(PurpleAccount *, PurpleBuddy *buddy);
      static GList *status_types(PurpleAccount *);
ede54633   Matti Virkkunen   More buddy list h...
106
107
      static char *status_text(PurpleBuddy *buddy);
      static void tooltip_text(PurpleBuddy *buddy, PurpleNotifyUserInfo *user_info, gboolean full);
b34f8916   Matti Virkkunen   Replaced wrappers...
108
      static void login(PurpleAccount *acct);
20733774   Matti Virkkunen   Initial commit - ...
109
  
20733774   Matti Virkkunen   Initial commit - ...
110
      void close();
f7bd89a8   Matti Virkkunen   Implemented text ...
111
      int send_im(const char *who, const char *message, PurpleMessageFlags flags);
38a0988d   Matti Virkkunen   Implemented leavi...
112
      void remove_buddy(PurpleBuddy *buddy, PurpleGroup *);
f7bd89a8   Matti Virkkunen   Implemented text ...
113
  
82987920   Matti Virkkunen   Implemented sendi...
114
      PurpleCmdRet cmd_sticker(PurpleConversation *conv,
5892b95c   Matti Virkkunen   Added /open comma...
115
          const gchar *, gchar **args, gchar **error, void *);
82987920   Matti Virkkunen   Implemented sendi...
116
  
4834479b   Matti Virkkunen   Add /history comm...
117
      PurpleCmdRet cmd_history(PurpleConversation *conv,
5892b95c   Matti Virkkunen   Added /open comma...
118
119
120
121
          const gchar *, gchar **args, gchar **error, void *);
  
      PurpleCmdRet cmd_open(PurpleConversation *conv,
          const gchar *, gchar **args, gchar **error, void *);
4834479b   Matti Virkkunen   Add /history comm...
122
  
dc8f9a72   Matti Virkkunen   Log in and get pr...
123
124
  private:
  
38a0988d   Matti Virkkunen   Implemented leavi...
125
126
127
      void connect_signals();
      void disconnect_signals();
  
5892b95c   Matti Virkkunen   Added /open comma...
128
129
130
131
132
133
      std::string get_tmp_dir(bool create=false);
  
      std::string conv_attachment_add(PurpleConversation *conv,
          line::ContentType::type type, std::string id);
      Attachment *conv_attachment_get(PurpleConversation *conv, std::string token);
  
61ed212d   Matti Virkkunen   Split purpleline....
134
135
136
      void write_message(line::Message &msg, bool replay);
      void write_message(PurpleConversation *conv, std::string &from, std::string &text,
          time_t mtime, int flags);
2f850f01   Matti Virkkunen   Separated chat-re...
137
138
139
140
141
142
143
  
      std::string get_room_display_name(line::Room &room);
      void set_chat_participants(PurpleConvChat *chat, line::Room &room);
      void set_chat_participants(PurpleConvChat *chat, line::Group &group);
  
      line::Contact &get_up_to_date_contact(line::Contact &c);
  
0fa93df6   Matti Virkkunen   Added support for...
144
145
146
147
148
      int send_message(std::string to, const char *markup);
      void send_message(
          line::Message &msg,
          std::function<void(line::Message &msg)> callback=std::function<void(line::Message &)>());
      void upload_media(std::string message_id, std::string type, std::string data);
2f850f01   Matti Virkkunen   Separated chat-re...
149
150
151
152
153
154
155
156
157
158
      void push_recent_message(std::string id);
  
      void signal_blist_node_removed(PurpleBlistNode *node);
      void signal_conversation_created(PurpleConversation *conv);
      void signal_deleting_conversation(PurpleConversation *conv);
  
      void fetch_conversation_history(PurpleConversation *conv, int count, bool requested);
  
      void notify_error(std::string msg);
  
a29acd28   Matti Virkkunen   Split login sync ...
159
160
161
162
163
164
165
      // sync
  
  private:
  
      // Login process methods, executed in this this order
      void login_start();
  
ba0ede92   Matti Virkkunen   Store auth token ...
166
      void get_auth_token();
86412402   Matti Virkkunen   Implement RSA log...
167
      std::string get_encrypted_credentials(line::RSAKey &key);
ba0ede92   Matti Virkkunen   Store auth token ...
168
      void set_auth_token(std::string auth_token);
a29acd28   Matti Virkkunen   Split login sync ...
169
170
171
172
173
      void get_last_op_revision();
      void get_profile();
      void get_contacts();
      void get_groups();
      void get_rooms();
67c95632   Matti Virkkunen   Included a minima...
174
      void update_rooms(line::MessageBoxWrapUpList wrap_up_list);
a29acd28   Matti Virkkunen   Split login sync ...
175
176
177
178
      void get_group_invites();
  
      void login_done();
  
2f850f01   Matti Virkkunen   Separated chat-re...
179
180
181
      // blist
  
  private:
fc604ce8   Matti Virkkunen   Added initial sup...
182
  
0d8a5e28   Matti Virkkunen   Even more buddy l...
183
      template <typename T>
9d4cbc44   Matti Virkkunen   Split fetchOperat...
184
      std::set<T *> blist_find(std::function<bool(T *)> predicate);
0d8a5e28   Matti Virkkunen   Even more buddy l...
185
186
187
188
189
190
  
      template <typename T>
      std::set<T *> blist_find() {
          return blist_find<T>([](T *){ return true; });
      }
  
ede54633   Matti Virkkunen   More buddy list h...
191
192
193
      PurpleGroup *blist_ensure_group(std::string group_name, bool temporary=false);
      PurpleBuddy *blist_ensure_buddy(std::string uid, bool temporary=false);
      void blist_update_buddy(std::string uid, bool temporary=false);
0d8a5e28   Matti Virkkunen   Even more buddy l...
194
195
196
197
198
199
200
201
202
203
204
205
206
      PurpleBuddy *blist_update_buddy(line::Contact &contact, bool temporary=false);
      bool blist_is_buddy_in_any_conversation(std::string uid, PurpleConvChat *ignore_chat);
      void blist_remove_buddy(std::string uid,
          bool temporary_only=false, PurpleConvChat *ignore_chat=nullptr);
  
      std::set<PurpleChat *> blist_find_chats_by_type(ChatType type);
      PurpleChat *blist_find_chat(std::string id, ChatType type);
      PurpleChat *blist_ensure_chat(std::string id, ChatType type);
      void blist_update_chat(std::string id, ChatType type);
      PurpleChat *blist_update_chat(line::Group &group);
      PurpleChat *blist_update_chat(line::Room &room);
      void blist_remove_chat(std::string id, ChatType type);
  
2f850f01   Matti Virkkunen   Separated chat-re...
207
      // chats
f7bd89a8   Matti Virkkunen   Implemented text ...
208
  
2f850f01   Matti Virkkunen   Separated chat-re...
209
210
  public:
      static char *get_chat_name(GHashTable *components);
239fea72   Matti Virkkunen   Implemented synci...
211
  
2f850f01   Matti Virkkunen   Separated chat-re...
212
213
214
215
216
217
      GList *chat_info();
      void join_chat(GHashTable *components);
      void reject_chat(GHashTable *components);
      void chat_leave(int id);
      int chat_send(int id, const char *message, PurpleMessageFlags flags);
      PurpleChat *find_blist_chat(const char *name);
f7bd89a8   Matti Virkkunen   Implemented text ...
218
  
2f850f01   Matti Virkkunen   Separated chat-re...
219
  private:
4834479b   Matti Virkkunen   Add /history comm...
220
  
2f850f01   Matti Virkkunen   Separated chat-re...
221
      static ChatType get_chat_type(const char *type_ptr);
4834479b   Matti Virkkunen   Add /history comm...
222
  
2f850f01   Matti Virkkunen   Separated chat-re...
223
224
225
      void join_chat_success(ChatType type, std::string id);
  
      void handle_group_invite(line::Group &group, line::Contact &invitee, line::Contact &inviter);
20733774   Matti Virkkunen   Initial commit - ...
226
  };
9d4cbc44   Matti Virkkunen   Split fetchOperat...
227
228
229
230
231
232
233
234
235
  
  template <typename T>
  std::set<T *> PurpleLine::blist_find(std::function<bool(T *)> predicate) {
      std::set<T *> results;
  
      for (PurpleBlistNode *node = purple_blist_get_root();
          node;
          node = purple_blist_node_next(node, FALSE))
      {
a1590b5a   Matti Virkkunen   Properly check th...
236
237
          if (purple_blist_node_get_type(node) == _blist_node_type<T>::type
              && _blist_node_type<T>::get_account((T *)node) == acct
9d4cbc44   Matti Virkkunen   Split fetchOperat...
238
239
240
241
242
243
244
245
              && predicate((T *)node))
          {
              results.insert((T *)node);
          }
      }
  
      return results;
  }