Changeset 85d7b85 for protocols/jabber/jabber.h
- Timestamp:
- 2008-04-02T14:22:57Z (16 years ago)
- Branches:
- master
- Children:
- f9dbc99
- Parents:
- 875ad42 (diff), dd34575 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
protocols/jabber/jabber.h
r875ad42 r85d7b85 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 <time.h> 31 #include <ctype.h> 32 #ifdef _WIN32 33 #undef DATADIR 34 #include "sock.h" 1 /***************************************************************************\ 2 * * 3 * BitlBee - An IRC to IM gateway * 4 * Jabber module - Main file * 5 * * 6 * Copyright 2006 Wilmer van der Gaast <wilmer@gaast.net> * 7 * * 8 * This program is free software; you can redistribute it and/or modify * 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 along * 19 * with this program; if not, write to the Free Software Foundation, Inc., * 20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * 21 * * 22 \***************************************************************************/ 23 24 #ifndef _JABBER_H 25 #define _JABBER_H 26 27 #include <glib.h> 28 29 #include "xmltree.h" 30 #include "bitlbee.h" 31 32 extern GSList *jabber_connections; 33 34 typedef enum 35 { 36 JFLAG_STREAM_STARTED = 1, /* Set when we detected the beginning of the stream 37 and want to do auth. */ 38 JFLAG_AUTHENTICATED = 2, /* Set when we're successfully authenticatd. */ 39 JFLAG_STREAM_RESTART = 4, /* Set when we want to restart the stream (after 40 SASL or TLS). */ 41 JFLAG_WAIT_SESSION = 8, /* Set if we sent a <session> tag and need a reply 42 before we continue. */ 43 JFLAG_WAIT_BIND = 16, /* ... for <bind> tag. */ 44 JFLAG_WANT_TYPING = 32, /* Set if we ever sent a typing notification, this 45 activates all XEP-85 related code. */ 46 JFLAG_XMLCONSOLE = 64, /* If the user added an xmlconsole buddy. */ 47 } jabber_flags_t; 48 49 typedef enum 50 { 51 JBFLAG_PROBED_XEP85 = 1, /* Set this when we sent our probe packet to make 52 sure it gets sent only once. */ 53 JBFLAG_DOES_XEP85 = 2, /* Set this when the resource seems to support 54 XEP85 (typing notification shite). */ 55 JBFLAG_IS_CHATROOM = 4, /* It's convenient to use this JID thingy for 56 groupchat state info too. */ 57 JBFLAG_IS_ANONYMOUS = 8, /* For anonymous chatrooms, when we don't have 58 have a real JID. */ 59 } jabber_buddy_flags_t; 60 61 typedef enum 62 { 63 JCFLAG_MESSAGE_SENT = 1, /* Set this after sending the first message, so 64 we can detect echoes/backlogs. */ 65 } jabber_chat_flags_t; 66 67 struct jabber_data 68 { 69 struct im_connection *ic; 70 71 int fd; 72 void *ssl; 73 char *txq; 74 int tx_len; 75 int r_inpa, w_inpa; 76 77 struct xt_parser *xt; 78 jabber_flags_t flags; 79 80 char *username; /* USERNAME@server */ 81 char *server; /* username@SERVER -=> server/domain, not hostname */ 82 83 /* After changing one of these two (or the priority setting), call 84 presence_send_update() to inform the server about the changes. */ 85 struct jabber_away_state *away_state; 86 char *away_message; 87 88 char *cached_id_prefix; 89 GHashTable *node_cache; 90 GHashTable *buddies; 91 }; 92 93 struct jabber_away_state 94 { 95 char code[5]; 96 char *full_name; 97 }; 98 99 typedef xt_status (*jabber_cache_event) ( struct im_connection *ic, struct xt_node *node, struct xt_node *orig ); 100 101 struct jabber_cache_entry 102 { 103 time_t saved_at; 104 struct xt_node *node; 105 jabber_cache_event func; 106 }; 107 108 struct jabber_buddy 109 { 110 char *bare_jid; 111 char *full_jid; 112 char *resource; 113 114 char *ext_jid; /* The JID to use in BitlBee. The real JID if possible, */ 115 /* otherwise something similar to the conference JID. */ 116 117 int priority; 118 struct jabber_away_state *away_state; 119 char *away_message; 120 121 time_t last_act; 122 jabber_buddy_flags_t flags; 123 124 struct jabber_buddy *next; 125 }; 126 127 struct jabber_chat 128 { 129 int flags; 130 char *name; 131 char *my_full_jid; /* Separate copy because of case sensitivity. */ 132 struct jabber_buddy *me; 133 }; 134 135 #define JABBER_XMLCONSOLE_HANDLE "xmlconsole" 136 137 #define JABBER_PORT_DEFAULT "5222" 138 #define JABBER_PORT_MIN 5220 139 #define JABBER_PORT_MAX 5229 140 141 /* Prefixes to use for packet IDs (mainly for IQ packets ATM). Usually the 142 first one should be used, but when storing a packet in the cache, a 143 "special" kind of ID is assigned to make it easier later to figure out 144 if we have to do call an event handler for the response packet. Also 145 we'll append a hash to make sure we won't trigger on cached packets from 146 other BitlBee users. :-) */ 147 #define JABBER_PACKET_ID "BeeP" 148 #define JABBER_CACHED_ID "BeeC" 149 150 /* The number of seconds to keep cached packets before garbage collecting 151 them. This gc is done on every keepalive (every minute). */ 152 #define JABBER_CACHE_MAX_AGE 600 153 154 /* RFC 392[01] stuff */ 155 #define XMLNS_TLS "urn:ietf:params:xml:ns:xmpp-tls" 156 #define XMLNS_SASL "urn:ietf:params:xml:ns:xmpp-sasl" 157 #define XMLNS_BIND "urn:ietf:params:xml:ns:xmpp-bind" 158 #define XMLNS_SESSION "urn:ietf:params:xml:ns:xmpp-session" 159 #define XMLNS_STANZA_ERROR "urn:ietf:params:xml:ns:xmpp-stanzas" 160 #define XMLNS_STREAM_ERROR "urn:ietf:params:xml:ns:xmpp-streams" 161 #define XMLNS_ROSTER "jabber:iq:roster" 162 163 /* Some supported extensions/legacy stuff */ 164 #define XMLNS_AUTH "jabber:iq:auth" /* XEP-0078 */ 165 #define XMLNS_VERSION "jabber:iq:version" /* XEP-0092 */ 166 #define XMLNS_TIME "jabber:iq:time" /* XEP-0090 */ 167 #define XMLNS_PING "urn:xmpp:ping" /* XEP-0199 */ 168 #define XMLNS_VCARD "vcard-temp" /* XEP-0054 */ 169 #define XMLNS_DELAY "jabber:x:delay" /* XEP-0091 */ 170 #define XMLNS_CHATSTATES "http://jabber.org/protocol/chatstates" /* 0085 */ 171 #define XMLNS_DISCOVER "http://jabber.org/protocol/disco#info" /* 0030 */ 172 #define XMLNS_MUC "http://jabber.org/protocol/muc" /* XEP-0045 */ 173 #define XMLNS_MUC_USER "http://jabber.org/protocol/muc#user"/* XEP-0045 */ 174 #define XMLNS_CAPS "http://jabber.org/protocol/caps" /* XEP-0115 */ 175 176 /* iq.c */ 177 xt_status jabber_pkt_iq( struct xt_node *node, gpointer data ); 178 int jabber_init_iq_auth( struct im_connection *ic ); 179 xt_status jabber_pkt_bind_sess( struct im_connection *ic, struct xt_node *node, struct xt_node *orig ); 180 int jabber_get_roster( struct im_connection *ic ); 181 int jabber_get_vcard( struct im_connection *ic, char *bare_jid ); 182 int jabber_add_to_roster( struct im_connection *ic, char *handle, char *name ); 183 int jabber_remove_from_roster( struct im_connection *ic, char *handle ); 184 185 /* message.c */ 186 xt_status jabber_pkt_message( struct xt_node *node, gpointer data ); 187 188 /* presence.c */ 189 xt_status jabber_pkt_presence( struct xt_node *node, gpointer data ); 190 int presence_send_update( struct im_connection *ic ); 191 int presence_send_request( struct im_connection *ic, char *handle, char *request ); 192 193 /* jabber_util.c */ 194 char *set_eval_priority( set_t *set, char *value ); 195 char *set_eval_tls( set_t *set, char *value ); 196 struct xt_node *jabber_make_packet( char *name, char *type, char *to, struct xt_node *children ); 197 struct xt_node *jabber_make_error_packet( struct xt_node *orig, char *err_cond, char *err_type ); 198 void jabber_cache_add( struct im_connection *ic, struct xt_node *node, jabber_cache_event func ); 199 struct xt_node *jabber_cache_get( struct im_connection *ic, char *id ); 200 void jabber_cache_entry_free( gpointer entry ); 201 void jabber_cache_clean( struct im_connection *ic ); 202 xt_status jabber_cache_handle_packet( struct im_connection *ic, struct xt_node *node ); 203 const struct jabber_away_state *jabber_away_state_by_code( char *code ); 204 const struct jabber_away_state *jabber_away_state_by_name( char *name ); 205 void jabber_buddy_ask( struct im_connection *ic, char *handle ); 206 char *jabber_normalize( const char *orig ); 207 208 typedef enum 209 { 210 GET_BUDDY_CREAT = 1, /* Try to create it, if necessary. */ 211 GET_BUDDY_EXACT = 2, /* Get an exact match (only makes sense with bare JIDs). */ 212 GET_BUDDY_FIRST = 4, /* No selection, simply get the first resource for this JID. */ 213 } get_buddy_flags_t; 214 215 struct jabber_error 216 { 217 char *code, *text, *type; 218 }; 219 220 struct jabber_buddy *jabber_buddy_add( struct im_connection *ic, char *full_jid ); 221 struct jabber_buddy *jabber_buddy_by_jid( struct im_connection *ic, char *jid, get_buddy_flags_t flags ); 222 struct jabber_buddy *jabber_buddy_by_ext_jid( struct im_connection *ic, char *jid, get_buddy_flags_t flags ); 223 int jabber_buddy_remove( struct im_connection *ic, char *full_jid ); 224 int jabber_buddy_remove_bare( struct im_connection *ic, char *bare_jid ); 225 time_t jabber_get_timestamp( struct xt_node *xt ); 226 struct jabber_error *jabber_error_parse( struct xt_node *node, char *xmlns ); 227 void jabber_error_free( struct jabber_error *err ); 228 229 extern const struct jabber_away_state jabber_away_state_list[]; 230 231 /* io.c */ 232 int jabber_write_packet( struct im_connection *ic, struct xt_node *node ); 233 int jabber_write( struct im_connection *ic, char *buf, int len ); 234 gboolean jabber_connected_plain( gpointer data, gint source, b_input_condition cond ); 235 gboolean jabber_connected_ssl( gpointer data, void *source, b_input_condition cond ); 236 gboolean jabber_start_stream( struct im_connection *ic ); 237 void jabber_end_stream( struct im_connection *ic ); 238 239 /* sasl.c */ 240 xt_status sasl_pkt_mechanisms( struct xt_node *node, gpointer data ); 241 xt_status sasl_pkt_challenge( struct xt_node *node, gpointer data ); 242 xt_status sasl_pkt_result( struct xt_node *node, gpointer data ); 243 gboolean sasl_supported( struct im_connection *ic ); 244 245 /* conference.c */ 246 struct groupchat *jabber_chat_join( struct im_connection *ic, char *room, char *nick, char *password ); 247 struct groupchat *jabber_chat_by_jid( struct im_connection *ic, const char *name ); 248 void jabber_chat_free( struct groupchat *c ); 249 int jabber_chat_msg( struct groupchat *ic, char *message, int flags ); 250 int jabber_chat_topic( struct groupchat *c, char *topic ); 251 int jabber_chat_leave( struct groupchat *c, const char *reason ); 252 void jabber_chat_pkt_presence( struct im_connection *ic, struct jabber_buddy *bud, struct xt_node *node ); 253 void jabber_chat_pkt_message( struct im_connection *ic, struct jabber_buddy *bud, struct xt_node *node ); 254 void jabber_chat_invite( struct groupchat *c, char *who, char *message ); 255 35 256 #endif 36 37 #include "lib.h"38 39 40 #ifndef INCL_JABBER_H41 #define INCL_JABBER_H42 43 #ifdef __cplusplus44 extern "C" {45 #endif46 47 /* --------------------------------------------------------- */48 /* */49 /* JID structures & constants */50 /* */51 /* --------------------------------------------------------- */52 #define JID_RESOURCE 153 #define JID_USER 254 #define JID_SERVER 455 56 typedef struct jid_struct57 {58 pool p;59 char* resource;60 char* user;61 char* server;62 char* full;63 struct jid_struct *next; /* for lists of jids */64 } *jid;65 66 jid jid_new(pool p, char *idstr); /* Creates a jabber id from the idstr */67 void jid_set(jid id, char *str, int item); /* Individually sets jid components */68 char* jid_full(jid id); /* Builds a string type=user/resource@server from the jid data */69 int jid_cmp(jid a, jid b); /* Compares two jid's, returns 0 for perfect match */70 int jid_cmpx(jid a, jid b, int parts); /* Compares just the parts specified as JID_|JID_ */71 jid jid_append(jid a, jid b); /* Appending b to a (list), no dups */72 xmlnode jid_xres(jid id); /* Returns xmlnode representation of the resource?query=string */73 xmlnode jid_nodescan(jid id, xmlnode x); /* Scans the children of the node for a matching jid attribute */74 jid jid_user(jid a); /* returns the same jid but just of the user@host part */75 76 77 /* --------------------------------------------------------- */78 /* */79 /* JPacket structures & constants */80 /* */81 /* --------------------------------------------------------- */82 #define JPACKET_UNKNOWN 0x0083 #define JPACKET_MESSAGE 0x0184 #define JPACKET_PRESENCE 0x0285 #define JPACKET_IQ 0x0486 #define JPACKET_S10N 0x0887 88 #define JPACKET__UNKNOWN 089 #define JPACKET__NONE 190 #define JPACKET__ERROR 291 #define JPACKET__CHAT 392 #define JPACKET__GROUPCHAT 493 #define JPACKET__GET 594 #define JPACKET__SET 695 #define JPACKET__RESULT 796 #define JPACKET__SUBSCRIBE 897 #define JPACKET__SUBSCRIBED 998 #define JPACKET__UNSUBSCRIBE 1099 #define JPACKET__UNSUBSCRIBED 11100 #define JPACKET__AVAILABLE 12101 #define JPACKET__UNAVAILABLE 13102 #define JPACKET__PROBE 14103 #define JPACKET__HEADLINE 15104 #define JPACKET__INVISIBLE 16105 106 typedef struct jpacket_struct107 {108 unsigned char type;109 int subtype;110 int flag;111 void* aux1;112 xmlnode x;113 jid to;114 jid from;115 char* iqns;116 xmlnode iq;117 pool p;118 } *jpacket, _jpacket;119 120 jpacket jpacket_new(xmlnode x); /* Creates a jabber packet from the xmlnode */121 int jpacket_subtype(jpacket p); /* Returns the subtype value (looks at xmlnode for it) */122 123 124 /* --------------------------------------------------------- */125 /* */126 /* Presence Proxy DB structures & constants */127 /* */128 /* --------------------------------------------------------- */129 typedef struct ppdb_struct130 {131 jid id; /* entry data */132 int pri;133 xmlnode x;134 struct ppdb_struct* user; /* linked list for user@server */135 pool p; /* db-level data */136 struct ppdb_struct* next;137 } _ppdb, *ppdb;138 139 ppdb ppdb_insert(ppdb db, jid id, xmlnode x); /* Inserts presence into the proxy */140 xmlnode ppdb_primary(ppdb db, jid id); /* Fetches the matching primary presence for the id */141 void ppdb_free(ppdb db); /* Frees the db and all entries */142 xmlnode ppdb_get(ppdb db, jid id); /* Called successively to return each presence xmlnode */143 /* for the id and children, returns NULL at the end */144 145 146 /* --------------------------------------------------------- */147 /* */148 /* Simple Jabber Rate limit functions */149 /* */150 /* --------------------------------------------------------- */151 typedef struct jlimit_struct152 {153 char *key;154 int start;155 int points;156 int maxt, maxp;157 pool p;158 } *jlimit, _jlimit;159 160 jlimit jlimit_new(int maxt, int maxp);161 void jlimit_free(jlimit r);162 int jlimit_check(jlimit r, char *key, int points);163 164 165 /* --------------------------------------------------------- */166 /* */167 /* Error structures & constants */168 /* */169 /* --------------------------------------------------------- */170 typedef struct terror_struct171 {172 int code;173 char msg[64];174 } terror;175 176 #define TERROR_BAD (terror){400,"Bad Request"}177 #define TERROR_AUTH (terror){401,"Unauthorized"}178 #define TERROR_PAY (terror){402,"Payment Required"}179 #define TERROR_FORBIDDEN (terror){403,"Forbidden"}180 #define TERROR_NOTFOUND (terror){404,"Not Found"}181 #define TERROR_NOTALLOWED (terror){405,"Not Allowed"}182 #define TERROR_NOTACCEPTABLE (terror){406,"Not Acceptable"}183 #define TERROR_REGISTER (terror){407,"Registration Required"}184 #define TERROR_REQTIMEOUT (terror){408,"Request Timeout"}185 #define TERROR_CONFLICT (terror){409,"Conflict"}186 187 #define TERROR_INTERNAL (terror){500,"Internal Server Error"}188 #define TERROR_NOTIMPL (terror){501,"Not Implemented"}189 #define TERROR_EXTERNAL (terror){502,"Remote Server Error"}190 #define TERROR_UNAVAIL (terror){503,"Service Unavailable"}191 #define TERROR_EXTTIMEOUT (terror){504,"Remote Server Timeout"}192 #define TERROR_DISCONNECTED (terror){510,"Disconnected"}193 194 /* --------------------------------------------------------- */195 /* */196 /* Namespace constants */197 /* */198 /* --------------------------------------------------------- */199 #define NSCHECK(x,n) (j_strcmp(xmlnode_get_attrib(x,"xmlns"),n) == 0)200 201 #define NS_CLIENT "jabber:client"202 #define NS_SERVER "jabber:server"203 #define NS_AUTH "jabber:iq:auth"204 #define NS_REGISTER "jabber:iq:register"205 #define NS_ROSTER "jabber:iq:roster"206 #define NS_OFFLINE "jabber:x:offline"207 #define NS_AGENT "jabber:iq:agent"208 #define NS_AGENTS "jabber:iq:agents"209 #define NS_DELAY "jabber:x:delay"210 #define NS_VERSION "jabber:iq:version"211 #define NS_TIME "jabber:iq:time"212 #define NS_VCARD "vcard-temp"213 #define NS_PRIVATE "jabber:iq:private"214 #define NS_SEARCH "jabber:iq:search"215 #define NS_OOB "jabber:iq:oob"216 #define NS_XOOB "jabber:x:oob"217 #define NS_ADMIN "jabber:iq:admin"218 #define NS_FILTER "jabber:iq:filter"219 #define NS_AUTH_0K "jabber:iq:auth:0k"220 221 222 /* --------------------------------------------------------- */223 /* */224 /* Message Types */225 /* */226 /* --------------------------------------------------------- */227 #define TMSG_NORMAL "normal"228 #define TMSG_ERROR "error"229 #define TMSG_CHAT "chat"230 #define TMSG_GROUPCHAT "groupchat"231 #define TMSG_HEADLINE "headline"232 233 234 /* --------------------------------------------------------- */235 /* */236 /* JUtil functions */237 /* */238 /* --------------------------------------------------------- */239 xmlnode jutil_presnew(int type, char *to, char *status); /* Create a skeleton presence packet */240 xmlnode jutil_iqnew(int type, char *ns); /* Create a skeleton iq packet */241 xmlnode jutil_msgnew(char *type, char *to, char *subj, char *body);242 /* Create a skeleton message packet */243 xmlnode jutil_header(char* xmlns, char* server); /* Create a skeleton stream packet */244 int jutil_priority(xmlnode x); /* Determine priority of this packet */245 void jutil_tofrom(xmlnode x); /* Swaps to/from fields on a packet */246 xmlnode jutil_iqresult(xmlnode x); /* Generate a skeleton iq/result, given a iq/query */247 char* jutil_timestamp(void); /* Get stringified timestamp */248 void jutil_error(xmlnode x, terror E); /* Append an <error> node to x */249 void jutil_delay(xmlnode msg, char *reason); /* Append a delay packet to msg */250 char* jutil_regkey(char *key, char *seed); /* pass a seed to generate a key, pass the key again to validate (returns it) */251 252 253 /* --------------------------------------------------------- */254 /* */255 /* JConn structures & functions */256 /* */257 /* --------------------------------------------------------- */258 #define JCONN_STATE_OFF 0259 #define JCONN_STATE_CONNECTED 1260 #define JCONN_STATE_ON 2261 #define JCONN_STATE_AUTH 3262 263 typedef struct jconn_struct264 {265 /* Core structure */266 pool p; /* Memory allocation pool */267 int state; /* Connection state flag */268 int fd; /* Connection file descriptor */269 jid user; /* User info */270 char *pass; /* User passwd */271 272 /* Stream stuff */273 int id; /* id counter for jab_getid() function */274 char idbuf[9]; /* temporary storage for jab_getid() */275 char *sid; /* stream id from server, for digest auth */276 XML_Parser parser; /* Parser instance */277 xmlnode current; /* Current node in parsing instance.. */278 279 /* Event callback ptrs */280 void (*on_state)(struct jconn_struct *j, int state);281 void (*on_packet)(struct jconn_struct *j, jpacket p);282 283 } *jconn, jconn_struct;284 285 typedef void (*jconn_state_h)(jconn j, int state);286 typedef void (*jconn_packet_h)(jconn j, jpacket p);287 288 289 jconn jab_new(char *user, char *pass);290 void jab_delete(jconn j);291 void jab_state_handler(jconn j, jconn_state_h h);292 void jab_packet_handler(jconn j, jconn_packet_h h);293 void jab_start(jconn j);294 void jab_stop(jconn j);295 296 int jab_getfd(jconn j);297 jid jab_getjid(jconn j);298 char *jab_getsid(jconn j);299 char *jab_getid(jconn j);300 301 void jab_send(jconn j, xmlnode x);302 void jab_send_raw(jconn j, const char *str);303 void jab_recv(jconn j);304 void jab_poll(jconn j, int timeout);305 306 char *jab_auth(jconn j);307 char *jab_reg(jconn j);308 309 310 311 #ifdef __cplusplus312 }313 #endif314 315 #endif /* INCL_JABBER_H */
Note: See TracChangeset
for help on using the changeset viewer.