Release 1.25.0 -- Buddy Idle Time, RTF
[siplcs.git] / src / miranda / miranda-chat.c
blob0f4f4efa7adac6bba5e7b705da153726e36deab6
1 /**
2 * @file miranda-chat.c
4 * pidgin-sipe
6 * Copyright (C) 2010-2018 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"
34 #include "newpluginapi.h"
35 #include "m_protosvc.h"
36 #include "m_protoint.h"
37 #include "m_chat.h"
38 #include "m_utils.h"
39 #include "m_system.h"
41 #include "miranda-private.h"
43 void sipe_backend_chat_session_destroy(SIPE_UNUSED_PARAMETER struct sipe_backend_chat_session *session)
45 /* Nothing to do here */
48 void sipe_backend_chat_add(struct sipe_backend_chat_session *backend_session,
49 const gchar *uri,
50 gboolean is_new)
52 SIPPROTO *pr = backend_session->pr;
53 struct sipe_core_public *sipe_public = pr->sip;
54 gchar *self = sipe_miranda_uri_self(pr);
55 GCDEST gcd = {0};
56 GCEVENT gce = {0};
57 int retval;
58 HANDLE hContact = sipe_backend_buddy_find( sipe_public, uri, NULL );
59 gchar *nick = sipe_miranda_getContactString(pr, hContact, "Nick");
61 SIPE_DEBUG_INFO("sipe_backend_chat_add: Adding user <%s> to chat <%s>", uri, backend_session->conv);
63 gcd.pszModule = pr->proto.m_szModuleName;
64 gcd.pszID = backend_session->conv;
65 gcd.iType = GC_EVENT_JOIN;
67 gce.cbSize = sizeof(gce);
68 gce.pDest = &gcd;
69 gce.pszNick = nick;
70 gce.pszUID = uri;
71 gce.pszStatus = "Normal";
72 gce.bIsMe = !strcmp(self, uri);
74 g_free(self);
75 retval = CallService( MS_GC_EVENT, 0, (LPARAM)&gce );
76 if (retval) {
77 SIPE_DEBUG_WARNING("sipe_backend_chat_add: Failed to add user to chat: <%d>", retval);
79 mir_free(nick);
82 void sipe_backend_chat_close(struct sipe_backend_chat_session *backend_session)
84 SIPPROTO *pr;
85 GCEVENT gce = {0};
86 GCDEST gcd = {0};
87 struct sipe_chat_session *session;
89 if (!backend_session)
91 SIPE_DEBUG_WARNING_NOFORMAT("Attempted to close NULL backend_session");
92 return;
95 pr = backend_session->pr;
97 gce.cbSize = sizeof(gce);
98 gce.pDest = &gcd;
100 gcd.pszModule = pr->proto.m_szModuleName;
101 gcd.pszID = backend_session->conv;
102 gcd.iType = GC_EVENT_CONTROL;
104 session = (struct sipe_chat_session*)CallServiceSync( MS_GC_EVENT, SESSION_TERMINATE, (LPARAM)&gce );
107 struct sipe_backend_chat_session *sipe_backend_chat_create(struct sipe_core_public *sipe_public,
108 struct sipe_chat_session *session,
109 const gchar *title,
110 const gchar *nick)
112 SIPPROTO *pr = sipe_public->backend_private;
113 GCSESSION gs;
114 GCDEST gcd = {0};
115 GCEVENT gce = {0};
116 gchar *id = g_strdup(title); /* FIXME: Generate ID */
117 struct sipe_backend_chat_session *conv = g_new0(struct sipe_backend_chat_session,1);
119 gs.cbSize = sizeof(gs);
120 gs.iType = GCW_CHATROOM;
121 gs.pszModule = pr->proto.m_szModuleName;
122 gs.pszName = title;
123 gs.pszID = id;
124 gs.pszStatusbarText = NULL;
125 gs.dwFlags = 0;
126 gs.dwItemData = (DWORD)session;
128 if (CallServiceSync( MS_GC_NEWSESSION, 0, (LPARAM)&gs ))
130 SIPE_DEBUG_ERROR("Failed to create chat session <%d> <%s>", id, title);
133 gcd.pszModule = pr->proto.m_szModuleName;
134 gcd.pszID = id;
136 gce.cbSize = sizeof(gce);
137 gce.pDest = &gcd;
139 gcd.iType = GC_EVENT_ADDGROUP;
140 gce.pszStatus = "Normal";
141 if (CallService( MS_GC_EVENT, 0, (LPARAM)&gce ))
143 SIPE_DEBUG_WARNING_NOFORMAT("Failed to add normal status to chat session");
146 gce.pszStatus = "Presenter";
147 if (CallService( MS_GC_EVENT, 0, (LPARAM)&gce ))
149 SIPE_DEBUG_WARNING_NOFORMAT("Failed to add presenter status to chat session");
153 gcd.iType = GC_EVENT_CONTROL;
155 if (CallServiceSync( MS_GC_EVENT, SESSION_INITDONE, (LPARAM)&gce ))
157 SIPE_DEBUG_WARNING_NOFORMAT("Failed to initdone chat session");
159 if (CallServiceSync( MS_GC_EVENT, SESSION_ONLINE, (LPARAM)&gce ))
161 SIPE_DEBUG_ERROR_NOFORMAT("Failed to set chat session online");
164 conv->conv = id;
165 conv->pr = pr;
167 return conv;
170 gboolean sipe_backend_chat_find(struct sipe_backend_chat_session *backend_session,
171 const gchar *uri)
173 SIPPROTO *pr = backend_session->pr;
174 GC_INFO gci = {0};
175 gchar *context;
176 const gchar *user;
178 gci.Flags = BYID | USERS;
179 gci.pszID = mir_a2t(backend_session->conv);
180 gci.pszModule = pr->proto.m_szModuleName;
182 if(CallServiceSync( MS_GC_GETINFO, 0, (LPARAM)&gci )) {
183 SIPE_DEBUG_ERROR_NOFORMAT("Failed to get chat user list");
184 return FALSE;
187 if (!gci.pszUsers)
188 return FALSE;
190 user = strtok_s(gci.pszUsers, " ", &context);
191 while (user)
193 SIPE_DEBUG_INFO("sipe_backend_chat_find: Found user <%s>", user);
194 if (!strcmp(uri, user)) {
195 mir_free(gci.pszUsers);
196 return TRUE;
198 user = strtok_s(NULL, " ", &context);
201 mir_free(gci.pszUsers);
202 return FALSE;
205 gboolean sipe_backend_chat_is_operator(struct sipe_backend_chat_session *backend_session,
206 const gchar *uri)
208 _NIF();
209 return TRUE;
212 void sipe_backend_chat_message(struct sipe_core_public *sipe_public,
213 struct sipe_backend_chat_session *backend_session,
214 const gchar *from,
215 time_t when,
216 const gchar *html)
218 SIPPROTO *pr = backend_session->pr;
219 gchar *self = sipe_miranda_uri_self(pr);
220 gchar *msg;
221 GCDEST gcd = {0};
222 GCEVENT gce = {0};
223 HANDLE hContact = sipe_backend_buddy_find( sipe_public, from, NULL );
224 gchar *nick = sipe_miranda_getContactString(pr, hContact, "Nick");
226 gcd.pszModule = pr->proto.m_szModuleName;
227 gcd.pszID = backend_session->conv;
228 gcd.iType = GC_EVENT_MESSAGE;
230 msg = sipe_miranda_eliminate_html(html, strlen(html));
232 gce.cbSize = sizeof(gce);
233 gce.pDest = &gcd;
234 gce.pszNick = nick;
235 gce.pszUID = from;
236 gce.pszText = msg;
237 gce.bIsMe = !strcmp(self, from);
238 // gce.time = mtime; // FIXME: Generate timestamp
239 g_free(self);
241 CallService( MS_GC_EVENT, 0, (LPARAM)&gce );
242 mir_free(nick);
243 mir_free(msg);
246 void sipe_backend_chat_operator(struct sipe_backend_chat_session *backend_session,
247 const gchar *uri)
249 SIPPROTO *pr;
250 GCEVENT gce = {0};
251 GCDEST gcd = {0};
252 HANDLE hContact;
253 gchar *nick;
254 struct sipe_core_public *sipe_public;
256 if (!backend_session)
258 SIPE_DEBUG_WARNING_NOFORMAT("Attempted to set operator on NULL backend_session");
259 return;
262 pr = backend_session->pr;
263 sipe_public = pr->sip;
265 hContact = sipe_backend_buddy_find( sipe_public, uri, NULL );
266 nick = sipe_miranda_getContactString(pr, hContact, "Nick");
268 gce.cbSize = sizeof(gce);
269 gce.pDest = &gcd;
270 gce.pszNick = nick;
271 gce.pszUID = uri;
272 gce.pszText = "Presenter";
273 gce.pszStatus = "Presenter";
275 gcd.pszModule = pr->proto.m_szModuleName;
276 gcd.pszID = backend_session->conv;
277 gcd.iType = GC_EVENT_ADDSTATUS;
279 if (CallServiceSync( MS_GC_EVENT, 0, (LPARAM)&gce ))
281 SIPE_DEBUG_WARNING_NOFORMAT("Failed to set presenter status");
283 mir_free(nick);
286 void sipe_backend_chat_rejoin(struct sipe_core_public *sipe_public,
287 struct sipe_backend_chat_session *backend_session,
288 const gchar *nick,
289 const gchar *title)
291 _NIF();
295 * Connection re-established: tell core what chats need to be rejoined
297 void sipe_backend_chat_rejoin_all(struct sipe_core_public *sipe_public)
299 _NIF();
302 void sipe_backend_chat_remove(struct sipe_backend_chat_session *backend_session,
303 const gchar *uri)
305 SIPPROTO *pr = backend_session->pr;
306 struct sipe_core_public *sipe_public = pr->sip;
307 gchar *self = sipe_miranda_uri_self(pr);
308 GCDEST gcd = {0};
309 GCEVENT gce = {0};
310 HANDLE hContact = sipe_backend_buddy_find( sipe_public, uri, NULL );
311 gchar *nick = sipe_miranda_getContactString(pr, hContact, "Nick");
313 SIPE_DEBUG_INFO("sipe_backend_chat_remove: Removing user <%s> from chat <%s>", uri, backend_session->conv);
315 gcd.pszModule = pr->proto.m_szModuleName;
316 gcd.pszID = backend_session->conv;
317 gcd.iType = GC_EVENT_PART;
319 gce.cbSize = sizeof(gce);
320 gce.pDest = &gcd;
321 gce.pszNick = nick;
322 gce.pszUID = uri;
323 gce.pszStatus = 0;
324 gce.bIsMe = !strcmp(self, uri);
326 g_free(self);
327 CallService( MS_GC_EVENT, 0, (LPARAM)&gce );
328 mir_free(nick);
331 void sipe_backend_chat_show(struct sipe_backend_chat_session *backend_session)
333 _NIF();
336 void sipe_backend_chat_topic(struct sipe_backend_chat_session *backend_session,
337 const gchar *topic)
339 SIPPROTO *pr = backend_session->pr;
340 GCDEST gcd = {0};
341 GCEVENT gce = {0};
343 SIPE_DEBUG_INFO("sipe_backend_chat_topic: conv <%s> topic <%s>", backend_session->conv, topic);
345 gcd.pszModule = pr->proto.m_szModuleName;
346 gcd.pszID = backend_session->conv;
347 gcd.iType = GC_EVENT_TOPIC;
349 gce.cbSize = sizeof(gce);
350 gce.pDest = &gcd;
351 gce.pszNick = NULL;
352 gce.pszUID = NULL;
353 gce.pszText = topic;
355 CallService( MS_GC_EVENT, 0, (LPARAM)&gce );
359 Local Variables:
360 mode: c
361 c-file-style: "bsd"
362 indent-tabs-mode: t
363 tab-width: 8
364 End: