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