d989beb686c74d42381f72bf887883524d3deefc
[citadel.git] / citadel / modules / xmpp / xmpp_messages.c
1 /*
2  * $Id$ 
3  *
4  * Handle messages sent and received using XMPP (Jabber) protocol
5  *
6  * Copyright (c) 2007-2010 by Art Cancro
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 3 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
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23
24 #include "sysdep.h"
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <stdio.h>
28 #include <fcntl.h>
29 #include <signal.h>
30 #include <pwd.h>
31 #include <errno.h>
32 #include <sys/types.h>
33
34 #if TIME_WITH_SYS_TIME
35 # include <sys/time.h>
36 # include <time.h>
37 #else
38 # if HAVE_SYS_TIME_H
39 #  include <sys/time.h>
40 # else
41 #  include <time.h>
42 # endif
43 #endif
44
45 #include <sys/wait.h>
46 #include <string.h>
47 #include <limits.h>
48 #include <ctype.h>
49 #include <expat.h>
50 #include <libcitadel.h>
51 #include "citadel.h"
52 #include "server.h"
53 #include "citserver.h"
54 #include "support.h"
55 #include "config.h"
56 #include "internet_addressing.h"
57 #include "md5.h"
58 #include "ctdl_module.h"
59 #include "serv_xmpp.h"
60
61
62 /*
63  * This function is called by the XMPP service's async loop.
64  * If the client session has instant messages waiting, it outputs
65  * unsolicited XML stanzas containing them.
66  */
67 void xmpp_output_incoming_messages(void) {
68
69         struct ExpressMessage *ptr;
70         char xmlbuf1[4096];
71         char xmlbuf2[4096];
72
73         while (CC->FirstExpressMessage != NULL) {
74
75                 begin_critical_section(S_SESSION_TABLE);
76                 ptr = CC->FirstExpressMessage;
77                 CC->FirstExpressMessage = CC->FirstExpressMessage->next;
78                 end_critical_section(S_SESSION_TABLE);
79
80                 cprintf("<message to=\"%s\" from=\"%s\" type=\"chat\">",
81                         xmlesc(xmlbuf1, XMPP->client_jid, sizeof xmlbuf1),
82                         xmlesc(xmlbuf2, ptr->sender_email, sizeof xmlbuf2)
83                 );
84                 if (ptr->text != NULL) {
85                         striplt(ptr->text);
86                         cprintf("<body>%s</body>", xmlesc(xmlbuf1, ptr->text, sizeof xmlbuf1));
87                         free(ptr->text);
88                 }
89                 cprintf("</message>");
90                 free(ptr);
91         }
92 }
93
94 /*
95  * Client is sending a message.
96  */
97 void xmpp_send_message(char *message_to, char *message_body) {
98         char *recp = NULL;
99         int message_sent = 0;
100         struct CitContext *cptr;
101
102         if (message_body == NULL) return;
103         if (message_to == NULL) return;
104         if (IsEmptyStr(message_to)) return;
105         if (!CC->logged_in) return;
106
107         for (cptr = ContextList; cptr != NULL; cptr = cptr->next) {
108                 if (    (cptr->logged_in)
109                         && (cptr->can_receive_im)
110                         && (!strcasecmp(cptr->cs_inet_email, message_to))
111                 ) {
112                         recp = cptr->user.fullname;
113                 }
114         }
115
116         if (recp) {
117                 message_sent = PerformXmsgHooks(CC->user.fullname, CC->cs_inet_email, recp, message_body);
118         }
119
120         free(XMPP->message_body);
121         XMPP->message_body = NULL;
122         XMPP->message_to[0] = 0;
123         time(&CC->lastidle);
124 }
125