source: protocols/bee_chat.c @ 6e991a9

Last change on this file since 6e991a9 was 6e991a9, checked in by dequis <dx@…>, at 2016-10-16T06:51:53Z

Turn purple_chatlist_free() into a imcb_chat_list_free()

I found myself copypasting this to jabber. Might as well make it part of
the API.

  • Property mode set to 100644
File size: 7.2 KB
RevLine 
[5ebff60]1/********************************************************************\
[f1a0890]2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
4  * Copyright 2002-2010 Wilmer van der Gaast and others                *
5  \********************************************************************/
6
7/* Stuff to handle rooms                                                */
8
9/*
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.
14
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.
19
20  You should have received a copy of the GNU General Public License with
21  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
[6f10697]22  if not, write to the Free Software Foundation, Inc., 51 Franklin St.,
23  Fifth Floor, Boston, MA  02110-1301  USA
[f1a0890]24*/
25
26#define BITLBEE_CORE
27#include "bitlbee.h"
28
[5ebff60]29struct groupchat *imcb_chat_new(struct im_connection *ic, const char *handle)
[f1a0890]30{
[5ebff60]31        struct groupchat *c = g_new0(struct groupchat, 1);
[f1a0890]32        bee_t *bee = ic->bee;
[5ebff60]33
[f1a0890]34        /* This one just creates the conversation structure, user won't see
35           anything yet until s/he is joined to the conversation. (This
36           allows you to add other already present participants first.) */
[5ebff60]37
38        ic->groupchats = g_slist_prepend(ic->groupchats, c);
[f1a0890]39        c->ic = ic;
[5ebff60]40        c->title = g_strdup(handle);
41        c->topic = g_strdup_printf(
42                "BitlBee groupchat: \"%s\". Please keep in mind that root-commands won't work here. Have fun!",
43                c->title);
44
45        if (set_getbool(&ic->bee->set, "debug")) {
46                imcb_log(ic, "Creating new conversation: (id=%p,handle=%s)", c, handle);
47        }
48
49        if (bee->ui->chat_new) {
50                bee->ui->chat_new(bee, c);
51        }
52
[f1a0890]53        return c;
54}
55
[5ebff60]56void imcb_chat_name_hint(struct groupchat *c, const char *name)
[f1a0890]57{
[d343eaa]58        bee_t *bee = c->ic->bee;
[5ebff60]59
60        if (bee->ui->chat_name_hint) {
61                bee->ui->chat_name_hint(bee, c, name);
62        }
[f1a0890]63}
64
[5ebff60]65void imcb_chat_free(struct groupchat *c)
[f1a0890]66{
67        struct im_connection *ic = c->ic;
68        bee_t *bee = ic->bee;
69        GList *ir;
[5ebff60]70
71        if (bee->ui->chat_free) {
72                bee->ui->chat_free(bee, c);
73        }
74
75        if (set_getbool(&ic->bee->set, "debug")) {
76                imcb_log(ic, "You were removed from conversation %p", c);
77        }
78
79        ic->groupchats = g_slist_remove(ic->groupchats, c);
80
81        for (ir = c->in_room; ir; ir = ir->next) {
82                g_free(ir->data);
83        }
84        g_list_free(c->in_room);
85        g_free(c->title);
86        g_free(c->topic);
87        g_free(c);
[f1a0890]88}
89
[5ebff60]90static gboolean handle_is_self(struct im_connection *ic, const char *handle)
[be1efa3]91{
[5ebff60]92        return (ic->acc->prpl->handle_is_self) ?
93               ic->acc->prpl->handle_is_self(ic, handle) :
94               (ic->acc->prpl->handle_cmp(ic->acc->user, handle) == 0);
[be1efa3]95}
96
[345577b]97void imcb_chat_msg(struct groupchat *c, const char *who, char *msg, guint32 flags, time_t sent_at)
[f1a0890]98{
99        struct im_connection *ic = c->ic;
[27e2c66]100        bee_t *bee = ic->bee;
101        bee_user_t *bu;
[345577b]102        gboolean temp = FALSE;
[27e2c66]103        char *s;
[5ebff60]104
[345577b]105        if (handle_is_self(ic, who) && !(flags & OPT_SELFMESSAGE)) {
[f1a0890]106                return;
[5ebff60]107        }
108
109        bu = bee_user_by_handle(bee, ic, who);
110        temp = (bu == NULL);
111
112        if (temp) {
113                bu = bee_user_new(bee, ic, who, BEE_USER_ONLINE);
114        }
115
116        s = set_getstr(&ic->bee->set, "strip_html");
117        if ((g_strcasecmp(s, "always") == 0) ||
118            ((ic->flags & OPT_DOES_HTML) && s)) {
119                strip_html(msg);
120        }
121
122        if (bee->ui->chat_msg) {
[345577b]123                bee->ui->chat_msg(bee, c, bu, msg, flags, sent_at);
[5ebff60]124        }
125
126        if (temp) {
127                bee_user_free(bee, bu);
128        }
[f1a0890]129}
130
[5ebff60]131void imcb_chat_log(struct groupchat *c, char *format, ...)
[f1a0890]132{
[27e2c66]133        struct im_connection *ic = c->ic;
134        bee_t *bee = ic->bee;
[f1a0890]135        va_list params;
136        char *text;
[5ebff60]137
138        if (!bee->ui->chat_log) {
[27e2c66]139                return;
[5ebff60]140        }
141
142        va_start(params, format);
143        text = g_strdup_vprintf(format, params);
144        va_end(params);
145
146        bee->ui->chat_log(bee, c, text);
147        g_free(text);
[f1a0890]148}
149
[5ebff60]150void imcb_chat_topic(struct groupchat *c, char *who, char *topic, time_t set_at)
[f1a0890]151{
152        struct im_connection *ic = c->ic;
[9e27f18]153        bee_t *bee = ic->bee;
154        bee_user_t *bu;
[5ebff60]155
156        if (!bee->ui->chat_topic) {
[9e27f18]157                return;
[5ebff60]158        }
159
160        if (who == NULL) {
[9e27f18]161                bu = NULL;
[5ebff60]162        } else if (handle_is_self(ic, who)) {
[9e27f18]163                bu = bee->user;
[5ebff60]164        } else {
165                bu = bee_user_by_handle(bee, ic, who);
166        }
167
168        if ((g_strcasecmp(set_getstr(&ic->bee->set, "strip_html"), "always") == 0) ||
169            ((ic->flags & OPT_DOES_HTML) && set_getbool(&ic->bee->set, "strip_html"))) {
170                strip_html(topic);
171        }
172
173        bee->ui->chat_topic(bee, c, topic, bu);
[f1a0890]174}
175
[5ebff60]176void imcb_chat_add_buddy(struct groupchat *c, const char *handle)
[f1a0890]177{
178        struct im_connection *ic = c->ic;
179        bee_t *bee = ic->bee;
[5ebff60]180        bee_user_t *bu = bee_user_by_handle(bee, ic, handle);
[f1a0890]181        gboolean me;
[5ebff60]182
183        if (set_getbool(&c->ic->bee->set, "debug")) {
184                imcb_log(c->ic, "User %s added to conversation %p", handle, c);
185        }
186
187        me = handle_is_self(ic, handle);
188
[f1a0890]189        /* Most protocols allow people to join, even when they're not in
190           your contact list. Try to handle that here */
[5ebff60]191        if (!me && !bu) {
192                bu = bee_user_new(bee, ic, handle, BEE_USER_LOCAL);
193        }
194
[f1a0890]195        /* Add the handle to the room userlist */
196        /* TODO: Use bu instead of a string */
[5ebff60]197        c->in_room = g_list_append(c->in_room, g_strdup(handle));
198
199        if (bee->ui->chat_add_user) {
200                bee->ui->chat_add_user(bee, c, me ? bee->user : bu);
201        }
202
203        if (me) {
[f1a0890]204                c->joined = 1;
[5ebff60]205        }
[f1a0890]206}
207
[5ebff60]208void imcb_chat_remove_buddy(struct groupchat *c, const char *handle, const char *reason)
[f1a0890]209{
[b17ce85]210        struct im_connection *ic = c->ic;
211        bee_t *bee = ic->bee;
212        bee_user_t *bu = NULL;
[5ebff60]213
214        if (set_getbool(&bee->set, "debug")) {
215                imcb_log(ic, "User %s removed from conversation %p (%s)", handle, c, reason ? reason : "");
216        }
217
[f1a0890]218        /* It might be yourself! */
[5ebff60]219        if (handle_is_self(ic, handle)) {
220                if (c->joined == 0) {
[f1a0890]221                        return;
[5ebff60]222                }
223
[b17ce85]224                bu = bee->user;
225                c->joined = 0;
[5ebff60]226        } else {
227                bu = bee_user_by_handle(bee, ic, handle);
[f1a0890]228        }
[5ebff60]229
230        if (bee->ui->chat_remove_user && bu) {
[6b56512]231                bee->ui->chat_remove_user(bee, c, bu, reason);
[f1a0890]232        }
233}
234
[5ebff60]235int bee_chat_msg(bee_t *bee, struct groupchat *c, const char *msg, int flags)
[a87754b]236{
237        struct im_connection *ic = c->ic;
238        char *buf = NULL;
[5ebff60]239
240        if ((ic->flags & OPT_DOES_HTML) && (g_strncasecmp(msg, "<html>", 6) != 0)) {
241                buf = escape_html(msg);
[a87754b]242                msg = buf;
[5ebff60]243        } else {
244                buf = g_strdup(msg);
[a87754b]245        }
[5ebff60]246
247        ic->acc->prpl->chat_msg(c, buf, flags);
248        g_free(buf);
249
[a87754b]250        return 1;
251}
[eaaa986]252
[5ebff60]253struct groupchat *bee_chat_by_title(bee_t *bee, struct im_connection *ic, const char *title)
[eaaa986]254{
255        struct groupchat *c;
256        GSList *l;
[5ebff60]257
258        for (l = ic->groupchats; l; l = l->next) {
[eaaa986]259                c = l->data;
[5ebff60]260                if (strcmp(c->title, title) == 0) {
[eaaa986]261                        return c;
[5ebff60]262                }
[eaaa986]263        }
[5ebff60]264
[eaaa986]265        return NULL;
266}
[1aa74f55]267
[5ebff60]268void imcb_chat_invite(struct im_connection *ic, const char *name, const char *who, const char *msg)
[1aa74f55]269{
[5ebff60]270        bee_user_t *bu = bee_user_by_handle(ic->bee, ic, who);
271
272        if (bu && ic->bee->ui->chat_invite) {
273                ic->bee->ui->chat_invite(ic->bee, bu, name, msg);
274        }
[1aa74f55]275}
[a33ee0f]276
[a08b2db]277void imcb_chat_list_finish(struct im_connection *ic)
[a33ee0f]278{
279        cmd_chat_list_finish(ic);
280}
[a08b2db]281
282void bee_chat_list_finish(struct im_connection *ic)
283{
284        imcb_log(ic, "Warning: using deprecated bee_chat_list_finish. This will be removed in the stable release.");
285        imcb_chat_list_finish(ic);
286}
[6e991a9]287
288void imcb_chat_list_free(struct im_connection *ic)
289{
290        bee_chat_info_t *ci;
291        GSList *l = ic->chatlist;
292
293        while (l) {
294                ci = l->data;
295                l = g_slist_delete_link(l, l);
296
297                g_free(ci->title);
298                g_free(ci->topic);
299                g_free(ci);
300        }
301
302        ic->chatlist = NULL;
303}
Note: See TracBrowser for help on using the repository browser.