forked from adamvr/XMPPArduino
-
Notifications
You must be signed in to change notification settings - Fork 1
/
XMPPClient.cpp
executable file
·339 lines (283 loc) · 7.78 KB
/
XMPPClient.cpp
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
#include "XMPPClient.h"
/****************/
/* XMPP STANZAS */
/***************/
static const prog_char PROGMEM open_stream_template[] = "<stream:stream "
"xmlns='jabber:client' "
"xmlns:stream='http://etherx.jabber.org/streams' "
"to='%s' "
"version='1.0'>";
static const prog_char PROGMEM plain_auth_template[] = "<auth "
"xmlns='urn:ietf:params:xml:ns:xmpp-sasl' "
"mechanism='PLAIN'>"
"%s"
"</auth>";
static const prog_char PROGMEM bind_template[] = "<iq "
"type='set' "
"id='bind_1'>"
"<bind "
"xmlns='urn:ietf:params:xml:ns:xmpp-bind'>"
"<resource>%s</resource>"
"</bind>"
"</iq>";
static const prog_char PROGMEM session_request_template[] = "<iq "
"to='%s' "
"type='set' "
"id='ard_sess'>"
"<session "
"xmlns='urn:ietf:params:xml:ns:xmpp-session' />"
"</iq>";
static const prog_char PROGMEM presence_template[] = "<presence>"
"<show/>"
"</presence>";
static const prog_char PROGMEM message_template[] = "<message "
"to='%s' "
"xmlns='jabber:client' "
"type='chat' "
"id='msg' "
"xml:lang='en'>"
"<body>%s</body>"
"</message>";
static const prog_char PROGMEM close_template[] = "<presence type='unavailable'/>"
"</stream:stream>";
/********************/
/* TRANSITION TABLE */
/********************/
struct XMPPTransitionTableEntry {
XMPPState currentState;
XMPPState nextState;
char *keyword;
};
int connTableSize = 6;
XMPPTransitionTableEntry connTable[] = {{INIT, AUTH, "PLAIN"},
{AUTH, AUTH_STREAM, "success"},
{AUTH_STREAM, BIND, "bind"},
{BIND, SESS, "jid"},
{SESS, READY, "result"},
{READY, WAIT, ""},
{WAIT, WAIT, ""}};
/************************/
/* STRING COPY FUNCTION */
/************************/
void strcpyuntil(char *dest, char *src, char *end) {
while(src != end)
*dest++ = *src++;
*dest = '\0';
}
/*****************/
/* CLASS METHODS */
/*****************/
int XMPPClient::xmppLogin(char *server, char *username, char *password, char *resource, byte *macAddress) {
int ret;
// todo, make mac address optional
boolean connected = false, error = false;
this->username = username;
this->server = server;
this->resource = resource;
this->password = password;
// start the Ethernet connection
if(Ethernet.begin(macAddress) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
return 0;
}
// give the Ethernet shield a second to initialize:
delay(1000);
if(connect(server, 5222)) {
Serial.print("Conneted to ");
Serial.println(server);
state = INIT;
} else {
Serial.print("Failed to connect to ");
Serial.println(server);
}
while(!connected) {
ret = stateAction();
if (state == READY) {
connected = true;
continue;
}
processInput();
}
return 1;
}
int XMPPClient::openStream(char *server) {
sendTemplate(open_stream_template, strlen(server), server);
}
int XMPPClient::authenticate(char *username, char *password) {
int plainStringLen = strlen(username) + strlen(password) + 2;
int encStringLen = base64_enc_len(plainStringLen);
char plainString[plainStringLen];
char encString[encStringLen];
/* Set up our plain auth string. It's in the form:
* "\0username\0password"
* where \0 is the null character
*/
memset(plainString, '\0', plainStringLen);
memcpy(plainString + 1, username, strlen(username));
memcpy(plainString + 2 + strlen(username), password, strlen(password));
/* Encode to base64 */
base64_encode(encString, plainString, plainStringLen);
sendTemplate(plain_auth_template, encStringLen, encString);
}
int XMPPClient::bindResource(char *resource) {
sendTemplate(bind_template, strlen(resource), resource);
}
int XMPPClient::openSession(char *server) {
// Serial.print("Trying to open session to: ");
// Serial.println(server);
sendTemplate(session_request_template, strlen(server), server);
}
int XMPPClient::sendMessage(char *recipientJid, char *message) {
sendTemplate(message_template, strlen(recipientJid) + strlen(message), recipientJid, message);
}
int XMPPClient::sendPresence() {
sendTemplate(presence_template, 0);
}
char * XMPPClient::receiveMessage() {
int bufLen = 600;
// int msgLen = 100;
// char buffer[bufLen];
//char recMsg[100] = "";
int nChar = available();
*recMsg = NULL;
*recBuffer = NULL;
if (nChar > 0 && nChar < bufLen) {
// Serial.println("receiveMessage called");
for(int i = 0 ; i < nChar; i++) {
recBuffer[i] = read();
}
// Terminate the string
recBuffer[nChar - 1] = '\0';
if(!strlen(recBuffer)) {
//Ignore what we've read if it's an empty string
}
else {
// Check that what we received is a message
char tag1[] = "<message";
char buf2[9];
strncpy(buf2, recBuffer, 8);
buf2[8] = '\0';
if (strcmp(tag1, buf2) != 0) {
// Ignore what we received
Serial.println("Received something that is not a message");
*recBuffer = NULL;
*recMsg = NULL;
}
else {
// Serial.print("The buffer:");
// Serial.println(recBuffer);
// Parse the message
char *startIndex = strstr(recBuffer, "<body>");
if (startIndex != NULL) {
Serial.println("Received a message");
startIndex = startIndex + 6;
char *ptrMsg;
ptrMsg = recMsg;
while (*startIndex != '<') {
*ptrMsg = *startIndex;
ptrMsg++;
startIndex++;
}
*ptrMsg = '\0';
//Serial.println(recMsg);
*recBuffer = NULL;
return recMsg;
}
else {
*recBuffer = NULL;
*recMsg = NULL;
return NULL;
}
}
}
}
return recMsg;
}
int XMPPClient::close() {
sendTemplate(close_template, 0);
}
int XMPPClient::sendTemplate(const prog_char *temp_P, int fillLen, ...) {
int tempLen = strlen_P(temp_P);
char temp[tempLen];
char buffer[tempLen + fillLen];
va_list args;
strcpy_P(temp, temp_P);
va_start(args, fillLen);
vsprintf(buffer, temp, args);
write(buffer);
return 1;
}
int XMPPClient::stateAction() {
Serial.print("State = ");
Serial.println(state);
switch(state) {
case INIT:
openStream(server);
break;
case AUTH:
authenticate(username, password);
break;
case AUTH_STREAM:
openStream(server);
break;
case BIND:
bindResource(resource);
break;
case SESS:
openSession(server);
break;
case READY:
return 1;
break;
case WAIT:
break;
default:
return -1;
break;
}
return 0;
}
void XMPPClient::processInput() {
int bufLen = 8;
char buffer[bufLen];
int i = 0;
memset(buffer, '\0', bufLen);
boolean stateChanged = false;
if(!connected()) {
state = WAIT;
Serial.println("Lost connection in processInput");
return;
}
// TODO: This process is pretty inefficient and naively implemented
// It might be an idea to rewrite it cleverer
while(!stateChanged) {
if(available()) {
/* Push a character from the ethernet interface into the buffer */
for(i = 0 ; i < bufLen; i++) {
buffer[i] = buffer[i+1];
}
buffer[i] = read();
/* Ignore what we've read if it's an empty string */
if(!strlen(buffer)) {
continue;
} else {
//Serial.println(buffer);
}
for(int i = 0; i < connTableSize; i++) {
if(state == connTable[i].currentState && strstr(buffer,connTable[i].keyword)) {
/*Serial.println(buffer);
Serial.println(connTable[i].keyword);
Serial.println((int)strstr(buffer, connTable[i].keyword));
Serial.print(connTable[i].keyword);
Serial.println(" seen, transitioning");*/
state = connTable[i].nextState;
flush();
stateChanged = true;
break;
}
}
} else {
delay(10);
}
}
}