b2e2dadf4869736fa5bc15a409f77505842d7917
[citadel.git] / citadel / modules / xmpp / xmpp_messages.c
1 /*
2  * Handle messages sent and received using XMPP (Jabber) protocol
3  *
4  * Copyright (c) 2007-2010 by Art Cancro
5  *
6  * This program is open source software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 3.
8  * 
9  * 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * 
17  * 
18  * 
19  *
20  */
21
22 #include "sysdep.h"
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <stdio.h>
26 #include <fcntl.h>
27 #include <signal.h>
28 #include <pwd.h>
29 #include <errno.h>
30 #include <sys/types.h>
31
32 #if TIME_WITH_SYS_TIME
33 # include <sys/time.h>
34 # include <time.h>
35 #else
36 # if HAVE_SYS_TIME_H
37 #  include <sys/time.h>
38 # else
39 #  include <time.h>
40 # endif
41 #endif
42
43 #include <sys/wait.h>
44 #include <string.h>
45 #include <limits.h>
46 #include <ctype.h>
47 #include <expat.h>
48 #include <libcitadel.h>
49 #include "citadel.h"
50 #include "server.h"
51 #include "citserver.h"
52 #include "support.h"
53 #include "config.h"
54 #include "internet_addressing.h"
55 #include "md5.h"
56 #include "ctdl_module.h"
57 #include "serv_xmpp.h"
58
59
60 /*
61  * This function is called by the XMPP service's async loop.
62  * If the client session has instant messages waiting, it outputs
63  * unsolicited XML stanzas containing them.
64  */
65 void xmpp_output_incoming_messages(void)
66 {
67         struct CitContext *CCC = CC;
68         citxmpp *Xmpp = XMPP;
69         struct ExpressMessage *ptr;
70
71         while (CCC->FirstExpressMessage != NULL) {
72
73                 begin_critical_section(S_SESSION_TABLE);
74                 ptr = CCC->FirstExpressMessage;
75                 CCC->FirstExpressMessage = CCC->FirstExpressMessage->next;
76                 end_critical_section(S_SESSION_TABLE);
77
78                 XPrint(HKEY("message"), 0,
79                        XCPROPERTY("type", "chat"),
80                        XPROPERTY("to", Xmpp->client_jid, strlen(Xmpp->client_jid)),
81                        XPROPERTY("from", ptr->sender_email, strlen(ptr->sender_email)),
82                        TYPE_ARGEND);
83
84                 if (ptr->text != NULL) {
85                         striplt(ptr->text);
86                         XPrint(HKEY("body"), XCLOSED,
87                                XBODY(ptr->text, strlen(ptr->text)),
88                                TYPE_ARGEND);
89                         free(ptr->text);
90                 }
91                 XPUT("</message>");
92                 free(ptr);
93         }
94         XUnbuffer();
95 }
96
97 /*
98  * Client is sending a message.
99  */
100 void xmpp_send_message(char *message_to, char *message_body) {
101         struct CitContext *CCC = CC;
102         char *recp = NULL;
103         struct CitContext *cptr;
104
105         if (message_body == NULL) return;
106         if (message_to == NULL) return;
107         if (IsEmptyStr(message_to)) return;
108         if (!CCC->logged_in) return;
109
110         for (cptr = ContextList; cptr != NULL; cptr = cptr->next) {
111                 if (    (cptr->logged_in)
112                         && (cptr->can_receive_im)
113                         && (!strcasecmp(cptr->cs_inet_email, message_to))
114                 ) {
115                         recp = cptr->user.fullname;
116                 }
117         }
118
119         if (recp) {
120                 PerformXmsgHooks(CCC->user.fullname, CCC->cs_inet_email, recp, message_body);
121         }
122
123         free(XMPP->message_body);
124         XMPP->message_body = NULL;
125         XMPP->message_to[0] = 0;
126         time(&CCC->lastidle);
127 }
128 void xmpp_end_message(void *data, const char *supplied_el, const char **attr)
129 {
130         xmpp_send_message(XMPP->message_to, XMPP->message_body);
131         XMPP->html_tag_level = 0;
132 }
133
134
135
136 CTDL_MODULE_INIT(xmpp_message)
137 {
138         if (!threading) {
139                 AddXMPPEndHandler(HKEY("message"),       xmpp_end_message, 0);
140         }
141         return "xmpp_message";
142 }