source: protocols/jabber/jpacket.c @ 3e1de87

Last change on this file since 3e1de87 was b7d3cc34, checked in by Wilmer van der Gaast <wilmer@…>, at 2005-11-06T18:23:18Z

Initial repository (0.99 release tree)

  • Property mode set to 100644
File size: 5.2 KB
Line 
1/* --------------------------------------------------------------------------
2 *
3 * License
4 *
5 * The contents of this file are subject to the Jabber Open Source License
6 * Version 1.0 (the "JOSL").  You may not copy or use this file, in either
7 * source code or executable form, except in compliance with the JOSL. You
8 * may obtain a copy of the JOSL at http://www.jabber.org/ or at
9 * http://www.opensource.org/. 
10 *
11 * Software distributed under the JOSL is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied.  See the JOSL
13 * for the specific language governing rights and limitations under the
14 * JOSL.
15 *
16 * Copyrights
17 *
18 * Portions created by or assigned to Jabber.com, Inc. are
19 * Copyright (c) 1999-2002 Jabber.com, Inc.  All Rights Reserved.  Contact
20 * information for Jabber.com, Inc. is available at http://www.jabber.com/.
21 *
22 * Portions Copyright (c) 1998-1999 Jeremie Miller.
23 *
24 * Acknowledgements
25 *
26 * Special thanks to the Jabber Open Source Contributors for their
27 * suggestions and support of Jabber.
28 *
29 * Alternatively, the contents of this file may be used under the terms of the
30 * GNU General Public License Version 2 or later (the "GPL"), in which case
31 * the provisions of the GPL are applicable instead of those above.  If you
32 * wish to allow use of your version of this file only under the terms of the
33 * GPL and not to allow others to use your version of this file under the JOSL,
34 * indicate your decision by deleting the provisions above and replace them
35 * with the notice and other provisions required by the GPL.  If you do not
36 * delete the provisions above, a recipient may use your version of this file
37 * under either the JOSL or the GPL.
38 *
39 *
40 * --------------------------------------------------------------------------*/
41
42#include "jabber.h"
43static jpacket jpacket_reset(jpacket p);
44
45jpacket jpacket_new(xmlnode x)
46{
47    jpacket p;
48
49    if(x == NULL)
50        return NULL;
51
52    p = pmalloc(xmlnode_pool(x),sizeof(_jpacket));
53    p->x = x;
54
55    return jpacket_reset(p);
56}
57
58static jpacket jpacket_reset(jpacket p)
59{
60    char *val;
61    xmlnode x;
62
63    x = p->x;
64    memset(p,0,sizeof(_jpacket));
65    p->x = x;
66    p->p = xmlnode_pool(x);
67
68    if(strncmp(xmlnode_get_name(x),"message",7) == 0)
69    {
70        p->type = JPACKET_MESSAGE;
71    }else if(strncmp(xmlnode_get_name(x),"presence",8) == 0)
72    {
73        p->type = JPACKET_PRESENCE;
74        val = xmlnode_get_attrib(x, "type");
75        if(val == NULL)
76            p->subtype = JPACKET__AVAILABLE;
77        else if(strcmp(val,"unavailable") == 0)
78            p->subtype = JPACKET__UNAVAILABLE;
79        else if(strcmp(val,"probe") == 0)
80            p->subtype = JPACKET__PROBE;
81        else if(strcmp(val,"error") == 0)
82            p->subtype = JPACKET__ERROR;
83        else if(strcmp(val,"invisible") == 0)
84            p->subtype = JPACKET__INVISIBLE;
85        else if(*val == 's' || *val == 'u')
86            p->type = JPACKET_S10N;
87        else if(strcmp(val,"available") == 0)
88        { /* someone is using type='available' which is frowned upon */
89            xmlnode_hide_attrib(x,"type");
90            p->subtype = JPACKET__AVAILABLE;
91        }else
92            p->type = JPACKET_UNKNOWN;
93    }else if(strncmp(xmlnode_get_name(x),"iq",2) == 0)
94    {
95        p->type = JPACKET_IQ;
96        p->iq = xmlnode_get_tag(x,"?xmlns");
97        p->iqns = xmlnode_get_attrib(p->iq,"xmlns");
98    }
99
100    /* set up the jids if any, flag packet as unknown if they are unparseable */
101    val = xmlnode_get_attrib(x,"to");
102    if(val != NULL)
103        if((p->to = jid_new(p->p, val)) == NULL)
104            p->type = JPACKET_UNKNOWN;
105    val = xmlnode_get_attrib(x,"from");
106    if(val != NULL)
107        if((p->from = jid_new(p->p, val)) == NULL)
108            p->type = JPACKET_UNKNOWN;
109
110    return p;
111}
112
113
114int jpacket_subtype(jpacket p)
115{
116    char *type;
117    int ret = p->subtype;
118
119    if(ret != JPACKET__UNKNOWN)
120        return ret;
121
122    ret = JPACKET__NONE; /* default, when no type attrib is specified */
123    type = xmlnode_get_attrib(p->x, "type");
124    if(j_strcmp(type,"error") == 0)
125        ret = JPACKET__ERROR;
126    else
127        switch(p->type)
128        {
129        case JPACKET_MESSAGE:
130            if(j_strcmp(type,"chat") == 0)
131                ret = JPACKET__CHAT;
132            else if(j_strcmp(type,"groupchat") == 0)
133                ret = JPACKET__GROUPCHAT;
134            else if(j_strcmp(type,"headline") == 0)
135                ret = JPACKET__HEADLINE;
136            break;
137        case JPACKET_S10N:
138            if(j_strcmp(type,"subscribe") == 0)
139                ret = JPACKET__SUBSCRIBE;
140            else if(j_strcmp(type,"subscribed") == 0)
141                ret = JPACKET__SUBSCRIBED;
142            else if(j_strcmp(type,"unsubscribe") == 0)
143                ret = JPACKET__UNSUBSCRIBE;
144            else if(j_strcmp(type,"unsubscribed") == 0)
145                ret = JPACKET__UNSUBSCRIBED;
146            break;
147        case JPACKET_IQ:
148            if(j_strcmp(type,"get") == 0)
149                ret = JPACKET__GET;
150            else if(j_strcmp(type,"set") == 0)
151                ret = JPACKET__SET;
152            else if(j_strcmp(type,"result") == 0)
153                ret = JPACKET__RESULT;
154            break;
155        }
156
157    p->subtype = ret;
158    return ret;
159}
Note: See TracBrowser for help on using the repository browser.