Mercurial > pidgin
comparison src/protocols/jabber/jutil.c @ 2086:424a40f12a6c
[gaim-migrate @ 2096]
moving protocols from plugins/ to src/protocols. making it so that you can select which protocols are compiled statically.
committer: Tailor Script <tailor@pidgin.im>
author | Eric Warmenhoven <eric@warmenhoven.org> |
---|---|
date | Tue, 31 Jul 2001 01:00:39 +0000 |
parents | |
children | 4e7cefc55971 |
comparison
equal
deleted
inserted
replaced
2085:7ebb4322f89b | 2086:424a40f12a6c |
---|---|
1 #include "jabber.h" | |
2 | |
3 /* util for making presence packets */ | |
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 | |
38 /* util for making IQ packets */ | |
39 xmlnode jutil_iqnew(int type, char *ns) | |
40 { | |
41 xmlnode iq; | |
42 | |
43 iq = xmlnode_new_tag("iq"); | |
44 switch(type) | |
45 { | |
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; | |
58 } | |
59 xmlnode_put_attrib(xmlnode_insert_tag(iq,"query"),"xmlns",ns); | |
60 | |
61 return iq; | |
62 } | |
63 | |
64 /* util for making message packets */ | |
65 xmlnode jutil_msgnew(char *type, char *to, char *subj, char *body) | |
66 { | |
67 xmlnode msg; | |
68 | |
69 msg = xmlnode_new_tag("message"); | |
70 xmlnode_put_attrib (msg, "type", type); | |
71 xmlnode_put_attrib (msg, "to", to); | |
72 | |
73 if (subj) | |
74 { | |
75 xmlnode_insert_cdata (xmlnode_insert_tag (msg, "subject"), subj, strlen (subj)); | |
76 } | |
77 | |
78 xmlnode_insert_cdata (xmlnode_insert_tag (msg, "body"), body, strlen (body)); | |
79 | |
80 return msg; | |
81 } | |
82 | |
83 /* util for making stream packets */ | |
84 xmlnode jutil_header(char* xmlns, char* server) | |
85 { | |
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); | |
93 | |
94 return result; | |
95 } | |
96 | |
97 /* returns the priority on a presence packet */ | |
98 int jutil_priority(xmlnode x) | |
99 { | |
100 char *str; | |
101 int p; | |
102 | |
103 if(x == NULL) | |
104 return -1; | |
105 | |
106 if(xmlnode_get_attrib(x,"type") != NULL) | |
107 return -1; | |
108 | |
109 x = xmlnode_get_tag(x,"priority"); | |
110 if(x == NULL) | |
111 return 0; | |
112 | |
113 str = xmlnode_get_data((x)); | |
114 if(str == NULL) | |
115 return 0; | |
116 | |
117 p = atoi(str); | |
118 if(p >= 0) | |
119 return p; | |
120 else | |
121 return 0; | |
122 } | |
123 | |
124 void jutil_tofrom(xmlnode x) | |
125 { | |
126 char *to, *from; | |
127 | |
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); | |
132 } | |
133 | |
134 xmlnode jutil_iqresult(xmlnode x) | |
135 { | |
136 xmlnode cur; | |
137 | |
138 jutil_tofrom(x); | |
139 | |
140 xmlnode_put_attrib(x,"type","result"); | |
141 | |
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); | |
145 | |
146 return x; | |
147 } | |
148 | |
149 char *jutil_timestamp(void) | |
150 { | |
151 time_t t; | |
152 struct tm *new_time; | |
153 static char timestamp[18]; | |
154 int ret; | |
155 | |
156 t = time(NULL); | |
157 | |
158 if(t == (time_t)-1) | |
159 return NULL; | |
160 new_time = gmtime(&t); | |
161 | |
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); | |
165 | |
166 if(ret == -1) | |
167 return NULL; | |
168 | |
169 return timestamp; | |
170 } | |
171 | |
172 void jutil_error(xmlnode x, terror E) | |
173 { | |
174 xmlnode err; | |
175 char code[4]; | |
176 | |
177 xmlnode_put_attrib(x,"type","error"); | |
178 err = xmlnode_insert_tag(x,"error"); | |
179 | |
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)); | |
184 | |
185 jutil_tofrom(x); | |
186 } | |
187 | |
188 void jutil_delay(xmlnode msg, char *reason) | |
189 { | |
190 xmlnode delay; | |
191 | |
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)); | |
198 } | |
199 | |
200 #define KEYBUF 100 | |
201 | |
202 char *jutil_regkey(char *key, char *seed) | |
203 { | |
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; | |
209 | |
210 /* blanket the keydb first time */ | |
211 if(last == -1) | |
212 { | |
213 last = 0; | |
214 memset(&keydb,0,KEYBUF*41); | |
215 memset(&seeddb,0,KEYBUF*41); | |
216 srand(time(NULL)); | |
217 } | |
218 | |
219 /* creation phase */ | |
220 if(key == NULL && seed != NULL) | |
221 { | |
222 /* create a random key hash and store it */ | |
223 sprintf(strint,"%d",rand()); | |
224 strcpy(keydb[last],shahash(strint)); | |
225 | |
226 /* store a hash for the seed associated w/ this key */ | |
227 strcpy(seeddb[last],shahash(seed)); | |
228 | |
229 /* return it all */ | |
230 str = keydb[last]; | |
231 last++; | |
232 if(last == KEYBUF) last = 0; | |
233 return str; | |
234 } | |
235 | |
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) | |
240 { | |
241 seeddb[i][0] = '\0'; /* invalidate this key */ | |
242 return keydb[i]; | |
243 } | |
244 | |
245 return NULL; | |
246 } | |
247 |