summaryrefslogtreecommitdiff
path: root/src/greetd.c
blob: eb356af5017cd7cfaa8b9d5a08b2a0f2b6638d5c (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
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
#include "greetd.h"
#include "ipc.h"
#include <json-c/json_object.h>
#include <string.h>

struct json_object *greetd_create_session(const char *username)
{
	struct json_object *request = json_object_new_object();

	struct json_object *type = json_object_new_string("create_session");
	json_object_object_add(request, "type", type);

	struct json_object *name = json_object_new_string(username);
	json_object_object_add(request, "username", name);

	struct json_object *resp = ipc_submit(request);
	json_object_put(request);
	return resp;
}

struct json_object *greetd_post_auth_message_response(const char *response)
{
	struct json_object *request = json_object_new_object();

	struct json_object *type = json_object_new_string("post_auth_message_response");
	json_object_object_add(request, "type", type);

	if (response != NULL) {
		struct json_object *resp = json_object_new_string(response);
		json_object_object_add(request, "response", resp);
	}

	struct json_object *resp = ipc_submit(request);
	json_object_put(request);
	return resp;
}

struct json_object *greetd_start_session(const char *command)
{
	struct json_object *request = json_object_new_object();

	struct json_object *type = json_object_new_string("start_session");
	json_object_object_add(request, "type", type);

	struct json_object *arr = json_object_new_array_ext(1);
	struct json_object *cmd = json_object_new_string(command);
	json_object_array_add(arr, cmd);
	json_object_object_add(request, "cmd", arr);

	struct json_object *resp = ipc_submit(request);
	json_object_put(request);
	return resp;
}

struct json_object *greetd_cancel_session(void)
{
	struct json_object *request = json_object_new_object();

	struct json_object *type = json_object_new_string("cancel_session");
	json_object_object_add(request, "type", type);

	struct json_object *resp = ipc_submit(request);
	json_object_put(request);
	return resp;
}

enum greetd_response_type greetd_parse_response_type(struct json_object *response)
{
	const char *str = json_object_get_string(json_object_object_get(response, "type"));
	if (!strcmp(str, "success")) {
		return GREETD_RESPONSE_SUCCESS;
	}
	if (!strcmp(str, "error")) {
		return GREETD_RESPONSE_ERROR;
	}
	if (!strcmp(str, "auth_message")) {
		return GREETD_RESPONSE_AUTH_MESSAGE;
	}
	return GREETD_RESPONSE_INVALID;
}

enum greetd_auth_message_type greetd_parse_auth_message_type(struct json_object *response)
{
	const char *str = json_object_get_string(json_object_object_get(response, "auth_message_type"));
	if (!strcmp(str, "visible")) {
		return GREETD_AUTH_MESSAGE_VISIBLE;
	}
	if (!strcmp(str, "secret")) {
		return GREETD_AUTH_MESSAGE_SECRET;
	}
	if (!strcmp(str, "info")) {
		return GREETD_AUTH_MESSAGE_INFO;
	}
	if (!strcmp(str, "error")) {
		return GREETD_AUTH_MESSAGE_ERROR;
	}
	return GREETD_AUTH_MESSAGE_INVALID;
}

enum greetd_error_type greetd_parse_error_type(struct json_object *response)
{
	const char *str = json_object_get_string(json_object_object_get(response, "error_type"));
	if (!strcmp(str, "auth_error")) {
		return GREETD_ERROR_AUTH_ERROR;
	}
	if (!strcmp(str, "error")) {
		return GREETD_ERROR_ERROR;
	}
	return GREETD_ERROR_INVALID;
}