Mercurial > pidgin
annotate src/protocols/jabber/jutil.c @ 5032:cb700c07ee07
[gaim-migrate @ 5375]
Rewrote the buddy pounce code. It's now core/UI split, and may allow for
more advanced stuff later. Pounce actions are now a UI thing, and the
backend logic for registering, unregistering, and activating pouncs is now
in core. Also, the buddy pounce dialog was redesigned.
Oh, and there are new pounce types. You can now choose from:
* Sign on
* Sign off
* Away
* Return from away
* Idle
* Return from idle
* Buddy starts typing
* Buddy stops typing
Should work. I've been using it for some time. If you find a bug, though,
let me know.
committer: Tailor Script <tailor@pidgin.im>
author | Christian Hammond <chipx86@chipx86.com> |
---|---|
date | Sat, 05 Apr 2003 10:14:21 +0000 |
parents | 988485669631 |
children | 67c4e9d39242 |
rev | line source |
---|---|
3127 | 1 /* -------------------------------------------------------------------------- |
2 * | |
3 * License | |
4 * | |
5 * The contents of this file are subject to the Jabber Open Source License | |
6 * Version 1.0 (the "JOSL"). You may not copy or use this file, in either | |
7 * source code or executable form, except in compliance with the JOSL. You | |
8 * may obtain a copy of the JOSL at http://www.jabber.org/ or at | |
9 * http://www.opensource.org/. | |
10 * | |
11 * Software distributed under the JOSL is distributed on an "AS IS" basis, | |
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the JOSL | |
13 * for the specific language governing rights and limitations under the | |
14 * JOSL. | |
15 * | |
16 * Copyrights | |
17 * | |
18 * Portions created by or assigned to Jabber.com, Inc. are | |
19 * Copyright (c) 1999-2002 Jabber.com, Inc. All Rights Reserved. Contact | |
20 * information for Jabber.com, Inc. is available at http://www.jabber.com/. | |
21 * | |
22 * Portions Copyright (c) 1998-1999 Jeremie Miller. | |
23 * | |
24 * Acknowledgements | |
25 * | |
26 * Special thanks to the Jabber Open Source Contributors for their | |
27 * suggestions and support of Jabber. | |
28 * | |
29 * Alternatively, the contents of this file may be used under the terms of the | |
30 * GNU General Public License Version 2 or later (the "GPL"), in which case | |
31 * the provisions of the GPL are applicable instead of those above. If you | |
32 * wish to allow use of your version of this file only under the terms of the | |
33 * GPL and not to allow others to use your version of this file under the JOSL, | |
34 * indicate your decision by deleting the provisions above and replace them | |
35 * with the notice and other provisions required by the GPL. If you do not | |
36 * delete the provisions above, a recipient may use your version of this file | |
37 * under either the JOSL or the GPL. | |
38 * | |
39 * | |
40 * --------------------------------------------------------------------------*/ | |
41 | |
42 #include "lib.h" | |
2086 | 43 |
3717
988485669631
[gaim-migrate @ 3850]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
3127
diff
changeset
|
44 #ifdef _WIN32 |
988485669631
[gaim-migrate @ 3850]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
3127
diff
changeset
|
45 #include "win32dep.h" |
988485669631
[gaim-migrate @ 3850]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
3127
diff
changeset
|
46 #endif |
988485669631
[gaim-migrate @ 3850]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
3127
diff
changeset
|
47 |
2086 | 48 /* util for making presence packets */ |
49 xmlnode jutil_presnew(int type, char *to, char *status) | |
50 { | |
51 xmlnode pres; | |
52 | |
53 pres = xmlnode_new_tag("presence"); | |
54 switch(type) | |
55 { | |
56 case JPACKET__SUBSCRIBE: | |
57 xmlnode_put_attrib(pres,"type","subscribe"); | |
58 break; | |
59 case JPACKET__UNSUBSCRIBE: | |
60 xmlnode_put_attrib(pres,"type","unsubscribe"); | |
61 break; | |
62 case JPACKET__SUBSCRIBED: | |
63 xmlnode_put_attrib(pres,"type","subscribed"); | |
64 break; | |
65 case JPACKET__UNSUBSCRIBED: | |
66 xmlnode_put_attrib(pres,"type","unsubscribed"); | |
67 break; | |
68 case JPACKET__PROBE: | |
69 xmlnode_put_attrib(pres,"type","probe"); | |
70 break; | |
71 case JPACKET__UNAVAILABLE: | |
72 xmlnode_put_attrib(pres,"type","unavailable"); | |
73 break; | |
3127 | 74 case JPACKET__INVISIBLE: |
75 xmlnode_put_attrib(pres,"type","invisible"); | |
76 break; | |
2086 | 77 } |
78 if(to != NULL) | |
79 xmlnode_put_attrib(pres,"to",to); | |
80 if(status != NULL) | |
81 xmlnode_insert_cdata(xmlnode_insert_tag(pres,"status"),status,strlen(status)); | |
82 | |
83 return pres; | |
84 } | |
85 | |
86 /* util for making IQ packets */ | |
87 xmlnode jutil_iqnew(int type, char *ns) | |
88 { | |
89 xmlnode iq; | |
90 | |
91 iq = xmlnode_new_tag("iq"); | |
92 switch(type) | |
93 { | |
94 case JPACKET__GET: | |
95 xmlnode_put_attrib(iq,"type","get"); | |
96 break; | |
97 case JPACKET__SET: | |
98 xmlnode_put_attrib(iq,"type","set"); | |
99 break; | |
100 case JPACKET__RESULT: | |
101 xmlnode_put_attrib(iq,"type","result"); | |
102 break; | |
103 case JPACKET__ERROR: | |
104 xmlnode_put_attrib(iq,"type","error"); | |
105 break; | |
106 } | |
107 xmlnode_put_attrib(xmlnode_insert_tag(iq,"query"),"xmlns",ns); | |
108 | |
109 return iq; | |
110 } | |
111 | |
112 /* util for making message packets */ | |
113 xmlnode jutil_msgnew(char *type, char *to, char *subj, char *body) | |
114 { | |
115 xmlnode msg; | |
116 | |
117 msg = xmlnode_new_tag("message"); | |
118 xmlnode_put_attrib (msg, "type", type); | |
119 xmlnode_put_attrib (msg, "to", to); | |
120 | |
121 if (subj) | |
122 { | |
123 xmlnode_insert_cdata (xmlnode_insert_tag (msg, "subject"), subj, strlen (subj)); | |
124 } | |
125 | |
126 xmlnode_insert_cdata (xmlnode_insert_tag (msg, "body"), body, strlen (body)); | |
127 | |
128 return msg; | |
129 } | |
130 | |
131 /* util for making stream packets */ | |
132 xmlnode jutil_header(char* xmlns, char* server) | |
133 { | |
134 xmlnode result; | |
135 if ((xmlns == NULL)||(server == NULL)) | |
136 return NULL; | |
137 result = xmlnode_new_tag("stream:stream"); | |
138 xmlnode_put_attrib(result, "xmlns:stream", "http://etherx.jabber.org/streams"); | |
139 xmlnode_put_attrib(result, "xmlns", xmlns); | |
140 xmlnode_put_attrib(result, "to", server); | |
141 | |
142 return result; | |
143 } | |
144 | |
145 /* returns the priority on a presence packet */ | |
146 int jutil_priority(xmlnode x) | |
147 { | |
148 char *str; | |
149 int p; | |
150 | |
151 if(x == NULL) | |
152 return -1; | |
153 | |
154 if(xmlnode_get_attrib(x,"type") != NULL) | |
155 return -1; | |
156 | |
157 x = xmlnode_get_tag(x,"priority"); | |
158 if(x == NULL) | |
159 return 0; | |
160 | |
161 str = xmlnode_get_data((x)); | |
162 if(str == NULL) | |
163 return 0; | |
164 | |
165 p = atoi(str); | |
166 if(p >= 0) | |
167 return p; | |
168 else | |
169 return 0; | |
170 } | |
171 | |
172 void jutil_tofrom(xmlnode x) | |
173 { | |
174 char *to, *from; | |
175 | |
176 to = xmlnode_get_attrib(x,"to"); | |
177 from = xmlnode_get_attrib(x,"from"); | |
178 xmlnode_put_attrib(x,"from",to); | |
179 xmlnode_put_attrib(x,"to",from); | |
180 } | |
181 | |
182 xmlnode jutil_iqresult(xmlnode x) | |
183 { | |
184 xmlnode cur; | |
185 | |
186 jutil_tofrom(x); | |
187 | |
188 xmlnode_put_attrib(x,"type","result"); | |
189 | |
190 /* hide all children of the iq, they go back empty */ | |
191 for(cur = xmlnode_get_firstchild(x); cur != NULL; cur = xmlnode_get_nextsibling(cur)) | |
192 xmlnode_hide(cur); | |
193 | |
194 return x; | |
195 } | |
196 | |
197 char *jutil_timestamp(void) | |
198 { | |
199 time_t t; | |
200 struct tm *new_time; | |
201 static char timestamp[18]; | |
202 int ret; | |
203 | |
204 t = time(NULL); | |
205 | |
206 if(t == (time_t)-1) | |
207 return NULL; | |
208 new_time = gmtime(&t); | |
209 | |
210 ret = snprintf(timestamp, 18, "%d%02d%02dT%02d:%02d:%02d", 1900+new_time->tm_year, | |
211 new_time->tm_mon+1, new_time->tm_mday, new_time->tm_hour, | |
212 new_time->tm_min, new_time->tm_sec); | |
213 | |
214 if(ret == -1) | |
215 return NULL; | |
216 | |
217 return timestamp; | |
218 } | |
219 | |
220 void jutil_error(xmlnode x, terror E) | |
221 { | |
222 xmlnode err; | |
223 char code[4]; | |
224 | |
225 xmlnode_put_attrib(x,"type","error"); | |
226 err = xmlnode_insert_tag(x,"error"); | |
227 | |
228 snprintf(code,4,"%d",E.code); | |
229 xmlnode_put_attrib(err,"code",code); | |
230 if(E.msg != NULL) | |
231 xmlnode_insert_cdata(err,E.msg,strlen(E.msg)); | |
232 | |
233 jutil_tofrom(x); | |
234 } | |
235 | |
236 void jutil_delay(xmlnode msg, char *reason) | |
237 { | |
238 xmlnode delay; | |
239 | |
240 delay = xmlnode_insert_tag(msg,"x"); | |
241 xmlnode_put_attrib(delay,"xmlns",NS_DELAY); | |
242 xmlnode_put_attrib(delay,"from",xmlnode_get_attrib(msg,"to")); | |
243 xmlnode_put_attrib(delay,"stamp",jutil_timestamp()); | |
244 if(reason != NULL) | |
245 xmlnode_insert_cdata(delay,reason,strlen(reason)); | |
246 } | |
247 | |
248 #define KEYBUF 100 | |
249 | |
250 char *jutil_regkey(char *key, char *seed) | |
251 { | |
252 static char keydb[KEYBUF][41]; | |
253 static char seeddb[KEYBUF][41]; | |
254 static int last = -1; | |
255 char *str, strint[32]; | |
256 int i; | |
257 | |
258 /* blanket the keydb first time */ | |
259 if(last == -1) | |
260 { | |
261 last = 0; | |
262 memset(&keydb,0,KEYBUF*41); | |
263 memset(&seeddb,0,KEYBUF*41); | |
264 srand(time(NULL)); | |
265 } | |
266 | |
267 /* creation phase */ | |
268 if(key == NULL && seed != NULL) | |
269 { | |
270 /* create a random key hash and store it */ | |
271 sprintf(strint,"%d",rand()); | |
272 strcpy(keydb[last],shahash(strint)); | |
273 | |
274 /* store a hash for the seed associated w/ this key */ | |
275 strcpy(seeddb[last],shahash(seed)); | |
276 | |
277 /* return it all */ | |
278 str = keydb[last]; | |
279 last++; | |
280 if(last == KEYBUF) last = 0; | |
281 return str; | |
282 } | |
283 | |
284 /* validation phase */ | |
285 str = shahash(seed); | |
286 for(i=0;i<KEYBUF;i++) | |
287 if(j_strcmp(keydb[i],key) == 0 && j_strcmp(seeddb[i],str) == 0) | |
288 { | |
289 seeddb[i][0] = '\0'; /* invalidate this key */ | |
290 return keydb[i]; | |
291 } | |
292 | |
293 return NULL; | |
294 } | |
295 |