4fa690b359d25454110be4424cf3da245b6688d0
[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  * 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 ExpressMessage *ptr;
61         char xmlbuf1[4096];
62         char xmlbuf2[4096];
63
64         while (CC->FirstExpressMessage != NULL) {
65
66                 begin_critical_section(S_SESSION_TABLE);
67                 ptr = CC->FirstExpressMessage;
68                 CC->FirstExpressMessage = CC->FirstExpressMessage->next;
69                 end_critical_section(S_SESSION_TABLE);
70
71                 cprintf("<message to=\"%s\" from=\"%s\" type=\"chat\">",
72                         xmlesc(xmlbuf1, XMPP->client_jid, sizeof xmlbuf1),
73                         xmlesc(xmlbuf2, ptr->sender_email, sizeof xmlbuf2)
74                 );
75                 if (ptr->text != NULL) {
76                         striplt(ptr->text);
77                         cprintf("<body>%s</body>", xmlesc(xmlbuf1, ptr->text, sizeof xmlbuf1));
78                         free(ptr->text);
79                 }
80                 cprintf("</message>");
81                 free(ptr);
82         }
83 }
84
85
86 /*
87  * Client is sending a message.
88  */
89 void xmpp_send_message(char *message_to, char *message_body) {
90         char *recp = NULL;
91         struct CitContext *cptr;
92
93         if (message_body == NULL) return;
94         if (message_to == NULL) return;
95         if (IsEmptyStr(message_to)) return;
96         if (!CC->logged_in) return;
97
98         for (cptr = ContextList; cptr != NULL; cptr = cptr->next) {
99                 if (    (cptr->logged_in)
100                         && (cptr->can_receive_im)
101                         && (!strcasecmp(cptr->cs_inet_email, message_to))
102                 ) {
103                         recp = cptr->user.fullname;
104                 }
105         }
106
107         if (recp) {
108                 PerformXmsgHooks(CC->user.fullname, CC->cs_inet_email, recp, message_body);
109         }
110
111         free(XMPP->message_body);
112         XMPP->message_body = NULL;
113         XMPP->message_to[0] = 0;
114         time(&CC->lastidle);
115 }
116