core cleanup: 6 more modules are purple free
[siplcs.git] / src / core / sipe-session.c
blob2115f804b7b21aebbd79281c51eefe6951490997
1 /**
2 * @file sipe-session.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 <stdlib.h>
24 #include <string.h>
25 #include <time.h>
27 #include <glib.h>
29 #include "sip-sec.h"
30 #include "sipe-backend-debug.h"
31 #include "sipe-dialog.h"
32 #include "sipe-session.h"
33 #include "sipe-utils.h"
34 #include "sipe.h"
36 void
37 sipe_free_queued_message(struct queued_message *message)
39 g_free(message->body);
40 g_free(message->content_type);
41 g_free(message);
44 struct sip_session *
45 sipe_session_add_chat(struct sipe_account_data *sip)
47 struct sip_session *session = g_new0(struct sip_session, 1);
48 session->callid = gencallid();
49 session->is_multiparty = TRUE;
50 session->chat_id = rand();
51 session->unconfirmed_messages = g_hash_table_new_full(
52 g_str_hash, g_str_equal, g_free, (GDestroyNotify)sipe_free_queued_message);
53 session->conf_unconfirmed_messages = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
54 sip->sessions = g_slist_append(sip->sessions, session);
55 return session;
58 struct sip_session *
59 sipe_session_find_or_add_chat_by_callid(struct sipe_account_data *sip,
60 const gchar *callid)
62 struct sip_session *session = sipe_session_find_chat_by_callid(sip, callid);
63 if (!session) {
64 SIPE_DEBUG_INFO("sipe_session_find_or_add_chat_by_callid: new session for %s", callid);
65 session = sipe_session_add_chat(sip);
66 session->callid = g_strdup(callid);
68 return session;
71 struct sip_session *
72 sipe_session_find_chat_by_callid(struct sipe_account_data *sip,
73 const gchar *callid)
75 if (sip == NULL || callid == NULL) {
76 return NULL;
79 SIPE_SESSION_FOREACH {
80 if (session->callid &&
81 sipe_strcase_equal(callid, session->callid)) {
82 return session;
84 } SIPE_SESSION_FOREACH_END;
85 return NULL;
88 struct sip_session *
89 sipe_session_find_chat_by_id(struct sipe_account_data *sip,
90 int id)
92 if (sip == NULL) {
93 return NULL;
96 SIPE_SESSION_FOREACH {
97 if (id == session->chat_id) {
98 return session;
100 } SIPE_SESSION_FOREACH_END;
101 return NULL;
104 struct sip_session *
105 sipe_session_find_chat_by_title(struct sipe_account_data *sip,
106 const gchar *name)
108 if (sip == NULL || name == NULL) {
109 return NULL;
112 SIPE_SESSION_FOREACH {
113 if (session->chat_title &&
114 !g_strcasecmp(name, session->chat_title)) {
115 return session;
117 } SIPE_SESSION_FOREACH_END;
118 return NULL;
121 struct sip_session *
122 sipe_session_find_conference(struct sipe_account_data *sip,
123 const gchar *focus_uri)
125 if (sip == NULL || focus_uri == NULL) {
126 return NULL;
129 SIPE_SESSION_FOREACH {
130 if (session->focus_uri &&
131 sipe_strcase_equal(focus_uri, session->focus_uri)) {
132 return session;
134 } SIPE_SESSION_FOREACH_END;
135 return NULL;
138 struct sip_session *
139 sipe_session_find_im(struct sipe_account_data *sip, const gchar *who)
141 if (sip == NULL || who == NULL) {
142 return NULL;
145 SIPE_SESSION_FOREACH {
146 if (session->with && sipe_strcase_equal(who, session->with)) {
147 return session;
149 } SIPE_SESSION_FOREACH_END;
150 return NULL;
153 struct sip_session *
154 sipe_session_find_or_add_im(struct sipe_account_data *sip,
155 const gchar *who)
157 struct sip_session *session = sipe_session_find_im(sip, who);
158 if (!session) {
159 SIPE_DEBUG_INFO("sipe_session_find_or_add_im: new session for %s", who);
160 session = g_new0(struct sip_session, 1);
161 session->is_multiparty = FALSE;
162 session->with = g_strdup(who);
163 session->unconfirmed_messages = g_hash_table_new_full(
164 g_str_hash, g_str_equal, g_free, (GDestroyNotify)sipe_free_queued_message);
165 sip->sessions = g_slist_append(sip->sessions, session);
167 return session;
170 void
171 sipe_session_remove(struct sipe_account_data *sip, struct sip_session *session)
173 GSList *entry;
175 sip->sessions = g_slist_remove(sip->sessions, session);
177 sipe_dialog_remove_all(session);
178 sipe_dialog_free(session->focus_dialog);
180 entry = session->outgoing_message_queue;
181 while (entry) {
182 struct queued_message *msg = entry->data;
183 g_free(msg->body);
184 g_free(msg->content_type);
185 g_free(msg);
186 entry = entry->next;
188 g_slist_free(session->outgoing_message_queue);
190 entry = session->pending_invite_queue;
191 while (entry) {
192 g_free(entry->data);
193 entry = entry->next;
195 g_slist_free(session->pending_invite_queue);
197 g_hash_table_destroy(session->unconfirmed_messages);
198 g_hash_table_destroy(session->conf_unconfirmed_messages);
200 g_free(session->with);
201 g_free(session->chat_title);
202 g_free(session->callid);
203 g_free(session->roster_manager);
204 g_free(session->focus_uri);
205 g_free(session->im_mcu_uri);
206 g_free(session->subject);
207 g_free(session);
210 void
211 sipe_session_remove_all(struct sipe_account_data *sip)
213 GSList *entry;
214 while ((entry = sip->sessions) != NULL) {
215 sipe_session_remove(sip, entry->data);
219 void
220 sipe_session_enqueue_message(struct sip_session *session,
221 const gchar *body, const gchar *content_type)
223 struct queued_message *msg = g_new0(struct queued_message,1);
224 msg->body = g_strdup(body);
225 if (content_type != NULL)
226 msg->content_type = g_strdup(content_type);
228 session->outgoing_message_queue = g_slist_append(session->outgoing_message_queue, msg);
231 GSList *
232 sipe_session_dequeue_message(struct sip_session *session)
234 struct queued_message *msg;
236 if (session->outgoing_message_queue == NULL)
237 return NULL;
239 msg = session->outgoing_message_queue->data;
240 session->outgoing_message_queue = g_slist_remove(session->outgoing_message_queue, msg);
241 g_free(msg->body);
242 g_free(msg->content_type);
243 g_free(msg);
245 return session->outgoing_message_queue;
249 Local Variables:
250 mode: c
251 c-file-style: "bsd"
252 indent-tabs-mode: t
253 tab-width: 8
254 End: