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