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
|
/*
* Usurpation – server daemon main logic
*
* Copyright (C) 2019 Gediminas Jakutis
* Copyright (C) 2019 Paulius Ratkevičius
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; version 2.1
* of the License.
*
* 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "settings.h"
#include "net.h"
#include "purple.h"
#include "proto_stdio.h"
#include "tlv.h"
#include "utils.h"
static struct _state {
int nd;
} __progstate;
static void cleanup(void);
static int __main_loop(const struct timespec * const iter_len);
static int does_my_kokoro_go_doki_doki(struct tlv *tlv);
/* the logic is a placeholder right now */
int main(int argc, char **argv)
{
extern const char * const version;
static const struct timespec t = {0, 250 * 1000 * 1000}; /* 250ms */
char *proto;
printf("Usurpation daemon version %s starting\n", version);
atexit(cleanup);
settings_init();
__progstate.nd = net_init(setting_port());
proto = setting_im_proto();
if (strcmp(proto, "null")) {
if (purple_init() && setting_verbose()) {
fprintf(stderr, "libpurple initialization failed\n");
}
} else {
if (proto_stdio_init() && setting_verbose()) {
fprintf(stderr, "stdio interface initialization failed\n");
}
}
free(proto);
proto = NULL;
/* by default and if running by as a system service, the init system
* needs to keep control of the process and thus only detach if
* requested when ran manually by a user.
*/
if (setting_detach()) {
daemon(0, 0);
}
return __main_loop(&t);
}
/* TODO: semi-stub */
static int __main_loop(const struct timespec * const iter_len)
{
struct tlv_packet pack;
struct tlv field;
struct timespec t;
char *buf = NULL;
size_t recvsize;
int ret = A_OK;
t = *iter_len;
tlv_packet_init(&pack);
while (ret != ERROR) {
ret = net_getlastdata(__progstate.nd, &buf, &recvsize);
if (ret != A_OK) {
goto skiprecv;
}
/* we're only sending one TLV per packet as of now,
* so no need to save the pointer for further parsing
*/
ret = tlv_get(buf, &field, NULL);
/* handles discovery */
if (ret != A_OK || !(does_my_kokoro_go_doki_doki(&field))) {
goto skiprecv;
}
/* handles receiving messages */
if (field.head.type == TEXT) {
message_receive(field.data);
}
/* jumping here if did not receive any data */
skiprecv:
/* handles sending messages */
if (message_send(&field)) {
if (field.head.size) {
tlv_pack(&pack, &field);
tlv_packet_finalize(&pack);
tlv_destroy(&field);
net_send(__progstate.nd, pack.data, pack.cursor + 1);
tlv_packet_reset(&pack);
}
}
nanosleep(&t, NULL);
}
tlv_packet_destroy(&pack);
return ret;
}
static int does_my_kokoro_go_doki_doki(struct tlv *tlv)
{
int ret = ERROR;
static struct tlv_packet pack;
static size_t firstrun = 1;
if (firstrun) {
struct tlv field;
tlv_init(&field, HEARTBEAT);
field.data = strdup(heartbeat_server);
field.head.size = sizeof(heartbeat_server);
tlv_packet_init(&pack);
tlv_pack(&pack, &field);
tlv_packet_finalize(&pack);
tlv_destroy(&field);
firstrun = 0;
}
if (tlv->head.type == HEARTBEAT && !(strcmp(tlv->data, heartbeat_device))) {
if (setting_verbose() >= INFO) {
fprintf(stderr, "heartbeat.\n");
}
ret = net_send(__progstate.nd, pack.data, pack.cursor + 1);
}
return ret;
}
static void cleanup(void)
{
purple_close();
proto_stdio_close();
net_close(__progstate.nd);
__progstate.nd = 0;
settings_cleanup();
}
|