1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
/*
* Copyright 2014 James Geboski <jgeboski@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file **/
#ifndef _FACEBOOK_API_H
#define _FACEBOOK_API_H
#include <bitlbee.h>
#include "facebook-http.h"
#include "facebook-id.h"
#include "facebook-json.h"
#include "facebook-mqtt.h"
#define FB_API_HOST "api.facebook.com"
#define FB_API_BHOST "b-api.facebook.com"
#define FB_API_GHOST "graph.facebook.com"
#define FB_API_AGENT "Facebook App / " PACKAGE " / " PACKAGE_VERSION
#define FB_API_KEY "256002347743983"
#define FB_API_SECRET "374e60f8b9bb6b8cbb30f78030438895"
#define FB_API_PATH_AUTH "/method/auth.login"
#define FB_API_PATH_FQL "/fql"
#define FB_API_PATH_GQL "/graphql"
#define FB_API_PATH_PARTS "/participants"
#define FB_API_PATH_THRDS "/me/threads"
#define FB_API_PATH_TOPIC "/method/messaging.setthreadname"
#define FB_API_QRYID_CONTACTS "10153122424521729"
/**
* Executes one of the #fb_api_funcs.
*
* @param a The #fb_api.
* @param f The function to execute.
* @param ... The function arguments.
**/
#define FB_API_FUNC(m, f, ...) \
G_STMT_START { \
if (G_LIKELY((m)->funcs.f != NULL)) { \
(m)->funcs.f(m, ##__VA_ARGS__, (m)->data); \
} \
} G_STMT_END
/**
* Creates a message identifier.
*
* @param m The time in miliseconds (UTC).
* @param i The random integer.
*
* @return The 64-bit message identifier.
**/
#define FB_API_MSGID(m, i) ((guint64) ( \
(((guint32) i) & 0x3FFFFF) | \
(((guint64) m) << 22) \
))
/** The #GError codes of #fb_api. **/
typedef enum fb_api_error fb_api_error_t;
/** The structure for interacting with the Facebook API. **/
typedef struct fb_api fb_api_t;
/** The main structure for #fb_api callback functions. **/
typedef struct fb_api_funcs fb_api_funcs_t;
/** The structure for representing an #fb_api message. **/
typedef struct fb_api_msg fb_api_msg_t;
/** The structure for representing an #fb_api presence. **/
typedef struct fb_api_pres fb_api_pres_t;
/** The structure for representing an #fb_api thread. **/
typedef struct fb_api_thread fb_api_thread_t;
/** The structure for representing an #fb_api user typing state. **/
typedef struct fb_api_typing fb_api_typing_t;
/** The structure for representing an #fb_api user. **/
typedef struct fb_api_user fb_api_user_t;
/**
* The #GError codes of #fb_api.
**/
enum fb_api_error
{
FB_API_ERROR_GENERAL /** General **/
};
/**
* The main structure for #fb_api callback functions.
**/
struct fb_api_funcs
{
/**
* The error function. This is called whenever an error occurs
* within the #fb_api.
*
* @param api The #fb_api.
* @param err The #GError.
* @param data The user-defined data or NULL.
**/
void (*error) (fb_api_t *api, GError *err, gpointer data);
/**
* The auth function. This is called whenever authentication has
* been successfully completed. This is called as a result of
* #fb_api_auth().
*
* @param api The #fb_api.
* @param data The user-defined data or NULL.
**/
void (*auth) (fb_api_t *api, gpointer data);
/**
* The connect function. This is called whenever the #fb_api has
* been successfully connected. This connects to the MQTT service.
* This is called as a result of #fb_api_connect().
*
* @param api The #fb_api.
* @param data The user-defined data or NULL.
**/
void (*connect) (fb_api_t *api, gpointer data);
/**
* The contacts function. This is called whenever the #fb_api has
* retrieved a set contacts. This is called as a result of
* #fb_api_contacts().
*
* @param api The #fb_api.
* @param users The #GSList of #fb_api_user.
* @param data The user-defined data or NULL.
**/
void (*contacts) (fb_api_t *api, const GSList *users, gpointer data);
/**
* The message function. This is called whenever the #fb_api has
* retrieved a message.
*
* @param api The #fb_api.
* @param msgs The #GSList of #fb_api_msg.
* @param data The user-defined data or NULL.
**/
void (*message) (fb_api_t *api, const GSList *msgs, gpointer data);
/**
* The presence function. This is called whenever the #fb_api has
* retrieved a presence update.
*
* @param api The #fb_api.
* @param press The #GSList of #fb_api_pres.
* @param data The user-defined data or NULL.
**/
void (*presence) (fb_api_t *api, const GSList *press, gpointer data);
/**
* The thread_create function. This is called whenever the #fb_api
* has created a thread. This is called as a result of
* #fb_api_thread_create().
*
* @param api The #fb_api.
* @param tid The thread #fb_id.
* @param data The user-defined data or NULL.
**/
void (*thread_create) (fb_api_t *api, fb_id_t tid, gpointer data);
/**
* The thread_info function. This is called whenever the #fb_api
* has retrieved thread information. This is called as a result of
* #fb_api_thread_info().
*
* @param api The #fb_api.
* @param thrd The #fb_api_thread.
* @param data The user-defined data or NULL.
**/
void (*thread_info) (fb_api_t *api, fb_api_thread_t *thrd, gpointer data);
/**
* The thread_list function. This is called whenever the #fb_api
* has retrieved a set of threads. This is called as a result of
* #fb_api_thread_list().
*
* @param api The #fb_api.
* @param thrds The #GSList of #fb_api_thread.
* @param data The user-defined data or NULL.
**/
void (*thread_list) (fb_api_t *api, const GSList *thrds, gpointer data);
/**
* The typing function. This is called whenever the #fb_api has
* retrieved a typing state update.
*
* @param api The #fb_api.
* @param typg The #fb_api_typing.
* @param data The user-defined data or NULL.
**/
void (*typing) (fb_api_t *api, fb_api_typing_t *typg, gpointer data);
};
/**
* The structure for interacting with the Facebook API.
**/
struct fb_api
{
fb_api_funcs_t funcs; /** The #fb_api_funcs. **/
gpointer data; /** The user-defined data or NULL. **/
fb_http_t *http; /** The #fb_http. **/
fb_mqtt_t *mqtt; /** The #fb_mqtt. **/
GError *err; /** The #GError or NULL. **/
GQueue *msgs; /** The #GQueue of raw messages. **/
fb_id_t uid; /** The The #fb_id of the user. **/
gchar *token; /** The session token. **/
gchar *stoken; /** The sync token. **/
gchar *cid; /** The client identifier. **/
gchar *mid; /** The MQTT identifier. **/
gchar *cuid; /** The client unique identifier. **/
};
/**
* The structure for representing an #fb_api message.
**/
struct fb_api_msg
{
fb_id_t uid; /** The #fb_id of the user. **/
fb_id_t tid; /** The #fb_id of the thread. **/
const gchar *text; /** The message text. **/
};
/**
* The structure for representing an #fb_api presence.
**/
struct fb_api_pres
{
fb_id_t uid; /** The #fb_id of the user. **/
gboolean active; /** TRUE if the user is active. **/
};
/**
* The structure for representing an #fb_api thread.
**/
struct fb_api_thread
{
fb_id_t tid; /** The #fb_id of the thread. **/
const gchar *topic; /** The topic of the thread or NULL. **/
GSList *users; /** The #GList of #fb_api_user. **/
};
/**
* The structure for representing an #fb_api user typing state.
**/
struct fb_api_typing
{
fb_id_t uid; /** The #fb_id of the user. **/
gboolean state; /** TRUE if the user is typing. **/
};
/**
* The structure for representing an #fb_api user.
**/
struct fb_api_user
{
fb_id_t uid; /** The #fb_id of the user. **/
const gchar *name; /** The name of the user. **/
};
#define FB_API_ERROR fb_api_error_quark()
GQuark fb_api_error_quark(void);
fb_api_t *fb_api_new(const fb_api_funcs_t *funcs, gpointer data);
void fb_api_rehash(fb_api_t *api);
void fb_api_free(fb_api_t *api);
void fb_api_error(fb_api_t *api, fb_api_error_t err, const gchar *fmt, ...);
void fb_api_auth(fb_api_t *api, const gchar *user, const gchar *pass);
void fb_api_contacts(fb_api_t *api);
void fb_api_connect(fb_api_t *api);
void fb_api_disconnect(fb_api_t *api);
void fb_api_message(fb_api_t *api, fb_id_t id, gboolean thread,
const gchar *msg);
void fb_api_publish(fb_api_t *api, const gchar *topic, const gchar *fmt, ...);
void fb_api_thread_create(fb_api_t *api, GSList *uids);
void fb_api_thread_info(fb_api_t *api, fb_id_t tid);
void fb_api_thread_invite(fb_api_t *api, fb_id_t tid, fb_id_t uid);
void fb_api_thread_list(fb_api_t *api, guint limit);
void fb_api_thread_topic(fb_api_t *api, fb_id_t tid, const gchar *topic);
void fb_api_typing(fb_api_t *api, fb_id_t uid, gboolean state);
#endif /* _FACEBOOK_API_H */
|