24
|
1 #include <arpa/inet.h>
|
|
2 #include <netdb.h>
|
|
3 #include <stdio.h>
|
|
4 #include <stdlib.h>
|
|
5 #include <string.h>
|
|
6 #include <sys/poll.h>
|
28
|
7 #include <sys/types.h>
|
|
8 #include <sys/socket.h>
|
|
9 #include <sys/select.h>
|
24
|
10
|
|
11 #include "list.h"
|
|
12 #include "parsecfg.h"
|
|
13 #include "screen.h"
|
|
14 #include "socket.h"
|
|
15 #include "utf8.h"
|
|
16 #include "server.h"
|
|
17 #include "harddefines.h"
|
|
18 #include "utils.h"
|
|
19 #include "buddies.h"
|
|
20
|
|
21 #define JABBERPORT 5222
|
|
22
|
|
23 /* Desc: poll data from server
|
|
24 *
|
|
25 * In : socket
|
|
26 * Out : pending buffer (or NULL if no incoming data)
|
|
27 *
|
|
28 * Note: it is up to the caller to free the returned string
|
|
29 */
|
|
30 char *srv_poll(int sock)
|
|
31 {
|
|
32 struct pollfd sock_p;
|
|
33 sock_p.fd = sock;
|
26
|
34 sock_p.events = (short int) (POLLIN | POLLPRI);
|
24
|
35 sock_p.revents = 0;
|
|
36 poll(&sock_p, 1, 0);
|
|
37
|
|
38 if (sock_p.revents) {
|
|
39 return sk_recv(sock);
|
|
40 }
|
|
41
|
|
42 return NULL;
|
|
43 }
|
|
44
|
|
45
|
|
46 /* Desc: resolve host
|
|
47 *
|
|
48 * In : hostname
|
|
49 * Out : 32bit address (or 0 if error)
|
|
50 *
|
|
51 * Note: -
|
|
52 */
|
|
53 static u_long srv_resolve(const char *host)
|
|
54 {
|
27
|
55 in_addr_t i;
|
24
|
56 struct hostent *he;
|
|
57
|
27
|
58 if ((i = inet_addr(host)) == INADDR_NONE) {
|
24
|
59 if (!(he = gethostbyname(host)))
|
|
60 return 0;
|
|
61 else
|
26
|
62 return (*(u_long *) he->h_addr); // XXX Memory leak
|
24
|
63 }
|
|
64
|
26
|
65 return (u_long) i;
|
24
|
66 }
|
|
67
|
|
68
|
|
69 /* Desc: connect to jabber server
|
|
70 *
|
|
71 * In : config
|
|
72 * Out : socket (or -1 on error)
|
|
73 *
|
|
74 * Note: if port is -1, the default Jabber port will be used
|
|
75 */
|
|
76 int srv_connect(const char *server, unsigned int port)
|
|
77 {
|
|
78 struct sockaddr_in name;
|
|
79 int sock;
|
|
80
|
|
81 if (server == NULL) {
|
|
82 fprintf(stderr, "You must supply a server name\n\r");
|
|
83 return -1;
|
|
84 }
|
|
85
|
|
86 if (port == -1U) {
|
|
87 port = JABBERPORT;
|
|
88 }
|
|
89
|
|
90 name.sin_family = AF_INET;
|
|
91 name.sin_port = htons(port);
|
|
92
|
|
93 if (!(name.sin_addr.s_addr = srv_resolve(server))) {
|
|
94 fprintf(stderr, "Cant resolve \"%s\"\n", server);
|
|
95 return -1;
|
|
96 }
|
|
97
|
|
98 if ((sock = sk_conn((struct sockaddr *) &name)) < 0) {
|
|
99 fprintf(stderr, "Can't connect to \"%s:%u\"\n", server, port);
|
|
100 return -1;
|
|
101 }
|
|
102
|
|
103 return sock;
|
|
104 }
|
|
105
|
|
106
|
|
107 /* Desc: login into jabber server
|
|
108 *
|
|
109 * In : socket, servername, user, password, resource
|
|
110 * Out : idsession
|
|
111 *
|
|
112 * Note: it is up to the caller to free the returned string
|
|
113 */
|
|
114 char *srv_login(int sock, const char *server, const char *user,
|
|
115 const char *pass, const char *resource)
|
|
116 {
|
|
117 char *stringtosend = malloc(2048);
|
|
118 char *response, *aux;
|
26
|
119 char *idsession = calloc(64, sizeof(char));
|
24
|
120 int pos = 0;
|
|
121
|
|
122 memset(stringtosend, 0, 2048);
|
|
123 strcpy(stringtosend, "<?xml version='1.0' encoding='UTF-8' ?>");
|
|
124 strcat(stringtosend, "<stream:stream to='");
|
|
125 strcat(stringtosend, server);
|
|
126 strcat(stringtosend, "' xmlns='jabber:client' xmlns:stream='");
|
|
127 strcat(stringtosend, "http://etherx.jabber.org/streams'>\n");
|
|
128
|
|
129 if (!sk_send(sock, stringtosend)) {
|
|
130 perror("senddata (server.c:132)");
|
26
|
131 free(stringtosend);
|
|
132 free(idsession);
|
24
|
133 return NULL;
|
|
134 }
|
|
135 response = sk_recv(sock);
|
|
136 if (strstr(response, "error")) {
|
|
137 /* fprintf(stderr, "Response not valid:\n%s\n\n", response); */
|
|
138 scr_CreatePopup("Error",
|
|
139 "Bad answer from the server", 60, 0, NULL);
|
26
|
140 free(response);
|
|
141 free(idsession);
|
|
142 free(stringtosend);
|
24
|
143 return NULL;
|
|
144 }
|
|
145 aux = response;
|
|
146 while (strncmp(aux, "id", 2))
|
|
147 aux++;
|
|
148 pos = 0;
|
|
149 aux += 4;
|
|
150 while (strncmp(aux, "'", 1)) {
|
|
151 aux++;
|
|
152 pos++;
|
|
153 }
|
|
154 aux -= pos;
|
26
|
155 if (pos > 64-1) {
|
|
156 ut_WriteLog("Bad session ID!\n");
|
|
157 free(response);
|
|
158 free(idsession);
|
|
159 free(stringtosend);
|
|
160 return NULL;
|
|
161 }
|
27
|
162 strncpy(idsession, aux, (size_t)pos);
|
24
|
163
|
|
164 free(response);
|
|
165
|
|
166 strcpy(stringtosend, "<iq type='set' id='1000'>");
|
|
167 strcat(stringtosend, "<query xmlns='jabber:iq:auth'>");
|
|
168 strcat(stringtosend, "<username>");
|
|
169 strcat(stringtosend, user);
|
|
170 strcat(stringtosend, "</username><password>");
|
|
171 strcat(stringtosend, pass);
|
|
172 strcat(stringtosend, "</password><resource>");
|
|
173 strcat(stringtosend, resource);
|
|
174 strcat(stringtosend, "</resource></query></iq>\n");
|
|
175 if (!sk_send(sock, stringtosend)) {
|
|
176 perror("senddata (server.c:167)");
|
|
177 return NULL;
|
|
178 }
|
|
179 response = sk_recv(sock);
|
|
180 if (strstr(response, "error")) {
|
|
181 /* fprintf(stderr, "Response not valid:\n%s\n\n", response);*/
|
|
182 scr_CreatePopup("Error",
|
|
183 "Account doesn't exist, or bad password", 60, 0,
|
|
184 NULL);
|
|
185
|
|
186 /*
|
|
187 scr_CreatePopup("Info", "Trying to create the account...", 60, 0, NULL);
|
|
188
|
|
189 strcpy(stringtosend, "<iq type='set' id='reg' to='");
|
|
190 strcat(stringtosend, server);
|
|
191 strcat(stringtosend, "'>");
|
|
192 strcat(stringtosend, "<query xmlns='jabber:iq:register'>");
|
|
193 strcat(stringtosend, "<username>");
|
|
194 strcat(stringtosend, user);
|
|
195 strcat(stringtosend, "</username><password>");
|
|
196 strcat(stringtosend, pass);
|
|
197 strcat(stringtosend, "</password>");
|
|
198 strcat(stringtosend, "</query></iq>\n");
|
|
199 if (!sk_send(sock, stringtosend)) {
|
|
200 perror("senddata (server.c:167)");
|
|
201 return NULL;
|
|
202 }
|
|
203
|
|
204 response = sk_recv(sock);
|
|
205 */
|
|
206 scr_TerminateCurses();
|
26
|
207 /* printf("Reinicie cabber!\n\n"); */
|
|
208 free(idsession);
|
|
209 free(stringtosend);
|
24
|
210 return NULL;
|
|
211 }
|
|
212 free(response);
|
|
213 free(stringtosend);
|
|
214
|
|
215 return idsession;
|
|
216 }
|
|
217
|
|
218
|
|
219 /* Desc: broadcast presence
|
|
220 *
|
|
221 * In : socket, presence string
|
|
222 * Out : ?
|
|
223 *
|
|
224 * Note: see `sk_send' for output values
|
|
225 */
|
|
226 int srv_setpresence(int sock, const char *type)
|
|
227 {
|
|
228 int rv;
|
|
229 char *str = malloc(1024);
|
|
230
|
26
|
231 sprintf(str, "<presence><status>%.512s</status></presence>", type);
|
24
|
232 if (!(rv = sk_send(sock, str))) {
|
|
233 perror("senddata (server.c:199)");
|
|
234 }
|
|
235 free(str);
|
|
236
|
|
237 return rv;
|
|
238 }
|
|
239
|
|
240
|
|
241 /* Desc: request roster
|
|
242 *
|
|
243 * In : socket
|
|
244 * Out : roster string
|
|
245 *
|
|
246 * Note: it is up to the caller to free the returned string
|
|
247 */
|
|
248 char *srv_getroster(int sock)
|
|
249 {
|
|
250 char *str = malloc(1024);
|
|
251
|
|
252 strcpy(str, "<iq type='get' id='1001'><query xmlns='");
|
|
253 strcat(str, "jabber:iq:roster'/></iq>\n");
|
|
254 if (!sk_send(sock, str)) {
|
|
255 perror("senddata (server.c:222)");
|
26
|
256 free(str);
|
24
|
257 return NULL;
|
|
258 }
|
|
259 free(str);
|
|
260
|
|
261 return sk_recv(sock);
|
|
262 }
|
|
263
|
|
264
|
|
265 /* Desc: send text to buddy
|
|
266 *
|
|
267 * In : socket, destination jid, text, source jid
|
|
268 * Out : 0 = ok
|
|
269 *
|
|
270 * Note: -
|
|
271 */
|
|
272 int
|
|
273 srv_sendtext(int sock, const char *to, const char *text, const char *from)
|
|
274 {
|
|
275 char *stringtosend = malloc(2048);
|
|
276 char *utf8inputline = utf8_encode(text);
|
|
277
|
|
278 sprintf(stringtosend,
|
27
|
279 "<message from='%s' to='%s' type='chat'><body>%.1536s</body></message>",
|
24
|
280 from, to, utf8inputline);
|
|
281 if (!sk_send(sock, stringtosend)) {
|
|
282 perror("senddata (server.c:247)");
|
26
|
283 free(stringtosend);
|
|
284 free(utf8inputline);
|
24
|
285 return -1;
|
|
286 }
|
|
287
|
|
288 free(stringtosend);
|
|
289 free(utf8inputline);
|
|
290 return 0;
|
|
291 }
|
|
292
|
|
293 int check_io(int fd1, int fd2)
|
|
294 {
|
|
295 int n = 0, i;
|
|
296 fd_set fds;
|
|
297 int io_pending = 0;
|
|
298
|
|
299 i = fd1;
|
|
300 if (fd2 > fd1)
|
|
301 i = fd2;
|
|
302
|
|
303 FD_ZERO(&fds);
|
|
304 if (fd1 >= 0)
|
|
305 FD_SET(fd1, &fds);
|
|
306 else
|
|
307 fd1 = 0;
|
|
308 if (fd2 >= 0)
|
|
309 FD_SET(fd2, &fds);
|
|
310 else
|
|
311 fd2 = 0;
|
|
312
|
|
313 if (fd2 == 0 && io_pending)
|
|
314 n = 2;
|
|
315 else if (select(i + 1, &fds, NULL, NULL, NULL) > 0)
|
|
316 n = 1 * (FD_ISSET(fd1, &fds) > 0) + 2 * (FD_ISSET(fd2, &fds) > 0);
|
|
317
|
|
318 return (n);
|
|
319 }
|
|
320
|
|
321 /* Desc: read data from server
|
|
322 *
|
|
323 * In : socket
|
|
324 * Out : ptr to newly allocated srv_msg struct
|
|
325 *
|
|
326 * Note: returns NULL if no input from server
|
|
327 */
|
|
328 srv_msg *readserver(int sock)
|
|
329 {
|
|
330 char *buffer = sk_recv(sock);
|
|
331
|
|
332 if (buffer != NULL) {
|
|
333 srv_msg *msg = calloc(1, sizeof(srv_msg));
|
|
334 char *to = getattr(buffer, "to='");
|
|
335 char *from = getattr(buffer, "from='");
|
|
336 char *id = getattr(buffer, "id='");
|
|
337 char *type = getattr(buffer, "type='");
|
|
338 char *body = gettag(buffer, "body");
|
|
339 char *status = gettag(buffer, "status");
|
|
340 char *show = gettag(buffer, "show");
|
|
341 char *line = (char *) malloc(1024);
|
|
342 memset(line, 0, 1024);
|
|
343
|
|
344 /* scan for buffer */
|
|
345 if (!strncmp(buffer, "<message", 8)) { /* manage messages */
|
|
346 msg->m = SM_MESSAGE;
|
|
347 } else if (!strncmp(buffer, "<presence", 9)) { /* manage presences */
|
|
348 msg->m = SM_PRESENCE;
|
|
349 if (!strncmp(type, "UNK", 3)) { /* assume online */
|
|
350 msg->connected = FLAG_BUDDY_CONNECTED;
|
|
351 } else if (!strncmp(type, "unavailable", 11)) { /* offline */
|
|
352 msg->connected = 0;
|
|
353 }
|
|
354 } else {
|
|
355 msg->m = SM_UNHANDLED;
|
|
356 }
|
|
357
|
|
358 /* write the parsed buffer */
|
|
359 switch (msg->m) {
|
|
360 case SM_MESSAGE:
|
|
361 {
|
|
362 char *aux = strstr(from, "/");
|
|
363 if (aux)
|
|
364 *aux = '\0';
|
|
365 msg->from = from;
|
|
366 msg->body = utf8_decode(body);
|
|
367 ut_WriteLog("+OK [%s]\n", buffer);
|
|
368 }
|
|
369 break;
|
|
370
|
|
371 case SM_PRESENCE:
|
|
372 {
|
|
373 char *aux = strstr(from, "/");
|
|
374 if (aux)
|
|
375 *aux = '\0';
|
|
376 msg->from = from;
|
|
377 }
|
|
378 break;
|
|
379
|
|
380 case SM_UNHANDLED:
|
|
381 ut_WriteLog("BAD [%s]\n", buffer);
|
|
382 break;
|
|
383
|
|
384 }
|
|
385 free(line);
|
|
386 if (strncmp(to, "UNK", 3))
|
|
387 free(to);
|
|
388 if (strncmp(from, "UNK", 3) && (msg->m != SM_MESSAGE)
|
|
389 && (msg->m != SM_PRESENCE))
|
|
390 free(from);
|
|
391 if (strncmp(id, "UNK", 3))
|
|
392 free(id);
|
|
393 if (strncmp(type, "UNK", 3))
|
|
394 free(type);
|
|
395 if (strncmp(body, "UNK", 3))
|
|
396 free(body);
|
|
397 if (strncmp(status, "UNK", 3))
|
|
398 free(status);
|
|
399 if (strncmp(show, "UNK", 3))
|
|
400 free(show);
|
|
401 free(buffer);
|
|
402
|
|
403 return msg;
|
|
404 }
|
|
405
|
|
406 return NULL;
|
|
407 }
|
|
408
|
|
409 void srv_AddBuddy(int sock, char *jidname)
|
|
410 {
|
|
411 char *buffer = (char *) malloc(1024);
|
|
412 char *p, *str;
|
|
413 int i;
|
|
414
|
|
415 memset(buffer, 0, 1024);
|
|
416 strcpy(buffer, "<iq type='set'>");
|
|
417 strcat(buffer, " <query xmlns='jabber:iq:roster'>");
|
|
418 strcat(buffer, " <item");
|
|
419 strcat(buffer, " jid='");
|
|
420 strcat(buffer, jidname);
|
|
421 strcat(buffer, "' name='");
|
|
422
|
|
423 str = strdup(jidname);
|
|
424 p = strstr(str, "@");
|
|
425 if (p)
|
|
426 *p = '\0';
|
|
427 strcat(buffer, str);
|
|
428 strcat(buffer, "'/></query></iq>");
|
|
429 sk_send(sock, buffer);
|
|
430 free(buffer);
|
|
431
|
|
432 for (i = 0; i < 2; i++) {
|
|
433 buffer = sk_recv(sock);
|
|
434 ut_WriteLog("[Subscription]: %s\n", buffer);
|
|
435 free(buffer);
|
|
436 }
|
|
437
|
|
438 buffer = (char *) malloc(1024);
|
|
439 memset(buffer, 0, 1024);
|
|
440 strcpy(buffer, "<presence to='");
|
|
441 strcat(buffer, jidname);
|
|
442 strcat(buffer, "' type='subscribe'>");
|
|
443 strcat(buffer, "<status>I would like to add you!</status></presence>");
|
|
444 sk_send(sock, buffer);
|
|
445 free(buffer);
|
|
446
|
|
447 buffer = sk_recv(sock);
|
|
448 ut_WriteLog("[Subscription]: %s\n", buffer);
|
|
449 free(buffer);
|
|
450
|
|
451 buffer = (char *) malloc(1024);
|
|
452 memset(buffer, 0, 1024);
|
|
453 strcpy(buffer, "<presence to='");
|
|
454 strcat(buffer, jidname);
|
|
455 strcat(buffer, "' type='subscribed'/>");
|
|
456 sk_send(sock, buffer);
|
|
457 free(buffer);
|
|
458
|
|
459 buffer = sk_recv(sock);
|
|
460 ut_WriteLog("[Subscription]: %s\n", buffer);
|
|
461 free(buffer);
|
|
462 }
|
|
463
|
|
464 void srv_DelBuddy(int sock, char *jidname)
|
|
465 {
|
|
466 char *buffer = (char *) malloc(1024);
|
|
467
|
|
468 strcpy(buffer, "<iq type='set'><query xmlns='jabber:iq:roster'>");
|
|
469 strcat(buffer, "<item jid='");
|
|
470 strcat(buffer, jidname);
|
|
471 strcat(buffer, "' subscription='remove'/></query></iq>");
|
|
472
|
|
473 sk_send(sock, buffer);
|
|
474 free(buffer);
|
|
475
|
|
476 buffer = sk_recv(sock);
|
|
477 ut_WriteLog("[SubscriptionRemove]: %s\n", buffer);
|
|
478 free(buffer);
|
|
479 }
|