-
Notifications
You must be signed in to change notification settings - Fork 5
/
xmpp_time.c
105 lines (86 loc) · 2.23 KB
/
xmpp_time.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
#include <err.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <mxml.h>
static void
send_time(FILE *fh, const char *to, const char *id)
{
char tzo[BUFSIZ];
char utc[BUFSIZ];
time_t t = time(NULL);
/* +HHMM */
strftime(tzo, sizeof tzo, "%z", localtime(&t));
/* convert "+HHMM" to "+HH:MM" */
tzo[6] = tzo[5];
tzo[5] = tzo[4];
tzo[4] = tzo[3];
tzo[3] = ':';
/* 2006-12-19T17:58:35Z */
strftime(utc, sizeof utc, "%FT%TZ", gmtime(&t));
fprintf(fh,
"<iq type='result' to='%s' id='%s'>"
"<time xmlns='urn:xmpp:time'>"
"<tzo>%s</tzo>"
"<utc>%s</utc>"
"</time>"
"</iq>", to, id, tzo, utc);
}
static void
usage(void)
{
fprintf(stderr, "xmpp:time [-d dir]\n");
exit(EXIT_FAILURE);
}
int
main(int argc, char *argv[])
{
/* HACK: we need this, cause mxml can't parse pure tags */
mxml_node_t *tree = NULL;
mxml_node_t *node = NULL;
const char *base = "<?xml ?>";
const char *id = NULL;
const char *from = NULL;
const char *type = NULL;
char *dir = ".";
char out_file[PATH_MAX];
FILE *fh;
int ch;
while ((ch = getopt(argc, argv, "d:h")) != -1) {
switch (ch) {
case 'd':
dir = optarg;
break;
case 'h':
default:
usage();
/* NOTREACHED */
}
}
argc -= optind;
argv += optind;
if ((tree = mxmlLoadString(NULL, base, MXML_NO_CALLBACK)) == NULL)
err(EXIT_FAILURE, "unable to read xml tree");
mxmlLoadFile(tree, stdin, MXML_NO_CALLBACK);
/* check iq tag */
if ((node = mxmlGetFirstChild(tree)) == NULL)
errx(EXIT_FAILURE, "unable to parse xml data");
if ((id = mxmlElementGetAttr(node, "id")) == NULL)
errx(EXIT_FAILURE, "iq stanze has no \"id\" attribute");
if ((from = mxmlElementGetAttr(node, "from")) == NULL)
errx(EXIT_FAILURE, "iq stanze has no \"from\" attribute");
if ((type = mxmlElementGetAttr(node, "type")) == NULL)
errx(EXIT_FAILURE, "iq stanze has no \"type\" attribute");
if (strcmp(type, "get") != 0)
errx(EXIT_FAILURE, "unable to handle iq type: %s", type);
/* open file for output */
snprintf(out_file, sizeof out_file, "%s/in", dir);
if ((fh = fopen(out_file, "w")) == NULL)
err(EXIT_FAILURE, "fopen");
send_time(fh, from, id);
if (fclose(fh) == EOF)
err(EXIT_FAILURE, "fclose");
return EXIT_SUCCESS;
}