source: protocols/jabber/hipchat.c @ fc650a8

Last change on this file since fc650a8 was 0e4c3dd, checked in by dequis <dx@…>, at 2015-02-21T06:18:21Z

Add hipchat support to the jabber module

  • Property mode set to 100644
File size: 5.3 KB
Line 
1/***************************************************************************\
2*                                                                           *
3*  BitlBee - An IRC to IM gateway                                           *
4*  Jabber module - HipChat specific functions                               *
5*                                                                           *
6*  Copyright 2015 Xamarin Inc                                               *
7*                                                                           *
8*  This program is free software; you can redistribute it and/or modify     *
9*  it under the terms of the GNU General Public License as published by     *
10*  the Free Software Foundation; either version 2 of the License, or        *
11*  (at your option) any later version.                                      *
12*                                                                           *
13*  This program is distributed in the hope that it will be useful,          *
14*  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
15*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
16*  GNU General Public License for more details.                             *
17*                                                                           *
18*  You should have received a copy of the GNU General Public License along  *
19*  with this program; if not, write to the Free Software Foundation, Inc.,  *
20*  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.              *
21*                                                                           *
22\***************************************************************************/
23
24#include "jabber.h"
25
26xt_status hipchat_handle_success(struct im_connection *ic, struct xt_node *node)
27{
28        struct jabber_data *jd = ic->proto_data;
29        char *sep, *jid, *muc_host;
30
31        jid = xt_find_attr(node, "jid");
32        muc_host = xt_find_attr(node, "muc_host");
33
34        sep = strchr(jid, '/');
35        if (sep) {
36                *sep = '\0';
37        }
38
39        jabber_set_me(ic, jid);
40        imcb_log(ic, "Setting Hipchat JID to %s", jid);
41
42        if (sep) {
43                *sep = '/';
44        }
45
46        /* Hipchat's auth doesn't expect a restart here */
47        jd->flags &= ~JFLAG_STREAM_RESTART;
48
49        if (!jabber_get_roster(ic)) {
50                return XT_ABORT;
51        }
52        if (!jabber_iq_disco_server(ic)) {
53                return XT_ABORT;
54        }
55        if (!jabber_get_hipchat_profile(ic)) {
56                return XT_ABORT;
57        }
58        if (!jabber_iq_disco_muc(ic, muc_host)) {
59                return XT_ABORT;
60        }
61
62        return XT_HANDLED;
63}
64
65int jabber_get_hipchat_profile(struct im_connection *ic)
66{
67        struct jabber_data *jd = ic->proto_data;
68        struct xt_node *node;
69        int st;
70
71        imcb_log(ic, "Fetching hipchat profile for %s", jd->me);
72
73        node = xt_new_node("query", NULL, NULL);
74        xt_add_attr(node, "xmlns", XMLNS_HIPCHAT_PROFILE);
75        node = jabber_make_packet("iq", "get", jd->me, node);
76
77        jabber_cache_add(ic, node, jabber_parse_hipchat_profile);
78        st = jabber_write_packet(ic, node);
79
80        return st;
81}
82
83xt_status jabber_parse_hipchat_profile(struct im_connection *ic, struct xt_node *node, struct xt_node *orig)
84{
85        struct xt_node *query, *name_node;
86
87        //char *name;
88
89        if (!(query = xt_find_node(node->children, "query"))) {
90                imcb_log(ic, "Warning: Received NULL profile packet");
91                return XT_ABORT;
92        }
93
94        name_node = xt_find_node(query->children, "name");
95        if (!name_node) {
96                imcb_log(ic, "Warning: Can't find real name in profile. Joining groupchats will not be possible.");
97                return XT_ABORT;
98        }
99
100        set_setstr(&ic->acc->set, "display_name", name_node->text);
101        return XT_HANDLED;
102
103}
104
105int jabber_iq_disco_muc(struct im_connection *ic, char *muc_server)
106{
107        struct xt_node *node;
108        int st;
109
110        imcb_log(ic, "Fetching MUC list");
111
112        node = xt_new_node("query", NULL, NULL);
113        xt_add_attr(node, "xmlns", XMLNS_DISCO_ITEMS);
114        node = jabber_make_packet("iq", "get", muc_server, node);
115
116        jabber_cache_add(ic, node, jabber_parse_muc_list);
117        st = jabber_write_packet(ic, node);
118
119        return st;
120}
121
122xt_status jabber_parse_muc_list(struct im_connection *ic, struct xt_node *node, struct xt_node *orig)
123{
124        struct xt_node *query, *c;
125
126        if (!(query = xt_find_node(node->children, "query"))) {
127                imcb_log(ic, "Warning: Received NULL MUC list packet");
128                return XT_HANDLED;
129        }
130
131        c = query->children;
132        while ((c = xt_find_node(c, "item"))) {
133                struct xt_node *c2;
134                struct groupchat *gc;
135                struct irc_channel *ircc;
136                //char *participants = NULL;
137                char *topic = NULL;
138                gboolean new_room = FALSE;
139                char *jid = xt_find_attr(c, "jid");
140                char *name = xt_find_attr(c, "name");
141
142                imcb_log(ic, "Debug: adding MUC to channel list: %s - '%s'", jid, name);
143
144                c2 = xt_find_node_by_attr(c->children, "x", "xmlns", XMLNS_HIPCHAT_MUC);
145
146                if (c2) {
147                        struct xt_node *node;
148                        /*
149                        if ( ( node = xt_find_node( c2->children, "num_participants" ) ) ) {
150                                participants = node->text;
151                        }
152                        */
153                        if ((node = xt_find_node(c2->children, "topic"))) {
154                                topic = node->text;
155                        }
156                }
157
158                gc = bee_chat_by_title(ic->bee, ic, jid);
159                if (!gc) {
160                        gc = imcb_chat_new(ic, jid);
161                        new_room = TRUE;
162                }
163                imcb_chat_name_hint(gc, name);
164                imcb_chat_topic(gc, NULL, topic, 0);
165
166                ircc = gc->ui_data;
167                set_setstr(&ircc->set, "account", ic->acc->tag);
168                set_setstr(&ircc->set, "room", jid);
169                set_setstr(&ircc->set, "chat_type", "room");
170
171                if (new_room) {
172                        /* This cleans everything but leaves the irc channel around,
173                         * since it just graduated to a room.*/
174                        imcb_chat_free(gc);
175                }
176
177                c = c->next;
178        }
179        return XT_HANDLED;
180
181}
Note: See TracBrowser for help on using the repository browser.