[gaim-migrate @ 3063]
[pidgin-git.git] / src / protocols / jabber / jutil.c
blob73860c97799dab6cdfe8dd08d40cae6426949783
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 /* util for making IQ packets */
39 xmlnode jutil_iqnew(int type, char *ns)
41 xmlnode iq;
43 iq = xmlnode_new_tag("iq");
44 switch(type)
46 case JPACKET__GET:
47 xmlnode_put_attrib(iq,"type","get");
48 break;
49 case JPACKET__SET:
50 xmlnode_put_attrib(iq,"type","set");
51 break;
52 case JPACKET__RESULT:
53 xmlnode_put_attrib(iq,"type","result");
54 break;
55 case JPACKET__ERROR:
56 xmlnode_put_attrib(iq,"type","error");
57 break;
59 xmlnode_put_attrib(xmlnode_insert_tag(iq,"query"),"xmlns",ns);
61 return iq;
64 /* util for making message packets */
65 xmlnode jutil_msgnew(char *type, char *to, char *subj, char *body)
67 xmlnode msg;
69 msg = xmlnode_new_tag("message");
70 xmlnode_put_attrib (msg, "type", type);
71 xmlnode_put_attrib (msg, "to", to);
73 if (subj)
75 xmlnode_insert_cdata (xmlnode_insert_tag (msg, "subject"), subj, strlen (subj));
78 xmlnode_insert_cdata (xmlnode_insert_tag (msg, "body"), body, strlen (body));
80 return msg;
83 /* util for making stream packets */
84 xmlnode jutil_header(char* xmlns, char* server)
86 xmlnode result;
87 if ((xmlns == NULL)||(server == NULL))
88 return NULL;
89 result = xmlnode_new_tag("stream:stream");
90 xmlnode_put_attrib(result, "xmlns:stream", "http://etherx.jabber.org/streams");
91 xmlnode_put_attrib(result, "xmlns", xmlns);
92 xmlnode_put_attrib(result, "to", server);
94 return result;
97 /* returns the priority on a presence packet */
98 int jutil_priority(xmlnode x)
100 char *str;
101 int p;
103 if(x == NULL)
104 return -1;
106 if(xmlnode_get_attrib(x,"type") != NULL)
107 return -1;
109 x = xmlnode_get_tag(x,"priority");
110 if(x == NULL)
111 return 0;
113 str = xmlnode_get_data((x));
114 if(str == NULL)
115 return 0;
117 p = atoi(str);
118 if(p >= 0)
119 return p;
120 else
121 return 0;
124 void jutil_tofrom(xmlnode x)
126 char *to, *from;
128 to = xmlnode_get_attrib(x,"to");
129 from = xmlnode_get_attrib(x,"from");
130 xmlnode_put_attrib(x,"from",to);
131 xmlnode_put_attrib(x,"to",from);
134 xmlnode jutil_iqresult(xmlnode x)
136 xmlnode cur;
138 jutil_tofrom(x);
140 xmlnode_put_attrib(x,"type","result");
142 /* hide all children of the iq, they go back empty */
143 for(cur = xmlnode_get_firstchild(x); cur != NULL; cur = xmlnode_get_nextsibling(cur))
144 xmlnode_hide(cur);
146 return x;
149 char *jutil_timestamp(void)
151 time_t t;
152 struct tm *new_time;
153 static char timestamp[18];
154 int ret;
156 t = time(NULL);
158 if(t == (time_t)-1)
159 return NULL;
160 new_time = gmtime(&t);
162 ret = snprintf(timestamp, 18, "%d%02d%02dT%02d:%02d:%02d", 1900+new_time->tm_year,
163 new_time->tm_mon+1, new_time->tm_mday, new_time->tm_hour,
164 new_time->tm_min, new_time->tm_sec);
166 if(ret == -1)
167 return NULL;
169 return timestamp;
172 void jutil_error(xmlnode x, terror E)
174 xmlnode err;
175 char code[4];
177 xmlnode_put_attrib(x,"type","error");
178 err = xmlnode_insert_tag(x,"error");
180 snprintf(code,4,"%d",E.code);
181 xmlnode_put_attrib(err,"code",code);
182 if(E.msg != NULL)
183 xmlnode_insert_cdata(err,E.msg,strlen(E.msg));
185 jutil_tofrom(x);
188 void jutil_delay(xmlnode msg, char *reason)
190 xmlnode delay;
192 delay = xmlnode_insert_tag(msg,"x");
193 xmlnode_put_attrib(delay,"xmlns",NS_DELAY);
194 xmlnode_put_attrib(delay,"from",xmlnode_get_attrib(msg,"to"));
195 xmlnode_put_attrib(delay,"stamp",jutil_timestamp());
196 if(reason != NULL)
197 xmlnode_insert_cdata(delay,reason,strlen(reason));
200 #define KEYBUF 100
202 char *jutil_regkey(char *key, char *seed)
204 static char keydb[KEYBUF][41];
205 static char seeddb[KEYBUF][41];
206 static int last = -1;
207 char *str, strint[32];
208 int i;
210 /* blanket the keydb first time */
211 if(last == -1)
213 last = 0;
214 memset(&keydb,0,KEYBUF*41);
215 memset(&seeddb,0,KEYBUF*41);
216 srand(time(NULL));
219 /* creation phase */
220 if(key == NULL && seed != NULL)
222 /* create a random key hash and store it */
223 sprintf(strint,"%d",rand());
224 strcpy(keydb[last],shahash(strint));
226 /* store a hash for the seed associated w/ this key */
227 strcpy(seeddb[last],shahash(seed));
229 /* return it all */
230 str = keydb[last];
231 last++;
232 if(last == KEYBUF) last = 0;
233 return str;
236 /* validation phase */
237 str = shahash(seed);
238 for(i=0;i<KEYBUF;i++)
239 if(j_strcmp(keydb[i],key) == 0 && j_strcmp(seeddb[i],str) == 0)
241 seeddb[i][0] = '\0'; /* invalidate this key */
242 return keydb[i];
245 return NULL;