comparison src/protocols/jabber/jabber.h @ 7014:67c4e9d39242

[gaim-migrate @ 7577] Here it is, the bulk of the new Jabber prpl. Left to do: - Implement registration - Implement password changing - Keep track of conversation threads (since I apparently have to) - Fix the bugs that always magically appear in code after I commit committer: Tailor Script <tailor@pidgin.im>
author Nathan Walp <nwalp@pidgin.im>
date Mon, 29 Sep 2003 15:23:19 +0000
parents 988485669631
children 1350352f5818
comparison
equal deleted inserted replaced
7013:859cafb6433f 7014:67c4e9d39242
1 /* 1 /**
2 * This program is free software; you can redistribute it and/or modify 2 * @file jabber.h
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 * 3 *
7 * This program is distributed in the hope that it will be useful, 4 * gaim
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 * 5 *
12 * You should have received a copy of the GNU General Public License 6 * Copyright (C) 2003 Nathan Walp <faceprint@faceprint.com>
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 * 7 *
16 * Jabber 8 * This program is free software; you can redistribute it and/or modify
17 * Copyright (C) 1998-1999 The Jabber Team http://jabber.org/ 9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */ 21 */
22 #ifndef _GAIM_JABBER_H_
23 #define _GAIM_JABBER_H_
19 24
20 #include <sys/types.h> 25 #include <glib.h>
21 /*this must happen before sys/socket.h or freebsd won't compile*/ 26 #include "connection.h"
27 #include "sslconn.h"
22 28
23 #ifndef _WIN32 29 #include "jutil.h"
24 #include <syslog.h> 30 #include "xmlnode.h"
25 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <netdb.h>
28 #include <arpa/inet.h>
29 #include <strings.h>
30 #include <unistd.h>
31 #endif
32 31
33 #include <string.h> 32 typedef enum {
34 #include <stdlib.h> 33 JABBER_STREAM_OFFLINE,
35 #include <stdio.h> 34 JABBER_STREAM_CONNECTING,
36 #include <setjmp.h> 35 JABBER_STREAM_INITIALIZING,
37 #include <sys/stat.h> 36 JABBER_STREAM_AUTHENTICATING,
38 #include <fcntl.h> 37 JABBER_STREAM_REINITIALIZING,
39 #include <errno.h> 38 JABBER_STREAM_CONNECTED
40 #include <signal.h> 39 } JabberStreamState;
41 #include <stdarg.h>
42 #include <sys/time.h>
43 #include <time.h>
44 #include <ctype.h>
45 40
46 #include "libxode.h" 41 typedef struct _JabberStream
42 {
43 int fd;
47 44
48 #ifndef INCL_JABBER_H 45 GMarkupParseContext *context;
49 #define INCL_JABBER_H 46 xmlnode *current;
50 47
51 #ifdef __cplusplus 48 enum {
52 extern "C" { 49 JABBER_PROTO_0_9,
53 #endif 50 JABBER_PROTO_1_0
51 } protocol_version;
52 char *stream_id;
53 JabberStreamState state;
54 54
55 /* --------------------------------------------------------- */ 55 /* SASL authentication */
56 /* */ 56 char *expected_rspauth;
57 /* JID structures & constants */
58 /* */
59 /* --------------------------------------------------------- */
60 #define JID_RESOURCE 1
61 #define JID_USER 2
62 #define JID_SERVER 4
63 57
64 typedef struct jid_struct 58 GHashTable *buddies;
65 { 59 gboolean roster_parsed;
66 pool p;
67 char* resource;
68 char* user;
69 char* server;
70 char* full;
71 struct jid_struct *next; /* for lists of jids */
72 } *jid;
73
74 jid jid_new(pool p, char *idstr); /* Creates a jabber id from the idstr */
75 void jid_set(jid id, char *str, int item); /* Individually sets jid components */
76 char* jid_full(jid id); /* Builds a string type=user/resource@server from the jid data */
77 int jid_cmp(jid a, jid b); /* Compares two jid's, returns 0 for perfect match */
78 int jid_cmpx(jid a, jid b, int parts); /* Compares just the parts specified as JID_|JID_ */
79 jid jid_append(jid a, jid b); /* Appending b to a (list), no dups */
80 xmlnode jid_xres(jid id); /* Returns xmlnode representation of the resource?query=string */
81 xmlnode jid_nodescan(jid id, xmlnode x); /* Scans the children of the node for a matching jid attribute */
82 60
61 GHashTable *chats;
83 62
84 /* --------------------------------------------------------- */ 63 GHashTable *callbacks;
85 /* */ 64 int next_id;
86 /* JPacket structures & constants */
87 /* */
88 /* --------------------------------------------------------- */
89 #define JPACKET_UNKNOWN 0x00
90 #define JPACKET_MESSAGE 0x01
91 #define JPACKET_PRESENCE 0x02
92 #define JPACKET_IQ 0x04
93 #define JPACKET_S10N 0x08
94 65
95 #define JPACKET__UNKNOWN 0 66 time_t idle;
96 #define JPACKET__NONE 1
97 #define JPACKET__ERROR 2
98 #define JPACKET__CHAT 3
99 #define JPACKET__GROUPCHAT 4
100 #define JPACKET__GET 5
101 #define JPACKET__SET 6
102 #define JPACKET__RESULT 7
103 #define JPACKET__SUBSCRIBE 8
104 #define JPACKET__SUBSCRIBED 9
105 #define JPACKET__UNSUBSCRIBE 10
106 #define JPACKET__UNSUBSCRIBED 11
107 #define JPACKET__AVAILABLE 12
108 #define JPACKET__UNAVAILABLE 13
109 #define JPACKET__PROBE 14
110 #define JPACKET__HEADLINE 15
111 67
112 typedef struct jpacket_struct 68 JabberID *user;
113 { 69 GaimConnection *gc;
114 unsigned char type; 70 GaimSslConnection *gsc;
115 int subtype; 71 } JabberStream;
116 int flag;
117 void* aux1;
118 xmlnode x;
119 jid to;
120 jid from;
121 char* iqns;
122 xmlnode iq;
123 pool p;
124 } *jpacket, _jpacket;
125
126 jpacket jpacket_new(xmlnode x); /* Creates a jabber packet from the xmlnode */
127 jpacket jpacket_reset(jpacket p); /* Resets the jpacket values based on the xmlnode */
128 int jpacket_subtype(jpacket p); /* Returns the subtype value (looks at xmlnode for it) */
129 72
73 typedef void (JabberCallback)(JabberStream *js, xmlnode *packet);
130 74
131 /* --------------------------------------------------------- */ 75 void jabber_process_packet(JabberStream *js, xmlnode *packet);
132 /* */ 76 void jabber_send(JabberStream *js, xmlnode *data);
133 /* Presence Proxy DB structures & constants */ 77 void jabber_send_raw(JabberStream *js, const char *data);
134 /* */
135 /* --------------------------------------------------------- */
136 typedef struct ppdb_struct
137 {
138 jid id; /* entry data */
139 int pri;
140 xmlnode x;
141 struct ppdb_struct* user; /* linked list for user@server */
142 pool p; /* db-level data */
143 struct ppdb_struct* next;
144 } _ppdb, *ppdb;
145 78
146 ppdb ppdb_insert(ppdb db, jid id, xmlnode x); /* Inserts presence into the proxy */ 79 void jabber_stream_set_state(JabberStream *js, JabberStreamState state);
147 xmlnode ppdb_primary(ppdb db, jid id); /* Fetches the matching primary presence for the id */
148 void ppdb_free(ppdb db); /* Frees the db and all entries */
149 xmlnode ppdb_get(ppdb db, jid id); /* Called successively to return each presence xmlnode */
150 /* for the id and children, returns NULL at the end */
151 80
81 char *jabber_get_next_id(JabberStream *js);
152 82
153 /* --------------------------------------------------------- */ 83 #endif /* _GAIM_JABBER_H_ */
154 /* */
155 /* Simple Jabber Rate limit functions */
156 /* */
157 /* --------------------------------------------------------- */
158 typedef struct jlimit_struct
159 {
160 char *key;
161 int start;
162 int points;
163 int maxt, maxp;
164 pool p;
165 } *jlimit, _jlimit;
166
167 jlimit jlimit_new(int maxt, int maxp);
168 void jlimit_free(jlimit r);
169 int jlimit_check(jlimit r, char *key, int points);
170
171
172 /* --------------------------------------------------------- */
173 /* */
174 /* Error structures & constants */
175 /* */
176 /* --------------------------------------------------------- */
177 typedef struct terror_struct
178 {
179 int code;
180 char msg[64];
181 } terror;
182
183 #define TERROR_BAD (terror){400,"Bad Request"}
184 #define TERROR_AUTH (terror){401,"Unauthorized"}
185 #define TERROR_PAY (terror){402,"Payment Required"}
186 #define TERROR_FORBIDDEN (terror){403,"Forbidden"}
187 #define TERROR_NOTFOUND (terror){404,"Not Found"}
188 #define TERROR_NOTALLOWED (terror){405,"Not Allowed"}
189 #define TERROR_NOTACCEPTABLE (terror){406,"Not Acceptable"}
190 #define TERROR_REGISTER (terror){407,"Registration Required"}
191 #define TERROR_REQTIMEOUT (terror){408,"Request Timeout"}
192 #define TERROR_CONFLICT (terror){409,"Conflict"}
193
194 #define TERROR_INTERNAL (terror){500,"Internal Server Error"}
195 #define TERROR_NOTIMPL (terror){501,"Not Implemented"}
196 #define TERROR_EXTERNAL (terror){502,"Remote Server Error"}
197 #define TERROR_UNAVAIL (terror){503,"Service Unavailable"}
198 #define TERROR_EXTTIMEOUT (terror){504,"Remote Server Timeout"}
199 #define TERROR_DISCONNECTED (terror){510,"Disconnected"}
200
201 /* --------------------------------------------------------- */
202 /* */
203 /* Namespace constants */
204 /* */
205 /* --------------------------------------------------------- */
206 #define NSCHECK(x,n) (j_strcmp(xmlnode_get_attrib(x,"xmlns"),n) == 0)
207
208 #define NS_CLIENT "jabber:client"
209 #define NS_SERVER "jabber:server"
210 #define NS_AUTH "jabber:iq:auth"
211 #define NS_REGISTER "jabber:iq:register"
212 #define NS_ROSTER "jabber:iq:roster"
213 #define NS_OFFLINE "jabber:x:offline"
214 #define NS_AGENT "jabber:iq:agent"
215 #define NS_AGENTS "jabber:iq:agents"
216 #define NS_DELAY "jabber:x:delay"
217 #define NS_VERSION "jabber:iq:version"
218 #define NS_TIME "jabber:iq:time"
219 #define NS_VCARD "vcard-temp"
220 #define NS_PRIVATE "jabber:iq:private"
221 #define NS_SEARCH "jabber:iq:search"
222 #define NS_OOB "jabber:iq:oob"
223 #define NS_XOOB "jabber:x:oob"
224 #define NS_ADMIN "jabber:iq:admin"
225 #define NS_FILTER "jabber:iq:filter"
226 #define NS_AUTH_0K "jabber:iq:auth:0k"
227
228
229 /* --------------------------------------------------------- */
230 /* */
231 /* Message Types */
232 /* */
233 /* --------------------------------------------------------- */
234 #define TMSG_NORMAL "normal"
235 #define TMSG_ERROR "error"
236 #define TMSG_CHAT "chat"
237 #define TMSG_GROUPCHAT "groupchat"
238 #define TMSG_HEADLINE "headline"
239
240
241 /* --------------------------------------------------------- */
242 /* */
243 /* JUtil functions */
244 /* */
245 /* --------------------------------------------------------- */
246 xmlnode jutil_presnew(int type, char *to, char *status); /* Create a skeleton presence packet */
247 xmlnode jutil_iqnew(int type, char *ns); /* Create a skeleton iq packet */
248 xmlnode jutil_msgnew(char *type, char *to, char *subj, char *body);
249 /* Create a skeleton message packet */
250 xmlnode jutil_header(char* xmlns, char* server); /* Create a skeleton stream packet */
251 int jutil_priority(xmlnode x); /* Determine priority of this packet */
252 void jutil_tofrom(xmlnode x); /* Swaps to/from fields on a packet */
253 xmlnode jutil_iqresult(xmlnode x); /* Generate a skeleton iq/result, given a iq/query */
254 char* jutil_timestamp(void); /* Get stringified timestamp */
255 void jutil_error(xmlnode x, terror E); /* Append an <error> node to x */
256 void jutil_delay(xmlnode msg, char *reason); /* Append a delay packet to msg */
257 char* jutil_regkey(char *key, char *seed); /* pass a seed to generate a key, pass the key again to validate (returns it) */
258
259
260 /* --------------------------------------------------------- */
261 /* */
262 /* JConn structures & functions */
263 /* */
264 /* --------------------------------------------------------- */
265 #define JCONN_STATE_OFF 0
266 #define JCONN_STATE_CONNECTED 1
267 #define JCONN_STATE_ON 2
268 #define JCONN_STATE_AUTH 3
269
270 typedef struct jconn_struct
271 {
272 /* Core structure */
273 pool p; /* Memory allocation pool */
274 int state; /* Connection state flag */
275 int fd; /* Connection file descriptor */
276 jid user; /* User info */
277 char *pass; /* User passwd */
278
279 /* Stream stuff */
280 int id; /* id counter for jab_getid() function */
281 char idbuf[9]; /* temporary storage for jab_getid() */
282 char *sid; /* stream id from server, for digest auth */
283 XML_Parser parser; /* Parser instance */
284 xmlnode current; /* Current node in parsing instance.. */
285
286 /* Event callback ptrs */
287 void (*on_state)(struct jconn_struct *j, int state);
288 void (*on_packet)(struct jconn_struct *j, jpacket p);
289
290 } *jconn, jconn_struct;
291
292 typedef void (*jconn_state_h)(jconn j, int state);
293 typedef void (*jconn_packet_h)(jconn j, jpacket p);
294
295
296 jconn jab_new(char *user, char *pass);
297 void jab_delete(jconn j);
298 void jab_state_handler(jconn j, jconn_state_h h);
299 void jab_packet_handler(jconn j, jconn_packet_h h);
300 void jab_start(jconn j);
301 void jab_stop(jconn j);
302
303 int jab_getfd(jconn j);
304 jid jab_getjid(jconn j);
305 char *jab_getsid(jconn j);
306 char *jab_getid(jconn j);
307
308 void jab_send(jconn j, xmlnode x);
309 void jab_send_raw(jconn j, const char *str);
310 void jab_recv(jconn j);
311 void jab_poll(jconn j, int timeout);
312
313 char *jab_auth(jconn j);
314 char *jab_reg(jconn j);
315
316
317
318 #ifdef __cplusplus
319 }
320 #endif
321
322 #endif /* INCL_JABBER_H */