Fix for "message was not delivered" bug #2832551
[siplcs.git] / src / sipe-dialog.c
blobd3f670db4802ec1e8e3af566e4643312ff4c81de
1 /**
2 * @file sipe-dialog.c
4 * pidgin-sipe
6 * Copyright (C) 2009 SIPE Project <http://sipe.sourceforge.net/>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <string.h>
24 #include <glib.h>
26 #include "debug.h"
28 #include "sipe.h"
29 #include "sipe-dialog.h"
30 #include "sipe-session.h"
31 #include "sipmsg.h"
33 void sipe_dialog_free(struct sip_dialog *dialog)
35 GSList *entry;
37 if (!dialog) return;
39 g_free(dialog->with);
40 g_free(dialog->endpoint_GUID);
41 entry = dialog->routes;
42 while (entry) {
43 g_free(entry->data);
44 entry = g_slist_remove(entry, entry->data);
46 entry = dialog->supported;
47 while (entry) {
48 g_free(entry->data);
49 entry = g_slist_remove(entry, entry->data);
52 g_free(dialog->callid);
53 g_free(dialog->ourtag);
54 g_free(dialog->theirtag);
55 g_free(dialog->theirepid);
56 g_free(dialog->request);
58 g_free(dialog);
61 struct sip_dialog *sipe_dialog_add(struct sip_session *session)
63 struct sip_dialog *dialog = g_new0(struct sip_dialog, 1);
64 session->dialogs = g_slist_append(session->dialogs, dialog);
65 return(dialog);
68 struct sip_dialog *sipe_dialog_find(struct sip_session *session,
69 const gchar *who)
71 if (session && who) {
72 SIPE_DIALOG_FOREACH {
73 if (dialog->with && !g_ascii_strcasecmp(who, dialog->with)) {
74 return dialog;
76 } SIPE_DIALOG_FOREACH_END;
78 return NULL;
81 void sipe_dialog_remove(struct sip_session *session, const gchar *who)
83 struct sip_dialog *dialog = sipe_dialog_find(session, who);
84 if (dialog) {
85 session->dialogs = g_slist_remove(session->dialogs, dialog);
86 sipe_dialog_free(dialog);
90 void sipe_dialog_remove_all(struct sip_session *session)
92 GSList *entry = session->dialogs;
93 while (entry) {
94 sipe_dialog_free(entry->data);
95 entry = g_slist_remove(entry, entry->data);
99 static void sipe_get_route_header(const struct sipmsg *msg,
100 struct sip_dialog *dialog,
101 gboolean outgoing)
103 GSList *hdr = msg->headers;
104 gchar *contact;
106 while (hdr) {
107 struct siphdrelement *elem = hdr->data;
108 if(!g_ascii_strcasecmp(elem->name, "Record-Route")) {
109 gchar **parts = g_strsplit(elem->value, ",", 0);
110 gchar **part = parts;
112 while (*part) {
113 gchar *route = sipmsg_find_part_of_header(*part, "<", ">", NULL);
114 purple_debug_info("sipe", "sipe_get_route_header: route %s \n", route);
115 dialog->routes = g_slist_append(dialog->routes, route);
116 part++;
119 g_strfreev(parts);
121 hdr = g_slist_next(hdr);
124 if (outgoing) {
125 dialog->routes = g_slist_reverse(dialog->routes);
128 if (dialog->routes) {
129 dialog->request = dialog->routes->data;
130 dialog->routes = g_slist_remove(dialog->routes, dialog->routes->data);
133 contact = sipmsg_find_part_of_header(sipmsg_find_header(msg, "Contact"), "<", ">", NULL);
134 if (contact) {
135 dialog->routes = g_slist_append(dialog->routes, contact);
139 static void
140 sipe_get_supported_header(const struct sipmsg *msg,
141 struct sip_dialog *dialog,
142 SIPE_UNUSED_PARAMETER gboolean outgoing)
144 GSList *hdr = msg->headers;
145 struct siphdrelement *elem;
146 while(hdr)
148 elem = hdr->data;
149 if(!g_ascii_strcasecmp(elem->name, "Supported")
150 && !g_slist_find_custom(dialog->supported, elem->value, (GCompareFunc)g_ascii_strcasecmp))
152 dialog->supported = g_slist_append(dialog->supported, g_strdup(elem->value));
155 hdr = g_slist_next(hdr);
159 static gchar *find_tag(const gchar *hdr)
161 gchar * tag = sipmsg_find_part_of_header (hdr, "tag=", ";", NULL);
162 if (!tag) {
163 // In case it's at the end and there's no trailing ;
164 tag = sipmsg_find_part_of_header (hdr, "tag=", NULL, NULL);
166 return tag;
169 void sipe_dialog_parse(struct sip_dialog *dialog,
170 const struct sipmsg *msg,
171 gboolean outgoing)
173 gchar *us = outgoing ? "From" : "To";
174 gchar *them = outgoing ? "To" : "From";
176 g_free(dialog->ourtag);
177 g_free(dialog->theirtag);
179 dialog->ourtag = find_tag(sipmsg_find_header(msg, us));
180 dialog->theirtag = find_tag(sipmsg_find_header(msg, them));
181 if (!dialog->theirepid) {
182 dialog->theirepid = sipmsg_find_part_of_header(sipmsg_find_header(msg, them), "epid=", ";", NULL);
183 if (!dialog->theirepid) {
184 dialog->theirepid = sipmsg_find_part_of_header(sipmsg_find_header(msg, them), "epid=", NULL, NULL);
188 // Catch a tag on the end of the To Header and get rid of it.
189 if (dialog->theirepid && strstr(dialog->theirepid, "tag=")) {
190 dialog->theirepid = strtok(dialog->theirepid, ";");
193 sipe_get_route_header(msg, dialog, outgoing);
194 sipe_get_supported_header(msg, dialog, outgoing);
198 Local Variables:
199 mode: c
200 c-file-style: "bsd"
201 indent-tabs-mode: t
202 tab-width: 8
203 End: