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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
|
/* Module: imo_request.c
Purpose: Posts XMLHHTP-Requests to imo.im server
Author: leecher
Date: 30.08.2009
*/
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#define LockMutex(x) EnterCriticalSection (&x)
#define UnlockMutex(x) LeaveCriticalSection(&x)
#define InitMutex(x) InitializeCriticalSection(&x)
#define ExitMutex(x) DeleteCriticalSection(&x)
#define mutex_t CRITICAL_SECTION
#else
#include <pthread.h>
#define LockMutex(x) pthread_mutex_lock(&x)
#define UnlockMutex(x) pthread_mutex_unlock(&x)
#define InitMutex(x) pthread_mutex_init(&x, NULL);
#define ExitMutex(x)
#define mutex_t pthread_mutex_t
#endif
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "fifo.h"
#include "imo_request.h"
#include "io_layer.h"
#define SSID_LENGTH 16
struct _tagIMORQ
{
IOLAYER *hIO;
char szSessId[SSID_LENGTH+2];
unsigned long send_ack; // Some Sending ACK number
unsigned long send_seq; // Within one ACK there seems to be a SEQ-Number?
unsigned long *psend_ack; // Pointer to send_ack to use
unsigned long *psend_seq; // Pointer to send_seq to use
mutex_t mutex; // Mutex for securing psend_ack and psend_seq read/write
BOOL bIsClone; // Indicates that the current handle is a clone
char *pszHost; // Login host
char szReqURL[32]; // Request-URL
};
static IOLAYER *(*IoLayer_Init)(void) =
#ifdef WIN32
IoLayerW32_Init;
#else
IoLayerCURL_Init;
#endif
// Forward declaration of private functions
static size_t add_data(char *data, size_t size, size_t nmemb, void *ctx);
static IMORQ *Init(void);
// -----------------------------------------------------------------------------
// Interface
// -----------------------------------------------------------------------------
void ImoRq_SetIOLayer(IOLAYER *(*fp_Init)(void))
{
IoLayer_Init = fp_Init;
}
// -----------------------------------------------------------------------------
IMORQ *ImoRq_Init(void)
{
IMORQ *hRq;
if (hRq = Init())
{
/* Create session ID */
ImoRq_CreateID (hRq->szSessId, SSID_LENGTH+1);
hRq->psend_seq = &hRq->send_seq;
hRq->psend_ack = &hRq->send_ack;
InitMutex(hRq->mutex);
/* Disptch version of imo.im protocol */
switch (IMO_API_VERSION)
{
case 0:
hRq->pszHost = "https://o.imo.im/";
break;
case 1:
hRq->pszHost = "https://imo.im/";
break;
}
sprintf (hRq->szReqURL, "%simo", hRq->pszHost);
/* Fetch start page to get cookies */
if (hRq->hIO->Get (hRq->hIO, hRq->pszHost, NULL))
/* Get new session ID from system */
{
char *pszRPC = ImoRq_ResetRPC (hRq), *pszRPCRes;
if (pszRPC)
{
if ((pszRPCRes = strstr(pszRPC, "ssid\":\"")) || (pszRPCRes = strstr(pszRPC, "ssid\": \"")))
strcpy (hRq->szSessId, strtok (pszRPCRes+7, "\""));
}
} else {
ImoRq_Exit(hRq);
hRq = NULL;
}
}
return hRq;
}
// -----------------------------------------------------------------------------
IMORQ *ImoRq_Clone (IMORQ *hRq)
{
IMORQ *hDup;
if (!(hDup = Init())) return NULL;
strcpy (hDup->szSessId, hRq->szSessId);
hDup->psend_seq = hRq->psend_seq;
hDup->psend_ack = hRq->psend_ack;
hDup->mutex = hRq->mutex;
hDup->bIsClone = TRUE;
hDup->pszHost = hRq->pszHost;
strcpy (hDup->szReqURL, hRq->szReqURL);
return hDup;
}
// -----------------------------------------------------------------------------
void ImoRq_Exit (IMORQ *hRq)
{
if (hRq->hIO) hRq->hIO->Exit(hRq->hIO);
if (!hRq->bIsClone) ExitMutex (hRq->mutex);
free (hRq);
}
// -----------------------------------------------------------------------------
void ImoRq_Cancel (IMORQ *hRq)
{
if (hRq->hIO) hRq->hIO->Cancel(hRq->hIO);
}
// -----------------------------------------------------------------------------
char *ImoRq_PostImo(IMORQ *hRq, char *pszMethod, cJSON *data)
{
TYP_FIFO *hPostString;
char *pszData, *pszEscData;
unsigned int uiCount = -1;
if (!(pszData = cJSON_Print(data))) return NULL;
printf ("-> %s\n", pszData);
#ifdef _WIN32
OutputDebugString (pszData);
OutputDebugString ("\n");
#endif
pszEscData = hRq->hIO->EscapeString(hRq->hIO, pszData);
free (pszData);
if (!pszEscData || !(hPostString = Fifo_Init(strlen(pszEscData)+32)))
{
if (pszEscData) hRq->hIO->FreeEscapeString (pszEscData);
return NULL;
}
Fifo_AddString (hPostString, "method=");
Fifo_AddString (hPostString, pszMethod);
Fifo_AddString (hPostString, "&data=");
Fifo_AddString (hPostString, pszEscData);
hRq->hIO->FreeEscapeString (pszEscData);
pszEscData = Fifo_Get(hPostString, &uiCount);
pszData = hRq->hIO->Post (hRq->hIO, hRq->szReqURL, pszEscData,
uiCount-1, NULL);
Fifo_Exit(hPostString);
printf ("<- %s\n", pszData);
return pszData;
}
// -----------------------------------------------------------------------------
char *ImoRq_PostSystem(IMORQ *hRq, char *pszMethod, char *pszSysTo, char *pszSysFrom, cJSON *data, int bFreeData)
{
cJSON *root, *msgs, *msg, *to, *from;
char *pszRet;
if (!(root=cJSON_CreateObject())) return NULL;
LockMutex (hRq->mutex);
cJSON_AddNumberToObject (root, "ack", *hRq->psend_ack);
if (*hRq->szSessId) cJSON_AddStringToObject (root, "ssid", hRq->szSessId);
else cJSON_AddNumberToObject (root, "ssid", 0);
cJSON_AddItemToObject (root, "messages", (msgs = cJSON_CreateArray()));
if (data)
{
msg=cJSON_CreateObject();
cJSON_AddItemToObject(msg, "data", data);
to = cJSON_CreateObject();
cJSON_AddStringToObject (to, "system", pszSysTo);
cJSON_AddItemToObject(msg, "to", to);
from = cJSON_CreateObject();
cJSON_AddStringToObject (from, "system", pszSysFrom);
if (*hRq->szSessId) cJSON_AddStringToObject (from, "ssid", hRq->szSessId);
else cJSON_AddNumberToObject (from, "ssid", 0);
cJSON_AddItemToObject(msg, "from", from);
cJSON_AddNumberToObject (msg, "seq", (*hRq->psend_seq)++);
cJSON_AddItemToArray (msgs, msg);
}
UnlockMutex (hRq->mutex);
pszRet = ImoRq_PostImo (hRq, pszMethod, root);
if (data && !bFreeData)
{
msg->child = data->next;
data->next = NULL;
}
cJSON_Delete (root);
return pszRet;
}
// -----------------------------------------------------------------------------
char *ImoRq_ResetRPC(IMORQ *hRq)
{
cJSON *root, *ssid;
char *pszRet;
if (!(root=cJSON_CreateObject())) return NULL;
cJSON_AddStringToObject (root, "method", (IMO_API_VERSION==0?"get_ssid":"get_cookie_and_ssid"));
ssid=cJSON_CreateObject();
cJSON_AddStringToObject (ssid, "ssid", hRq->szSessId);
if (IMO_API_VERSION > 0)
{
cJSON_AddStringToObject (ssid, "kind", "reui");
cJSON_AddStringToObject (ssid, "version", "1336611734.48");
}
cJSON_AddItemToObject(root, "data", ssid);
if (IMO_API_VERSION == 0) *hRq->szSessId = 0;
pszRet = ImoRq_PostSystem (hRq, "rest_rpc", (IMO_API_VERSION==0?"ssid":"session"), "client", root, 1);
LockMutex (hRq->mutex);
*hRq->psend_seq=0;
UnlockMutex (hRq->mutex);
return pszRet;
}
// -----------------------------------------------------------------------------
char *ImoRq_UserActivity(IMORQ *hRq)
{
cJSON *ssid;
ssid=cJSON_CreateObject();
cJSON_AddStringToObject (ssid, "ssid", hRq->szSessId);
return ImoRq_PostToSys (hRq, "observed_user_activity", "session", ssid, 1, NULL);
}
// -----------------------------------------------------------------------------
char *ImoRq_Echo(IMORQ *hRq)
{
cJSON *data;
time_t t;
char szTime[16], *pszRet;
if (!(data=cJSON_CreateObject())) return NULL;
sprintf (szTime, "%ld", time(&t)*1000);
cJSON_AddStringToObject (data, "t", szTime);
pszRet = ImoRq_PostImo (hRq, "echo", data);
cJSON_Delete (data);
return pszRet;
}
// -----------------------------------------------------------------------------
char *ImoRq_Reui_Session(IMORQ *hRq)
{
cJSON *ssid;
ssid=cJSON_CreateObject();
cJSON_AddStringToObject (ssid, "ssid", hRq->szSessId);
return ImoRq_PostToSys (hRq, "reui_session", "session", ssid, 1, NULL);
}
// -----------------------------------------------------------------------------
char *ImoRq_PostToSys(IMORQ *hRq, char *pszMethod, char *pszSysTo, cJSON *data, int bFreeData, int *pireqid)
{
cJSON *root;
char *pszRet;
if (!(root=cJSON_CreateObject())) return NULL;
cJSON_AddStringToObject (root, "method", pszMethod);
if (pireqid) cJSON_AddNumberToObject(root, "request_id", *pireqid);
cJSON_AddItemToObject(root, "data", data);
pszRet = ImoRq_PostSystem (hRq, "forward_to_server", pszSysTo, "client", root, bFreeData);
if (!bFreeData)
{
data->prev->next = data->next;
if (data->next) data->next->prev = data->prev;
data->prev = data->next = NULL;
cJSON_Delete (root);
}
return pszRet;
}
// -----------------------------------------------------------------------------
char *ImoRq_PostAmy(IMORQ *hRq, char *pszMethod, cJSON *data)
{
return ImoRq_PostToSys (hRq, pszMethod, "im", data, FALSE, NULL);
}
// -----------------------------------------------------------------------------
char *ImoRq_SessId(IMORQ *hRq)
{
return hRq->szSessId;
}
// -----------------------------------------------------------------------------
char *ImoRq_GetLastError(IMORQ *hRq)
{
return hRq->hIO->GetLastError (hRq->hIO);
}
// -----------------------------------------------------------------------------
char *ImoRq_GetHost(IMORQ *hRq)
{
return hRq->pszHost;
}
// -----------------------------------------------------------------------------
void ImoRq_UpdateAck(IMORQ *hRq, unsigned long lAck)
{
LockMutex (hRq->mutex);
*hRq->psend_ack = lAck;
UnlockMutex (hRq->mutex);
}
// -----------------------------------------------------------------------------
unsigned long ImoRq_GetSeq(IMORQ *hRq)
{
unsigned long lRet;
LockMutex (hRq->mutex);
lRet = *hRq->psend_seq;
UnlockMutex (hRq->mutex);
return lRet;
}
// -----------------------------------------------------------------------------
void ImoRq_CreateID(char *pszID, int cbID)
{
int i, r;
time_t curtime;
srand(time(&curtime));
for (i=0; i<cbID; i++)
{
r = rand()%62;
if (r<26) pszID[i]='A'+r; else
if (r<52) pszID[i]='a'+(r-26); else
pszID[i]='0'+(r-52);
}
pszID[i]=0;
return;
}
// -----------------------------------------------------------------------------
char *ImoRq_HTTPGet(IMORQ *hRq, char *pszURL, unsigned int *pdwLength)
{
return hRq->hIO->Get (hRq->hIO, pszURL, pdwLength);
}
// -----------------------------------------------------------------------------
static IMORQ *Init(void)
{
IMORQ *hRq = calloc(1, sizeof(IMORQ));
/* Setup CURL */
if (!hRq) return NULL;
if (!(hRq->hIO = IoLayer_Init()))
{
ImoRq_Exit(hRq);
return NULL;
}
return hRq;
}
|