Sync up miranda files with current libsipe branch
[siplcs.git] / src / miranda / miranda-chat.c
blob10902947f829c8bcbbe29db3eaf48cef5dd25d7e
1 /**
2 * @file miranda-chat.c
4 * pidgin-sipe
6 * Copyright (C) 2010 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-core-private.h"
34 #include "sipe-nls.h"
35 #include "sipe-utils.h"
37 #include "newpluginapi.h"
38 #include "m_protosvc.h"
39 #include "m_protoint.h"
40 #include "m_chat.h"
41 #include "m_utils.h"
42 #include "m_system.h"
44 #include "libsipe.h"
45 #include "miranda-private.h"
47 struct sipe_backend_chat_session {
48 SIPPROTO *pr;
49 gchar *conv;
52 void sipe_backend_chat_session_destroy(SIPE_UNUSED_PARAMETER struct sipe_backend_chat_session *session)
54 /* Nothing to do here */
57 void sipe_backend_chat_add(struct sipe_backend_chat_session *backend_session,
58 const gchar *uri,
59 gboolean is_new)
61 SIPPROTO *pr = backend_session->pr;
62 struct sipe_core_public *sipe_public = pr->sip;
63 gchar *self = sip_uri_self(SIPE_CORE_PRIVATE);
64 GCDEST gcd = {0};
65 GCEVENT gce = {0};
66 int retval;
68 SIPE_DEBUG_INFO("sipe_backend_chat_add: Adding user <%s> to chat <%s>", uri, backend_session->conv);
70 gcd.pszModule = pr->proto.m_szModuleName;
71 gcd.pszID = backend_session->conv;
72 gcd.iType = GC_EVENT_JOIN;
74 gce.cbSize = sizeof(gce);
75 gce.pDest = &gcd;
76 gce.pszNick = uri;
77 gce.pszUID = uri;
78 gce.pszStatus = "Normal";
79 gce.bIsMe = !strcmp(self, uri);
81 g_free(self);
82 retval = CallService( MS_GC_EVENT, 0, (LPARAM)&gce );
83 if (retval) {
84 SIPE_DEBUG_WARNING("sipe_backend_chat_add: Failed to add user to chat: <%d>", retval);
88 void sipe_backend_chat_close(struct sipe_backend_chat_session *backend_session)
90 SIPPROTO *pr;
91 GCEVENT gce = {0};
92 GCDEST gcd = {0};
94 if (!backend_session)
96 SIPE_DEBUG_WARNING_NOFORMAT("Attempted to close NULL backend_session");
97 return;
100 pr = backend_session->pr;
102 gce.cbSize = sizeof(gce);
103 gce.pDest = &gcd;
105 gcd.pszModule = pr->proto.m_szModuleName;
106 gcd.pszID = backend_session->conv;
107 gcd.iType = GC_EVENT_CONTROL;
109 if (CallServiceSync( MS_GC_EVENT, SESSION_TERMINATE, (LPARAM)&gce ))
111 SIPE_DEBUG_WARNING_NOFORMAT("sipe_backend_groupchat_room_terminate: Failed to close chat session");
115 struct sipe_backend_chat_session *sipe_backend_chat_create(struct sipe_core_public *sipe_public,
116 struct sipe_chat_session *session,
117 const gchar *title,
118 const gchar *nick)
120 SIPPROTO *pr = sipe_public->backend_private;
121 GCSESSION gs;
122 GCDEST gcd = {0};
123 GCEVENT gce = {0};
124 gchar *id = g_strdup(title); /* FIXME: Generate ID */
125 struct sipe_backend_chat_session *conv = g_new0(struct sipe_backend_chat_session,1);
127 gs.cbSize = sizeof(gs);
128 gs.iType = GCW_CHATROOM;
129 gs.pszModule = pr->proto.m_szModuleName;
130 gs.pszName = title;
131 gs.pszID = id;
132 gs.pszStatusbarText = NULL;
133 gs.dwFlags = 0;
134 gs.dwItemData = (DWORD)session;
136 if (CallServiceSync( MS_GC_NEWSESSION, 0, (LPARAM)&gs ))
138 SIPE_DEBUG_ERROR("sipe_backend_chat_create: Failed to create chat session <%d> <%s>", id, title);
141 gcd.pszModule = pr->proto.m_szModuleName;
142 gcd.pszID = id;
144 gce.cbSize = sizeof(gce);
145 gce.pDest = &gcd;
147 gcd.iType = GC_EVENT_ADDGROUP;
148 gce.pszStatus = "Normal";
149 if (CallService( MS_GC_EVENT, 0, (LPARAM)&gce ))
151 SIPE_DEBUG_WARNING_NOFORMAT("sipe_backend_chat_create: Failed to add normal status to chat session");
154 gce.pszStatus = "Presenter";
155 if (CallService( MS_GC_EVENT, 0, (LPARAM)&gce ))
157 SIPE_DEBUG_WARNING_NOFORMAT("sipe_backend_chat_create: Failed to add presenter status to chat session");
161 gcd.iType = GC_EVENT_CONTROL;
163 if (CallServiceSync( MS_GC_EVENT, SESSION_INITDONE, (LPARAM)&gce ))
165 SIPE_DEBUG_WARNING_NOFORMAT("sipe_backend_chat_create: Failed to initdone chat session");
167 if (CallServiceSync( MS_GC_EVENT, SESSION_ONLINE, (LPARAM)&gce ))
169 SIPE_DEBUG_ERROR_NOFORMAT("sipe_backend_chat_create: Failed to set chat session online");
172 conv->conv = id;
173 conv->pr = pr;
175 return conv;
178 gboolean sipe_backend_chat_find(struct sipe_backend_chat_session *backend_session,
179 const gchar *uri)
181 SIPPROTO *pr = backend_session->pr;
182 GC_INFO gci = {0};
183 const gchar *context;
184 const gchar *user;
186 gci.Flags = BYID | USERS;
187 gci.pszID = mir_a2t(backend_session->conv);
188 gci.pszModule = pr->proto.m_szModuleName;
190 if(CallServiceSync( MS_GC_GETINFO, 0, (LPARAM)&gci )) {
191 SIPE_DEBUG_ERROR_NOFORMAT("sipe_backend_chat_find: Failed to get chat user list");
192 return FALSE;
195 if (!gci.pszUsers)
196 return FALSE;
198 user = strtok_s(gci.pszUsers, " ", &context);
199 while (user)
201 SIPE_DEBUG_INFO("sipe_backend_chat_find: Found user <%s>", user);
202 if (!strcmp(uri, user)) {
203 mir_free(gci.pszUsers);
204 return TRUE;
206 user = strtok_s(NULL, " ", &context);
209 mir_free(gci.pszUsers);
210 return FALSE;
213 gboolean sipe_backend_chat_is_operator(struct sipe_backend_chat_session *backend_session,
214 const gchar *uri)
216 _NIF();
217 return TRUE;
220 void sipe_backend_chat_message(struct sipe_core_public *sipe_public,
221 struct sipe_backend_chat_session *backend_session,
222 const gchar *from,
223 const gchar *html)
225 SIPPROTO *pr = backend_session->pr;
226 gchar *self = sip_uri_self(SIPE_CORE_PRIVATE);
227 gchar *msg;
228 GCDEST gcd = {0};
229 GCEVENT gce = {0};
231 gcd.pszModule = pr->proto.m_szModuleName;
232 gcd.pszID = backend_session->conv;
233 gcd.iType = GC_EVENT_MESSAGE;
235 msg = sipe_miranda_eliminate_html(html, strlen(html));
237 gce.cbSize = sizeof(gce);
238 gce.pDest = &gcd;
239 gce.pszNick = from;
240 gce.pszUID = from;
241 gce.pszText = msg;
242 gce.bIsMe = !strcmp(self, from);
243 // gce.time = mtime; // FIXME: Generate timestamp
244 g_free(self);
246 CallService( MS_GC_EVENT, 0, (LPARAM)&gce );
247 mir_free(msg);
250 void sipe_backend_chat_operator(struct sipe_backend_chat_session *backend_session,
251 const gchar *uri)
253 _NIF();
256 void sipe_backend_chat_rejoin(struct sipe_core_public *sipe_public,
257 struct sipe_backend_chat_session *backend_session,
258 const gchar *nick,
259 const gchar *title)
261 _NIF();
265 * Connection re-established: tell core what chats need to be rejoined
267 void sipe_backend_chat_rejoin_all(struct sipe_core_public *sipe_public)
269 _NIF();
272 void sipe_backend_chat_remove(struct sipe_backend_chat_session *backend_session,
273 const gchar *uri)
275 SIPPROTO *pr = backend_session->pr;
276 struct sipe_core_public *sipe_public = pr->sip;
277 gchar *self = sip_uri_self(SIPE_CORE_PRIVATE);
278 GCDEST gcd = {0};
279 GCEVENT gce = {0};
281 SIPE_DEBUG_INFO("sipe_backend_chat_remove: Removing user <%s> from chat <%s>", uri, backend_session->conv);
283 gcd.pszModule = pr->proto.m_szModuleName;
284 gcd.pszID = backend_session->conv;
285 gcd.iType = GC_EVENT_PART;
287 gce.cbSize = sizeof(gce);
288 gce.pDest = &gcd;
289 gce.pszNick = uri;
290 gce.pszUID = uri;
291 gce.pszStatus = 0;
292 gce.bIsMe = !strcmp(self, uri);
294 g_free(self);
295 CallService( MS_GC_EVENT, 0, (LPARAM)&gce );
298 void sipe_backend_chat_show(struct sipe_backend_chat_session *backend_session)
300 _NIF();
303 void sipe_backend_chat_topic(struct sipe_backend_chat_session *backend_session,
304 const gchar *topic)
306 SIPPROTO *pr = backend_session->pr;
307 GCDEST gcd = {0};
308 GCEVENT gce = {0};
310 SIPE_DEBUG_INFO("sipe_backend_chat_topic: conv <%s> topic <%s>", backend_session->conv, topic);
312 gcd.pszModule = pr->proto.m_szModuleName;
313 gcd.pszID = backend_session->conv;
314 gcd.iType = GC_EVENT_TOPIC;
316 gce.cbSize = sizeof(gce);
317 gce.pDest = &gcd;
318 gce.pszNick = NULL;
319 gce.pszUID = NULL;
320 gce.pszText = topic;
322 CallService( MS_GC_EVENT, 0, (LPARAM)&gce );
326 Local Variables:
327 mode: c
328 c-file-style: "bsd"
329 indent-tabs-mode: t
330 tab-width: 8
331 End: