1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
/***************************************************************************\
* *
* BitlBee - An IRC to IM gateway *
* Jabber module - Handling of presence (tags), etc *
* *
* Copyright 2006 Wilmer van der Gaast <wilmer@gaast.net> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License along *
* with this program; if not, write to the Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
* *
\***************************************************************************/
#include "jabber.h"
xt_status jabber_pkt_presence(struct xt_node *node, gpointer data)
{
struct im_connection *ic = data;
char *from = xt_find_attr(node, "from");
char *type = xt_find_attr(node, "type"); /* NULL should mean the person is online. */
struct xt_node *c, *cap;
struct jabber_buddy *bud, *send_presence = NULL;
int is_chat = 0;
char *s;
if (!from) {
return XT_HANDLED;
}
if ((s = strchr(from, '/'))) {
*s = 0;
if (jabber_chat_by_jid(ic, from)) {
is_chat = 1;
}
*s = '/';
}
if (type == NULL) {
if (!(bud = jabber_buddy_by_jid(ic, from, GET_BUDDY_EXACT | GET_BUDDY_CREAT))) {
/*
imcb_log( ic, "Warning: Could not handle presence information from JID: %s", from );
*/
return XT_HANDLED;
}
g_free(bud->away_message);
if ((c = xt_find_node(node->children, "status")) && c->text_len > 0) {
bud->away_message = g_strdup(c->text);
} else {
bud->away_message = NULL;
}
if ((c = xt_find_node(node->children, "show")) && c->text_len > 0) {
bud->away_state = (void *) jabber_away_state_by_code(c->text);
} else {
bud->away_state = NULL;
}
if ((c = xt_find_node(node->children, "priority")) && c->text_len > 0) {
bud->priority = atoi(c->text);
} else {
bud->priority = 0;
}
if (bud && (cap = xt_find_node(node->children, "c")) &&
(s = xt_find_attr(cap, "xmlns")) && strcmp(s, XMLNS_CAPS) == 0) {
/* This <presence> stanza includes an XEP-0115
capabilities part. Not too interesting, but we can
see if it has an ext= attribute. */
s = xt_find_attr(cap, "ext");
if (s && (strstr(s, "cstates") || strstr(s, "chatstate"))) {
bud->flags |= JBFLAG_DOES_XEP85;
}
/* This field can contain more information like xhtml
support, but we don't support that ourselves.
Officially the ext= tag was deprecated, but enough
clients do send it.
(I'm aware that this is not the right way to use
this field.) See for an explanation of ext=:
http://www.xmpp.org/extensions/attic/xep-0115-1.3.html*/
}
if (is_chat) {
jabber_chat_pkt_presence(ic, bud, node);
} else {
send_presence = jabber_buddy_by_jid(ic, bud->bare_jid, 0);
}
} else if (strcmp(type, "unavailable") == 0) {
if ((bud = jabber_buddy_by_jid(ic, from, 0)) == NULL) {
/*
imcb_log( ic, "Warning: Received presence information from unknown JID: %s", from );
*/
return XT_HANDLED;
}
/* Handle this before we delete the JID. */
if (is_chat) {
jabber_chat_pkt_presence(ic, bud, node);
}
if (strchr(from, '/') == NULL) {
/* Sometimes servers send a type="unavailable" from a
bare JID, which should mean that suddenly all
resources for this JID disappeared. */
jabber_buddy_remove_bare(ic, from);
} else {
jabber_buddy_remove(ic, from);
}
if (is_chat) {
/* Nothing else to do for now? */
} else if ((s = strchr(from, '/'))) {
*s = 0;
/* If another resource is still available, send its presence
information. */
if ((send_presence = jabber_buddy_by_jid(ic, from, 0)) /********************************************************************\
* BitlBee -- An IRC to other IM-networks gateway *
* *
* Copyright 2002-2010 Wilmer van der Gaast and others *
\********************************************************************/
/* Some glue to put the IRC and the IM stuff together. */
/*
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License with
the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
if not, write to the Free Software Foundation, Inc., 59 Temple Place,
Suite 330, Boston, MA 02111-1307 USA
*/
#include "bitlbee.h"
#include "dcc.h"
/* IM->IRC callbacks: Simple IM/buddy-related stuff. */
static const struct irc_user_funcs irc_user_im_funcs;
static void bee_irc_imc_connected( struct im_connection *ic )
{
irc_t *irc = (irc_t*) ic->bee->ui_data;
irc_channel_auto_joins( irc, ic->acc );
}
static void bee_irc_imc_disconnected( struct im_connection *ic )
{
/* Maybe try to send /QUITs here instead of later on. */
}
static gboolean bee_irc_user_new( bee_t *bee, bee_user_t *bu )
{
irc_user_t *iu;
irc_t *irc = (irc_t*) bee->ui_data;
char nick[MAX_NICK_LENGTH+1], *s;
memset( nick, 0, MAX_NICK_LENGTH + 1 );
strcpy( nick, nick_get( bu ) );
bu->ui_data = iu = irc_user_new( irc, nick );
iu->bu = bu;
if( set_getbool( &irc->b->set, "private" ) )
iu->last_channel = NULL;
else
iu->last_channel = irc_channel_with_user( irc, iu );
if( ( s = strchr( bu->handle, '@' ) ) )
{
iu->host = g_strdup( s + 1 );
iu->user = g_strndup( bu->handle, s - bu->handle );
}
else
{
iu->user = g_strdup( bu->handle );
if( bu->ic->acc->server )
iu->host = g_strdup( bu->ic->acc->server );
else
iu->host = g_strdup( bu->ic->acc->prpl->name );
}
while( ( s = strchr( iu->user, ' ' ) ) )
*s = '_';
if( bu->flags & BEE_USER_LOCAL )
{
char *s = set_getstr( &bee->set, "handle_unknown" );
if( strcmp( s, "add_private" ) == 0 )
iu->last_channel = NULL;
else if( strcmp( s, "add_channel" ) == 0 )
iu->last_channel = irc->default_channel;
}
iu->f = &irc_user_im_funcs;
return TRUE;
}
static gboolean bee_irc_user_free( bee_t *bee, bee_user_t *bu )
{
return irc_user_free( bee->ui_data, (irc_user_t *) bu->ui_data );
}
static gboolean bee_irc_user_status( bee_t *bee, bee_user_t *bu, bee_user_t *old )
{
irc_t *irc = bee->ui_data;
irc_user_t *iu = bu->ui_data;
/* Do this outside the if below since away state can change without
the online state changing. */
iu->flags &= ~IRC_USER_AWAY;
if( bu->flags &
|