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