tls: add Client Key Exchange message
[siplcs.git] / src / core / sipe-group.c
blob3bf2aa788fa9500f47b572e97be10d807196553d
1 /**
2 * @file sipe-group.c
4 * pidgin-sipe
6 * Copyright (C) 2011 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 <glib.h>
25 #include "sipe-core.h"
26 #include "sipe-core-private.h"
27 #include "sipe-backend.h"
28 #include "sip-transport.h"
29 #include "sipe-xml.h"
30 #include "sipe-buddy.h"
31 #include "sipmsg.h"
32 #include "sipe-group.h"
33 #include "sipe-nls.h"
34 #include "sipe.h"
36 static void
37 sipe_group_context_destroy(gpointer data)
39 struct group_user_context *ctx = data;
40 g_free(ctx->group_name);
41 g_free(ctx->user_name);
42 g_free(ctx);
45 static gboolean
46 process_add_group_response(struct sipe_core_private *sipe_private,
47 struct sipmsg *msg,
48 struct transaction *trans)
50 if (msg->response == 200) {
51 struct sipe_group *group;
52 struct group_user_context *ctx = trans->payload->data;
53 sipe_xml *xml;
54 const sipe_xml *node;
55 char *group_id;
56 struct sipe_buddy *buddy;
58 xml = sipe_xml_parse(msg->body, msg->bodylen);
59 if (!xml) {
60 return FALSE;
63 node = sipe_xml_child(xml, "Body/addGroup/groupID");
64 if (!node) {
65 sipe_xml_free(xml);
66 return FALSE;
69 group_id = sipe_xml_data(node);
70 if (!group_id) {
71 sipe_xml_free(xml);
72 return FALSE;
75 group = g_new0(struct sipe_group, 1);
76 group->id = (int)g_ascii_strtod(group_id, NULL);
77 g_free(group_id);
78 group->name = g_strdup(ctx->group_name);
80 sipe_group_add(sipe_private, group);
82 if (ctx->user_name) {
83 buddy = g_hash_table_lookup(sipe_private->buddies, ctx->user_name);
84 if (buddy) {
85 buddy->groups = slist_insert_unique_sorted(buddy->groups, group, (GCompareFunc)sipe_group_compare);
88 sipe_core_group_set_user(SIPE_CORE_PUBLIC, ctx->user_name);
91 sipe_xml_free(xml);
92 return TRUE;
94 return FALSE;
97 int
98 sipe_group_compare(struct sipe_group *group1, struct sipe_group *group2) {
99 return group1->id - group2->id;
102 struct sipe_group*
103 sipe_group_find_by_id(struct sipe_core_private *sipe_private,
104 int id)
106 struct sipe_account_data *sip = SIPE_ACCOUNT_DATA_PRIVATE;
107 struct sipe_group *group;
108 GSList *entry;
109 if (sip == NULL) {
110 return NULL;
113 entry = sip->groups;
114 while (entry) {
115 group = entry->data;
116 if (group->id == id) {
117 return group;
119 entry = entry->next;
121 return NULL;
124 struct sipe_group*
125 sipe_group_find_by_name(struct sipe_core_private *sipe_private,
126 const gchar * name)
128 struct sipe_account_data *sip = SIPE_ACCOUNT_DATA_PRIVATE;
129 struct sipe_group *group;
130 GSList *entry;
131 if (!sip || !name) {
132 return NULL;
135 entry = sip->groups;
136 while (entry) {
137 group = entry->data;
138 if (sipe_strequal(group->name, name)) {
139 return group;
141 entry = entry->next;
143 return NULL;
146 void
147 sipe_group_create(struct sipe_core_private *sipe_private,
148 const gchar *name,
149 const gchar * who)
151 struct sipe_account_data *sip = SIPE_ACCOUNT_DATA_PRIVATE;
152 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
153 struct group_user_context *ctx = g_new0(struct group_user_context, 1);
154 const gchar *soap_name = sipe_strequal(name, _("Other Contacts")) ? "~" : name;
155 gchar *body;
156 ctx->group_name = g_strdup(name);
157 ctx->user_name = g_strdup(who);
158 payload->destroy = sipe_group_context_destroy;
159 payload->data = ctx;
161 body = g_markup_printf_escaped(SIPE_SOAP_ADD_GROUP, soap_name, sip->contacts_delta++);
162 send_soap_request_with_cb(sipe_private, NULL, body, process_add_group_response, payload);
163 g_free(body);
166 void
167 sipe_group_add(struct sipe_core_private *sipe_private,
168 struct sipe_group * group)
170 struct sipe_account_data *sip = SIPE_ACCOUNT_DATA_PRIVATE;
171 if (sipe_backend_buddy_group_add(SIPE_CORE_PUBLIC,group->name))
173 SIPE_DEBUG_INFO("added group %s (id %d)", group->name, group->id);
174 sip->groups = g_slist_append(sip->groups, group);
176 else
178 SIPE_DEBUG_INFO("did not add group %s", group->name ? group->name : "");
182 void
183 sipe_core_group_rename(struct sipe_core_public *sipe_public,
184 const gchar *old_name,
185 const gchar *new_name)
187 struct sipe_group *s_group = sipe_group_find_by_name(SIPE_CORE_PRIVATE, old_name);
189 if (s_group) {
190 struct sipe_account_data *sip = SIPE_ACCOUNT_DATA;
191 gchar *body;
192 SIPE_DEBUG_INFO("Renaming group %s to %s", old_name, new_name);
193 body = g_markup_printf_escaped(SIPE_SOAP_MOD_GROUP, s_group->id, new_name, sip->contacts_delta++);
194 send_soap_request(SIPE_CORE_PRIVATE, body);
195 g_free(body);
196 g_free(s_group->name);
197 s_group->name = g_strdup(new_name);
198 } else {
199 SIPE_DEBUG_INFO("Cannot find group %s to rename", old_name);
203 void
204 sipe_core_group_remove(struct sipe_core_public *sipe_public,
205 const gchar *name)
207 struct sipe_group * s_group = sipe_group_find_by_name(SIPE_CORE_PRIVATE, name);
209 if (s_group) {
210 struct sipe_account_data *sip = SIPE_ACCOUNT_DATA;
211 gchar *body;
212 SIPE_DEBUG_INFO("Deleting group %s", name);
213 body = g_strdup_printf(SIPE_SOAP_DEL_GROUP, s_group->id, sip->contacts_delta++);
214 send_soap_request(SIPE_CORE_PRIVATE, body);
215 g_free(body);
217 sip->groups = g_slist_remove(sip->groups, s_group);
218 g_free(s_group->name);
219 g_free(s_group);
220 } else {
221 SIPE_DEBUG_INFO("Cannot find group %s to delete", name);
226 Local Variables:
227 mode: c
228 c-file-style: "bsd"
229 indent-tabs-mode: t
230 tab-width: 8
231 End: