Mercurial > pidgin.yaz
annotate src/protocols/simple/simple.c @ 11409:1e495a5fcbbc
[gaim-migrate @ 13646]
ntlm, tcp and connection close fixes
committer: Tailor Script <tailor@pidgin.im>
| author | Thomas Butter <tbutter> |
|---|---|
| date | Thu, 01 Sep 2005 09:09:33 +0000 |
| parents | be776f9b1818 |
| children | e1ab173ef3b5 |
| rev | line source |
|---|---|
| 11181 | 1 /** |
| 2 * @file simple.c | |
| 3 * | |
| 4 * gaim | |
| 5 * | |
| 6 * Copyright (C) 2005 Thomas Butter <butter@uni-mannheim.de> | |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
7 * |
| 11345 | 8 * *** |
| 9 * Thanks to Google's Summer of Code Program and the helpful mentors | |
| 10 * *** | |
| 11181 | 11 * |
| 12 * This program is free software; you can redistribute it and/or modify | |
| 13 * it under the terms of the GNU General Public License as published by | |
| 14 * the Free Software Foundation; either version 2 of the License, or | |
| 15 * (at your option) any later version. | |
| 16 * | |
| 17 * This program is distributed in the hope that it will be useful, | |
| 18 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 20 * GNU General Public License for more details. | |
| 21 * | |
| 22 * You should have received a copy of the GNU General Public License | |
| 23 * along with this program; if not, write to the Free Software | |
| 24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 25 */ | |
| 26 | |
| 27 #include "internal.h" | |
| 28 | |
| 29 #include "accountopt.h" | |
| 30 #include "blist.h" | |
| 31 #include "conversation.h" | |
| 32 #include "debug.h" | |
| 33 #include "notify.h" | |
| 11345 | 34 #include "privacy.h" |
| 11181 | 35 #include "prpl.h" |
| 36 #include "plugin.h" | |
| 37 #include "util.h" | |
| 38 #include "version.h" | |
| 39 #include "network.h" | |
| 40 #include "xmlnode.h" | |
| 41 | |
| 42 #include "simple.h" | |
| 43 #include "sipmsg.h" | |
| 11383 | 44 #include "dnssrv.h" |
| 11409 | 45 #include "ntlm.h" |
| 11181 | 46 |
| 47 static char *gentag() { | |
| 48 return g_strdup_printf("%04d%04d", rand() & 0xFFFF, rand() & 0xFFFF); | |
| 49 } | |
| 50 | |
| 51 static char *genbranch() { | |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
52 return g_strdup_printf("z9hG4bK%04X%04X%04X%04X%04X", |
| 11181 | 53 rand() & 0xFFFF, |
| 54 rand() & 0xFFFF, | |
| 55 rand() & 0xFFFF, | |
| 56 rand() & 0xFFFF, | |
| 57 rand() & 0xFFFF); | |
| 58 } | |
| 59 | |
| 60 static char *gencallid() { | |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
61 return g_strdup_printf("%04Xg%04Xa%04Xi%04Xm%04Xt%04Xb%04Xx%04Xx", |
| 11181 | 62 rand() & 0xFFFF, |
| 63 rand() & 0xFFFF, | |
| 64 rand() & 0xFFFF, | |
| 65 rand() & 0xFFFF, | |
| 66 rand() & 0xFFFF, | |
| 67 rand() & 0xFFFF, | |
| 68 rand() & 0xFFFF, | |
| 69 rand() & 0xFFFF); | |
| 70 } | |
| 71 | |
| 72 static const char *simple_list_icon(GaimAccount *a, GaimBuddy *b) { | |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
73 return "simple"; |
| 11181 | 74 } |
| 75 | |
| 76 static void simple_keep_alive(GaimConnection *gc) { | |
| 11194 | 77 struct simple_account_data *sip = gc->proto_data; |
| 11341 | 78 if(sip->udp) { /* in case of UDP send a packet only with a 0 byte to |
| 79 remain in the NAT table */ | |
| 11194 | 80 gchar buf[2]={0,0}; |
| 81 gaim_debug_info("simple", "sending keep alive\n"); | |
| 82 sendto(sip->fd, buf, 1, 0, (struct sockaddr*)&sip->serveraddr, sizeof(struct sockaddr_in)); | |
| 83 } | |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
84 return; |
| 11181 | 85 } |
| 86 | |
| 87 static gboolean process_register_response(struct simple_account_data *sip, struct sipmsg *msg, struct transaction *tc); | |
| 88 static void send_notify(struct simple_account_data *sip, struct simple_watcher *); | |
| 89 | |
| 90 static void send_publish(struct simple_account_data *sip); | |
| 91 | |
| 92 static void do_notifies(struct simple_account_data *sip) { | |
| 93 GSList *tmp = sip->watcher; | |
| 94 gaim_debug_info("simple", "do_notifies()\n"); | |
| 11345 | 95 if((sip->republish != -1) || sip->republish < time(NULL)) { |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
96 if(gaim_account_get_bool(sip->account, "dopublish", TRUE)) { |
| 11345 | 97 send_publish(sip); |
| 98 } | |
| 99 } | |
| 11181 | 100 |
| 101 while(tmp) { | |
| 102 gaim_debug_info("simple", "notifying %s\n", ((struct simple_watcher*)tmp->data)->name); | |
| 103 send_notify(sip, tmp->data); | |
| 104 tmp = tmp->next; | |
| 105 } | |
| 106 } | |
| 107 | |
| 108 static void simple_set_status(GaimAccount *account, GaimStatus *status) { | |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
109 GaimStatusPrimitive primitive = gaim_status_type_get_primitive(gaim_status_get_type(status)); |
| 11181 | 110 struct simple_account_data *sip = NULL; |
| 111 if (!gaim_status_is_active(status)) | |
| 112 return; | |
| 113 | |
| 114 if(account->gc) sip = account->gc->proto_data; | |
| 115 if(sip) { | |
| 116 if(sip->status) g_free(sip->status); | |
| 117 if(primitive == GAIM_STATUS_AVAILABLE) sip->status = g_strdup("available"); | |
| 118 else sip->status = g_strdup("busy"); | |
| 119 | |
| 120 do_notifies(sip); | |
| 121 } | |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
122 if ((primitive != GAIM_STATUS_OFFLINE) |
| 11181 | 123 && (!gaim_account_is_connected(account))) { |
| 124 gaim_account_connect(account); | |
| 125 } | |
| 126 } | |
| 127 | |
| 128 static struct sip_connection *connection_find(struct simple_account_data *sip, int fd) { | |
| 129 struct sip_connection *ret = NULL; | |
| 130 GSList *entry = sip->openconns; | |
| 131 while(entry) { | |
| 132 ret = entry->data; | |
| 133 if(ret->fd == fd) return ret; | |
| 134 entry = entry->next; | |
| 135 } | |
| 136 return NULL; | |
| 137 } | |
| 138 | |
| 139 static struct simple_watcher *watcher_find(struct simple_account_data *sip, gchar *name) { | |
| 140 struct simple_watcher *watcher; | |
| 141 GSList *entry = sip->watcher; | |
| 142 while(entry) { | |
| 143 watcher = entry->data; | |
| 144 if(!strcmp(name, watcher->name)) return watcher; | |
| 145 entry = entry->next; | |
| 146 } | |
| 147 return NULL; | |
| 148 } | |
| 149 | |
| 150 static struct simple_watcher *watcher_create(struct simple_account_data *sip, gchar *name, gchar *callid, gchar *ourtag, gchar *theirtag) { | |
| 151 struct simple_watcher *watcher = g_new0(struct simple_watcher,1); | |
| 152 watcher->name = g_strdup(name); | |
| 153 watcher->dialog.callid = g_strdup(callid); | |
| 154 watcher->dialog.ourtag = g_strdup(ourtag); | |
| 155 watcher->dialog.theirtag = g_strdup(theirtag); | |
| 156 sip->watcher = g_slist_append(sip->watcher, watcher); | |
| 157 return watcher; | |
| 158 } | |
| 159 | |
| 160 static void watcher_remove(struct simple_account_data *sip, gchar *name) { | |
| 161 struct simple_watcher *watcher = watcher_find(sip, name); | |
| 162 sip->watcher = g_slist_remove(sip->watcher, watcher); | |
| 163 g_free(watcher->name); | |
| 164 g_free(watcher->dialog.callid); | |
| 165 g_free(watcher->dialog.ourtag); | |
| 166 g_free(watcher->dialog.theirtag); | |
| 167 g_free(watcher); | |
| 168 } | |
| 169 | |
| 170 static struct sip_connection *connection_create(struct simple_account_data *sip, int fd) { | |
| 171 struct sip_connection *ret = g_new0(struct sip_connection,1); | |
| 172 ret->fd = fd; | |
| 173 sip->openconns = g_slist_append(sip->openconns, ret); | |
| 174 return ret; | |
| 175 } | |
| 176 | |
| 177 static void connection_remove(struct simple_account_data *sip, int fd) { | |
| 178 struct sip_connection *conn = connection_find(sip, fd); | |
| 179 sip->openconns = g_slist_remove(sip->openconns, conn); | |
| 180 if(conn->inputhandler) gaim_input_remove(conn->inputhandler); | |
| 181 if(conn->inbuf) g_free(conn->inbuf); | |
| 182 g_free(conn); | |
| 183 } | |
| 184 | |
| 11346 | 185 static void connection_free_all(struct simple_account_data *sip) { |
| 186 struct sip_connection *ret = NULL; | |
| 187 GSList *entry = sip->openconns; | |
| 188 while(entry) { | |
| 189 ret = entry->data; | |
| 190 connection_remove(sip, ret->fd); | |
| 191 entry = sip->openconns; | |
| 192 } | |
| 193 } | |
| 194 | |
| 11181 | 195 static void simple_add_buddy(GaimConnection *gc, GaimBuddy *buddy, GaimGroup *group) |
| 196 { | |
| 197 struct simple_account_data *sip = (struct simple_account_data *)gc->proto_data; | |
| 198 struct simple_buddy *b; | |
| 199 if(strncmp("sip:", buddy->name,4)) { | |
| 200 gchar *buf = g_strdup_printf(_("Could not add the buddy %s because every simple user has to start with 'sip:'."), buddy->name); | |
| 201 gaim_notify_error(gc, NULL, _("Unable To Add"), buf); | |
| 202 g_free(buf); | |
| 203 gaim_blist_remove_buddy(buddy); | |
| 204 return; | |
| 205 } | |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
206 if(!g_hash_table_lookup(sip->buddies, buddy->name)) { |
| 11181 | 207 b = g_new0(struct simple_buddy, 1); |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
208 gaim_debug_info("simple","simple_add_buddy %s\n",buddy->name); |
| 11181 | 209 b->name = g_strdup(buddy->name); |
| 210 g_hash_table_insert(sip->buddies, b->name, b); | |
| 211 } else { | |
| 212 gaim_debug_info("simple","buddy %s already in internal list\n", buddy->name); | |
| 213 } | |
| 214 } | |
| 215 | |
| 216 static void simple_get_buddies(GaimConnection *gc) { | |
| 217 GaimBlistNode *gnode, *cnode, *bnode; | |
| 218 | |
| 219 gaim_debug_info("simple","simple_get_buddies\n"); | |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
220 |
| 11181 | 221 for(gnode = gaim_get_blist()->root; gnode; gnode = gnode->next) { |
| 222 if(!GAIM_BLIST_NODE_IS_GROUP(gnode)) continue; | |
| 223 for(cnode = gnode->child; cnode; cnode = cnode->next) { | |
| 224 if(!GAIM_BLIST_NODE_IS_CONTACT(cnode)) continue; | |
| 225 for(bnode = cnode->child; bnode; bnode = bnode->next) { | |
| 226 if(!GAIM_BLIST_NODE_IS_BUDDY(bnode)) continue; | |
| 11192 | 227 if(((GaimBuddy*)bnode)->account == gc->account) |
| 228 simple_add_buddy(gc, (GaimBuddy*)bnode, (GaimGroup *)gnode); | |
| 11181 | 229 } |
| 230 } | |
| 231 } | |
| 232 } | |
| 233 | |
| 234 static void simple_remove_buddy(GaimConnection *gc, GaimBuddy *buddy, GaimGroup *group) | |
| 235 { | |
| 236 struct simple_account_data *sip = (struct simple_account_data *)gc->proto_data; | |
| 237 struct simple_buddy *b = g_hash_table_lookup(sip->buddies, buddy->name); | |
| 238 g_hash_table_remove(sip->buddies, buddy->name); | |
| 239 g_free(b->name); | |
| 240 g_free(b); | |
| 241 } | |
| 242 | |
| 243 static GList *simple_status_types(GaimAccount *acc) { | |
| 244 GaimStatusType *type; | |
| 245 GList *types = NULL; | |
| 246 gaim_debug_info("simple","called simple_status_types\n"); | |
| 247 type = gaim_status_type_new(GAIM_STATUS_OFFLINE, "offline", _("Offline"), FALSE); | |
| 248 types = g_list_append(types, type); | |
| 249 | |
| 250 type = gaim_status_type_new(GAIM_STATUS_ONLINE, "online", _("Online"), FALSE); | |
| 251 types = g_list_append(types, type); | |
| 252 | |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
253 type = gaim_status_type_new_with_attrs( |
|
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
254 GAIM_STATUS_AVAILABLE, "available", _("Available"), |
|
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
255 TRUE, TRUE, FALSE, |
|
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
256 "message", _("Message"), gaim_value_new(GAIM_TYPE_STRING), NULL); |
|
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
257 types = g_list_append(types, type); |
|
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
258 |
| 11181 | 259 return types; |
| 260 } | |
| 261 | |
| 11346 | 262 static gchar *auth_header(struct simple_account_data *sip, struct sip_auth *auth, gchar *method, gchar *target) { |
| 263 gchar noncecount[9]; | |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
264 HASHHEX HA2; |
| 11346 | 265 HASHHEX response; |
| 266 gchar *ret; | |
| 11409 | 267 gchar *tmp; |
| 268 | |
| 269 if(auth->type == 1) { /* Digest */ | |
| 270 sprintf(noncecount, "%08d", auth->nc++); | |
| 271 DigestCalcResponse(auth->HA1, auth->nonce, noncecount, "", "", method, target, HA2, response); | |
| 272 gaim_debug(GAIM_DEBUG_MISC, "simple", "response %s\n", response); | |
| 273 ret = g_strdup_printf("Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", nc=\"%s\", response=\"%s\"\r\n",sip->username, auth->realm, auth->nonce, target, noncecount, response); | |
| 274 return ret; | |
| 275 } else if(auth->type == 2) { /* NTLM */ | |
| 276 if(auth->nc == 3) { | |
| 277 ret = gaim_ntlm_gen_type3(sip->username, sip->password, "gaim", sip->servername, auth->nonce); | |
| 278 tmp = g_strdup_printf("NTLM %s\r\n",ret); | |
| 279 g_free(ret); | |
| 280 return tmp; | |
| 281 } | |
| 282 ret = gaim_ntlm_gen_type1("gaim", sip->servername); | |
| 283 tmp = g_strdup_printf("NTLM %s\r\n", ret); | |
| 284 g_free(ret); | |
| 285 return tmp; | |
| 286 } | |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
287 |
| 11346 | 288 sprintf(noncecount, "%08d", auth->nc++); |
| 289 DigestCalcResponse(auth->HA1, auth->nonce, noncecount, "", "", method, target, HA2, response); | |
| 290 gaim_debug(GAIM_DEBUG_MISC, "simple", "response %s\n", response); | |
| 291 ret = g_strdup_printf("Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", nc=\"%s\", response=\"%s\"\r\n",sip->username, auth->realm, auth->nonce, target, noncecount, response); | |
| 292 return ret; | |
| 293 } | |
| 294 | |
| 295 static void fill_auth(struct simple_account_data *sip, gchar *hdr, struct sip_auth *auth) { | |
| 11409 | 296 int i=0; |
| 11346 | 297 if(!hdr) { |
| 11409 | 298 gaim_debug_error("simple", "fill_auth: hdr==NULL\n"); |
| 11346 | 299 return; |
| 300 } | |
| 11409 | 301 |
| 302 if(!g_strncasecmp(hdr, "NTLM", 4)) { | |
| 303 gaim_debug_info("simple", "found NTLM\n"); | |
| 304 auth->type = 2; | |
| 305 if(!auth->nonce && !auth->nc) { | |
| 306 auth->nc = 1; | |
| 307 } | |
| 308 if(!auth->nonce && auth->nc==2) { | |
| 309 auth->nc = 3; | |
| 310 auth->nonce = gaim_ntlm_parse_type2(hdr+5); | |
| 311 } | |
| 312 return; | |
| 313 } | |
| 314 | |
| 315 auth->type = 1; | |
| 11346 | 316 gchar **parts = g_strsplit(hdr, " ", 0); |
| 317 while(parts[i]) { | |
| 318 if(!strncmp(parts[i],"nonce",5)) { | |
| 319 auth->nonce = g_strndup(parts[i]+7,strlen(parts[i]+7)-1); | |
| 320 } | |
| 321 if(!strncmp(parts[i],"realm",5)) { | |
| 322 auth->realm = g_strndup(parts[i]+7,strlen(parts[i]+7)-2); | |
| 323 } | |
| 324 i++; | |
| 325 } | |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
326 |
| 11346 | 327 gaim_debug(GAIM_DEBUG_MISC, "simple", "nonce: %s realm: %s ", auth->nonce, auth->realm); |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
328 |
| 11346 | 329 DigestCalcHA1("md5", sip->username, auth->realm, sip->password, auth->nonce, "", auth->HA1); |
| 330 | |
| 331 auth->nc=1; | |
| 332 } | |
| 333 | |
| 11181 | 334 static void simple_input_cb(gpointer data, gint source, GaimInputCondition cond); |
| 335 | |
| 336 static void send_later_cb(gpointer data, gint source, GaimInputCondition cond) { | |
| 337 GaimConnection *gc = data; | |
| 338 struct simple_account_data *sip = gc->proto_data; | |
| 339 struct sip_connection *conn; | |
| 340 | |
| 341 if( source < 0 ) { | |
| 342 gaim_connection_error(gc,"Could not connect"); | |
| 343 return; | |
| 344 } | |
| 345 | |
| 346 sip->fd = source; | |
| 347 sip->connecting = 0; | |
| 348 write(sip->fd, sip->sendlater, strlen(sip->sendlater)); | |
| 349 conn = connection_create(sip, source); | |
| 350 conn->inputhandler = gaim_input_add(sip->fd, GAIM_INPUT_READ, simple_input_cb, gc); | |
| 351 g_free(sip->sendlater); | |
| 352 sip->sendlater = 0; | |
| 353 } | |
| 354 | |
| 355 | |
| 356 static void sendlater(GaimConnection *gc, const char *buf) { | |
| 357 struct simple_account_data *sip = gc->proto_data; | |
| 358 int error = 0; | |
| 359 if(!sip->connecting) { | |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
360 gaim_debug_info("simple","connecting to %s port %d\n", sip->realhostname ? sip->realhostname : "{NULL}", sip->realport); |
|
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
361 error = gaim_proxy_connect(sip->account, sip->realhostname, sip->realport, send_later_cb, gc); |
|
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
362 if(error) { |
|
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
363 gaim_connection_error(gc, _("Couldn't create socket")); |
|
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
364 } |
| 11181 | 365 sip->connecting = 1; |
| 366 } | |
| 367 if(sip->sendlater) { | |
| 368 gchar *old = sip->sendlater; | |
| 369 sip->sendlater = g_strdup_printf("%s\r\n%s",old, buf); | |
| 370 } else { | |
| 371 sip->sendlater = g_strdup(buf); | |
| 372 } | |
| 373 } | |
| 374 | |
| 375 static int sendout_pkt(GaimConnection *gc, const char *buf) { | |
| 376 struct simple_account_data *sip = gc->proto_data; | |
| 377 time_t currtime = time(NULL); | |
| 11189 | 378 int ret = 0; |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
379 |
| 11181 | 380 gaim_debug(GAIM_DEBUG_MISC, "simple", "\n\nsending - %s\n######\n%s\n######\n\n", ctime(&currtime), buf); |
| 11189 | 381 if(sip->udp) { |
| 382 if(sendto(sip->fd, buf, strlen(buf), 0, (struct sockaddr*)&sip->serveraddr, sizeof(struct sockaddr_in)) < strlen(buf)) { | |
| 383 gaim_debug_info("simple", "could not send packet\n"); | |
| 384 } | |
| 385 } else { | |
| 386 if(sip->fd <0 ) { | |
| 387 sendlater(gc, buf); | |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
388 return 0; |
| 11189 | 389 } |
| 390 ret = write(sip->fd, buf, strlen(buf)); | |
| 391 if(ret < 0) { | |
| 392 sendlater(gc,buf); | |
| 393 return 0; | |
| 394 } | |
| 11181 | 395 } |
| 396 return ret; | |
| 397 } | |
| 398 | |
| 11194 | 399 static void sendout_sipmsg(struct simple_account_data *sip, struct sipmsg *msg) { |
| 400 gchar *oldstr; | |
| 401 gchar *outstr = g_strdup_printf("%s %s SIP/2.0\r\n", msg->method, msg->target); | |
| 402 gchar *name; | |
| 403 gchar *value; | |
| 404 GSList *tmp = msg->headers; | |
| 405 while(tmp) { | |
| 406 oldstr = outstr; | |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
407 name = ((struct siphdrelement*)(tmp->data))->name; |
|
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
408 value = ((struct siphdrelement*)(tmp->data))->value; |
|
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
409 outstr = g_strdup_printf("%s%s: %s\r\n",oldstr, name, value); |
| 11194 | 410 g_free(oldstr); |
| 411 tmp = g_slist_next(tmp); | |
| 412 } | |
| 413 oldstr = outstr; | |
| 414 if(msg->body) outstr = g_strdup_printf("%s\r\n%s", outstr, msg->body); | |
| 415 else outstr = g_strdup_printf("%s\r\n", outstr); | |
| 416 g_free(oldstr); | |
| 417 sendout_pkt(sip->gc, outstr); | |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
418 g_free(outstr); |
| 11194 | 419 } |
| 420 | |
| 11181 | 421 static void send_sip_response(GaimConnection *gc, struct sipmsg *msg, int code, char *text, char *body) { |
| 422 GSList *tmp = msg->headers; | |
| 423 char *oldstr; | |
| 424 char *name; | |
| 425 char *value; | |
| 426 char *outstr = g_strdup_printf("SIP/2.0 %d %s\r\n",code, text); | |
| 427 while(tmp) { | |
| 428 oldstr = outstr; | |
| 429 name = ((struct siphdrelement*)(tmp->data))->name; | |
| 430 value = ((struct siphdrelement*)(tmp->data))->value; | |
| 431 outstr = g_strdup_printf("%s%s: %s\r\n",oldstr, name, value); | |
| 432 g_free(oldstr); | |
| 433 tmp = g_slist_next(tmp); | |
| 434 } | |
| 435 oldstr = outstr; | |
| 436 if(body) outstr = g_strdup_printf("%s\r\n%s",outstr,body); | |
| 437 else outstr = g_strdup_printf("%s\r\n",outstr); | |
| 438 g_free(oldstr); | |
| 439 sendout_pkt(gc, outstr); | |
| 440 g_free(outstr); | |
| 441 } | |
| 442 | |
| 11194 | 443 static void transactions_remove(struct simple_account_data *sip, struct transaction *trans) { |
| 444 if(trans->msg) sipmsg_free(trans->msg); | |
| 445 sip->transactions = g_slist_remove(sip->transactions, trans); | |
| 446 g_free(trans); | |
| 447 } | |
| 448 | |
| 11181 | 449 static void transactions_add_buf(struct simple_account_data *sip, gchar *buf, void *callback) { |
| 450 struct transaction *trans = g_new0(struct transaction, 1); | |
| 451 trans->time = time(NULL); | |
| 452 trans->msg = sipmsg_parse_msg(buf); | |
| 453 trans->cseq = sipmsg_find_header(trans->msg, "CSeq"); | |
| 454 trans->callback = callback; | |
| 455 sip->transactions = g_slist_append(sip->transactions, trans); | |
| 456 } | |
| 457 | |
| 458 static struct transaction *transactions_find(struct simple_account_data *sip, struct sipmsg *msg) { | |
| 459 struct transaction *trans; | |
| 460 GSList *transactions = sip->transactions; | |
| 461 gchar *cseq = sipmsg_find_header(msg, "CSeq"); | |
| 462 | |
| 463 while(transactions) { | |
| 464 trans = transactions->data; | |
| 465 if(!strcmp(trans->cseq, cseq)) { | |
| 466 return trans; | |
| 467 } | |
| 468 transactions = transactions->next; | |
| 469 } | |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
470 |
| 11181 | 471 return (struct transaction *)NULL; |
| 472 } | |
| 473 | |
| 474 static void send_sip_request(GaimConnection *gc, gchar *method, gchar *url, gchar *to, gchar *addheaders, gchar *body, struct sip_dialog *dialog, TransCallback tc) { | |
| 475 struct simple_account_data *sip = gc->proto_data; | |
| 476 char *callid= dialog ? g_strdup(dialog->callid) : gencallid(); | |
| 477 char *auth=""; | |
| 478 char *addh=""; | |
| 479 gchar *branch = genbranch(); | |
| 480 char *buf; | |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
481 |
| 11181 | 482 if(addheaders) addh=addheaders; |
| 11409 | 483 if(sip->registrar.type && !strcmp(method,"REGISTER")) { |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
484 buf = auth_header(sip, &sip->registrar, method, url); |
| 11346 | 485 auth = g_strdup_printf("Authorization: %s",buf); |
| 486 g_free(buf); | |
| 11181 | 487 gaim_debug(GAIM_DEBUG_MISC, "simple", "header %s", auth); |
| 488 } | |
| 489 | |
| 11409 | 490 if(sip->proxy.type && strcmp(method,"REGISTER")) { |
| 11346 | 491 buf = auth_header(sip, &sip->proxy, method, url); |
| 492 auth = g_strdup_printf("Proxy-Authorization: %s",buf); | |
| 493 g_free(buf); | |
| 11181 | 494 gaim_debug(GAIM_DEBUG_MISC, "simple", "header %s", auth); |
| 495 } | |
| 496 | |
| 497 buf = g_strdup_printf("%s %s SIP/2.0\r\n" | |
| 11190 | 498 "Via: SIP/2.0/%s %s:%d;branch=%s\r\n" |
| 11181 | 499 "From: <sip:%s@%s>;tag=%s\r\n" |
| 500 "To: <%s>%s%s\r\n" | |
| 501 "Max-Forwards: 10\r\n" | |
| 502 "CSeq: %d %s\r\n" | |
| 503 "User-Agent: Gaim SIP/SIMPLE Plugin\r\n" | |
| 504 "Call-ID: %s\r\n" | |
| 505 "%s%s" | |
| 506 "Content-Length: %d\r\n\r\n%s", | |
| 507 method, | |
| 508 url, | |
| 11190 | 509 sip->udp ? "UDP" : "TCP", |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
510 sip->ip ? sip->ip : "", |
| 11181 | 511 sip->listenport, |
| 512 branch, | |
| 513 sip->username, | |
| 514 sip->servername, | |
| 515 dialog ? dialog->ourtag : gentag(), | |
| 516 to, | |
| 517 dialog ? ";tag=" : "", | |
| 518 dialog ? dialog->theirtag : "", | |
| 519 ++sip->cseq, | |
| 520 method, | |
| 521 callid, | |
| 522 auth, | |
| 523 addh, | |
| 524 strlen(body), | |
| 525 body); | |
| 526 g_free(branch); | |
| 527 g_free(callid); | |
| 528 | |
| 11341 | 529 /* add to ongoing transactions */ |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
530 |
| 11181 | 531 transactions_add_buf(sip, buf, tc); |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
532 |
| 11181 | 533 sendout_pkt(gc,buf); |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
534 |
| 11181 | 535 g_free(buf); |
| 536 } | |
| 537 | |
| 11194 | 538 static void do_register_exp(struct simple_account_data *sip, int expire) { |
| 11181 | 539 sip->registerstatus = 1; |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
540 |
| 11181 | 541 char *uri = g_strdup_printf("sip:%s",sip->servername); |
| 542 char *to = g_strdup_printf("sip:%s@%s",sip->username,sip->servername); | |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
543 char *contact = g_strdup_printf("Contact: <sip:%s@%s:%d;transport=%s>;methods=\"MESSAGE, SUBSCRIBE, NOTIFY\"\r\nExpires: %d\r\n", sip->username, sip->ip ? sip->ip : "", sip->listenport, sip->udp ? "udp" : "tcp", expire); |
|
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
544 |
| 11194 | 545 if(expire) { |
| 546 sip->reregister = time(NULL) + expire - 50; | |
| 547 } else { | |
| 548 sip->reregister = time(NULL) + 600; | |
| 549 } | |
| 550 send_sip_request(sip->gc,"REGISTER",uri,to, contact, "", NULL, process_register_response); | |
| 11341 | 551 g_free(contact); |
| 11181 | 552 g_free(uri); |
| 553 g_free(to); | |
| 554 } | |
| 555 | |
| 11194 | 556 static void do_register(struct simple_account_data *sip) { |
| 557 do_register_exp(sip, sip->registerexpire); | |
| 558 } | |
| 559 | |
| 11181 | 560 static gchar *parse_from(gchar *hdr) { |
| 561 gchar *from = hdr; | |
| 562 gchar *tmp; | |
| 563 | |
| 564 if(!from) return NULL; | |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
565 gaim_debug_info("simple", "parsing address out of %s\n",from); |
| 11181 | 566 tmp = strchr(from, '<'); |
| 567 | |
| 11341 | 568 /* i hate the different SIP UA behaviours... */ |
| 569 if(tmp) { /* sip address in <...> */ | |
| 11181 | 570 from = tmp+1; |
| 571 tmp = strchr(from,'>'); | |
| 572 if(tmp) { | |
| 573 from = g_strndup(from,tmp-from); | |
| 574 } else { | |
| 575 gaim_debug_info("simple", "found < without > in From\n"); | |
| 576 return NULL; | |
| 577 } | |
| 578 } else { | |
| 579 tmp = strchr(from, ';'); | |
| 580 if(tmp) { | |
| 581 from = g_strndup(from,tmp-from); | |
| 582 } | |
| 583 } | |
| 584 gaim_debug_info("simple", "got %s\n",from); | |
| 585 return from; | |
| 586 } | |
| 587 | |
| 588 static gboolean process_subscribe_response(struct simple_account_data *sip, struct sipmsg *msg, struct transaction *tc) { | |
| 11341 | 589 gchar *to = parse_from(sipmsg_find_header(tc->msg,"To")); /* cant be NULL since it is our own msg */ |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
590 |
| 11181 | 591 if(msg->response==200 || msg->response==202) { |
| 592 return TRUE; | |
| 593 } | |
| 594 | |
| 11341 | 595 /* we can not subscribe -> user is offline (TODO unknown status?) */ |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
596 |
| 11181 | 597 gaim_prpl_got_user_status(sip->account, to, "offline", NULL); |
| 598 g_free(to); | |
| 599 return TRUE; | |
| 600 } | |
| 601 | |
| 602 static void simple_subscribe(struct simple_account_data *sip, struct simple_buddy *buddy) { | |
| 11341 | 603 gchar *contact = "Expires: 300\r\nAccept: application/pidf+xml\r\nEvent: presence\r\n"; |
| 11181 | 604 gchar *to; |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
605 if(strstr(buddy->name,"sip:")) to = g_strdup(buddy->name); |
| 11181 | 606 else to = g_strdup_printf("sip:%s",buddy->name); |
| 11341 | 607 contact = g_strdup_printf("%sContact: <%s@%s>\r\n", contact, sip->username, sip->servername); |
| 608 /* subscribe to buddy presence | |
| 609 * we dont need to know the status so we do not need a callback */ | |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
610 |
| 11181 | 611 send_sip_request(sip->gc, "SUBSCRIBE",to, to, contact, "", NULL, process_subscribe_response); |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
612 |
| 11181 | 613 g_free(to); |
| 11341 | 614 g_free(contact); |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
615 |
| 11341 | 616 /* resubscribe before subscription expires */ |
| 617 /* add some jitter */ | |
| 618 buddy->resubscribe = time(NULL)+250+(rand()%50); | |
| 11181 | 619 } |
| 620 | |
| 621 static void simple_buddy_resub(char *name, struct simple_buddy *buddy, struct simple_account_data *sip) { | |
| 622 time_t curtime = time(NULL); | |
| 11341 | 623 gaim_debug_info("simple","buddy resub\n"); |
| 11181 | 624 if(buddy->resubscribe < curtime) { |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
625 gaim_debug(GAIM_DEBUG_MISC, "simple", "simple_buddy_resub %s\n",name); |
| 11181 | 626 simple_subscribe(sip, buddy); |
| 627 } | |
| 628 } | |
| 629 | |
| 11194 | 630 static gboolean resend_timeout(struct simple_account_data *sip) { |
| 631 GSList *tmp = sip->transactions; | |
| 632 time_t currtime = time(NULL); | |
| 633 while(tmp) { | |
| 634 struct transaction *trans = tmp->data; | |
| 635 tmp = tmp->next; | |
| 636 gaim_debug_info("simple", "have open transaction age: %d\n", currtime- trans->time); | |
| 637 if((currtime - trans->time > 5) && trans->retries >= 1) { | |
| 11341 | 638 /* TODO 408 */ |
| 11194 | 639 } else { |
| 640 if((currtime - trans->time > 2) && trans->retries == 0) { | |
| 641 trans->retries++; | |
| 642 sendout_sipmsg(sip, trans->msg); | |
| 643 } | |
| 644 } | |
| 645 } | |
| 646 return TRUE; | |
| 647 } | |
| 648 | |
| 11181 | 649 static gboolean register_timeout(struct simple_account_data *sip) { |
| 650 GSList *tmp; | |
| 651 time_t curtime = time(NULL); | |
| 11341 | 652 /* register again if first registration expires */ |
| 11181 | 653 if(sip->reregister < curtime) { |
| 11194 | 654 do_register(sip); |
| 11181 | 655 } |
| 11341 | 656 gaim_debug_info("simple","in register timeout\n"); |
| 657 /* check for every subscription if we need to resubscribe */ | |
| 11181 | 658 g_hash_table_foreach(sip->buddies, (GHFunc)simple_buddy_resub, (gpointer)sip); |
| 659 | |
| 11341 | 660 /* remove a timed out suscriber */ |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
661 |
| 11181 | 662 tmp = sip->watcher; |
| 663 while(tmp) { | |
| 664 struct simple_watcher *watcher = tmp->data; | |
| 665 if(watcher->expire < curtime) { | |
| 666 watcher_remove(sip, watcher->name); | |
| 667 tmp = sip->watcher; | |
| 668 } | |
| 669 if(tmp) tmp = tmp->next; | |
| 670 } | |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
671 |
| 11181 | 672 return TRUE; |
| 673 } | |
| 674 | |
| 675 static void simple_send_message(struct simple_account_data *sip, char *to, char *msg, char *type) { | |
| 676 gchar *hdr; | |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
677 if(type) { |
| 11181 | 678 hdr = g_strdup_printf("Content-Type: %s\r\n",type); |
| 679 } else { | |
| 680 hdr = g_strdup("Content-Type: text/plain\r\n"); | |
| 681 } | |
| 682 send_sip_request(sip->gc, "MESSAGE", to, to, hdr, msg, NULL, NULL); | |
| 683 g_free(hdr); | |
| 684 } | |
| 685 | |
| 686 static int simple_im_send(GaimConnection *gc, const char *who, const char *what, GaimConvImFlags flags) { | |
| 687 struct simple_account_data *sip = gc->proto_data; | |
| 688 char *to = g_strdup(who); | |
| 689 char *text = g_strdup(what); | |
| 690 simple_send_message(sip, to, text, NULL); | |
| 691 g_free(to); | |
| 692 g_free(text); | |
| 693 return 1; | |
| 694 } | |
| 695 | |
| 696 static void process_incoming_message(struct simple_account_data *sip, struct sipmsg *msg) { | |
| 697 gchar *from; | |
| 698 gchar *contenttype; | |
| 699 gboolean found = FALSE; | |
| 700 | |
| 701 from = parse_from(sipmsg_find_header(msg, "From")); | |
| 702 | |
| 703 if(!from) return; | |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
704 |
| 11181 | 705 gaim_debug(GAIM_DEBUG_MISC, "simple", "got message from %s: %s\n", from, msg->body); |
| 706 | |
| 707 contenttype = sipmsg_find_header(msg, "Content-Type"); | |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
708 if(!contenttype || !strncmp(contenttype, "text/plain", 10) || !strncmp(contenttype, "text/html", 9)) { |
| 11181 | 709 serv_got_im(sip->gc, from, msg->body, 0, time(NULL)); |
| 710 send_sip_response(sip->gc, msg, 200, "OK", NULL); | |
| 711 found = TRUE; | |
| 712 } | |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
713 if(!strncmp(contenttype, "application/im-iscomposing+xml",30)) { |
|
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
714 xmlnode *isc = xmlnode_from_str(msg->body, msg->bodylen); |
| 11181 | 715 xmlnode *state; |
| 716 gchar *statedata; | |
| 717 | |
| 718 if(!isc) { | |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
719 gaim_debug_info("simple","process_incoming_message: can not parse iscomposing\n"); |
|
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
720 return; |
|
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
721 } |
| 11181 | 722 |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
723 state = xmlnode_get_child(isc, "state"); |
| 11181 | 724 |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
725 if(!state) { |
|
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
726 gaim_debug_info("simple","process_incoming_message: no state found\n"); |
|
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
727 return; |
|
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
728 } |
| 11181 | 729 |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
730 statedata = xmlnode_get_data(state); |
| 11181 | 731 if(statedata) { |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
732 if(strstr(statedata,"active")) serv_got_typing(sip->gc, from, 0, GAIM_TYPING); |
| 11181 | 733 else serv_got_typing_stopped(sip->gc, from); |
| 734 } | |
| 735 xmlnode_free(isc); | |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
736 send_sip_response(sip->gc, msg, 200, "OK", NULL); |
| 11181 | 737 found = TRUE; |
| 738 } | |
| 739 if(!found) { | |
| 740 gaim_debug_info("simple", "got unknown mime-type"); | |
| 741 send_sip_response(sip->gc, msg, 415, "Unsupported media type", NULL); | |
| 742 } | |
| 743 g_free(from); | |
| 744 } | |
| 745 | |
| 746 | |
| 747 gboolean process_register_response(struct simple_account_data *sip, struct sipmsg *msg, struct transaction *tc) { | |
| 748 gchar *tmp; | |
| 749 gaim_debug(GAIM_DEBUG_MISC, "simple", "in process register response response: %d\n", msg->response); | |
| 750 switch (msg->response) { | |
| 751 case 200: | |
| 11341 | 752 if(sip->registerstatus<3) { /* registered */ |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
753 if(gaim_account_get_bool(sip->account, "dopublish", TRUE)) { |
| 11345 | 754 send_publish(sip); |
| 755 } | |
| 11181 | 756 } |
| 757 sip->registerstatus=3; | |
| 758 gaim_connection_set_state(sip->gc, GAIM_CONNECTED); | |
| 11341 | 759 |
| 760 /* get buddies from blist */ | |
| 761 simple_get_buddies(sip->gc); | |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
762 |
| 11181 | 763 register_timeout(sip); |
| 764 break; | |
| 765 case 401: | |
| 766 if(sip->registerstatus!=2) { | |
| 11346 | 767 gaim_debug_info("simple","REGISTER retries %d\n",sip->registrar.retries); |
| 768 if(sip->registrar.retries>3) { | |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
769 gaim_connection_error(sip->gc,"Wrong Password"); |
| 11346 | 770 return TRUE; |
| 771 } | |
| 11181 | 772 tmp = sipmsg_find_header(msg, "WWW-Authenticate"); |
| 773 fill_auth(sip, tmp, &sip->registrar); | |
| 774 sip->registerstatus=2; | |
| 11194 | 775 do_register(sip); |
| 11181 | 776 } |
| 777 break; | |
| 778 } | |
| 779 return TRUE; | |
| 780 } | |
| 781 | |
| 782 static void process_incoming_notify(struct simple_account_data *sip, struct sipmsg *msg) { | |
| 783 gchar *from; | |
| 784 gchar *fromhdr; | |
| 785 gchar *tmp2; | |
| 786 xmlnode *pidf; | |
| 787 xmlnode *basicstatus; | |
| 788 gboolean isonline = FALSE; | |
| 789 | |
| 790 fromhdr = sipmsg_find_header(msg,"From"); | |
| 791 from = parse_from(fromhdr); | |
| 792 if(!from) return; | |
| 793 | |
| 794 pidf = xmlnode_from_str(msg->body, msg->bodylen); | |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
795 |
| 11181 | 796 if(!pidf) { |
| 797 gaim_debug_info("simple","process_incoming_notify: no parseable pidf\n"); | |
| 798 return; | |
| 799 } | |
| 800 | |
| 801 basicstatus = xmlnode_get_child(xmlnode_get_child(xmlnode_get_child(pidf,"tuple"),"status"), "basic"); | |
| 802 | |
| 803 if(!basicstatus) { | |
| 804 gaim_debug_info("simple","process_incoming_notify: no basic found\n"); | |
| 805 return; | |
| 806 } | |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
807 |
| 11181 | 808 tmp2 = xmlnode_get_data(basicstatus); |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
809 |
| 11181 | 810 if(!tmp2) { |
| 811 gaim_debug_info("simple","process_incoming_notify: no basic data found\n"); | |
| 812 return; | |
| 813 } | |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
814 |
| 11181 | 815 if(strstr(tmp2, "open")) { |
| 816 isonline = TRUE; | |
| 817 } | |
| 818 | |
| 819 if(isonline) gaim_prpl_got_user_status(sip->account, from, "available", NULL); | |
| 820 else gaim_prpl_got_user_status(sip->account, from, "offline", NULL); | |
| 821 | |
| 822 xmlnode_free(pidf); | |
| 823 | |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
824 g_free(from); |
| 11181 | 825 send_sip_response(sip->gc, msg, 200, "OK", NULL); |
| 826 } | |
| 827 | |
| 828 static int simple_typing(GaimConnection *gc, const char *name, int typing) { | |
| 829 struct simple_account_data *sip = gc->proto_data; | |
| 830 | |
| 831 gchar *xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" | |
| 832 "<isComposing xmlns=\"urn:ietf:params:xml:ns:im-iscomposing\"\n" | |
| 833 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" | |
| 834 "xsi:schemaLocation=\"urn:ietf:params:xml:ns:im-composing iscomposing.xsd\">\n" | |
| 835 "<state>%s</state>\n" | |
| 836 "<contenttype>text/plain</contenttype>\n" | |
| 837 "<refresh>60</refresh>\n" | |
| 838 "</isComposing>"; | |
| 839 gchar *recv = g_strdup(name); | |
| 840 if(typing == GAIM_TYPING) { | |
| 841 gchar *msg = g_strdup_printf(xml, "active"); | |
| 842 simple_send_message(sip, recv, msg, "application/im-iscomposing+xml"); | |
| 843 g_free(msg); | |
| 844 } else { | |
| 845 gchar *msg = g_strdup_printf(xml, "idle"); | |
| 846 simple_send_message(sip, recv, msg, "application/im-iscomposing+xml"); | |
| 847 g_free(msg); | |
| 848 } | |
| 849 g_free(recv); | |
| 850 return 1; | |
| 851 } | |
| 852 | |
| 853 static gchar *find_tag(gchar *hdr) { | |
| 854 gchar *tmp = strstr(hdr, ";tag="); | |
| 855 gchar *tmp2; | |
| 856 if(!tmp) return NULL; | |
| 857 tmp += 5; | |
| 858 if((tmp2 = strchr(tmp, ';'))) { | |
| 859 tmp2[0] = '\0'; | |
| 860 tmp = g_strdup(tmp); | |
| 861 tmp2[0] = ';'; | |
| 862 return tmp; | |
| 863 } | |
| 864 return g_strdup(tmp); | |
| 865 } | |
| 866 | |
| 867 static gchar* gen_pidf(struct simple_account_data *sip) { | |
| 868 gchar *doc = g_strdup_printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" | |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
869 "<presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n" |
|
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
870 "xmlns:im=\"urn:ietf:params:xml:ns:pidf:im\"\n" |
|
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
871 "entity=\"sip:%s@%s\">\n" |
|
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
872 "<tuple id=\"bs35r9f\">\n" |
| 11181 | 873 "<status>\n" |
| 874 "<basic>open</basic>\n" | |
| 875 "<im:im>%s</im:im>\n" | |
| 876 "</status>\n" | |
| 877 "</tuple>\n" | |
| 878 "</presence>", | |
| 879 sip->username, | |
| 880 sip->servername, | |
| 881 sip->status); | |
| 882 return doc; | |
| 883 } | |
| 884 | |
| 885 static void send_notify(struct simple_account_data *sip, struct simple_watcher *watcher) { | |
| 886 gchar *doc = gen_pidf(sip); | |
| 887 send_sip_request(sip->gc, "NOTIFY", watcher->name, watcher->name, "Event: presence\r\nContent-Type: application/pidf+xml\r\n", doc, &watcher->dialog, NULL); | |
| 888 g_free(doc); | |
| 889 } | |
| 890 | |
| 891 static gboolean process_publish_response(struct simple_account_data *sip, struct sipmsg *msg, struct transaction *tc) { | |
| 11345 | 892 if(msg->response != 200 && msg->response != 408) { |
| 11341 | 893 /* never send again */ |
| 11181 | 894 sip->republish = -1; |
| 895 } | |
| 896 return TRUE; | |
| 897 } | |
| 898 | |
| 899 static void send_publish(struct simple_account_data *sip) { | |
| 900 gchar *uri = g_strdup_printf("sip:%s@%s", sip->username, sip->servername); | |
| 901 gchar *doc = gen_pidf(sip); | |
| 11341 | 902 send_sip_request(sip->gc, "PUBLISH", uri, uri, "Expires: 600\r\nEvent: presence\r\nContent-Type: application/pidf+xml\r\n", doc, NULL, process_publish_response); |
| 11181 | 903 sip->republish = time(NULL) + 500; |
| 904 g_free(doc); | |
| 905 } | |
| 906 | |
| 907 static void process_incoming_subscribe(struct simple_account_data *sip, struct sipmsg *msg) { | |
| 908 gchar *from = parse_from(sipmsg_find_header(msg, "From")); | |
| 909 gchar *theirtag = find_tag(sipmsg_find_header(msg, "From")); | |
| 910 gchar *ourtag = find_tag(sipmsg_find_header(msg, "To")); | |
| 911 gboolean tagadded = FALSE; | |
| 912 gchar *callid = sipmsg_find_header(msg, "Call-ID"); | |
| 913 gchar *expire = sipmsg_find_header(msg, "Expire"); | |
| 914 gchar *tmp; | |
| 915 struct simple_watcher *watcher = watcher_find(sip, from); | |
| 916 if(!ourtag) { | |
| 917 tagadded = TRUE; | |
| 918 ourtag = gentag(); | |
| 919 } | |
| 11341 | 920 if(!watcher) { /* new subscription */ |
| 11345 | 921 if(!gaim_privacy_check(sip->account, from)) { |
| 922 send_sip_response(sip->gc, msg, 202, "Ok", NULL); | |
| 923 goto privend; | |
| 924 } | |
| 11181 | 925 watcher = watcher_create(sip, from, callid, ourtag, theirtag); |
| 926 } | |
| 927 if(tagadded) { | |
| 928 gchar *to = g_strdup_printf("%s;tag=%s", sipmsg_find_header(msg, "To"), ourtag); | |
| 929 sipmsg_remove_header(msg, "To"); | |
| 930 sipmsg_add_header(msg, "To", to); | |
| 931 } | |
| 932 if(expire) | |
| 933 watcher->expire = time(NULL) + strtol(expire, NULL, 10); | |
| 934 else | |
| 935 watcher->expire = time(NULL) + 600; | |
| 936 sipmsg_remove_header(msg, "Contact"); | |
| 937 tmp = g_strdup_printf("<%s@%s>",sip->username, sip->servername); | |
| 938 sipmsg_add_header(msg, "Contact", tmp); | |
| 939 gaim_debug_info("simple","got subscribe: name %s ourtag %s theirtag %s callid %s\n", watcher->name, watcher->dialog.ourtag, watcher->dialog.theirtag, watcher->dialog.callid); | |
| 940 send_sip_response(sip->gc, msg, 200, "Ok", NULL); | |
| 941 g_free(tmp); | |
| 942 send_notify(sip, watcher); | |
| 11345 | 943 privend: |
| 944 g_free(from); | |
| 945 g_free(theirtag); | |
| 946 g_free(ourtag); | |
| 947 g_free(callid); | |
| 948 g_free(expire); | |
| 11181 | 949 } |
| 950 | |
| 11189 | 951 static void process_input_message(struct simple_account_data *sip, struct sipmsg *msg) { |
| 952 int found = 0; | |
| 11341 | 953 if( msg->response == 0 ) { /* request */ |
| 11189 | 954 if(!strcmp(msg->method, "MESSAGE")) { |
| 955 process_incoming_message(sip, msg); | |
| 956 found = 1; | |
| 957 } | |
| 958 if(!strcmp(msg->method, "NOTIFY")) { | |
| 959 process_incoming_notify(sip, msg); | |
| 960 found = 1; | |
| 961 } | |
| 962 if(!strcmp(msg->method, "SUBSCRIBE")) { | |
| 963 process_incoming_subscribe(sip, msg); | |
| 964 found = 1; | |
| 965 } | |
| 11190 | 966 if(!found) { |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
967 send_sip_response(sip->gc, msg, 501, "Not implemented", NULL); |
| 11190 | 968 } |
| 11341 | 969 } else { /* response */ |
| 11189 | 970 struct transaction *trans = transactions_find(sip, msg); |
| 971 if(trans) { | |
| 972 if(msg->response == 407) { | |
| 11346 | 973 if(sip->proxy.retries>3) return; |
| 974 sip->proxy.retries++; | |
| 11341 | 975 /* do proxy authentication */ |
| 11189 | 976 |
| 977 gchar *ptmp = sipmsg_find_header(msg,"Proxy-Authenticate"); | |
| 978 gchar *resend; | |
| 979 gchar *auth; | |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
980 |
| 11189 | 981 fill_auth(sip, ptmp, &sip->proxy); |
| 11346 | 982 auth = auth_header(sip, &sip->proxy, trans->msg->method, trans->msg->target); |
| 11189 | 983 sipmsg_remove_header(msg, "Proxy-Authorization"); |
| 984 sipmsg_add_header(trans->msg, "Proxy-Authorization", auth); | |
| 985 g_free(auth); | |
| 986 resend = sipmsg_to_string(trans->msg); | |
| 11341 | 987 /* resend request */ |
| 11189 | 988 sendout_pkt(sip->gc, resend); |
| 989 g_free(resend); | |
| 990 } else { | |
| 11346 | 991 sip->proxy.retries = 0; |
| 992 if(msg->response == 401) sip->registrar.retries++; | |
| 993 else sip->registrar.retries = 0; | |
| 11189 | 994 if(trans->callback) { |
| 11341 | 995 /* call the callback to process response*/ |
| 11189 | 996 (trans->callback)(sip, msg, trans); |
| 997 } | |
| 11194 | 998 transactions_remove(sip, trans); |
| 11189 | 999 } |
| 1000 found = 1; | |
| 1001 } else { | |
| 1002 gaim_debug(GAIM_DEBUG_MISC, "simple", "received response to unknown transaction"); | |
| 1003 } | |
| 1004 } | |
| 1005 if(!found) { | |
| 1006 gaim_debug(GAIM_DEBUG_MISC, "simple", "received a unknown sip message with method %sand response %d\n",msg->method, msg->response); | |
| 1007 } | |
| 1008 } | |
| 1009 | |
| 11181 | 1010 static void process_input(struct simple_account_data *sip, struct sip_connection *conn) |
| 1011 { | |
| 1012 char *cur; | |
| 1013 char *dummy; | |
| 1014 struct sipmsg *msg; | |
| 1015 int restlen; | |
| 1016 cur = conn->inbuf; | |
| 1017 | |
| 11341 | 1018 /* according to the RFC remove CRLF at the beginning */ |
| 11181 | 1019 while(*cur == '\r' || *cur == '\n') { |
| 1020 cur++; | |
| 1021 } | |
| 1022 if(cur != conn->inbuf) { | |
| 1023 memmove(conn->inbuf, cur, conn->inbufused-(cur-conn->inbuf)); | |
| 1024 conn->inbufused=strlen(conn->inbuf); | |
| 1025 } | |
| 1026 | |
| 11341 | 1027 /* Received a full Header? */ |
| 11181 | 1028 if((cur = strstr(conn->inbuf, "\r\n\r\n"))!=NULL) { |
| 1029 time_t currtime = time(NULL); | |
| 1030 cur += 2; | |
| 1031 cur[0] = '\0'; | |
| 1032 gaim_debug_info("simple","\n\nreceived - %s\n######\n%s\n#######\n\n",ctime(&currtime), conn->inbuf); | |
| 1033 msg = sipmsg_parse_header(conn->inbuf); | |
| 1034 cur[0] = '\r'; | |
| 1035 cur += 2; | |
| 1036 restlen = conn->inbufused - (cur-conn->inbuf); | |
| 1037 if(restlen>=msg->bodylen) { | |
| 1038 dummy = g_malloc(msg->bodylen+1); | |
| 1039 memcpy(dummy, cur, msg->bodylen); | |
| 1040 dummy[msg->bodylen]='\0'; | |
| 1041 msg->body = dummy; | |
| 1042 cur+=msg->bodylen; | |
| 1043 memmove(conn->inbuf, cur, conn->inbuflen); | |
| 1044 conn->inbufused=strlen(conn->inbuf); | |
| 1045 } else { | |
| 1046 sipmsg_free(msg); | |
| 1047 return; | |
| 1048 } | |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
1049 gaim_debug(GAIM_DEBUG_MISC, "simple", "in process response response: %d\n", msg->response); |
| 11189 | 1050 process_input_message(sip,msg); |
| 11181 | 1051 } else { |
| 1052 gaim_debug(GAIM_DEBUG_MISC, "simple", "received a incomplete sip msg: %s\n", conn->inbuf); | |
| 1053 } | |
| 1054 } | |
| 1055 | |
| 11189 | 1056 static void simple_udp_process(gpointer data, gint source, GaimInputCondition con) { |
| 1057 GaimConnection *gc = data; | |
| 1058 struct simple_account_data *sip = gc->proto_data; | |
| 1059 struct sipmsg *msg; | |
| 1060 int len; | |
| 1061 time_t currtime; | |
| 1062 | |
| 1063 static char buffer[65536]; | |
| 1064 len = recv(source, buffer, 65536, 0); | |
| 1065 buffer[len] = 0; | |
| 1066 gaim_debug_info("simple","\n\nreceived - %s\n######\n%s\n#######\n\n",ctime(&currtime), buffer); | |
| 1067 msg = sipmsg_parse_msg(buffer); | |
| 1068 if(msg) process_input_message(sip, msg); | |
| 1069 } | |
| 1070 | |
| 11181 | 1071 static void simple_input_cb(gpointer data, gint source, GaimInputCondition cond) |
| 1072 { | |
| 1073 GaimConnection *gc = data; | |
| 1074 struct simple_account_data *sip = gc->proto_data; | |
| 1075 int len; | |
| 1076 struct sip_connection *conn = connection_find(sip, source); | |
| 1077 if(!conn) { | |
| 1078 gaim_debug_error("simple", "Connection not found!\n"); | |
| 1079 return; | |
| 1080 } | |
| 1081 | |
| 1082 if (conn->inbuflen < conn->inbufused + SIMPLE_BUF_INC) { | |
| 1083 conn->inbuflen += SIMPLE_BUF_INC; | |
| 1084 conn->inbuf = g_realloc(conn->inbuf, conn->inbuflen); | |
| 1085 } | |
| 1086 | |
| 1087 if ((len = read(source, conn->inbuf + conn->inbufused, SIMPLE_BUF_INC - 1)) <= 0) { | |
| 1088 gaim_debug_info("simple","simple_input_cb: read error\n"); | |
| 1089 connection_remove(sip, source); | |
| 1090 if(sip->fd == source) sip->fd = -1; | |
| 1091 return; | |
| 1092 } | |
| 1093 if(len == 0) { | |
| 11341 | 1094 /* connection was closed */ |
| 11181 | 1095 connection_remove(sip, source); |
| 1096 if(sip->fd == source) sip->fd = -1; | |
| 1097 } | |
| 1098 | |
| 1099 conn->inbufused += len; | |
| 1100 conn->inbuf[conn->inbufused]='\0'; | |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
1101 |
| 11181 | 1102 process_input(sip, conn); |
| 1103 } | |
| 1104 | |
| 1105 /* Callback for new connections on incoming TCP port */ | |
| 1106 static void simple_newconn_cb(gpointer data, gint source, GaimInputCondition cond) { | |
| 1107 GaimConnection *gc = data; | |
| 1108 struct simple_account_data *sip = gc->proto_data; | |
| 1109 struct sip_connection *conn; | |
| 1110 | |
| 1111 int newfd = accept(source, NULL, NULL); | |
| 1112 | |
| 1113 conn = connection_create(sip, newfd); | |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
1114 |
| 11181 | 1115 conn->inputhandler = gaim_input_add(newfd, GAIM_INPUT_READ, simple_input_cb, gc); |
| 1116 } | |
| 1117 | |
| 1118 static void login_cb(gpointer data, gint source, GaimInputCondition cond) { | |
| 1119 GaimConnection *gc = data; | |
| 1120 struct simple_account_data *sip = gc->proto_data; | |
| 1121 struct sip_connection *conn; | |
| 1122 | |
| 1123 if( source < 0 ) { | |
| 1124 gaim_connection_error(gc,"Could not connect"); | |
| 1125 return; | |
| 1126 } | |
| 1127 | |
| 1128 sip->fd = source; | |
| 1129 | |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
1130 conn = connection_create(sip, source); |
| 11181 | 1131 |
| 11341 | 1132 /* get the local ip */ |
| 11181 | 1133 sip->ip = g_strdup(gaim_network_get_my_ip(source)); |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
1134 |
| 11194 | 1135 do_register(sip); |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
1136 |
| 11181 | 1137 conn->inputhandler = gaim_input_add(sip->fd, GAIM_INPUT_READ, simple_input_cb, gc); |
| 1138 } | |
| 1139 | |
| 1140 static guint simple_ht_hash_nick(const char *nick) { | |
| 1141 char *lc = g_utf8_strdown(nick, -1); | |
| 1142 guint bucket = g_str_hash(lc); | |
| 1143 g_free(lc); | |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
1144 |
| 11181 | 1145 return bucket; |
| 1146 } | |
| 1147 | |
| 1148 static gboolean simple_ht_equals_nick(const char *nick1, const char *nick2) { | |
| 1149 return (gaim_utf8_strcasecmp(nick1, nick2) == 0); | |
| 1150 } | |
| 1151 | |
| 11383 | 1152 static void srvresolved(struct srv_response *resp, int results, gpointer data) { |
| 1153 struct simple_account_data *sip = (struct simple_account_data*) data; | |
| 1154 | |
| 1155 gchar *hostname; | |
| 1156 int port = 5060; | |
| 1157 | |
| 1158 int error = 0; | |
| 1159 struct sockaddr_in addr; | |
| 1160 struct hostent *h; | |
| 1161 | |
| 1162 /* find the host to connect to */ | |
| 1163 if(results) { | |
| 1164 hostname = g_strdup(resp->hostname); | |
| 1165 port = resp->port; | |
| 1166 g_free(resp); | |
| 1167 } else { | |
| 1168 if(!gaim_account_get_bool(sip->account, "useproxy", FALSE)) { | |
| 1169 hostname = g_strdup(sip->servername); | |
| 1170 } else { | |
| 1171 hostname = g_strdup(gaim_account_get_string(sip->account, "proxy", sip->servername)); | |
| 1172 } | |
| 1173 } | |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
1174 |
| 11383 | 1175 sip->realhostname = hostname; |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
1176 sip->realport = port; |
| 11383 | 1177 /* TCP case */ |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
1178 if(! sip->udp) { |
| 11409 | 1179 /* create socket for incoming connections */ |
| 1180 sip->listenfd = gaim_network_listen_range(5060, 5160); | |
| 1181 if(sip->listenfd == -1) { | |
| 1182 gaim_connection_error(sip->gc, _("Could not create listen socket")); | |
| 1183 return; | |
| 1184 } | |
| 1185 gaim_debug_info("simple", "listenfd: %d\n", sip->listenfd); | |
| 1186 sip->listenport = gaim_network_get_port_from_fd(sip->listenfd); | |
| 1187 sip->listenpa = gaim_input_add(sip->listenfd, GAIM_INPUT_READ, simple_newconn_cb, sip->gc); | |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
1188 gaim_debug_info("simple","connecting to %s port %d\n", hostname, port); |
| 11383 | 1189 /* open tcp connection to the server */ |
| 1190 error = gaim_proxy_connect(sip->account, hostname, port, login_cb, sip->gc); | |
| 1191 if(error) { | |
| 1192 gaim_connection_error(sip->gc, _("Couldn't create socket")); | |
| 1193 } | |
| 1194 | |
| 1195 } else { /* UDP */ | |
| 1196 gaim_debug_info("simple", "using udp with server %s and port %d\n", hostname, port); | |
| 1197 sip->fd = socket(AF_INET, SOCK_DGRAM, 0); | |
| 1198 | |
| 1199 addr.sin_family = AF_INET; | |
| 1200 addr.sin_port = htons(5060); | |
| 1201 addr.sin_addr.s_addr = INADDR_ANY; | |
| 1202 while((bind(sip->fd, (struct sockaddr*)&addr, sizeof(struct sockaddr_in)) <0) && ntohs(addr.sin_port)<5160) { | |
| 1203 addr.sin_port = htons(ntohs(addr.sin_port)+1); | |
| 1204 } | |
| 1205 sip->listenport = ntohs(addr.sin_port); | |
| 1206 sip->listenfd = sip->fd; | |
| 1207 | |
| 1208 gaim_input_add(sip->fd, GAIM_INPUT_READ, simple_udp_process, sip->gc); | |
| 1209 sip->serveraddr.sin_family = AF_INET; | |
| 1210 sip->serveraddr.sin_port = htons(port); | |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
1211 |
| 11383 | 1212 h = gethostbyname(hostname); |
| 1213 sip->serveraddr.sin_addr.s_addr = ((struct in_addr*)h->h_addr)->s_addr; | |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
1214 sip->ip = g_strdup(gaim_network_get_my_ip(sip->listenfd)); |
| 11383 | 1215 sip->resendtimeout = gaim_timeout_add(2500, (GSourceFunc)resend_timeout, sip); |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
1216 do_register(sip); |
| 11383 | 1217 } |
| 1218 } | |
| 1219 | |
| 11181 | 1220 static void simple_login(GaimAccount *account, GaimStatus *status) |
| 1221 { | |
| 1222 GaimConnection *gc; | |
| 1223 struct simple_account_data *sip; | |
| 1224 gchar **userserver; | |
| 11210 | 1225 gchar *hosttoconnect; |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
1226 |
| 11181 | 1227 const char *username = gaim_account_get_username(account); |
| 1228 | |
| 1229 gc = gaim_account_get_connection(account); | |
| 1230 gc->proto_data = sip = g_new0(struct simple_account_data,1); | |
| 1231 sip->gc=gc; | |
| 11189 | 1232 sip->account = account; |
| 11194 | 1233 sip->registerexpire = 900; |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
1234 sip->udp = gaim_account_get_bool(account, "udp", FALSE); |
|
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
1235 if (strpbrk(username, " \t\v\r\n") != NULL) { |
|
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
1236 gaim_connection_error(gc, _("SIP usernames may not contain whitespaces or @ symbols")); |
|
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
1237 return; |
|
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
1238 } |
| 11181 | 1239 |
| 1240 userserver = g_strsplit(username, "@", 2); | |
| 1241 gaim_connection_set_display_name(gc,userserver[0]); | |
| 1242 sip->username = g_strdup(userserver[0]); | |
| 1243 sip->servername = g_strdup(userserver[1]); | |
| 1244 sip->password = g_strdup(gaim_connection_get_password(gc)); | |
| 1245 g_strfreev(userserver); | |
| 1246 | |
| 1247 sip->buddies = g_hash_table_new((GHashFunc)simple_ht_hash_nick, (GEqualFunc)simple_ht_equals_nick); | |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
1248 |
|
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
1249 gaim_connection_update_progress(gc, _("Connecting"), 1, 2); |
| 11181 | 1250 |
| 1251 sip->status = g_strdup("available"); | |
| 11189 | 1252 |
| 11210 | 1253 if(!gaim_account_get_bool(account, "useproxy", FALSE)) { |
| 1254 hosttoconnect = g_strdup(sip->servername); | |
| 1255 } else { | |
| 1256 hosttoconnect = g_strdup(gaim_account_get_string(account, "proxy", sip->servername)); | |
| 1257 } | |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
1258 |
| 11341 | 1259 /* TCP case */ |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
1260 if(! sip->udp) { |
| 11383 | 1261 gaim_srv_resolve("sip","tcp",hosttoconnect,srvresolved, sip); |
| 11341 | 1262 } else { /* UDP */ |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
1263 gaim_srv_resolve("sip","udp",hosttoconnect,srvresolved, sip); |
| 11181 | 1264 } |
| 11210 | 1265 g_free(hosttoconnect); |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
1266 |
| 11341 | 1267 /* register timeout callback for register / subscribe renewal */ |
| 11194 | 1268 sip->registertimeout = gaim_timeout_add((rand()%100)+10*1000, (GSourceFunc)register_timeout, sip); |
| 11181 | 1269 } |
| 1270 | |
| 1271 static void simple_close(GaimConnection *gc) | |
| 1272 { | |
| 1273 struct simple_account_data *sip = gc->proto_data; | |
| 11194 | 1274 |
| 11341 | 1275 /* unregister */ |
| 11194 | 1276 do_register_exp(sip, 0); |
| 11346 | 1277 connection_free_all(sip); |
| 11341 | 1278 if(sip) { |
| 11181 | 1279 if(sip->servername) g_free(sip->servername); |
| 1280 if(sip->username) g_free(sip->username); | |
| 1281 if(sip->password) g_free(sip->password); | |
| 1282 if(sip->registrar.nonce) g_free(sip->registrar.nonce); | |
| 11341 | 1283 if(sip->registrar.realm) g_free(sip->registrar.realm); |
| 11181 | 1284 if(sip->proxy.nonce) g_free(sip->proxy.nonce); |
| 1285 if(sip->proxy.realm) g_free(sip->proxy.realm); | |
| 1286 if(sip->sendlater) g_free(sip->sendlater); | |
| 1287 if(sip->ip) g_free(sip->ip); | |
| 11383 | 1288 if(sip->realhostname) g_free(sip->realhostname); |
| 11409 | 1289 if(sip->listenpa) gaim_input_remove(sip->listenpa); |
| 11346 | 1290 if(sip->registertimeout) gaim_timeout_remove(sip->registertimeout); |
| 11383 | 1291 sip->servername = sip->username = sip->password = sip->registrar.nonce = sip->registrar.realm = sip->proxy.nonce = sip->proxy.realm = sip->sendlater = sip->ip = sip->realhostname = NULL; |
| 11181 | 1292 } |
| 11341 | 1293 if(gc->proto_data) g_free(gc->proto_data); |
| 1294 gc->proto_data = 0; | |
| 11181 | 1295 } |
| 1296 | |
| 11345 | 1297 /* not needed since privacy is checked for every subscribe */ |
| 1298 static void dummy_add_deny(GaimConnection *gc, const char *name) { | |
| 1299 } | |
| 1300 | |
| 1301 static void dummy_permit_deny(GaimConnection *gc) { | |
| 1302 } | |
| 1303 | |
| 11181 | 1304 static GaimPluginProtocolInfo prpl_info = |
| 1305 { | |
| 1306 0, | |
| 1307 NULL, /* user_splits */ | |
| 1308 NULL, /* protocol_options */ | |
| 1309 NO_BUDDY_ICONS, /* icon_spec */ | |
| 1310 simple_list_icon, /* list_icon */ | |
| 1311 NULL, /* list_emblems */ | |
| 1312 NULL, /* status_text */ | |
| 1313 NULL, /* tooltip_text */ | |
| 1314 simple_status_types, /* away_states */ | |
| 1315 NULL, /* blist_node_menu */ | |
| 1316 NULL, /* chat_info */ | |
| 1317 NULL, /* chat_info_defaults */ | |
| 1318 simple_login, /* login */ | |
| 1319 simple_close, /* close */ | |
| 1320 simple_im_send, /* send_im */ | |
| 1321 NULL, /* set_info */ | |
| 1322 simple_typing, /* send_typing */ | |
| 1323 NULL, /* get_info */ | |
| 1324 simple_set_status, /* set_status */ | |
| 1325 NULL, /* set_idle */ | |
| 1326 NULL, /* change_passwd */ | |
| 1327 simple_add_buddy, /* add_buddy */ | |
| 1328 NULL, /* add_buddies */ | |
| 1329 simple_remove_buddy, /* remove_buddy */ | |
| 1330 NULL, /* remove_buddies */ | |
| 11345 | 1331 dummy_add_deny, /* add_permit */ |
| 1332 dummy_add_deny, /* add_deny */ | |
| 1333 dummy_add_deny, /* rem_permit */ | |
| 1334 dummy_add_deny, /* rem_deny */ | |
| 1335 dummy_permit_deny, /* set_permit_deny */ | |
| 11181 | 1336 NULL, /* join_chat */ |
| 1337 NULL, /* reject_chat */ | |
| 1338 NULL, /* get_chat_name */ | |
| 1339 NULL, /* chat_invite */ | |
| 1340 NULL, /* chat_leave */ | |
| 1341 NULL, /* chat_whisper */ | |
| 1342 NULL, /* chat_send */ | |
| 1343 simple_keep_alive, /* keepalive */ | |
| 1344 NULL, /* register_user */ | |
| 1345 NULL, /* get_cb_info */ | |
| 1346 NULL, /* get_cb_away */ | |
| 1347 NULL, /* alias_buddy */ | |
| 1348 NULL, /* group_buddy */ | |
| 1349 NULL, /* rename_group */ | |
| 1350 NULL, /* buddy_free */ | |
| 1351 NULL, /* convo_closed */ | |
| 1352 NULL, /* normalize */ | |
| 1353 NULL, /* set_buddy_icon */ | |
| 1354 NULL, /* remove_group */ | |
| 1355 NULL, /* get_cb_real_name */ | |
| 1356 NULL, /* set_chat_topic */ | |
| 1357 NULL, /* find_blist_chat */ | |
| 1358 NULL, /* roomlist_get_list */ | |
| 1359 NULL, /* roomlist_cancel */ | |
| 1360 NULL, /* roomlist_expand_category */ | |
| 1361 NULL, /* can_receive_file */ | |
| 1362 NULL /* send_file */ | |
| 1363 }; | |
| 1364 | |
| 1365 | |
| 1366 static GaimPluginInfo info = | |
| 1367 { | |
| 1368 GAIM_PLUGIN_MAGIC, | |
| 1369 GAIM_MAJOR_VERSION, | |
| 1370 GAIM_MINOR_VERSION, | |
| 1371 GAIM_PLUGIN_PROTOCOL, /**< type */ | |
| 1372 NULL, /**< ui_requirement */ | |
| 1373 0, /**< flags */ | |
| 1374 NULL, /**< dependencies */ | |
| 1375 GAIM_PRIORITY_DEFAULT, /**< priority */ | |
| 1376 | |
| 1377 "prpl-simple", /**< id */ | |
| 1378 "SIMPLE", /**< name */ | |
| 1379 VERSION, /**< version */ | |
| 1380 N_("SIP/SIMPLE Protocol Plugin"), /** summary */ | |
| 1381 N_("The SIP/SIMPLE Protocol Plugin"), /** description */ | |
| 1382 N_("Thomas Butter <butter@uni-mannheim.de>"), /**< author */ | |
| 1383 GAIM_WEBSITE, /**< homepage */ | |
| 1384 | |
| 1385 NULL, /**< load */ | |
| 1386 NULL, /**< unload */ | |
| 1387 NULL, /**< destroy */ | |
| 1388 | |
| 1389 NULL, /**< ui_info */ | |
| 1390 &prpl_info, /**< extra_info */ | |
| 1391 NULL, | |
| 1392 NULL | |
| 1393 }; | |
| 1394 | |
| 1395 static void _init_plugin(GaimPlugin *plugin) | |
| 1396 { | |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
1397 GaimAccountUserSplit *split; |
| 11189 | 1398 GaimAccountOption *option; |
| 11181 | 1399 |
|
11396
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
1400 split = gaim_account_user_split_new(_("Server"), "", '@'); |
|
be776f9b1818
[gaim-migrate @ 13627]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11383
diff
changeset
|
1401 prpl_info.user_splits = g_list_append(prpl_info.user_splits, split); |
| 11181 | 1402 |
| 11345 | 1403 option = gaim_account_option_bool_new(_("Publish Status (note: everyone may watch you)"), "dopublish", TRUE); |
| 1404 prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, option); | |
| 1405 | |
| 11189 | 1406 option = gaim_account_option_bool_new(_("Use UDP"), "udp", FALSE); |
| 1407 prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, option); | |
| 11210 | 1408 option = gaim_account_option_bool_new(_("Use Proxy"), "useproxy", FALSE); |
| 1409 prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, option); | |
| 1410 option = gaim_account_option_string_new(_("Proxy"), "proxy", ""); | |
| 1411 prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, option); | |
| 11181 | 1412 } |
| 1413 | |
| 1414 GAIM_INIT_PLUGIN(simple, _init_plugin, info); |
