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