source: protocols/bee_chat.c @ 29ff5c2

Last change on this file since 29ff5c2 was 29ff5c2, checked in by dequis <dx@…>, at 2015-11-21T00:01:50Z

Merge branch 'master' into feat/hip-cat

  • Property mode set to 100644
File size: 6.9 KB
Line 
1/********************************************************************\
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;
22  if not, write to the Free Software Foundation, Inc., 51 Franklin St.,
23  Fifth Floor, Boston, MA  02110-1301  USA
24*/
25
26#define BITLBEE_CORE
27#include "bitlbee.h"
28
29struct groupchat *imcb_chat_new(struct im_connection *ic, const char *handle)
30{
31        struct groupchat *c = g_new0(struct groupchat, 1);
32        bee_t *bee = ic->bee;
33
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.) */
37
38        ic->groupchats = g_slist_prepend(ic->groupchats, c);
39        c->ic = ic;
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
53        return c;
54}
55
56void imcb_chat_placeholder_new(struct im_connection *ic, const char *handle, const char *name, const char *topic)
57{
58        bee_t *bee = ic->bee;
59
60        if (bee->ui->chat_placeholder_new) {
61                bee->ui->chat_placeholder_new(bee, ic, handle, name, topic);
62        }
63}
64
65void imcb_chat_name_hint(struct groupchat *c, const char *name)
66{
67        bee_t *bee = c->ic->bee;
68
69        if (bee->ui->chat_name_hint) {
70                bee->ui->chat_name_hint(bee, c, name);
71        }
72}
73
74void imcb_chat_free(struct groupchat *c)
75{
76        struct im_connection *ic = c->ic;
77        bee_t *bee = ic->bee;
78        GList *ir;
79
80        if (bee->ui->chat_free) {
81                bee->ui->chat_free(bee, c);
82        }
83
84        if (set_getbool(&ic->bee->set, "debug")) {
85                imcb_log(ic, "You were removed from conversation %p", c);
86        }
87
88        ic->groupchats = g_slist_remove(ic->groupchats, c);
89
90        for (ir = c->in_room; ir; ir = ir->next) {
91                g_free(ir->data);
92        }
93        g_list_free(c->in_room);
94        g_free(c->title);
95        g_free(c->topic);
96        g_free(c);
97}
98
99static gboolean handle_is_self(struct im_connection *ic, const char *handle)
100{
101        return (ic->acc->prpl->handle_is_self) ?
102               ic->acc->prpl->handle_is_self(ic, handle) :
103               (ic->acc->prpl->handle_cmp(ic->acc->user, handle) == 0);
104}
105
106void imcb_chat_msg(struct groupchat *c, const char *who, char *msg, guint32 flags, time_t sent_at)
107{
108        struct im_connection *ic = c->ic;
109        bee_t *bee = ic->bee;
110        bee_user_t *bu;
111        gboolean temp = FALSE;
112        char *s;
113
114        if (handle_is_self(ic, who) && !(flags & OPT_SELFMESSAGE)) {
115                return;
116        }
117
118        bu = bee_user_by_handle(bee, ic, who);
119        temp = (bu == NULL);
120
121        if (temp) {
122                bu = bee_user_new(bee, ic, who, BEE_USER_ONLINE);
123        }
124
125        s = set_getstr(&ic->bee->set, "strip_html");
126        if ((g_strcasecmp(s, "always") == 0) ||
127            ((ic->flags & OPT_DOES_HTML) && s)) {
128                strip_html(msg);
129        }
130
131        if (bee->ui->chat_msg) {
132                bee->ui->chat_msg(bee, c, bu, msg, flags, sent_at);
133        }
134
135        if (temp) {
136                bee_user_free(bee, bu);
137        }
138}
139
140void imcb_chat_log(struct groupchat *c, char *format, ...)
141{
142        struct im_connection *ic = c->ic;
143        bee_t *bee = ic->bee;
144        va_list params;
145        char *text;
146
147        if (!bee->ui->chat_log) {
148                return;
149        }
150
151        va_start(params, format);
152        text = g_strdup_vprintf(format, params);
153        va_end(params);
154
155        bee->ui->chat_log(bee, c, text);
156        g_free(text);
157}
158
159void imcb_chat_topic(struct groupchat *c, char *who, char *topic, time_t set_at)
160{
161        struct im_connection *ic = c->ic;
162        bee_t *bee = ic->bee;
163        bee_user_t *bu;
164
165        if (!bee->ui->chat_topic) {
166                return;
167        }
168
169        if (who == NULL) {
170                bu = NULL;
171        } else if (handle_is_self(ic, who)) {
172                bu = bee->user;
173        } else {
174                bu = bee_user_by_handle(bee, ic, who);
175        }
176
177        if ((g_strcasecmp(set_getstr(&ic->bee->set, "strip_html"), "always") == 0) ||
178            ((ic->flags & OPT_DOES_HTML) && set_getbool(&ic->bee->set, "strip_html"))) {
179                strip_html(topic);
180        }
181
182        bee->ui->chat_topic(bee, c, topic, bu);
183}
184
185void imcb_chat_add_buddy(struct groupchat *c, const char *handle)
186{
187        struct im_connection *ic = c->ic;
188        bee_t *bee = ic->bee;
189        bee_user_t *bu = bee_user_by_handle(bee, ic, handle);
190        gboolean me;
191
192        if (set_getbool(&c->ic->bee->set, "debug")) {
193                imcb_log(c->ic, "User %s added to conversation %p", handle, c);
194        }
195
196        me = handle_is_self(ic, handle);
197
198        /* Most protocols allow people to join, even when they're not in
199           your contact list. Try to handle that here */
200        if (!me && !bu) {
201                bu = bee_user_new(bee, ic, handle, BEE_USER_LOCAL);
202        }
203
204        /* Add the handle to the room userlist */
205        /* TODO: Use bu instead of a string */
206        c->in_room = g_list_append(c->in_room, g_strdup(handle));
207
208        if (bee->ui->chat_add_user) {
209                bee->ui->chat_add_user(bee, c, me ? bee->user : bu);
210        }
211
212        if (me) {
213                c->joined = 1;
214        }
215}
216
217void imcb_chat_remove_buddy(struct groupchat *c, const char *handle, const char *reason)
218{
219        struct im_connection *ic = c->ic;
220        bee_t *bee = ic->bee;
221        bee_user_t *bu = NULL;
222
223        if (set_getbool(&bee->set, "debug")) {
224                imcb_log(ic, "User %s removed from conversation %p (%s)", handle, c, reason ? reason : "");
225        }
226
227        /* It might be yourself! */
228        if (handle_is_self(ic, handle)) {
229                if (c->joined == 0) {
230                        return;
231                }
232
233                bu = bee->user;
234                c->joined = 0;
235        } else {
236                bu = bee_user_by_handle(bee, ic, handle);
237        }
238
239        if (bee->ui->chat_remove_user && bu) {
240                bee->ui->chat_remove_user(bee, c, bu, reason);
241        }
242}
243
244int bee_chat_msg(bee_t *bee, struct groupchat *c, const char *msg, int flags)
245{
246        struct im_connection *ic = c->ic;
247        char *buf = NULL;
248
249        if ((ic->flags & OPT_DOES_HTML) && (g_strncasecmp(msg, "<html>", 6) != 0)) {
250                buf = escape_html(msg);
251                msg = buf;
252        } else {
253                buf = g_strdup(msg);
254        }
255
256        ic->acc->prpl->chat_msg(c, buf, flags);
257        g_free(buf);
258
259        return 1;
260}
261
262struct groupchat *bee_chat_by_title(bee_t *bee, struct im_connection *ic, const char *title)
263{
264        struct groupchat *c;
265        GSList *l;
266
267        for (l = ic->groupchats; l; l = l->next) {
268                c = l->data;
269                if (strcmp(c->title, title) == 0) {
270                        return c;
271                }
272        }
273
274        return NULL;
275}
276
277void imcb_chat_invite(struct im_connection *ic, const char *name, const char *who, const char *msg)
278{
279        bee_user_t *bu = bee_user_by_handle(ic->bee, ic, who);
280
281        if (bu && ic->bee->ui->chat_invite) {
282                ic->bee->ui->chat_invite(ic->bee, bu, name, msg);
283        }
284}
Note: See TracBrowser for help on using the repository browser.