comparison mcabber/libjabber/jpacket.c @ 417:c3ae9251c197

Sync libjabber with upstream Sync with jabberd-1.4.4.
author Mikael Berthe <mikael@lilotux.net>
date Thu, 01 Sep 2005 23:29:21 +0200
parents bf3d6e241714
children
comparison
equal deleted inserted replaced
416:48e7808c4191 417:c3ae9251c197
11 * 11 *
12 * You should have received a copy of the GNU General Public License 12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software 13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 * 15 *
16 * Jabber 16 * Copyrights
17 * Copyright (C) 1998-1999 The Jabber Team http://jabber.org/ 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 */
30
31 /**
32 * @file jpacket.c
33 * @brief a jpacket is a wrapper around an xmlnode that contains an XMPP stanza
34 *
35 * A jpacket adds some variables to an xmlnode that contains a stanza, so that
36 * jabberd is able to cache information on the stanza type (message, presence, iq)
37 * that is contained in this jpacket and to further classify the presence stanzas.
38 * It also adds some pointers to important data inside the xmlnode, that has
39 * to be accessed often (e.g. sender and receiver of a stanza).
18 */ 40 */
19 41
20 #include "jabber.h" 42 #include "jabber.h"
21 43
44 /**
45 * create a new jpacket by wrapping an xmlnode
46 *
47 * @param x the xmlnode that should be wrapped
48 * @return the newly created jpacket (NULL on failure)
49 */
22 jpacket jpacket_new(xmlnode x) 50 jpacket jpacket_new(xmlnode x)
23 { 51 {
24 jpacket p; 52 jpacket p;
25 53
26 if(x == NULL) 54 if(x == NULL)
27 return NULL; 55 return NULL;
28 56
29 p = pmalloc(xmlnode_pool(x),sizeof(_jpacket)); 57 p = pmalloc(xmlnode_pool(x),sizeof(_jpacket));
30 p->x = x; 58 p->x = x;
31 59
32 return jpacket_reset(p); 60 return jpacket_reset(p);
33 } 61 }
34 62
63 /**
64 * recalculate the information the jpacket holds about the stanza
65 *
66 * @param p the packet that should get its information recalculated
67 * @return the jpacket (as given as the p parameter)
68 */
35 jpacket jpacket_reset(jpacket p) 69 jpacket jpacket_reset(jpacket p)
36 { 70 {
37 char *val; 71 char *val;
38 xmlnode x; 72 xmlnode x;
39 73
40 x = p->x; 74 x = p->x;
41 memset(p,0,sizeof(_jpacket)); 75 memset(p,0,sizeof(_jpacket));
42 p->x = x; 76 p->x = x;
43 p->p = xmlnode_pool(x); 77 p->p = xmlnode_pool(x);
44 78
45 if(strncmp(xmlnode_get_name(x),"message",7) == 0) 79 if(strncmp(xmlnode_get_name(x),"message",7) == 0) {
46 { 80 p->type = JPACKET_MESSAGE;
47 p->type = JPACKET_MESSAGE; 81 } else if(strncmp(xmlnode_get_name(x),"presence",8) == 0) {
48 }else if(strncmp(xmlnode_get_name(x),"presence",8) == 0) 82 p->type = JPACKET_PRESENCE;
49 { 83 val = xmlnode_get_attrib(x, "type");
50 p->type = JPACKET_PRESENCE; 84 if(val == NULL)
51 val = xmlnode_get_attrib(x, "type"); 85 p->subtype = JPACKET__AVAILABLE;
52 if(val == NULL) 86 else if(strcmp(val,"unavailable") == 0)
53 p->subtype = JPACKET__AVAILABLE; 87 p->subtype = JPACKET__UNAVAILABLE;
54 else if(strcmp(val,"unavailable") == 0) 88 else if(strcmp(val,"probe") == 0)
55 p->subtype = JPACKET__UNAVAILABLE; 89 p->subtype = JPACKET__PROBE;
56 else if(strcmp(val,"probe") == 0) 90 else if(strcmp(val,"error") == 0)
57 p->subtype = JPACKET__PROBE; 91 p->subtype = JPACKET__ERROR;
58 else if(*val == 's' || *val == 'u') 92 else if(strcmp(val,"invisible") == 0)
59 p->type = JPACKET_S10N; 93 p->subtype = JPACKET__INVISIBLE;
60 else if(strcmp(val,"available") == 0) 94 else if(*val == 's' || *val == 'u')
61 { /* someone is using type='available' which is frowned upon */ 95 p->type = JPACKET_S10N;
62 xmlnode_hide_attrib(x,"type"); 96 else if(strcmp(val,"available") == 0) {
63 p->subtype = JPACKET__AVAILABLE; 97 /* someone is using type='available' which is frowned upon */
64 }else 98 /* XXX better reject this presence? */
65 p->type = JPACKET_UNKNOWN; 99 xmlnode_hide_attrib(x,"type");
66 }else if(strncmp(xmlnode_get_name(x),"iq",2) == 0) 100 p->subtype = JPACKET__AVAILABLE;
67 { 101 } else
68 p->type = JPACKET_IQ; 102 p->type = JPACKET_UNKNOWN;
69 p->iq = xmlnode_get_tag(x,"?xmlns"); 103 } else if(strncmp(xmlnode_get_name(x),"iq",2) == 0) {
70 p->iqns = xmlnode_get_attrib(p->iq,"xmlns"); 104 p->type = JPACKET_IQ;
105 p->iq = xmlnode_get_tag(x,"?xmlns");
106 p->iqns = xmlnode_get_attrib(p->iq,"xmlns");
71 } 107 }
72 108
73 /* set up the jids if any, flag packet as unknown if they are unparseable */ 109 /* set up the jids if any, flag packet as unknown if they are unparseable */
74 val = xmlnode_get_attrib(x,"to"); 110 val = xmlnode_get_attrib(x,"to");
75 if(val != NULL) 111 if(val != NULL)
76 if((p->to = jid_new(p->p, val)) == NULL) 112 if((p->to = jid_new(p->p, val)) == NULL)
77 p->type = JPACKET_UNKNOWN; 113 p->type = JPACKET_UNKNOWN;
78 val = xmlnode_get_attrib(x,"from"); 114 val = xmlnode_get_attrib(x,"from");
79 if(val != NULL) 115 if(val != NULL)
80 if((p->from = jid_new(p->p, val)) == NULL) 116 if((p->from = jid_new(p->p, val)) == NULL)
81 p->type = JPACKET_UNKNOWN; 117 p->type = JPACKET_UNKNOWN;
82 118
83 return p; 119 return p;
84 } 120 }
85 121
86 122 /**
123 * get the subtype of a jpacket
124 *
125 * @param p the jpacket for which the caller wants to know the subtype
126 * @return the subtype of the jpacket (one of the JPACKET__* constants)
127 */
87 int jpacket_subtype(jpacket p) 128 int jpacket_subtype(jpacket p)
88 { 129 {
89 char *type; 130 char *type;
90 int ret = p->subtype; 131 int ret = p->subtype;
91 132
92 if(ret != JPACKET__UNKNOWN) 133 if(ret != JPACKET__UNKNOWN)
93 return ret; 134 return ret;
94 135
95 ret = JPACKET__NONE; /* default, when no type attrib is specified */ 136 ret = JPACKET__NONE; /* default, when no type attrib is specified */
96 type = xmlnode_get_attrib(p->x, "type"); 137 type = xmlnode_get_attrib(p->x, "type");
97 if(j_strcmp(type,"error") == 0) 138 if(j_strcmp(type,"error") == 0)
98 ret = JPACKET__ERROR; 139 ret = JPACKET__ERROR;
99 else 140 else
100 switch(p->type) 141 switch(p->type)
101 { 142 {
102 case JPACKET_MESSAGE: 143 case JPACKET_MESSAGE:
103 if(j_strcmp(type,"chat") == 0) 144 if(j_strcmp(type,"chat") == 0)
104 ret = JPACKET__CHAT; 145 ret = JPACKET__CHAT;
105 else if(j_strcmp(type,"groupchat") == 0) 146 else if(j_strcmp(type,"groupchat") == 0)
106 ret = JPACKET__GROUPCHAT; 147 ret = JPACKET__GROUPCHAT;
107 else if(j_strcmp(type,"headline") == 0) 148 else if(j_strcmp(type,"headline") == 0)
108 ret = JPACKET__HEADLINE; 149 ret = JPACKET__HEADLINE;
109 break; 150 break;
110 case JPACKET_S10N: 151 case JPACKET_S10N:
111 if(j_strcmp(type,"subscribe") == 0) 152 if(j_strcmp(type,"subscribe") == 0)
112 ret = JPACKET__SUBSCRIBE; 153 ret = JPACKET__SUBSCRIBE;
113 else if(j_strcmp(type,"subscribed") == 0) 154 else if(j_strcmp(type,"subscribed") == 0)
114 ret = JPACKET__SUBSCRIBED; 155 ret = JPACKET__SUBSCRIBED;
115 else if(j_strcmp(type,"unsubscribe") == 0) 156 else if(j_strcmp(type,"unsubscribe") == 0)
116 ret = JPACKET__UNSUBSCRIBE; 157 ret = JPACKET__UNSUBSCRIBE;
117 else if(j_strcmp(type,"unsubscribed") == 0) 158 else if(j_strcmp(type,"unsubscribed") == 0)
118 ret = JPACKET__UNSUBSCRIBED; 159 ret = JPACKET__UNSUBSCRIBED;
119 break; 160 break;
120 case JPACKET_IQ: 161 case JPACKET_IQ:
121 if(j_strcmp(type,"get") == 0) 162 if(j_strcmp(type,"get") == 0)
122 ret = JPACKET__GET; 163 ret = JPACKET__GET;
123 else if(j_strcmp(type,"set") == 0) 164 else if(j_strcmp(type,"set") == 0)
124 ret = JPACKET__SET; 165 ret = JPACKET__SET;
125 else if(j_strcmp(type,"result") == 0) 166 else if(j_strcmp(type,"result") == 0)
126 ret = JPACKET__RESULT; 167 ret = JPACKET__RESULT;
127 break; 168 break;
128 } 169 }
129 170
130 p->subtype = ret; 171 p->subtype = ret;
131 return ret; 172 return ret;
132 } 173 }