aboutsummaryrefslogtreecommitdiff
path: root/close_direct_conversations/main.c
blob: 122420d124654c05b17fe26943ef3ce879d33b9d (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
#include <stdlib.h>
#include <stdio.h>

#include "common/config.h"
#include "common/cli.h"
#include "common/util.h"
#include "restapi/auth.h"
#include "restapi/im.h"
#include "restapi/subscriptions.h"

void print_subscription(const struct subscription* sub)
{
    if (sub->type == SUBSCRIPTION_DIRECT)
        printf("\t%s\n", sub->name);
}

int main(void)
{
    if (config_load(CONFIG_PATH) != 0) {
        return 1;
    }

    const char* login = config_get_login();
    if (login == NULL)
        login = common_cli_get_login();

    const char* password = config_get_password();
    if (password == NULL)
        password = common_cli_get_password();

    if (restapi_login(login, password) != NULL) {
        struct subscription* subscriptions = restapi_subscriptions_get();

        printf("Active direct conversations :\n");
        common_subscriptions_const_walk(subscriptions, &print_subscription);
        common_subscriptions_free(subscriptions);

        while(1) {
            char* buff = NULL;
            size_t len2;
            printf("Direct conversation to close: ");
            ssize_t entry = getline(&buff, &len2, stdin);
            if (entry > 1) {
                buff[entry-1] = 0;
            } else {
                free(buff);
                break;
            }
            restapi_im_close(buff);
            free(buff);

            restapi_logout();
        }
    } else {
        printf("Couldn't init rest api.\n");
    }

    config_clean();
    common_cli_free();

    return 0;
}