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