connwrap - initialize gnutls session in cw_connect
[centerim.git] / libjabber / jutil.c
blobe99999bf916b03cf5ff000cb1277a1f31f60a3d7
1 #include "jabber.h"
3 /* util for making presence packets */
4 xmlnode jutil_presnew(int type, char *to, char *status)
6 xmlnode pres;
8 pres = xmlnode_new_tag("presence");
9 switch(type)
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;
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));
35 return pres;
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.
40 xmlnode iq;
42 iq = xmlnode_new_tag("iq");
43 switch(type)
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;
59 return iq;
62 /* util for making IQ packets */
63 xmlnode jutil_iqnew(int type, char *ns)
65 xmlnode iq;
67 iq = xmlnode_new_tag("iq");
68 switch(type)
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;
83 xmlnode_put_attrib(xmlnode_insert_tag(iq,"query"),"xmlns",ns);
85 return iq;
88 /* util for making message packets */
89 xmlnode jutil_msgnew(char *type, char *to, char *subj, char *body)
91 xmlnode msg;
93 msg = xmlnode_new_tag("message");
94 xmlnode_put_attrib (msg, "type", type);
95 xmlnode_put_attrib (msg, "to", to);
97 if (subj)
99 xmlnode_insert_cdata (xmlnode_insert_tag (msg, "subject"), subj, strlen (subj));
102 xmlnode_insert_cdata (xmlnode_insert_tag (msg, "body"), body, strlen (body));
104 return msg;
107 /* util for making message receipt packets */
108 xmlnode jutil_receiptnew(const char *to, const char *id)
110 xmlnode msg;
112 msg = xmlnode_new_tag("message");
113 xmlnode_put_attrib (msg, "to", to);
114 if(id != NULL)
115 xmlnode_put_attrib (msg, "id", id);
117 xmlnode_put_attrib(xmlnode_insert_tag (msg, "received"), "xmlns", "urn:xmpp:receipts");
119 return msg;
122 /* util for making stream packets */
123 xmlnode jutil_header(char* xmlns, char* server)
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);
133 return result;
136 /* returns the priority on a presence packet */
137 int jutil_priority(xmlnode x)
139 char *str;
140 int p;
142 if(x == NULL)
143 return -1;
145 if(xmlnode_get_attrib(x,"type") != NULL)
146 return -1;
148 x = xmlnode_get_tag(x,"priority");
149 if(x == NULL)
150 return 0;
152 str = xmlnode_get_data((x));
153 if(str == NULL)
154 return 0;
156 p = atoi(str);
157 return ((char)p);
160 void jutil_tofrom(xmlnode x)
162 char *to, *from;
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);
170 xmlnode jutil_iqresult(xmlnode x)
172 xmlnode cur;
174 jutil_tofrom(x);
176 xmlnode_put_attrib(x,"type","result");
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);
182 return x;
185 char *jutil_timestamp(void)
187 time_t t;
188 struct tm *new_time;
189 static char timestamp[18];
190 int ret;
192 t = time(NULL);
194 if(t == (time_t)-1)
195 return NULL;
196 new_time = gmtime(&t);
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);
202 if(ret == -1)
203 return NULL;
205 return timestamp;
208 void jutil_error(xmlnode x, terror E)
210 xmlnode err;
211 char code[4];
213 xmlnode_put_attrib(x,"type","error");
214 err = xmlnode_insert_tag(x,"error");
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));
221 jutil_tofrom(x);
224 void jutil_delay(xmlnode msg, char *reason)
226 xmlnode delay;
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));
236 #define KEYBUF 100
238 char *jutil_regkey(char *key, char *seed)
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;
246 /* blanket the keydb first time */
247 if(last == -1)
249 last = 0;
250 memset(&keydb,0,KEYBUF*41);
251 memset(&seeddb,0,KEYBUF*41);
252 srand(time(NULL));
255 /* creation phase */
256 if(key == NULL && seed != NULL)
258 /* create a random key hash and store it */
259 sprintf(strint,"%d",rand());
260 strcpy(keydb[last],shahash(strint));
262 /* store a hash for the seed associated w/ this key */
263 strcpy(seeddb[last],shahash(seed));
265 /* return it all */
266 str = keydb[last];
267 last++;
268 if(last == KEYBUF) last = 0;
269 return str;
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)
277 seeddb[i][0] = '\0'; /* invalidate this key */
278 return keydb[i];
281 return NULL;