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