715abd41dc0524d05fc301c1e9a84773d5c7583c
[citadel.git] / citadel / modules / xmpp / serv_xmpp.h
1 /*
2  * Copyright (c) 2007-2009 by Art Cancro
3  *
4  *  This program is open source software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License version 3.
6  *  
7  *  
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  
15  *  
16  *  
17  */
18
19 typedef struct _citxmpp {                       /* Information about the current session */
20         StrBuf *OutBuf;
21         XML_Parser xp;                  /* XML parser instance for incoming client stream */
22         char server_name[256];          /* who they think we are */
23         char *chardata;
24         int chardata_len;
25         int chardata_alloc;
26         char client_jid[256];           /* "full JID" of the client */
27         int last_event_processed;
28
29         char iq_type[256];              /* for <iq> stanzas */
30         char iq_id[256];
31         char iq_from[256];
32         char iq_to[256];
33         char iq_client_username[256];   /* username requested by the client (NON SASL ONLY) */
34         char iq_client_password[256];   /* password requested by the client (NON SASL ONLY) */
35         char iq_client_resource[256];   /* resource name requested by the client */
36         int iq_session;                 /* nonzero == client is requesting a session */
37         char iq_query_xmlns[256];       /* Namespace of <query> */
38
39         char sasl_auth_mech[32];        /* SASL auth mechanism requested by the client */
40
41         char message_to[256];
42         char *message_body;             /* Message body in transit */
43         int html_tag_level;             /* <html> tag nesting level */
44
45         int bind_requested;             /* In this stanza, client is asking server to bind a resource. */
46         int ping_requested;             /* In this stanza, client is pinging the server. */
47 } citxmpp;
48
49 #define XMPP ((citxmpp *)CC->session_specific_data)
50
51 struct xmpp_event {
52         struct xmpp_event *next;
53         int event_seq;
54         time_t event_time;
55         int event_type;
56         char event_jid[256];
57         int session_which_generated_this_event;
58 };
59
60 extern struct xmpp_event *xmpp_queue;
61 extern int queue_event_seq;
62
63 enum {
64         XMPP_EVT_LOGIN,
65         XMPP_EVT_LOGOUT
66 };
67
68
69 typedef void (*xmpp_handler_func)(void *data, const char *supplied_el, const char **attr);
70
71 typedef struct __xmpp_handler {
72         int               Flags;
73         xmpp_handler_func Handler;
74 }xmpp_handler;
75
76
77 void xmpp_cleanup_function(void);
78 void xmpp_greeting(void);
79 void xmpp_command_loop(void);
80 void xmpp_async_loop(void);
81 void xmpp_sasl_auth(char *, char *);
82 void xmpp_output_auth_mechs(void);
83 void xmpp_query_namespace(char *, char *, char *, char *);
84 void xmpp_output_incoming_messages(void);
85 void xmpp_queue_event(int, char *);
86 void xmpp_process_events(void);
87 void xmpp_presence_notify(char *, int);
88 void xmpp_roster_item(struct CitContext *);
89 void xmpp_send_message(char *, char *);
90 void xmpp_non_sasl_authenticate(char *, char *, char *, char *);
91 void xmpp_massacre_roster(void);
92 void xmpp_delete_old_buddies_who_no_longer_exist_from_the_client_roster(void);
93 int xmpp_is_visible(struct CitContext *from, struct CitContext *to_whom);
94 char *xmlesc(char *buf, char *str, int bufsiz);
95
96 extern int XMPPSrvDebugEnable;
97
98 #define DBGLOG(LEVEL) if ((LEVEL != LOG_DEBUG) || (XMPPSrvDebugEnable != 0))
99
100 #define XMPP_syslog(LEVEL, FORMAT, ...)                         \
101         DBGLOG(LEVEL) syslog(LEVEL,                             \
102                              "XMPP: " FORMAT, __VA_ARGS__)
103
104 #define XMPPM_syslog(LEVEL, FORMAT)             \
105         DBGLOG(LEVEL) syslog(LEVEL,             \
106                              "XMPP: " FORMAT);
107
108
109 void XUnbuffer(void);
110 void XPutBody(const char *Str, long Len);
111 void XPutProp(const char *Str, long Len);
112 void XPut(const char *Str, long Len);
113 #define XPUT(CONSTSTR) XPut(CONSTSTR, sizeof(CONSTSTR) -1)
114
115 void XPrintf(const char *Format, ...);
116
117
118 void AddXMPPStartHandler(const char *key,
119                          long len,
120                          xmpp_handler_func Handler,
121                          int Flags);
122
123 void AddXMPPEndHandler(const char *key,
124                        long len,
125                        xmpp_handler_func Handler,
126                        int Flags);
127
128
129 #define XCLOSED (1<<0)
130 void XPrint(const char *Token, long tlen,
131             int Flags,
132             ...);
133
134 #define TYPE_STR 1
135 #define TYPE_OPTSTR 2
136 #define TYPE_INT 3
137 #define TYPE_BODYSTR 4
138 #define TYPE_ARGEND 5
139 #define XPROPERTY(NAME, VALUE, VLEN) TYPE_STR, NAME, sizeof(NAME)-1, VALUE, VLEN
140 #define XOPROPERTY(NAME, VALUE, VLEN) TYPE_OPTSTR, NAME, sizeof(NAME)-1, VALUE, VLEN
141 #define XCPROPERTY(NAME, VALUE) TYPE_STR, NAME, sizeof(NAME)-1, VALUE, sizeof(VALUE) - 1
142 #define XIPROPERTY(NAME, LVALUE) TYPE_INT, NAME, SIZEOF(NAME)-1
143 #define XBODY(VALUE, VLEN) TYPE_BODYSTR, VALUE, VLEN
144 #define XCFGBODY(WHICH) TYPE_BODYSTR, config.WHICH, configlen.WHICH