ucs: fix Persona key extraction
[siplcs.git] / src / miranda / miranda-chat.c
blobf543d592dabf78025f4365eb334873268207f618
1 /**
2 * @file miranda-chat.c
4 * pidgin-sipe
6 * Copyright (C) 2010-2013 SIPE Project <http://sipe.sourceforge.net/>
7 * Copyright (C) 2009 pier11 <pier11@operamail.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <windows.h>
26 #include <stdio.h>
28 #include <glib.h>
30 #include "sipe-common.h"
31 #include "sipe-backend.h"
32 #include "sipe-core.h"
33 #include "sipe-nls.h"
35 #include "newpluginapi.h"
36 #include "m_protosvc.h"
37 #include "m_protoint.h"
38 #include "m_chat.h"
39 #include "m_utils.h"
40 #include "m_system.h"
42 #include "miranda-private.h"
44 void sipe_backend_chat_session_destroy(SIPE_UNUSED_PARAMETER struct sipe_backend_chat_session *session)
46 /* Nothing to do here */
49 void sipe_backend_chat_add(struct sipe_backend_chat_session *backend_session,
50 const gchar *uri,
51 gboolean is_new)
53 SIPPROTO *pr = backend_session->pr;
54 struct sipe_core_public *sipe_public = pr->sip;
55 gchar *self = sipe_miranda_uri_self(pr);
56 GCDEST gcd = {0};
57 GCEVENT gce = {0};
58 int retval;
59 HANDLE hContact = sipe_backend_buddy_find( sipe_public, uri, NULL );
60 gchar *nick = sipe_miranda_getContactString(pr, hContact, "Nick");
62 SIPE_DEBUG_INFO("sipe_backend_chat_add: Adding user <%s> to chat <%s>", uri, backend_session->conv);
64 gcd.pszModule = pr->proto.m_szModuleName;
65 gcd.pszID = backend_session->conv;
66 gcd.iType = GC_EVENT_JOIN;
68 gce.cbSize = sizeof(gce);
69 gce.pDest = &gcd;
70 gce.pszNick = nick;
71 gce.pszUID = uri;
72 gce.pszStatus = "Normal";
73 gce.bIsMe = !strcmp(self, uri);
75 g_free(self);
76 retval = CallService( MS_GC_EVENT, 0, (LPARAM)&gce );
77 if (retval) {
78 SIPE_DEBUG_WARNING("sipe_backend_chat_add: Failed to add user to chat: <%d>", retval);
80 mir_free(nick);
83 void sipe_backend_chat_close(struct sipe_backend_chat_session *backend_session)
85 SIPPROTO *pr;
86 GCEVENT gce = {0};
87 GCDEST gcd = {0};
88 struct sipe_chat_session *session;
90 if (!backend_session)
92 SIPE_DEBUG_WARNING_NOFORMAT("Attempted to close NULL backend_session");
93 return;
96 pr = backend_session->pr;
98 gce.cbSize = sizeof(gce);
99 gce.pDest = &gcd;
101 gcd.pszModule = pr->proto.m_szModuleName;
102 gcd.pszID = backend_session->conv;
103 gcd.iType = GC_EVENT_CONTROL;
105 session = (struct sipe_chat_session*)CallServiceSync( MS_GC_EVENT, SESSION_TERMINATE, (LPARAM)&gce );
108 struct sipe_backend_chat_session *sipe_backend_chat_create(struct sipe_core_public *sipe_public,
109 struct sipe_chat_session *session,
110 const gchar *title,
111 const gchar *nick)
113 SIPPROTO *pr = sipe_public->backend_private;
114 GCSESSION gs;
115 GCDEST gcd = {0};
116 GCEVENT gce = {0};
117 gchar *id = g_strdup(title); /* FIXME: Generate ID */
118 struct sipe_backend_chat_session *conv = g_new0(struct sipe_backend_chat_session,1);
120 gs.cbSize = sizeof(gs);
121 gs.iType = GCW_CHATROOM;
122 gs.pszModule = pr->proto.m_szModuleName;
123 gs.pszName = title;
124 gs.pszID = id;
125 gs.pszStatusbarText = NULL;
126 gs.dwFlags = 0;
127 gs.dwItemData = (DWORD)session;
129 if (CallServiceSync( MS_GC_NEWSESSION, 0, (LPARAM)&gs ))
131 SIPE_DEBUG_ERROR("Failed to create chat session <%d> <%s>", id, title);
134 gcd.pszModule = pr->proto.m_szModuleName;
135 gcd.pszID = id;
137 gce.cbSize = sizeof(gce);
138 gce.pDest = &gcd;
140 gcd.iType = GC_EVENT_ADDGROUP;
141 gce.pszStatus = "Normal";
142 if (CallService( MS_GC_EVENT, 0, (LPARAM)&gce ))
144 SIPE_DEBUG_WARNING_NOFORMAT("Failed to add normal status to chat session");
147 gce.pszStatus = "Presenter";
148 if (CallService( MS_GC_EVENT, 0, (LPARAM)&gce ))
150 SIPE_DEBUG_WARNING_NOFORMAT("Failed to add presenter status to chat session");
154 gcd.iType = GC_EVENT_CONTROL;
156 if (CallServiceSync( MS_GC_EVENT, SESSION_INITDONE, (LPARAM)&gce ))
158 SIPE_DEBUG_WARNING_NOFORMAT("Failed to initdone chat session");
160 if (CallServiceSync( MS_GC_EVENT, SESSION_ONLINE, (LPARAM)&gce ))
162 SIPE_DEBUG_ERROR_NOFORMAT("Failed to set chat session online");
165 conv->conv = id;
166 conv->pr = pr;
168 return conv;
171 gboolean sipe_backend_chat_find(struct sipe_backend_chat_session *backend_session,
172 const gchar *uri)
174 SIPPROTO *pr = backend_session->pr;
175 GC_INFO gci = {0};
176 gchar *context;
177 const gchar *user;
179 gci.Flags = BYID | USERS;
180 gci.pszID = mir_a2t(backend_session->conv);
181 gci.pszModule = pr->proto.m_szModuleName;
183 if(CallServiceSync( MS_GC_GETINFO, 0, (LPARAM)&gci )) {
184 SIPE_DEBUG_ERROR_NOFORMAT("Failed to get chat user list");
185 return FALSE;
188 if (!gci.pszUsers)
189 return FALSE;
191 user = strtok_s(gci.pszUsers, " ", &context);
192 while (user)
194 SIPE_DEBUG_INFO("sipe_backend_chat_find: Found user <%s>", user);
195 if (!strcmp(uri, user)) {
196 mir_free(gci.pszUsers);
197 return TRUE;
199 user = strtok_s(NULL, " ", &context);
202 mir_free(gci.pszUsers);
203 return FALSE;
206 gboolean sipe_backend_chat_is_operator(struct sipe_backend_chat_session *backend_session,
207 const gchar *uri)
209 _NIF();
210 return TRUE;
213 void sipe_backend_chat_message(struct sipe_core_public *sipe_public,
214 struct sipe_backend_chat_session *backend_session,
215 const gchar *from,
216 time_t when,
217 const gchar *html)
219 SIPPROTO *pr = backend_session->pr;
220 gchar *self = sipe_miranda_uri_self(pr);
221 gchar *msg;
222 GCDEST gcd = {0};
223 GCEVENT gce = {0};
224 HANDLE hContact = sipe_backend_buddy_find( sipe_public, from, NULL );
225 gchar *nick = sipe_miranda_getContactString(pr, hContact, "Nick");
227 gcd.pszModule = pr->proto.m_szModuleName;
228 gcd.pszID = backend_session->conv;
229 gcd.iType = GC_EVENT_MESSAGE;
231 msg = sipe_miranda_eliminate_html(html, strlen(html));
233 gce.cbSize = sizeof(gce);
234 gce.pDest = &gcd;
235 gce.pszNick = nick;
236 gce.pszUID = from;
237 gce.pszText = msg;
238 gce.bIsMe = !strcmp(self, from);
239 // gce.time = mtime; // FIXME: Generate timestamp
240 g_free(self);
242 CallService( MS_GC_EVENT, 0, (LPARAM)&gce );
243 mir_free(nick);
244 mir_free(msg);
247 void sipe_backend_chat_operator(struct sipe_backend_chat_session *backend_session,
248 const gchar *uri)
250 SIPPROTO *pr;
251 GCEVENT gce = {0};
252 GCDEST gcd = {0};
253 HANDLE hContact;
254 gchar *nick;
255 struct sipe_core_public *sipe_public;
257 if (!backend_session)
259 SIPE_DEBUG_WARNING_NOFORMAT("Attempted to set operator on NULL backend_session");
260 return;
263 pr = backend_session->pr;
264 sipe_public = pr->sip;
266 hContact = sipe_backend_buddy_find( sipe_public, uri, NULL );
267 nick = sipe_miranda_getContactString(pr, hContact, "Nick");
269 gce.cbSize = sizeof(gce);
270 gce.pDest = &gcd;
271 gce.pszNick = nick;
272 gce.pszUID = uri;
273 gce.pszText = "Presenter";
274 gce.pszStatus = "Presenter";
276 gcd.pszModule = pr->proto.m_szModuleName;
277 gcd.pszID = backend_session->conv;
278 gcd.iType = GC_EVENT_ADDSTATUS;
280 if (CallServiceSync( MS_GC_EVENT, 0, (LPARAM)&gce ))
282 SIPE_DEBUG_WARNING_NOFORMAT("Failed to set presenter status");
284 mir_free(nick);
287 void sipe_backend_chat_rejoin(struct sipe_core_public *sipe_public,
288 struct sipe_backend_chat_session *backend_session,
289 const gchar *nick,
290 const gchar *title)
292 _NIF();
296 * Connection re-established: tell core what chats need to be rejoined
298 void sipe_backend_chat_rejoin_all(struct sipe_core_public *sipe_public)
300 _NIF();
303 void sipe_backend_chat_remove(struct sipe_backend_chat_session *backend_session,
304 const gchar *uri)
306 SIPPROTO *pr = backend_session->pr;
307 struct sipe_core_public *sipe_public = pr->sip;
308 gchar *self = sipe_miranda_uri_self(pr);
309 GCDEST gcd = {0};
310 GCEVENT gce = {0};
311 HANDLE hContact = sipe_backend_buddy_find( sipe_public, uri, NULL );
312 gchar *nick = sipe_miranda_getContactString(pr, hContact, "Nick");
314 SIPE_DEBUG_INFO("sipe_backend_chat_remove: Removing user <%s> from chat <%s>", uri, backend_session->conv);
316 gcd.pszModule = pr->proto.m_szModuleName;
317 gcd.pszID = backend_session->conv;
318 gcd.iType = GC_EVENT_PART;
320 gce.cbSize = sizeof(gce);
321 gce.pDest = &gcd;
322 gce.pszNick = nick;
323 gce.pszUID = uri;
324 gce.pszStatus = 0;
325 gce.bIsMe = !strcmp(self, uri);
327 g_free(self);
328 CallService( MS_GC_EVENT, 0, (LPARAM)&gce );
329 mir_free(nick);
332 void sipe_backend_chat_show(struct sipe_backend_chat_session *backend_session)
334 _NIF();
337 void sipe_backend_chat_topic(struct sipe_backend_chat_session *backend_session,
338 const gchar *topic)
340 SIPPROTO *pr = backend_session->pr;
341 GCDEST gcd = {0};
342 GCEVENT gce = {0};
344 SIPE_DEBUG_INFO("sipe_backend_chat_topic: conv <%s> topic <%s>", backend_session->conv, topic);
346 gcd.pszModule = pr->proto.m_szModuleName;
347 gcd.pszID = backend_session->conv;
348 gcd.iType = GC_EVENT_TOPIC;
350 gce.cbSize = sizeof(gce);
351 gce.pDest = &gcd;
352 gce.pszNick = NULL;
353 gce.pszUID = NULL;
354 gce.pszText = topic;
356 CallService( MS_GC_EVENT, 0, (LPARAM)&gce );
360 Local Variables:
361 mode: c
362 c-file-style: "bsd"
363 indent-tabs-mode: t
364 tab-width: 8
365 End: