1 /*
2 * Ostatnia aktualizacja:
3 *
4 * - $Id: roster.c,v 1.25 2004/01/19 12:37:31 mati Exp $
5 *
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <string.h>
12
13 #include "libtlen.h"
14
15 /*
16 * tlen_presence()
17 *
18 * Pozwala na ustawienie swojego stanu dost�pno�ci
19 *
20 * - sesja - nasza sesja
21 * - status - stan w postaci TLEN_STATUS_*
22 * - description - opcjonalny status opisowy, je�eli NULL to ""
23 *
24 * Brak uwag dodatkowych
25 *
26 */
27
tlen_presence(struct tlen_session * sesja,int status,const char * description)28 int tlen_presence (struct tlen_session *sesja, int status, const char *description)
29 {
30 char *presence;
31 char *query;
32 char *urlencoded;
33
34 if (description==NULL) { description = ""; }
35
36 urlencoded = tlen_encode(description);
37
38 switch (status)
39 {
40 case TLEN_STATUS_AVAILABLE: presence = "available"; break;
41 case TLEN_STATUS_AWAY: presence = "away"; break;
42 case TLEN_STATUS_CHATTY: presence = "chat"; break;
43 case TLEN_STATUS_EXT_AWAY: presence = "xa"; break;
44 case TLEN_STATUS_DND: presence = "dnd"; break;
45 case TLEN_STATUS_INVISIBLE: tlen_presence_invisible(sesja); return 1; break;
46 case TLEN_STATUS_UNAVAILABLE: tlen_presence_disconnect(sesja); return 1; break;
47 default: presence = "available"; break;
48 }
49
50 tlen_debug ("Status: %s\nDescription: %s\n", presence, description);
51
52 if (!(query = (char *) malloc (strlen("<presence><show></show><status></status></presence>") + strlen (presence) + strlen (urlencoded) + 1)))
53 {
54 perror ("malloc");
55 sesja->error = TLEN_ERROR_MALLOC;
56 }
57 sprintf (query, "<presence><show>%s</show><status>%s</status></presence>",presence, urlencoded);
58 tlen_socket_write_string (sesja, query);
59 sesja->status = status;
60 sesja->description = strdup(description);
61 free (query);
62 free (urlencoded);
63
64 return 1;
65 }
66
67 /*
68 * tlen_presence_invisible()
69 *
70 * Funkcja wewn�trzna wysy�aj�ca nietypowy status niewidoczny,
71 * zgodno�� z nowym Tlenem, brak mo�liwo�ci �atwego wykrycia.
72 *
73 * - sesja - nasza sesja
74 *
75 * Nie u�ywa� w swoich programach, od tego jest tlen_presence()
76 *
77 */
78
tlen_presence_invisible(struct tlen_session * sesja)79 int tlen_presence_invisible (struct tlen_session *sesja)
80 {
81 tlen_socket_write_string (sesja, "<presence type='invisible'></presence>");
82 tlen_debug("Went to invisible\n");
83
84 return 1;
85 }
86
87 /*
88 * tlen_presence_disconnect()
89 *
90 * Funkcja wewn�trzna zamykaj�ca po��czenie z serwerem,
91 * wywo�ywana przez wys�anie statusu TLEN_STATUS_UNAVAILABLE
92 *
93 * - sesja - nasza sesja
94 *
95 * Nie u�ywa� w swoich programach, od tego jest tlen_presence()
96 *
97 */
98
tlen_presence_disconnect(struct tlen_session * sesja)99 int tlen_presence_disconnect (struct tlen_session *sesja)
100 {
101 tlen_socket_write_string (sesja, "<presence type='unavailable'/>");
102 tlen_socket_write_string (sesja, "</s>");
103 tlen_debug("Disconnected.\n");
104
105 return 1;
106 }
107
108 /*
109 * tlen_getroster()
110 *
111 * Wysy�a pro�b� o ksia�k� adresow�
112 *
113 * - sesja - nasza sesja
114 *
115 * brak uwag
116 *
117 */
118
tlen_getroster(struct tlen_session * sesja)119 int tlen_getroster (struct tlen_session *sesja)
120 {
121 tlen_socket_write_string (sesja, "<iq type='get' id='GetRoster'><query xmlns='jabber:iq:roster'></query></iq>");
122
123 return 1;
124 }
125
126 /*
127 * tlen_addcontact()
128 *
129 * Dodaje u�ytkownika do ksi��ki adresowej
130 *
131 * - sesja - nasza sesja
132 * - name - nazwa u�ytkownika
133 * - jid - identyfikator u�ytkownika w @tlen.pl
134 * - group - grupa dla u�ytkownika
135 *
136 * Po dodaniu wypada�oby poprosi� o subskrypcj� przez
137 * tlen_request_subscription()
138 *
139 */
140
tlen_addcontact(struct tlen_session * sesja,const char * name,const char * jid,const char * group)141 int tlen_addcontact (struct tlen_session *sesja, const char *name,
142 const char *jid, const char *group)
143 {
144 char *query;
145 char *fname;
146 char *fgroup=NULL;
147
148 if (!jid)
149 {
150 perror("NULL jid");
151 return 0;
152 }
153 if (!name)
154 {
155 fname = tlen_encode(jid);
156 }
157 else
158 fname = tlen_encode(name);
159 if (!group)
160 {
161 /* Mallocujemy +10, bo trzeba doliczyc 8 dlugosci sessid */
162 if (!(query = (char *) malloc (strlen("<iq type='set' id=''><query xmlns='jabber:iq:roster'><item name='' jid=''></item></query></iq>") + strlen (fname) + strlen (jid) + 10)))
163 {
164 perror ("malloc");
165 }
166
167 sprintf (query,"<iq type='set' id='%s'><query xmlns='jabber:iq:roster'><item name='%s' jid='%s'></item></query></iq>",sesja->sid, fname, jid);
168 }
169 else
170 {
171 fgroup = tlen_encode(group);
172 /* Mallocujemy +10, bo trzeba doliczyc 8 dlugosci sessid */
173 if (!(query = (char *) malloc (strlen("<iq type='set' id=''><query xmlns='jabber:iq:roster'><item name='' jid=''><group></group></item></query></iq>")
174 + strlen (fname) + strlen (jid) + strlen (fgroup) + 10)))
175 {
176 perror ("malloc");
177 }
178
179 sprintf (query,"<iq type='set' id='%s'><query xmlns='jabber:iq:roster'><item name='%s' jid='%s'><group>%s</group></item></query></iq>",
180 sesja->sid, fname, jid, fgroup);
181 }
182 tlen_socket_write_string (sesja, query);
183 free(query);
184 free(fname);
185 if(fgroup)
186 free(fgroup);
187 return 1;
188 }
189
190 /*
191 * tlen_removecontact()
192 *
193 * Usuwa u�ytkownika z ksi��ki adresowej
194 *
195 * - sesja - nasza sesja
196 * - jid - identyfikator w @tlen.pl
197 *
198 * Przed nale�y poprosi� o desubskrypcj� przez tlen_request_unsubscribe()
199 *
200 */
201
tlen_removecontact(struct tlen_session * sesja,const char * jid)202 int tlen_removecontact (struct tlen_session *sesja, const char *jid)
203 {
204 char *query;
205
206 if (!(query = (char *) malloc (strlen("<iq type='set'><query xmlns='jabber:iq:roster'><item jid='' subscription='remove'></item></query></iq>") + strlen (jid) + 2)))
207 {
208 perror ("malloc");
209 }
210
211 sprintf (query,"<iq type='set'><query xmlns='jabber:iq:roster'><item jid='%s' subscription='remove'></item></query></iq>", jid);
212 tlen_socket_write_string (sesja, query);
213 free(query);
214 return 1;
215
216 }
217
218 /*
219 * tlen_request_subscribe()
220 *
221 * Wysy�a pro�b� o subskrybcj�
222 *
223 * - sesja - nasza sesja
224 * - jid - identyfikator w @tlen.pl
225 *
226 */
227
tlen_request_subscribe(struct tlen_session * sesja,const char * jid)228 int tlen_request_subscribe (struct tlen_session *sesja, const char* jid)
229 {
230 char *query;
231 if (!(query = (char *) malloc (strlen ("<presence to='' type='subscribe'/>") + strlen(jid) + 1)))
232 {
233 perror ("malloc");
234 }
235 sprintf (query, "<presence to='%s' type='subscribe'/>", jid);
236 tlen_socket_write_string (sesja, query);
237 free (query);
238 return 1;
239 }
240
241 /*
242 * tlen_request_unsubscribe()
243 *
244 * Wysy�a pro�b� o desubskrybcj�
245 *
246 * - sesja - nasza sesja
247 * - jid - identyfikator w @tlen.pl
248 *
249 */
250
tlen_request_unsubscribe(struct tlen_session * sesja,const char * jid)251 int tlen_request_unsubscribe (struct tlen_session *sesja, const char *jid)
252 {
253 char *query;
254 if (!(query = (char *) malloc (strlen ("<presence to='' type='unsubscribe'/>") + strlen (jid) + 1)))
255 {
256 perror ("malloc");
257 }
258 sprintf (query, "<presence to='%s' type='unsubscribe'/>", jid);
259 tlen_socket_write_string (sesja, query);
260 free (query);
261 return 1;
262 }
263
264 /*
265 * tlen_accept_subcribe()
266 *
267 * Zgadza si� na subskrybcj�
268 *
269 * - sesja - nasza sesja
270 * - jid - identyfikator w @tlen.pl
271 *
272 */
273
tlen_accept_subscribe(struct tlen_session * sesja,const char * jid)274 int tlen_accept_subscribe (struct tlen_session *sesja, const char *jid)
275 {
276 char *query;
277
278 if (!(query = (char *) malloc (strlen ("<presence to='' type='subscribed'/>") + strlen (jid) + 1)))
279 {
280 perror ("malloc");
281 }
282 sprintf (query, "<presence to='%s' type='subscribed'/>", jid);
283 tlen_socket_write_string (sesja, query);
284 tlen_debug ("Subscription from %s accepted.\n", jid);
285
286 free (query);
287 return 1;
288 }
289
290 /*
291 * tlen_accept_unsubcribe()
292 *
293 * Zgadza si� na desubskrybcj�
294 *
295 * - sesja - nasza sesja
296 * - jid - identyfikator w @tlen.pl
297 *
298 */
299
tlen_accept_unsubscribe(struct tlen_session * sesja,const char * jid)300 int tlen_accept_unsubscribe (struct tlen_session *sesja, const char *jid)
301 {
302 char *query;
303
304 if (!(query = (char *) malloc (strlen ("<presence to='' type='unsubscribed'/>") + strlen (jid) + 1)))
305 {
306 perror ("malloc");
307 }
308 sprintf (query, "<presence to='%s' type='unsubscribed'/>", jid);
309 tlen_socket_write_string (sesja, query);
310 tlen_debug ("%s unsubscribed.\n", jid);
311
312 free (query);
313 return 1;
314 }
315