Mercurial > hg
annotate mcabber/libjabber/jpacket.c @ 430:d03663d2e7d9
Display error messages as specified in RFC3920 (9.3)
If possible, we display the child element corresponding to the stanza
error conditions defined in RFC3920. Error code and error text will
be displayed if available.
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Sun, 11 Sep 2005 22:01:57 +0200 |
parents | c3ae9251c197 |
children |
rev | line source |
---|---|
25 | 1 /* |
2 * This program is free software; you can redistribute it and/or modify | |
3 * it under the terms of the GNU General Public License as published by | |
4 * the Free Software Foundation; either version 2 of the License, or | |
5 * (at your option) any later version. | |
6 * | |
7 * This program is distributed in the hope that it will be useful, | |
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
10 * GNU General Public License for more details. | |
11 * | |
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 | |
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
15 * | |
417
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
16 * Copyrights |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
17 * |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
18 * Portions created by or assigned to Jabber.com, Inc. are |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
19 * Copyright (c) 1999-2002 Jabber.com, Inc. All Rights Reserved. Contact |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
20 * information for Jabber.com, Inc. is available at http://www.jabber.com/. |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
21 * |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
22 * Portions Copyright (c) 1998-1999 Jeremie Miller. |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
23 * |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
24 * Acknowledgements |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
25 * |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
26 * Special thanks to the Jabber Open Source Contributors for their |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
27 * suggestions and support of Jabber. |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
28 * |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
29 */ |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
30 |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
31 /** |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
32 * @file jpacket.c |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
33 * @brief a jpacket is a wrapper around an xmlnode that contains an XMPP stanza |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
34 * |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
35 * A jpacket adds some variables to an xmlnode that contains a stanza, so that |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
36 * jabberd is able to cache information on the stanza type (message, presence, iq) |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
37 * that is contained in this jpacket and to further classify the presence stanzas. |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
38 * It also adds some pointers to important data inside the xmlnode, that has |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
39 * to be accessed often (e.g. sender and receiver of a stanza). |
25 | 40 */ |
41 | |
42 #include "jabber.h" | |
43 | |
417
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
44 /** |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
45 * create a new jpacket by wrapping an xmlnode |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
46 * |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
47 * @param x the xmlnode that should be wrapped |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
48 * @return the newly created jpacket (NULL on failure) |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
49 */ |
25 | 50 jpacket jpacket_new(xmlnode x) |
51 { | |
52 jpacket p; | |
53 | |
54 if(x == NULL) | |
417
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
55 return NULL; |
25 | 56 |
57 p = pmalloc(xmlnode_pool(x),sizeof(_jpacket)); | |
58 p->x = x; | |
59 | |
60 return jpacket_reset(p); | |
61 } | |
62 | |
417
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
63 /** |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
64 * recalculate the information the jpacket holds about the stanza |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
65 * |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
66 * @param p the packet that should get its information recalculated |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
67 * @return the jpacket (as given as the p parameter) |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
68 */ |
25 | 69 jpacket jpacket_reset(jpacket p) |
70 { | |
71 char *val; | |
72 xmlnode x; | |
73 | |
74 x = p->x; | |
75 memset(p,0,sizeof(_jpacket)); | |
76 p->x = x; | |
77 p->p = xmlnode_pool(x); | |
78 | |
417
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
79 if(strncmp(xmlnode_get_name(x),"message",7) == 0) { |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
80 p->type = JPACKET_MESSAGE; |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
81 } else if(strncmp(xmlnode_get_name(x),"presence",8) == 0) { |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
82 p->type = JPACKET_PRESENCE; |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
83 val = xmlnode_get_attrib(x, "type"); |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
84 if(val == NULL) |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
85 p->subtype = JPACKET__AVAILABLE; |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
86 else if(strcmp(val,"unavailable") == 0) |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
87 p->subtype = JPACKET__UNAVAILABLE; |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
88 else if(strcmp(val,"probe") == 0) |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
89 p->subtype = JPACKET__PROBE; |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
90 else if(strcmp(val,"error") == 0) |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
91 p->subtype = JPACKET__ERROR; |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
92 else if(strcmp(val,"invisible") == 0) |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
93 p->subtype = JPACKET__INVISIBLE; |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
94 else if(*val == 's' || *val == 'u') |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
95 p->type = JPACKET_S10N; |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
96 else if(strcmp(val,"available") == 0) { |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
97 /* someone is using type='available' which is frowned upon */ |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
98 /* XXX better reject this presence? */ |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
99 xmlnode_hide_attrib(x,"type"); |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
100 p->subtype = JPACKET__AVAILABLE; |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
101 } else |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
102 p->type = JPACKET_UNKNOWN; |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
103 } else if(strncmp(xmlnode_get_name(x),"iq",2) == 0) { |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
104 p->type = JPACKET_IQ; |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
105 p->iq = xmlnode_get_tag(x,"?xmlns"); |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
106 p->iqns = xmlnode_get_attrib(p->iq,"xmlns"); |
25 | 107 } |
108 | |
109 /* set up the jids if any, flag packet as unknown if they are unparseable */ | |
110 val = xmlnode_get_attrib(x,"to"); | |
111 if(val != NULL) | |
417
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
112 if((p->to = jid_new(p->p, val)) == NULL) |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
113 p->type = JPACKET_UNKNOWN; |
25 | 114 val = xmlnode_get_attrib(x,"from"); |
115 if(val != NULL) | |
417
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
116 if((p->from = jid_new(p->p, val)) == NULL) |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
117 p->type = JPACKET_UNKNOWN; |
25 | 118 |
119 return p; | |
120 } | |
121 | |
417
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
122 /** |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
123 * get the subtype of a jpacket |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
124 * |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
125 * @param p the jpacket for which the caller wants to know the subtype |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
126 * @return the subtype of the jpacket (one of the JPACKET__* constants) |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
127 */ |
25 | 128 int jpacket_subtype(jpacket p) |
129 { | |
130 char *type; | |
131 int ret = p->subtype; | |
132 | |
133 if(ret != JPACKET__UNKNOWN) | |
417
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
134 return ret; |
25 | 135 |
136 ret = JPACKET__NONE; /* default, when no type attrib is specified */ | |
137 type = xmlnode_get_attrib(p->x, "type"); | |
138 if(j_strcmp(type,"error") == 0) | |
417
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
139 ret = JPACKET__ERROR; |
25 | 140 else |
417
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
141 switch(p->type) |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
142 { |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
143 case JPACKET_MESSAGE: |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
144 if(j_strcmp(type,"chat") == 0) |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
145 ret = JPACKET__CHAT; |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
146 else if(j_strcmp(type,"groupchat") == 0) |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
147 ret = JPACKET__GROUPCHAT; |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
148 else if(j_strcmp(type,"headline") == 0) |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
149 ret = JPACKET__HEADLINE; |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
150 break; |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
151 case JPACKET_S10N: |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
152 if(j_strcmp(type,"subscribe") == 0) |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
153 ret = JPACKET__SUBSCRIBE; |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
154 else if(j_strcmp(type,"subscribed") == 0) |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
155 ret = JPACKET__SUBSCRIBED; |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
156 else if(j_strcmp(type,"unsubscribe") == 0) |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
157 ret = JPACKET__UNSUBSCRIBE; |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
158 else if(j_strcmp(type,"unsubscribed") == 0) |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
159 ret = JPACKET__UNSUBSCRIBED; |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
160 break; |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
161 case JPACKET_IQ: |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
162 if(j_strcmp(type,"get") == 0) |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
163 ret = JPACKET__GET; |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
164 else if(j_strcmp(type,"set") == 0) |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
165 ret = JPACKET__SET; |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
166 else if(j_strcmp(type,"result") == 0) |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
167 ret = JPACKET__RESULT; |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
168 break; |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
169 } |
25 | 170 |
171 p->subtype = ret; | |
172 return ret; | |
173 } |