source: protocols/jabber/message.c @ 91ae87d

Last change on this file since 91ae87d was 91ae87d, checked in by Wilmer van der Gaast <wilmer@…>, at 2012-10-28T23:54:21Z

Merging XEP-0184 support patch from Michael Stapelberg, #999.

  • Property mode set to 100644
File size: 6.2 KB
Line 
1/***************************************************************************\
2*                                                                           *
3*  BitlBee - An IRC to IM gateway                                           *
4*  Jabber module - Handling of message(s) (tags), etc                       *
5*                                                                           *
6*  Copyright 2006 Wilmer van der Gaast <wilmer@gaast.net>                   *
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 jabber_pkt_message( struct xt_node *node, gpointer data )
27{
28        struct im_connection *ic = data;
29        char *from = xt_find_attr( node, "from" );
30        char *type = xt_find_attr( node, "type" );
31        char *id = xt_find_attr( node, "id" );
32        struct xt_node *body = xt_find_node( node->children, "body" ), *c;
33        struct xt_node *request = xt_find_node( node->children, "request" );
34        struct jabber_buddy *bud = NULL;
35        char *s, *room = NULL, *reason = NULL;
36       
37        if( !from )
38                return XT_HANDLED; /* Consider this packet corrupted. */
39
40        if( request && id )
41        {
42                /* Send a message receipt (XEP-0184), looking like this:
43                 * <message
44                 *  from='kingrichard@royalty.england.lit/throne'
45                 *  id='bi29sg183b4v'
46                 *  to='northumberland@shakespeare.lit/westminster'>
47                 *  <received xmlns='urn:xmpp:receipts' id='richard2-4.1.247'/>
48                 * </message> */
49                struct xt_node *received, *receipt;
50               
51                received = xt_new_node( "received", NULL, NULL );
52                xt_add_attr( received, "xmlns", XMLNS_RECEIPTS );
53                xt_add_attr( received, "id", id );
54                receipt = jabber_make_packet( "message", NULL, from, received );
55
56                jabber_write_packet( ic, receipt );
57                xt_free_node( receipt );
58        }
59       
60        bud = jabber_buddy_by_jid( ic, from, GET_BUDDY_EXACT );
61       
62        if( type && strcmp( type, "error" ) == 0 )
63        {
64                /* Handle type=error packet. */
65        }
66        else if( type && from && strcmp( type, "groupchat" ) == 0 )
67        {
68                jabber_chat_pkt_message( ic, bud, node );
69        }
70        else /* "chat", "normal", "headline", no-type or whatever. Should all be pretty similar. */
71        {
72                GString *fullmsg = g_string_new( "" );
73
74                for( c = node->children; ( c = xt_find_node( c, "x" ) ); c = c->next )
75                {
76                        char *ns = xt_find_attr( c, "xmlns" );
77                        struct xt_node *inv;
78                       
79                        if( ns && strcmp( ns, XMLNS_MUC_USER ) == 0 &&
80                            ( inv = xt_find_node( c->children, "invite" ) ) )
81                        {
82                                /* This is an invitation. Set some vars which
83                                   will be passed to imcb_chat_invite() below. */
84                                room = from;
85                                if( ( from = xt_find_attr( inv, "from" ) ) == NULL )
86                                        from = room;
87                                if( ( inv = xt_find_node( inv->children, "reason" ) ) && inv->text_len > 0 )
88                                        reason = inv->text;
89                        }
90                }
91               
92                if( ( s = strchr( from, '/' ) ) )
93                {
94                        if( bud )
95                        {
96                                bud->last_msg = time( NULL );
97                                from = bud->ext_jid ? bud->ext_jid : bud->bare_jid;
98                        }
99                        else
100                                *s = 0; /* We need to generate a bare JID now. */
101                }
102               
103                if( type && strcmp( type, "headline" ) == 0 )
104                {
105                        if( ( c = xt_find_node( node->children, "subject" ) ) && c->text_len > 0 )
106                                g_string_append_printf( fullmsg, "Headline: %s\n", c->text );
107                       
108                        /* <x xmlns="jabber:x:oob"><url>http://....</url></x> can contain a URL, it seems. */
109                        for( c = node->children; c; c = c->next )
110                        {
111                                struct xt_node *url;
112                               
113                                if( ( url = xt_find_node( c->children, "url" ) ) && url->text_len > 0 )
114                                        g_string_append_printf( fullmsg, "URL: %s\n", url->text );
115                        }
116                }
117                else if( ( c = xt_find_node( node->children, "subject" ) ) && c->text_len > 0 &&
118                         ( !bud || !( bud->flags & JBFLAG_HIDE_SUBJECT ) ) )
119                {
120                        g_string_append_printf( fullmsg, "<< \002BitlBee\002 - Message with subject: %s >>\n", c->text );
121                        if( bud )
122                                bud->flags |= JBFLAG_HIDE_SUBJECT;
123                }
124                else if( bud && !c )
125                {
126                        /* Yeah, possibly we're hiding changes to this field now. But nobody uses
127                           this for anything useful anyway, except GMail when people reply to an
128                           e-mail via chat, repeating the same subject all the time. I don't want
129                           to have to remember full subject strings for everyone. */
130                        bud->flags &= ~JBFLAG_HIDE_SUBJECT;
131                }
132               
133                if( body && body->text_len > 0 ) /* Could be just a typing notification. */
134                        fullmsg = g_string_append( fullmsg, body->text );
135               
136                if( fullmsg->len > 0 )
137                        imcb_buddy_msg( ic, from, fullmsg->str,
138                                        0, jabber_get_timestamp( node ) );
139                if( room )
140                        imcb_chat_invite( ic, room, from, reason );
141               
142                g_string_free( fullmsg, TRUE );
143               
144                /* Handling of incoming typing notifications. */
145                if( bud == NULL )
146                {
147                        /* Can't handle these for unknown buddies. */
148                }
149                else if( xt_find_node( node->children, "composing" ) )
150                {
151                        bud->flags |= JBFLAG_DOES_XEP85;
152                        imcb_buddy_typing( ic, from, OPT_TYPING );
153                }
154                /* No need to send a "stopped typing" signal when there's a message. */
155                else if( xt_find_node( node->children, "active" ) && ( body == NULL ) )
156                {
157                        bud->flags |= JBFLAG_DOES_XEP85;
158                        imcb_buddy_typing( ic, from, 0 );
159                }
160                else if( xt_find_node( node->children, "paused" ) )
161                {
162                        bud->flags |= JBFLAG_DOES_XEP85;
163                        imcb_buddy_typing( ic, from, OPT_THINKING );
164                }
165               
166                if( s )
167                        *s = '/'; /* And convert it back to a full JID. */
168        }
169       
170        return XT_HANDLED;
171}
Note: See TracBrowser for help on using the repository browser.