-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpresenced.c
343 lines (290 loc) · 7.34 KB
/
presenced.c
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
/*
* Copyright (c) 2015 Jan Klemkow <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/queue.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <mxml.h>
#include "bxml/bxml.h"
struct contact {
char *jid; /* buddies jabber ID */
char path[PATH_MAX]; /* path to buddy specific status file */
char *mystatus; /* buddy specific status message */
LIST_ENTRY(contact) next;
};
struct context {
int fd_in;
struct bxml_ctx *bxml;
char *dir;
char out_file[PATH_MAX];
LIST_HEAD(listhead, contact) roster;
};
#define NULL_CONTEXT { \
STDIN_FILENO, \
NULL, \
".", \
{0}, \
LIST_HEAD_INITIALIZER() \
}
static void
send_presence(const struct context *ctx, const struct contact *c)
{
FILE *fh = NULL;
if (ctx == NULL || c == NULL)
return;
if (c->mystatus == NULL)
return;
if ((fh = fopen(ctx->out_file, "w")) == NULL)
goto err;
if (fprintf(fh,
"<presence to='%s'>"
"<status>%s</status>"
"<priority>1</priority>"
"</presence>", c->jid, c->mystatus) == -1)
goto err;
if (fclose(fh) == EOF)
goto err;
return;
err:
if (errno != 0)
perror(__func__);
}
static void
check_contact(struct context *ctx, struct contact *c)
{
FILE *fh;
char buf[BUFSIZ];
if (c == NULL)
return;
if ((fh = fopen(c->path, "r")) == NULL) {
if (errno == ENOENT) {
errno = 0;
if (c->mystatus != NULL) {
free(c->mystatus);
c->mystatus = NULL;
send_presence(ctx, c);
}
return;
}
goto err;
}
if (fgets(buf, sizeof buf, fh) == NULL) {
/* distinguish between empty file and error */
if (feof(fh))
buf[0] = '\0';
else
goto err;
}
if (c->mystatus != NULL) {
if (strncmp(buf, c->mystatus, sizeof buf) != 0) {
free(c->mystatus);
c->mystatus = strdup(buf);
send_presence(ctx, c);
}
} else {
c->mystatus = strdup(buf);
send_presence(ctx, c);
}
if (fclose(fh) == EOF)
goto err;
return;
err:
if (errno != 0)
perror(__func__);
}
static void
free_contact(struct contact *c)
{
if (c == NULL) return;
free(c->jid);
free(c);
}
static struct contact *
add_contact(struct context *ctx, char *jid)
{
char *slash = NULL;
struct contact *c = NULL;
/* just handle the bare jabber id without resources */
if ((slash = strchr(jid, '/')) != NULL)
*slash = '\0';
/* return contact if it exists already */
LIST_FOREACH(c, &ctx->roster, next)
if (strcmp(c->jid, jid) == 0)
return c;
/* create a new contact */
if ((c = calloc(1, sizeof *c)) == NULL) goto err;
if ((c->jid = strdup(jid)) == NULL) goto err;
/* prepare path of "mystatus" file */
if (snprintf(c->path, sizeof c->path, "%s/%s/mystatus",
ctx->dir, c->jid) == 0)
goto err;
LIST_INSERT_HEAD(&ctx->roster, c, next);
return c;
err:
free_contact(c);
if (errno != 0)
perror(__func__);
return NULL;
}
static void
check_roster(struct context *ctx)
{
DIR *dirp;
struct dirent *dp;
struct contact *c;
if (ctx->dir == NULL) return;
if ((dirp = opendir(ctx->dir)) == NULL) goto err;
while ((dp = readdir(dirp)) != NULL) {
if (strcmp(dp->d_name, ".") == 0 ||
strcmp(dp->d_name, "..") == 0 ||
dp->d_type != DT_DIR) continue;
if ((c = add_contact(ctx, dp->d_name)) != NULL)
check_contact(ctx, c);
}
closedir(dirp);
return;
err:
if (errno != 0)
perror(__func__);
return;
}
static void
recv_presence(char *tag, void *data)
{
struct context *ctx = data;
/* HACK: we need this, cause mxml can't parse tags by itself */
static mxml_node_t *tree = NULL;
static mxml_node_t *node = NULL;
const char *base = "<?xml ?><stream:stream></stream:stream>";
const char *tag_name = NULL;
const char *from = NULL;
char *slash = NULL;
char path[PATH_MAX];
int fd;
bool is_online = false;
if (tree == NULL) tree = mxmlLoadString(NULL, base, MXML_NO_CALLBACK);
if (tree == NULL) err(EXIT_FAILURE, "%s: no xml tree found", __func__);
mxmlLoadString(tree, tag, MXML_NO_CALLBACK);
if ((node = mxmlGetNextSibling(mxmlGetFirstChild(tree))) == NULL) goto err;
if ((tag_name = mxmlGetElement(node)) == NULL) goto err;
if (strcmp("presence", tag_name) != 0)
goto err;
if ((from = mxmlElementGetAttr(node, "from")) == NULL)
goto err;
/* The presence of the 'type' attribute indicates offline.
The lack of it indicates online. */
if (mxmlElementGetAttr(node, "type"))
is_online = false;
else
is_online = true;
/* cut off resourcepart from jabber ID */
if ((slash = strchr(from, '/')) != NULL)
*slash = '\0';
if (mkdir(ctx->dir, S_IRUSR|S_IWUSR|S_IXUSR) == -1) {
if (errno != EEXIST) err(EXIT_FAILURE, "mkdir");
errno = 0;
}
snprintf(path, sizeof path, "%s/%s", ctx->dir, from);
if (mkdir(path, S_IRUSR|S_IWUSR|S_IXUSR) == -1) {
if (errno != EEXIST) err(EXIT_FAILURE, "mkdir");
errno = 0;
}
snprintf(path, sizeof path, "%s/%s/status", ctx->dir, from);
if ((fd = open(path, O_WRONLY|O_TRUNC|O_CREAT, S_IRUSR|S_IWUSR)) == -1)
goto err;
if (is_online) {
mxml_node_t *show = NULL;
const char *status;
if ((show = mxmlFindElement(node, tree, "show", NULL, NULL,
MXML_DESCEND_FIRST)) != NULL)
status = mxmlGetText(show, NULL);
else
status = "online";
if (write(fd, status, strlen(status)) == -1) goto err;
}
/* write nothing; make fd an empty file */
if (close(fd) == -1) goto err;
err:
if (errno != 0)
perror(__func__);
mxmlDelete(node);
}
static void
usage(void)
{
fprintf(stderr, "usage: presenced -d DIR\n");
exit(EXIT_FAILURE);
}
int
main(int argc, char *argv[])
{
struct context ctx = NULL_CONTEXT;
int ch;
while ((ch = getopt(argc, argv, "d:i:")) != -1) {
switch (ch) {
case 'i':
ctx.fd_in = strtol(optarg, NULL, 0);
break;
case 'd':
ctx.dir = optarg;
break;
default:
usage();
/* NOTREACHED */
}
}
argc -= optind;
argv += optind;
snprintf(ctx.out_file, sizeof ctx.out_file, "%s/in", ctx.dir);
check_roster(&ctx);
/* initialize block parser and set callback function */
ctx.bxml = bxml_ctx_init(recv_presence, &ctx);
for (;;) {
int max_fd = 0;
ssize_t n;
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(ctx.fd_in, &readfds);
max_fd = ctx.fd_in;
/* wait for input */
if (select(max_fd+1, &readfds, NULL, NULL, NULL) < 0)
err(EXIT_FAILURE, "select");
/* check for input from server */
if (FD_ISSET(ctx.fd_in, &readfds)) {
char buf[BUFSIZ];
if ((n = read(ctx.fd_in, buf, BUFSIZ)) < 0) goto err;
if (n == 0) break; /* connection closed */
bxml_add_buf(ctx.bxml, buf, n);
}
}
return EXIT_SUCCESS;
err:
if (errno != 0)
perror(NULL);
return EXIT_FAILURE;
}