3 /* util for making presence packets */
4 xmlnode
jutil_presnew(int type
, char *to
, char *status
)
8 pres
= xmlnode_new_tag("presence");
11 case JPACKET__SUBSCRIBE
:
12 xmlnode_put_attrib(pres
,"type","subscribe");
14 case JPACKET__UNSUBSCRIBE
:
15 xmlnode_put_attrib(pres
,"type","unsubscribe");
17 case JPACKET__SUBSCRIBED
:
18 xmlnode_put_attrib(pres
,"type","subscribed");
20 case JPACKET__UNSUBSCRIBED
:
21 xmlnode_put_attrib(pres
,"type","unsubscribed");
24 xmlnode_put_attrib(pres
,"type","probe");
26 case JPACKET__UNAVAILABLE
:
27 xmlnode_put_attrib(pres
,"type","unavailable");
31 xmlnode_put_attrib(pres
,"to",to
);
33 xmlnode_insert_cdata(xmlnode_insert_tag(pres
,"status"),status
,strlen(status
));
38 /* util for making IQ packets */
39 xmlnode
jutil_iqnew(int type
, char *ns
)
43 iq
= xmlnode_new_tag("iq");
47 xmlnode_put_attrib(iq
,"type","get");
50 xmlnode_put_attrib(iq
,"type","set");
53 xmlnode_put_attrib(iq
,"type","result");
56 xmlnode_put_attrib(iq
,"type","error");
59 xmlnode_put_attrib(xmlnode_insert_tag(iq
,"query"),"xmlns",ns
);
64 /* util for making message packets */
65 xmlnode
jutil_msgnew(char *type
, char *to
, char *subj
, char *body
)
69 msg
= xmlnode_new_tag("message");
70 xmlnode_put_attrib (msg
, "type", type
);
71 xmlnode_put_attrib (msg
, "to", to
);
75 xmlnode_insert_cdata (xmlnode_insert_tag (msg
, "subject"), subj
, strlen (subj
));
78 xmlnode_insert_cdata (xmlnode_insert_tag (msg
, "body"), body
, strlen (body
));
83 /* util for making stream packets */
84 xmlnode
jutil_header(char* xmlns
, char* server
)
87 if ((xmlns
== NULL
)||(server
== 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
);
97 /* returns the priority on a presence packet */
98 int jutil_priority(xmlnode x
)
106 if(xmlnode_get_attrib(x
,"type") != NULL
)
109 x
= xmlnode_get_tag(x
,"priority");
113 str
= xmlnode_get_data((x
));
124 void jutil_tofrom(xmlnode x
)
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
)
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
))
149 char *jutil_timestamp(void)
153 static char timestamp
[18];
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
);
172 void jutil_error(xmlnode x
, terror E
)
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
);
183 xmlnode_insert_cdata(err
,E
.msg
,strlen(E
.msg
));
188 void jutil_delay(xmlnode msg
, char *reason
)
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());
197 xmlnode_insert_cdata(delay
,reason
,strlen(reason
));
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];
210 /* blanket the keydb first time */
214 memset(&keydb
,0,KEYBUF
*41);
215 memset(&seeddb
,0,KEYBUF
*41);
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
));
232 if(last
== KEYBUF
) last
= 0;
236 /* validation phase */
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 */