comparison mcabber/src/server.c @ 26:8588f5a4b638

[/trunk] Changeset 42 by mikael * Potential bugfixes.
author mikael
date Mon, 28 Mar 2005 08:54:43 +0000
parents e88b15cbf2de
children 77e6bd2ccde6
comparison
equal deleted inserted replaced
25:bf3d6e241714 26:8588f5a4b638
27 */ 27 */
28 char *srv_poll(int sock) 28 char *srv_poll(int sock)
29 { 29 {
30 struct pollfd sock_p; 30 struct pollfd sock_p;
31 sock_p.fd = sock; 31 sock_p.fd = sock;
32 sock_p.events = POLLIN | POLLPRI; 32 sock_p.events = (short int) (POLLIN | POLLPRI);
33 sock_p.revents = 0; 33 sock_p.revents = 0;
34 poll(&sock_p, 1, 0); 34 poll(&sock_p, 1, 0);
35 35
36 if (sock_p.revents) { 36 if (sock_p.revents) {
37 return sk_recv(sock); 37 return sk_recv(sock);
55 55
56 if ((i = inet_addr(host)) == -1) { 56 if ((i = inet_addr(host)) == -1) {
57 if (!(he = gethostbyname(host))) 57 if (!(he = gethostbyname(host)))
58 return 0; 58 return 0;
59 else 59 else
60 return (*(u_long *) he->h_addr); 60 return (*(u_long *) he->h_addr); // XXX Memory leak
61 } 61 }
62 62
63 return i; 63 return (u_long) i;
64 } 64 }
65 65
66 66
67 /* Desc: connect to jabber server 67 /* Desc: connect to jabber server
68 * 68 *
112 char *srv_login(int sock, const char *server, const char *user, 112 char *srv_login(int sock, const char *server, const char *user,
113 const char *pass, const char *resource) 113 const char *pass, const char *resource)
114 { 114 {
115 char *stringtosend = malloc(2048); 115 char *stringtosend = malloc(2048);
116 char *response, *aux; 116 char *response, *aux;
117 char *idsession = malloc(128); 117 char *idsession = calloc(64, sizeof(char));
118 int pos = 0; 118 int pos = 0;
119 119
120 memset(stringtosend, 0, 2048); 120 memset(stringtosend, 0, 2048);
121 strcpy(stringtosend, "<?xml version='1.0' encoding='UTF-8' ?>"); 121 strcpy(stringtosend, "<?xml version='1.0' encoding='UTF-8' ?>");
122 strcat(stringtosend, "<stream:stream to='"); 122 strcat(stringtosend, "<stream:stream to='");
124 strcat(stringtosend, "' xmlns='jabber:client' xmlns:stream='"); 124 strcat(stringtosend, "' xmlns='jabber:client' xmlns:stream='");
125 strcat(stringtosend, "http://etherx.jabber.org/streams'>\n"); 125 strcat(stringtosend, "http://etherx.jabber.org/streams'>\n");
126 126
127 if (!sk_send(sock, stringtosend)) { 127 if (!sk_send(sock, stringtosend)) {
128 perror("senddata (server.c:132)"); 128 perror("senddata (server.c:132)");
129 free(stringtosend);
130 free(idsession);
129 return NULL; 131 return NULL;
130 } 132 }
131 response = sk_recv(sock); 133 response = sk_recv(sock);
132 if (strstr(response, "error")) { 134 if (strstr(response, "error")) {
133 /* fprintf(stderr, "Response not valid:\n%s\n\n", response); */ 135 /* fprintf(stderr, "Response not valid:\n%s\n\n", response); */
134 scr_CreatePopup("Error", 136 scr_CreatePopup("Error",
135 "Bad answer from the server", 60, 0, NULL); 137 "Bad answer from the server", 60, 0, NULL);
138 free(response);
139 free(idsession);
140 free(stringtosend);
136 return NULL; 141 return NULL;
137 } 142 }
138 aux = response; 143 aux = response;
139 while (strncmp(aux, "id", 2)) 144 while (strncmp(aux, "id", 2))
140 aux++; 145 aux++;
143 while (strncmp(aux, "'", 1)) { 148 while (strncmp(aux, "'", 1)) {
144 aux++; 149 aux++;
145 pos++; 150 pos++;
146 } 151 }
147 aux -= pos; 152 aux -= pos;
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 }
148 strncpy(idsession, aux, pos); 160 strncpy(idsession, aux, pos);
149 161
150 free(response); 162 free(response);
151 163
152 strcpy(stringtosend, "<iq type='set' id='1000'>"); 164 strcpy(stringtosend, "<iq type='set' id='1000'>");
188 } 200 }
189 201
190 response = sk_recv(sock); 202 response = sk_recv(sock);
191 */ 203 */
192 scr_TerminateCurses(); 204 scr_TerminateCurses();
193 printf("Reinicie cabber!\n\n"); 205 /* printf("Reinicie cabber!\n\n"); */
206 free(idsession);
207 free(stringtosend);
194 return NULL; 208 return NULL;
195 } 209 }
196 free(response); 210 free(response);
197 free(stringtosend); 211 free(stringtosend);
198 212
210 int srv_setpresence(int sock, const char *type) 224 int srv_setpresence(int sock, const char *type)
211 { 225 {
212 int rv; 226 int rv;
213 char *str = malloc(1024); 227 char *str = malloc(1024);
214 228
215 sprintf(str, "<presence><status>%s</status></presence>", type); 229 sprintf(str, "<presence><status>%.512s</status></presence>", type);
216 if (!(rv = sk_send(sock, str))) { 230 if (!(rv = sk_send(sock, str))) {
217 perror("senddata (server.c:199)"); 231 perror("senddata (server.c:199)");
218 } 232 }
219 free(str); 233 free(str);
220 234
235 249
236 strcpy(str, "<iq type='get' id='1001'><query xmlns='"); 250 strcpy(str, "<iq type='get' id='1001'><query xmlns='");
237 strcat(str, "jabber:iq:roster'/></iq>\n"); 251 strcat(str, "jabber:iq:roster'/></iq>\n");
238 if (!sk_send(sock, str)) { 252 if (!sk_send(sock, str)) {
239 perror("senddata (server.c:222)"); 253 perror("senddata (server.c:222)");
254 free(str);
240 return NULL; 255 return NULL;
241 } 256 }
242 free(str); 257 free(str);
243 258
244 return sk_recv(sock); 259 return sk_recv(sock);
261 sprintf(stringtosend, 276 sprintf(stringtosend,
262 "<message from='%s' to='%s' type='chat'><body>%s</body></message>", 277 "<message from='%s' to='%s' type='chat'><body>%s</body></message>",
263 from, to, utf8inputline); 278 from, to, utf8inputline);
264 if (!sk_send(sock, stringtosend)) { 279 if (!sk_send(sock, stringtosend)) {
265 perror("senddata (server.c:247)"); 280 perror("senddata (server.c:247)");
281 free(stringtosend);
282 free(utf8inputline);
266 return -1; 283 return -1;
267 } 284 }
268 285
269 free(stringtosend); 286 free(stringtosend);
270 free(utf8inputline); 287 free(utf8inputline);