-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmpprecv.c
274 lines (231 loc) · 6.24 KB
/
xmpprecv.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
/**@file xmpprecv.c
* @author Craig Hesling <craig@hesling.com>
* @date August 5, 2015
* This is a utility to receive xmpp stanzas with the option
* to use tag name, type, namespace, or PubSub node as a filter.
*
* Standard Handler Filter:
* xmpprecv <jid> <pass> -s [name [type [ns]]]
* PubSub Filter:
* xmpprecv <jid> <pass> -p <node>
*/
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h> /* NULL */
#include <string.h>
#include <signal.h>
#include <assert.h>
#include <ctype.h>
#include <strophe.h>
#define XMPP_MESSAGE_SIZE_MAX 4096
int ready = 0;
int recv_response = 0;
int doExit = 0;
/**
* The blob of data to transfer to filter handlers
*/
struct handler_data {
xmpp_ctx_t *ctx;
const char *op1;
const char *op2;
const char *op3;
};
/**
* Interprets NULL given as a string.
* @param filter The commandline argument to interpret
* @return The interpreted argument
*/
const char *interpret_filter(const char *filter) {
assert(filter);
if (strcmp(filter, "NULL") == 0) {
return NULL ;
}
return filter;
}
/* define a handler for connection events */
void conn_handler( xmpp_conn_t * const conn,
const xmpp_conn_event_t status,
const int error,
xmpp_stream_error_t * const stream_error,
void * const userdata) {
xmpp_ctx_t *ctx = (xmpp_ctx_t *) userdata;
if (status == XMPP_CONN_CONNECT) {
xmpp_stanza_t *pres;
fprintf(stderr, "DEBUG: connected\n");
//xmpp_disconnect(conn);
/* Send initial <presence/> so that we appear online to contacts */
pres = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(pres, "presence");
xmpp_send(conn, pres);
xmpp_stanza_release(pres);
ready = 1;
} else {
fprintf(stderr, "DEBUG: disconnected\n");
xmpp_stop(ctx);
}
}
/**
* Prints the given stanza.
* @param ctx The context to free from
* @param stanza The stanza to display
*/
void print_stanza(xmpp_ctx_t *ctx, xmpp_stanza_t * const stanza) {
char *rawtext;
size_t rawtext_size;
xmpp_stanza_to_text(stanza, &rawtext, &rawtext_size);
fprintf(stdout, "%s\n", rawtext);
fflush(stdout);
xmpp_free(ctx, rawtext);
}
int handler_dump( xmpp_conn_t * const conn,
xmpp_stanza_t * const stanza,
void * const userdata) {
struct handler_data *hdata = (struct handler_data *) userdata;
print_stanza(hdata->ctx, stanza);
return 1;
}
int handler_pubsub( xmpp_conn_t * const conn,
xmpp_stanza_t * const stanza,
void * const userdata) {
xmpp_stanza_t *event;
xmpp_stanza_t *items;
struct handler_data *hdata = (struct handler_data *) userdata;
const char *node = hdata->op1;
assert(hdata->op2 == NULL);
assert(hdata->op3 == NULL);
event = xmpp_stanza_get_child_by_ns(stanza, "http://jabber.org/protocol/pubsub#event");
if(event == NULL) {
/* not PubSub */
return 1;
}
if(node == NULL) {
print_stanza(hdata->ctx, stanza);
return 1;
}
items = xmpp_stanza_get_children(event);
while (items) {
/* check that it is an item */
if(strcmp(xmpp_stanza_get_name(items), "items") == 0) {
/* check node id */
if(strcmp(xmpp_stanza_get_attribute(items, "node"), node) == 0) {
print_stanza(hdata->ctx, items);
}
}
items = xmpp_stanza_get_next(items);
}
return 1;
}
/**
* Signal to main loop to exit gracefully
* @param[in] sig The signal number passed in from libc
*/
void sigint_handler(int sig) {
fprintf(stderr, "# Attempting exit\n");
doExit = 1;
}
/**
* Standard Handler Filter:
* xmpprecv <jid> <pass> -s [name [type [ns]]]
* PubSub Filter:
* xmpprecv <jid> <pass> -p <node>
*/
int main(int argc, char **argv) {
xmpp_ctx_t *ctx;
xmpp_conn_t *conn;
xmpp_log_t *log;
/* command line options */
char *jid, *pass, *host, *action;
const char *op1, *op2, *op3;
struct handler_data hdata;
unsigned short port = 0;
/* take a jid, password, action and options on the command line */
if (argc < 4 || argc > 7 || (strcmp(argv[1], "--help")==0)) {
fprintf(stderr,
"Usage: xmpprecv <jid> <pass> [ -s [name [type [ns]]] | -p [node] ]\n\n");
return 1;
}
jid = argv[1];
pass = argv[2];
host = NULL;
action = argv[3];
if ((strlen(action) != 2) || (action[0] != '-') || (!isalpha(action[1]))) {
fprintf(stderr, "Error - Invalid action specifier\n");
return 1;
}
/* setup action options/filters */
switch (action[1]) {
case 's':
fprintf(stderr, "Handler filter specified\n");
op1 = op2 = op3 = NULL;
if (argc > 4) {
op1 = interpret_filter(argv[4]);
}
if (argc > 5) {
op2 = interpret_filter(argv[5]);
}
if (argc > 6) {
op3 = interpret_filter(argv[6]);
}
break;
case 'p':
fprintf(stderr, "PubSub filter specified\n");
op1 = op2 = op3 = NULL;
if (argc == 5) {
op1 = interpret_filter(argv[4]);
}
break;
default:
fprintf(stderr, "Error - Invalid action\n");
exit(1);
break;
}
/* setup handler data */
hdata.op1 = op1;
hdata.op2 = op2;
hdata.op3 = op3;
signal(SIGINT, sigint_handler);
/* init library */
xmpp_initialize();
/* create a context */
//log = xmpp_get_default_logger(XMPP_LEVEL_DEBUG); /* pass NULL instead to silence output */
//log = xmpp_get_default_logger(XMPP_LEVEL_INFO); /* pass NULL instead to silence output */
log = xmpp_get_default_logger(XMPP_LEVEL_WARN); /* pass NULL instead to silence output */
//log = xmpp_get_default_logger(XMPP_LEVEL_ERROR); /* pass NULL instead to silence output */
ctx = xmpp_ctx_new(NULL, log);
/* create a connection */
conn = xmpp_conn_new(ctx);
/* setup authentication information */
xmpp_conn_set_jid(conn, jid);
xmpp_conn_set_pass(conn, pass);
/* initiate connection */
xmpp_connect_client(conn, host, port, conn_handler, ctx);
hdata.ctx = ctx;
/*-------- Opened Connection --------*/
switch (action[1]) {
case 's':
xmpp_handler_add(conn, handler_dump, op3, op1, op2, (void *) &hdata);
//xmpp_id_handler_add(conn, message_handler, stanza_id, (void *)ctx);
break;
case 'p':
xmpp_handler_add(conn, handler_pubsub, NULL, "message", "headline",
(void *) &hdata);
break;
default:
fprintf(stderr, "Error - Invalid option\n");
return 1;
break;
}
while(!doExit) {
xmpp_run_once(ctx, 10);
}
/*-------- Close Connection --------*/
xmpp_stop(ctx);
xmpp_disconnect(conn);
/* release our connection and context */
xmpp_conn_release(conn);
xmpp_ctx_free(ctx);
/* final shutdown of the library */
xmpp_shutdown();
return 0;
}
/* vim: set noexpandtab ts=4 : */