aboutsummaryrefslogtreecommitdiff
path: root/liveapi/messages.c
blob: 2ef7958778e72acf6e733fd57840779344ee0959 (plain)
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
#include <cjson/cJSON.h>
#include <stdio.h>
#include <stdlib.h>

#include <features.h>
#include "libuwsc/uwsc.h"

#include "liveapi.h"
#include "messages.h"

struct methodid {
    size_t id;
    char sid[33];
};

static const struct methodid* get_next_method_id(void)
{
    static struct methodid methodid = { .id = 1, .sid = "" };
    snprintf(methodid.sid, sizeof(methodid.sid), "%zu", ++methodid.id);
    return &methodid;
}

void liveapi_connect(void)
{
    cJSON* json = cJSON_CreateObject();
    cJSON_AddItemToObject(json, "msg", cJSON_CreateString("connect"));
    cJSON_AddItemToObject(json, "version", cJSON_CreateString("1"));

    cJSON* support = cJSON_CreateArray();
    cJSON_AddItemToArray(support, cJSON_CreateString("1"));
    cJSON_AddItemToArray(support, cJSON_CreateString("pre2"));
    cJSON_AddItemToArray(support, cJSON_CreateString("pre1"));
    cJSON_AddItemToObject(json, "support", support);

    liveapi_send_json(0, json);
}

void liveapi_login(const char * authToken)
{
    const struct methodid * id = get_next_method_id();

    cJSON* json = cJSON_CreateObject();
    cJSON_AddItemToObject(json, "msg", cJSON_CreateString("method"));
    cJSON_AddItemToObject(json, "id", cJSON_CreateString(id->sid));
    cJSON_AddItemToObject(json, "method", cJSON_CreateString("login"));

    cJSON* params = cJSON_CreateArray();
    cJSON* resume = cJSON_CreateObject();
    cJSON_AddItemToObject(resume, "resume", cJSON_CreateString(authToken));
    cJSON_AddItemToArray(params, resume);
    cJSON_AddItemToObject(json, "params", params);

    liveapi_send_json(id->id, json);
}

void liveapi_mark_read(const char * rid)
{
    const struct methodid * id = get_next_method_id();

    cJSON* json = cJSON_CreateObject();
    cJSON_AddItemToObject(json, "msg", cJSON_CreateString("method"));
    cJSON_AddItemToObject(json, "id", cJSON_CreateString(id->sid));
    cJSON_AddItemToObject(json, "method", cJSON_CreateString("readMessages"));

    cJSON* params = cJSON_CreateArray();
    cJSON_AddItemToArray(params, cJSON_CreateString(rid));
    cJSON_AddItemToObject(json, "params", params);

    liveapi_send_json(id->id, json);
}