3127
|
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 "lib.h"
|
2086
|
43
|
|
44 /* util for making presence packets */
|
|
45 xmlnode jutil_presnew(int type, char *to, char *status)
|
|
46 {
|
|
47 xmlnode pres;
|
|
48
|
|
49 pres = xmlnode_new_tag("presence");
|
|
50 switch(type)
|
|
51 {
|
|
52 case JPACKET__SUBSCRIBE:
|
|
53 xmlnode_put_attrib(pres,"type","subscribe");
|
|
54 break;
|
|
55 case JPACKET__UNSUBSCRIBE:
|
|
56 xmlnode_put_attrib(pres,"type","unsubscribe");
|
|
57 break;
|
|
58 case JPACKET__SUBSCRIBED:
|
|
59 xmlnode_put_attrib(pres,"type","subscribed");
|
|
60 break;
|
|
61 case JPACKET__UNSUBSCRIBED:
|
|
62 xmlnode_put_attrib(pres,"type","unsubscribed");
|
|
63 break;
|
|
64 case JPACKET__PROBE:
|
|
65 xmlnode_put_attrib(pres,"type","probe");
|
|
66 break;
|
|
67 case JPACKET__UNAVAILABLE:
|
|
68 xmlnode_put_attrib(pres,"type","unavailable");
|
|
69 break;
|
3127
|
70 case JPACKET__INVISIBLE:
|
|
71 xmlnode_put_attrib(pres,"type","invisible");
|
|
72 break;
|
2086
|
73 }
|
|
74 if(to != NULL)
|
|
75 xmlnode_put_attrib(pres,"to",to);
|
|
76 if(status != NULL)
|
|
77 xmlnode_insert_cdata(xmlnode_insert_tag(pres,"status"),status,strlen(status));
|
|
78
|
|
79 return pres;
|
|
80 }
|
|
81
|
|
82 /* util for making IQ packets */
|
|
83 xmlnode jutil_iqnew(int type, char *ns)
|
|
84 {
|
|
85 xmlnode iq;
|
|
86
|
|
87 iq = xmlnode_new_tag("iq");
|
|
88 switch(type)
|
|
89 {
|
|
90 case JPACKET__GET:
|
|
91 xmlnode_put_attrib(iq,"type","get");
|
|
92 break;
|
|
93 case JPACKET__SET:
|
|
94 xmlnode_put_attrib(iq,"type","set");
|
|
95 break;
|
|
96 case JPACKET__RESULT:
|
|
97 xmlnode_put_attrib(iq,"type","result");
|
|
98 break;
|
|
99 case JPACKET__ERROR:
|
|
100 xmlnode_put_attrib(iq,"type","error");
|
|
101 break;
|
|
102 }
|
|
103 xmlnode_put_attrib(xmlnode_insert_tag(iq,"query"),"xmlns",ns);
|
|
104
|
|
105 return iq;
|
|
106 }
|
|
107
|
|
108 /* util for making message packets */
|
|
109 xmlnode jutil_msgnew(char *type, char *to, char *subj, char *body)
|
|
110 {
|
|
111 xmlnode msg;
|
|
112
|
|
113 msg = xmlnode_new_tag("message");
|
|
114 xmlnode_put_attrib (msg, "type", type);
|
|
115 xmlnode_put_attrib (msg, "to", to);
|
|
116
|
|
117 if (subj)
|
|
118 {
|
|
119 xmlnode_insert_cdata (xmlnode_insert_tag (msg, "subject"), subj, strlen (subj));
|
|
120 }
|
|
121
|
|
122 xmlnode_insert_cdata (xmlnode_insert_tag (msg, "body"), body, strlen (body));
|
|
123
|
|
124 return msg;
|
|
125 }
|
|
126
|
|
127 /* util for making stream packets */
|
|
128 xmlnode jutil_header(char* xmlns, char* server)
|
|
129 {
|
|
130 xmlnode result;
|
|
131 if ((xmlns == NULL)||(server == NULL))
|
|
132 return NULL;
|
|
133 result = xmlnode_new_tag("stream:stream");
|
|
134 xmlnode_put_attrib(result, "xmlns:stream", "http://etherx.jabber.org/streams");
|
|
135 xmlnode_put_attrib(result, "xmlns", xmlns);
|
|
136 xmlnode_put_attrib(result, "to", server);
|
|
137
|
|
138 return result;
|
|
139 }
|
|
140
|
|
141 /* returns the priority on a presence packet */
|
|
142 int jutil_priority(xmlnode x)
|
|
143 {
|
|
144 char *str;
|
|
145 int p;
|
|
146
|
|
147 if(x == NULL)
|
|
148 return -1;
|
|
149
|
|
150 if(xmlnode_get_attrib(x,"type") != NULL)
|
|
151 return -1;
|
|
152
|
|
153 x = xmlnode_get_tag(x,"priority");
|
|
154 if(x == NULL)
|
|
155 return 0;
|
|
156
|
|
157 str = xmlnode_get_data((x));
|
|
158 if(str == NULL)
|
|
159 return 0;
|
|
160
|
|
161 p = atoi(str);
|
|
162 if(p >= 0)
|
|
163 return p;
|
|
164 else
|
|
165 return 0;
|
|
166 }
|
|
167
|
|
168 void jutil_tofrom(xmlnode x)
|
|
169 {
|
|
170 char *to, *from;
|
|
171
|
|
172 to = xmlnode_get_attrib(x,"to");
|
|
173 from = xmlnode_get_attrib(x,"from");
|
|
174 xmlnode_put_attrib(x,"from",to);
|
|
175 xmlnode_put_attrib(x,"to",from);
|
|
176 }
|
|
177
|
|
178 xmlnode jutil_iqresult(xmlnode x)
|
|
179 {
|
|
180 xmlnode cur;
|
|
181
|
|
182 jutil_tofrom(x);
|
|
183
|
|
184 xmlnode_put_attrib(x,"type","result");
|
|
185
|
|
186 /* hide all children of the iq, they go back empty */
|
|
187 for(cur = xmlnode_get_firstchild(x); cur != NULL; cur = xmlnode_get_nextsibling(cur))
|
|
188 xmlnode_hide(cur);
|
|
189
|
|
190 return x;
|
|
191 }
|
|
192
|
|
193 char *jutil_timestamp(void)
|
|
194 {
|
|
195 time_t t;
|
|
196 struct tm *new_time;
|
|
197 static char timestamp[18];
|
|
198 int ret;
|
|
199
|
|
200 t = time(NULL);
|
|
201
|
|
202 if(t == (time_t)-1)
|
|
203 return NULL;
|
|
204 new_time = gmtime(&t);
|
|
205
|
|
206 ret = snprintf(timestamp, 18, "%d%02d%02dT%02d:%02d:%02d", 1900+new_time->tm_year,
|
|
207 new_time->tm_mon+1, new_time->tm_mday, new_time->tm_hour,
|
|
208 new_time->tm_min, new_time->tm_sec);
|
|
209
|
|
210 if(ret == -1)
|
|
211 return NULL;
|
|
212
|
|
213 return timestamp;
|
|
214 }
|
|
215
|
|
216 void jutil_error(xmlnode x, terror E)
|
|
217 {
|
|
218 xmlnode err;
|
|
219 char code[4];
|
|
220
|
|
221 xmlnode_put_attrib(x,"type","error");
|
|
222 err = xmlnode_insert_tag(x,"error");
|
|
223
|
|
224 snprintf(code,4,"%d",E.code);
|
|
225 xmlnode_put_attrib(err,"code",code);
|
|
226 if(E.msg != NULL)
|
|
227 xmlnode_insert_cdata(err,E.msg,strlen(E.msg));
|
|
228
|
|
229 jutil_tofrom(x);
|
|
230 }
|
|
231
|
|
232 void jutil_delay(xmlnode msg, char *reason)
|
|
233 {
|
|
234 xmlnode delay;
|
|
235
|
|
236 delay = xmlnode_insert_tag(msg,"x");
|
|
237 xmlnode_put_attrib(delay,"xmlns",NS_DELAY);
|
|
238 xmlnode_put_attrib(delay,"from",xmlnode_get_attrib(msg,"to"));
|
|
239 xmlnode_put_attrib(delay,"stamp",jutil_timestamp());
|
|
240 if(reason != NULL)
|
|
241 xmlnode_insert_cdata(delay,reason,strlen(reason));
|
|
242 }
|
|
243
|
|
244 #define KEYBUF 100
|
|
245
|
|
246 char *jutil_regkey(char *key, char *seed)
|
|
247 {
|
|
248 static char keydb[KEYBUF][41];
|
|
249 static char seeddb[KEYBUF][41];
|
|
250 static int last = -1;
|
|
251 char *str, strint[32];
|
|
252 int i;
|
|
253
|
|
254 /* blanket the keydb first time */
|
|
255 if(last == -1)
|
|
256 {
|
|
257 last = 0;
|
|
258 memset(&keydb,0,KEYBUF*41);
|
|
259 memset(&seeddb,0,KEYBUF*41);
|
|
260 srand(time(NULL));
|
|
261 }
|
|
262
|
|
263 /* creation phase */
|
|
264 if(key == NULL && seed != NULL)
|
|
265 {
|
|
266 /* create a random key hash and store it */
|
|
267 sprintf(strint,"%d",rand());
|
|
268 strcpy(keydb[last],shahash(strint));
|
|
269
|
|
270 /* store a hash for the seed associated w/ this key */
|
|
271 strcpy(seeddb[last],shahash(seed));
|
|
272
|
|
273 /* return it all */
|
|
274 str = keydb[last];
|
|
275 last++;
|
|
276 if(last == KEYBUF) last = 0;
|
|
277 return str;
|
|
278 }
|
|
279
|
|
280 /* validation phase */
|
|
281 str = shahash(seed);
|
|
282 for(i=0;i<KEYBUF;i++)
|
|
283 if(j_strcmp(keydb[i],key) == 0 && j_strcmp(seeddb[i],str) == 0)
|
|
284 {
|
|
285 seeddb[i][0] = '\0'; /* invalidate this key */
|
|
286 return keydb[i];
|
|
287 }
|
|
288
|
|
289 return NULL;
|
|
290 }
|
|
291
|