Mercurial > pidgin.yaz
annotate src/protocols/jabber/iq.c @ 7289:ab2085024a29
[gaim-migrate @ 7870]
Let new MSN users with empty buddy lists sign on. This fixes a lot of bug
numbers.
committer: Tailor Script <tailor@pidgin.im>
author | Christian Hammond <chipx86@chipx86.com> |
---|---|
date | Fri, 17 Oct 2003 15:03:27 +0000 |
parents | 1cf80749f2f0 |
children | b250288fa948 |
rev | line source |
---|---|
7014 | 1 /* |
2 * gaim - Jabber Protocol Plugin | |
3 * | |
4 * Copyright (C) 2003, Nathan Walp <faceprint@faceprint.com> | |
5 * | |
6 * This program is free software; you can redistribute it and/or modify | |
7 * it under the terms of the GNU General Public License as published by | |
8 * the Free Software Foundation; either version 2 of the License, or | |
9 * (at your option) any later version. | |
10 * | |
11 * This program is distributed in the hope that it will be useful, | |
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 * GNU General Public License for more details. | |
15 * | |
16 * You should have received a copy of the GNU General Public License | |
17 * along with this program; if not, write to the Free Software | |
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 * | |
20 */ | |
21 #include "internal.h" | |
22 #include "debug.h" | |
23 #include "prefs.h" | |
24 | |
25 #include "iq.h" | |
7170 | 26 #include "oob.h" |
7014 | 27 #include "roster.h" |
28 | |
7058
06e7697f3fae
[gaim-migrate @ 7621]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
7014
diff
changeset
|
29 #ifdef _WIN32 |
06e7697f3fae
[gaim-migrate @ 7621]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
7014
diff
changeset
|
30 #include "utsname.h" |
06e7697f3fae
[gaim-migrate @ 7621]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
7014
diff
changeset
|
31 #endif |
7014 | 32 |
33 JabberIq *jabber_iq_new(JabberStream *js, JabberIqType type) | |
34 { | |
35 JabberIq *iq; | |
36 | |
37 iq = g_new0(JabberIq, 1); | |
38 | |
39 iq->type = type; | |
40 | |
41 iq->node = xmlnode_new("iq"); | |
42 switch(iq->type) { | |
43 case JABBER_IQ_SET: | |
44 xmlnode_set_attrib(iq->node, "type", "set"); | |
45 break; | |
46 case JABBER_IQ_GET: | |
47 xmlnode_set_attrib(iq->node, "type", "get"); | |
48 break; | |
49 case JABBER_IQ_ERROR: | |
50 xmlnode_set_attrib(iq->node, "type", "error"); | |
51 break; | |
52 case JABBER_IQ_RESULT: | |
53 xmlnode_set_attrib(iq->node, "type", "result"); | |
54 break; | |
55 case JABBER_IQ_NONE: | |
56 /* this shouldn't ever happen */ | |
57 break; | |
58 } | |
59 | |
60 iq->js = js; | |
61 | |
62 if(type == JABBER_IQ_GET || type == JABBER_IQ_SET) { | |
63 iq->id = jabber_get_next_id(js); | |
64 xmlnode_set_attrib(iq->node, "id", iq->id); | |
65 } | |
66 | |
67 return iq; | |
68 } | |
69 | |
70 JabberIq *jabber_iq_new_query(JabberStream *js, JabberIqType type, | |
71 const char *xmlns) | |
72 { | |
73 JabberIq *iq = jabber_iq_new(js, type); | |
74 xmlnode *query; | |
75 | |
76 query = xmlnode_new_child(iq->node, "query"); | |
77 xmlnode_set_attrib(query, "xmlns", xmlns); | |
78 | |
79 return iq; | |
80 } | |
81 | |
82 void jabber_iq_set_callback(JabberIq *iq, JabberCallback *callback) | |
83 { | |
84 iq->callback = callback; | |
85 } | |
86 | |
87 void jabber_iq_set_id(JabberIq *iq, const char *id) | |
88 { | |
89 if(iq->id) | |
90 g_free(iq->id); | |
91 | |
92 if(id) { | |
93 xmlnode_set_attrib(iq->node, "id", id); | |
94 iq->id = g_strdup(id); | |
95 } else { | |
96 xmlnode_remove_attrib(iq->node, "id"); | |
97 iq->id = NULL; | |
98 } | |
99 } | |
100 | |
101 void jabber_iq_send(JabberIq *iq) | |
102 { | |
103 g_return_if_fail(iq != NULL); | |
104 | |
105 jabber_send(iq->js, iq->node); | |
106 | |
107 if(iq->id && iq->callback) | |
108 g_hash_table_insert(iq->js->callbacks, g_strdup(iq->id), iq->callback); | |
109 | |
110 jabber_iq_free(iq); | |
111 } | |
112 | |
113 void jabber_iq_free(JabberIq *iq) | |
114 { | |
115 g_return_if_fail(iq != NULL); | |
116 | |
117 g_free(iq->id); | |
118 xmlnode_free(iq->node); | |
119 g_free(iq); | |
120 } | |
121 | |
122 static void jabber_iq_handle_last(JabberStream *js, xmlnode *packet) | |
123 { | |
124 JabberIq *iq; | |
125 const char *from; | |
126 const char *id; | |
127 xmlnode *query; | |
128 char *idle_time; | |
129 | |
130 from = xmlnode_get_attrib(packet, "from"); | |
131 id = xmlnode_get_attrib(packet, "id"); | |
132 | |
133 iq = jabber_iq_new_query(js, JABBER_IQ_RESULT, "jabber:iq:last"); | |
134 jabber_iq_set_id(iq, id); | |
135 xmlnode_set_attrib(iq->node, "to", from); | |
136 | |
137 query = xmlnode_get_child(iq->node, "query"); | |
138 | |
139 idle_time = g_strdup_printf("%ld", js->idle ? time(NULL) - js->idle : 0); | |
140 xmlnode_set_attrib(query, "seconds", idle_time); | |
141 g_free(idle_time); | |
142 } | |
143 | |
144 static void jabber_iq_handle_time(JabberStream *js, xmlnode *packet) | |
145 { | |
146 const char *from, *id; | |
147 JabberIq *iq; | |
148 char buf[1024]; | |
149 xmlnode *query; | |
150 time_t now_t; | |
151 struct tm now; | |
152 time(&now_t); | |
153 localtime_r(&now_t, &now); | |
154 | |
155 from = xmlnode_get_attrib(packet, "from"); | |
156 id = xmlnode_get_attrib(packet, "id"); | |
157 | |
158 iq = jabber_iq_new_query(js, JABBER_IQ_RESULT, "jabber:iq:time"); | |
159 jabber_iq_set_id(iq, id); | |
160 xmlnode_set_attrib(iq->node, "to", from); | |
161 | |
162 query = xmlnode_get_child(iq->node, "query"); | |
163 | |
164 strftime(buf, sizeof(buf), "%Y%m%dT%T", &now); | |
165 xmlnode_insert_data(xmlnode_new_child(query, "utc"), buf, -1); | |
166 strftime(buf, sizeof(buf), "%Z", &now); | |
167 xmlnode_insert_data(xmlnode_new_child(query, "tz"), buf, -1); | |
168 strftime(buf, sizeof(buf), "%d %b %Y %T", &now); | |
169 xmlnode_insert_data(xmlnode_new_child(query, "display"), buf, -1); | |
170 | |
171 jabber_iq_send(iq); | |
172 } | |
173 | |
174 static void jabber_iq_handle_version(JabberStream *js, xmlnode *packet) | |
175 { | |
176 JabberIq *iq; | |
177 const char *from, *id; | |
178 xmlnode *query; | |
179 char *os = NULL; | |
180 | |
181 if(!gaim_prefs_get_bool("/plugins/prpl/jabber/hide_os")) { | |
182 struct utsname osinfo; | |
183 | |
184 uname(&osinfo); | |
185 os = g_strdup_printf("%s %s %s", osinfo.sysname, osinfo.release, | |
186 osinfo.machine); | |
187 } | |
188 | |
189 from = xmlnode_get_attrib(packet, "from"); | |
190 id = xmlnode_get_attrib(packet, "id"); | |
191 | |
192 iq = jabber_iq_new_query(js, JABBER_IQ_RESULT, "jabber:iq:version"); | |
193 xmlnode_set_attrib(iq->node, "to", from); | |
194 jabber_iq_set_id(iq, id); | |
195 | |
196 query = xmlnode_get_child(iq->node, "query"); | |
197 | |
198 xmlnode_insert_data(xmlnode_new_child(query, "name"), PACKAGE, -1); | |
199 xmlnode_insert_data(xmlnode_new_child(query, "version"), VERSION, -1); | |
200 if(os) { | |
201 xmlnode_insert_data(xmlnode_new_child(query, "os"), os, -1); | |
202 g_free(os); | |
203 } | |
204 | |
205 jabber_iq_send(iq); | |
206 } | |
207 | |
208 void jabber_iq_parse(JabberStream *js, xmlnode *packet) | |
209 { | |
210 xmlnode *query; | |
211 const char *xmlns; | |
212 | |
213 query = xmlnode_get_child(packet, "query"); | |
214 | |
215 if(!query) | |
216 return; | |
217 | |
218 xmlns = xmlnode_get_attrib(query, "xmlns"); | |
219 | |
220 if(!xmlns) | |
221 return; | |
222 | |
223 if(!strcmp(xmlns, "jabber:iq:roster")) { | |
224 jabber_roster_parse(js, packet); | |
225 } else if(!strcmp(xmlns, "jabber:iq:last")) { | |
226 jabber_iq_handle_last(js, packet); | |
227 } else if(!strcmp(xmlns, "jabber:iq:time")) { | |
228 jabber_iq_handle_time(js, packet); | |
229 } else if(!strcmp(xmlns, "jabber:iq:version")) { | |
230 jabber_iq_handle_version(js, packet); | |
7072 | 231 } else if(!strcmp(xmlns, "jabber:iq:register")) { |
232 jabber_register_parse(js, packet); | |
7116 | 233 } else if(!strcmp(xmlns, "jabber:iq:oob")) { |
7170 | 234 jabber_oob_parse(js, packet); |
7014 | 235 } else { |
236 gaim_debug(GAIM_DEBUG_WARNING, "jabber", "Unknown query: %s\n", xmlns); | |
237 } | |
238 } | |
239 |