filetransfer: use 'deallocate' callback in sipe_file_transfer
[siplcs.git] / src / core / sipe-dialog.c
blob276c9efa4540a35774ba459d1385316c4b6e361a
1 /**
2 * @file sipe-dialog.c
4 * pidgin-sipe
6 * Copyright (C) 2009-2015 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 <stdlib.h>
24 #include <string.h>
25 #include <time.h>
27 #include <glib.h>
29 #include "sipe-core.h"
30 #include "sipe-common.h"
31 #include "sipe-ft.h"
32 #include "sipmsg.h"
33 #include "sipe-backend.h"
34 #include "sipe-dialog.h"
35 #include "sipe-session.h"
36 #include "sipe-utils.h"
38 void sipe_dialog_free(struct sip_dialog *dialog)
40 GSList *entry;
41 void *data;
43 if (!dialog) return;
45 g_free(dialog->with);
46 g_free(dialog->endpoint_GUID);
47 entry = dialog->routes;
48 while (entry) {
49 data = entry->data;
50 entry = g_slist_remove(entry, data);
51 g_free(data);
53 entry = dialog->supported;
54 while (entry) {
55 data = entry->data;
56 entry = g_slist_remove(entry, data);
57 g_free(data);
60 while (dialog->filetransfers) {
61 struct sipe_file_transfer *ft = dialog->filetransfers->data;
62 sipe_ft_free(ft);
65 g_free(dialog->callid);
66 g_free(dialog->ourtag);
67 g_free(dialog->theirtag);
68 g_free(dialog->theirepid);
69 g_free(dialog->request);
71 g_free(dialog);
74 struct sip_dialog *sipe_dialog_add(struct sip_session *session)
76 struct sip_dialog *dialog = g_new0(struct sip_dialog, 1);
77 session->dialogs = g_slist_append(session->dialogs, dialog);
78 return(dialog);
81 static struct sip_dialog *
82 sipe_dialog_find_3(struct sip_session *session,
83 struct sip_dialog *dialog_in)
85 if (session && dialog_in) {
86 SIPE_DIALOG_FOREACH {
87 if ( dialog_in->callid &&
88 dialog_in->ourtag &&
89 dialog_in->theirtag &&
91 dialog->callid &&
92 dialog->ourtag &&
93 dialog->theirtag &&
95 sipe_strcase_equal(dialog_in->callid, dialog->callid) &&
96 sipe_strcase_equal(dialog_in->ourtag, dialog->ourtag) &&
97 sipe_strcase_equal(dialog_in->theirtag, dialog->theirtag))
99 SIPE_DEBUG_INFO("sipe_dialog_find_3 who='%s'",
100 dialog->with ? dialog->with : "");
101 return dialog;
103 } SIPE_DIALOG_FOREACH_END;
105 return NULL;
108 struct sip_dialog *sipe_dialog_find(struct sip_session *session,
109 const gchar *who)
111 if (session && who) {
112 SIPE_DIALOG_FOREACH {
113 if (dialog->with && sipe_strcase_equal(who, dialog->with)) {
114 SIPE_DEBUG_INFO("sipe_dialog_find who='%s'", who);
115 return dialog;
117 } SIPE_DIALOG_FOREACH_END;
119 return NULL;
122 void sipe_dialog_remove(struct sip_session *session, const gchar *who)
124 struct sip_dialog *dialog = sipe_dialog_find(session, who);
125 if (dialog) {
126 SIPE_DEBUG_INFO("sipe_dialog_remove who='%s' with='%s'", who, dialog->with ? dialog->with : "");
127 session->dialogs = g_slist_remove(session->dialogs, dialog);
128 sipe_dialog_free(dialog);
132 void
133 sipe_dialog_remove_3(struct sip_session *session,
134 struct sip_dialog *dialog_in)
136 struct sip_dialog *dialog = sipe_dialog_find_3(session, dialog_in);
137 if (dialog) {
138 SIPE_DEBUG_INFO("sipe_dialog_remove_3 with='%s'",
139 dialog->with ? dialog->with : "");
140 session->dialogs = g_slist_remove(session->dialogs, dialog);
141 sipe_dialog_free(dialog);
145 void sipe_dialog_remove_all(struct sip_session *session)
147 GSList *entry = session->dialogs;
148 while (entry) {
149 struct sip_dialog *dialog = entry->data;
150 entry = g_slist_remove(entry, dialog);
151 sipe_dialog_free(dialog);
155 static void sipe_dialog_parse_routes(struct sip_dialog *dialog,
156 const struct sipmsg *msg,
157 gboolean outgoing)
159 GSList *hdr = msg->headers;
160 gchar *contact = sipmsg_find_part_of_header(sipmsg_find_header(msg, "Contact"), "<", ">", NULL);
162 /* Remove old routes */
163 while (dialog->routes) {
164 void *data = dialog->routes->data;
165 dialog->routes = g_slist_remove(dialog->routes, data);
166 g_free(data);
168 g_free(dialog->request);
169 dialog->request = NULL;
171 while (hdr) {
172 struct sipnameval *elem = hdr->data;
173 if (sipe_strcase_equal(elem->name, "Record-Route")) {
174 gchar **parts = g_strsplit(elem->value, ",", 0);
175 gchar **part = parts;
177 while (*part) {
178 SIPE_DEBUG_INFO("sipe_dialog_parse_routes: route %s", *part);
179 dialog->routes = g_slist_append(dialog->routes,
180 g_strdup(*part));
181 part++;
183 g_strfreev(parts);
185 hdr = g_slist_next(hdr);
187 if (outgoing) {
188 dialog->routes = g_slist_reverse(dialog->routes);
191 if (contact) {
192 dialog->request = contact;
195 /* logic for strict router only - RFC3261 - 12.2.1.1 */
196 /* @TODO: proper check for presence of 'lr' PARAMETER in URI */
197 if (dialog->routes && !strstr(dialog->routes->data, ";lr")) {
198 gchar *route = dialog->routes->data;
199 dialog->request = sipmsg_find_part_of_header(route, "<", ">", NULL);
200 SIPE_DEBUG_INFO("sipe_dialog_parse_routes: strict route, contact %s", dialog->request);
201 dialog->routes = g_slist_remove(dialog->routes, route);
202 g_free(route);
203 if (contact) {
204 dialog->routes = g_slist_append(dialog->routes,
205 g_strdup_printf("<%s>", contact));
206 g_free(contact);
211 static void
212 sipe_get_supported_header(const struct sipmsg *msg,
213 struct sip_dialog *dialog,
214 SIPE_UNUSED_PARAMETER gboolean outgoing)
216 GSList *hdr = msg->headers;
217 struct sipnameval *elem;
218 while(hdr)
220 elem = hdr->data;
221 if (sipe_strcase_equal(elem->name, "Supported")
222 && !g_slist_find_custom(dialog->supported, elem->value, (GCompareFunc)g_ascii_strcasecmp))
224 dialog->supported = g_slist_append(dialog->supported, g_strdup(elem->value));
227 hdr = g_slist_next(hdr);
231 static gchar *find_tag(const gchar *hdr)
233 gchar * tag = sipmsg_find_part_of_header (hdr, "tag=", ";", NULL);
234 if (!tag) {
235 // In case it's at the end and there's no trailing ;
236 tag = sipmsg_find_part_of_header (hdr, "tag=", NULL, NULL);
238 return tag;
241 void sipe_dialog_parse(struct sip_dialog *dialog,
242 const struct sipmsg *msg,
243 gboolean outgoing)
245 gchar *us = outgoing ? "From" : "To";
246 gchar *them = outgoing ? "To" : "From";
247 const gchar *session_expires_header;
249 g_free(dialog->ourtag);
250 g_free(dialog->theirtag);
252 dialog->ourtag = find_tag(sipmsg_find_header(msg, us));
253 dialog->theirtag = find_tag(sipmsg_find_header(msg, them));
254 if (!dialog->theirepid) {
255 dialog->theirepid = sipmsg_find_part_of_header(sipmsg_find_header(msg, them), "epid=", ";", NULL);
256 if (!dialog->theirepid) {
257 dialog->theirepid = sipmsg_find_part_of_header(sipmsg_find_header(msg, them), "epid=", NULL, NULL);
261 // Catch a tag on the end of the To Header and get rid of it.
262 if (dialog->theirepid && strstr(dialog->theirepid, "tag=")) {
263 dialog->theirepid = strtok(dialog->theirepid, ";");
266 if ((session_expires_header = sipmsg_find_header(msg, "Session-Expires"))) {
267 dialog->expires = atoi(session_expires_header);
270 sipe_dialog_parse_routes(dialog, msg, outgoing);
271 sipe_get_supported_header(msg, dialog, outgoing);
275 Local Variables:
276 mode: c
277 c-file-style: "bsd"
278 indent-tabs-mode: t
279 tab-width: 8
280 End: