3a114273ac94fee72a8398124d9ba9e01b5e6144
[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 "ctdl_module.h"
49 #include "serv_xmpp.h"
50
51
52 /*
53  * This function is called by the XMPP service's async loop.
54  * If the client session has instant messages waiting, it outputs
55  * unsolicited XML stanzas containing them.
56  */
57 void xmpp_output_incoming_messages(void) {
58
59         struct ExpressMessage *ptr;
60         char xmlbuf1[4096];
61         char xmlbuf2[4096];
62
63         while (CC->FirstExpressMessage != NULL) {
64
65                 begin_critical_section(S_SESSION_TABLE);
66                 ptr = CC->FirstExpressMessage;
67                 CC->FirstExpressMessage = CC->FirstExpressMessage->next;
68                 end_critical_section(S_SESSION_TABLE);
69
70                 cprintf("<message to=\"%s\" from=\"%s\" type=\"chat\">",
71                         xmlesc(xmlbuf1, XMPP->client_jid, sizeof xmlbuf1),
72                         xmlesc(xmlbuf2, ptr->sender_email, sizeof xmlbuf2)
73                 );
74                 if (ptr->text != NULL) {
75                         striplt(ptr->text);
76                         cprintf("<body>%s</body>", xmlesc(xmlbuf1, ptr->text, sizeof xmlbuf1));
77                         free(ptr->text);
78                 }
79                 cprintf("</message>");
80                 free(ptr);
81         }
82 }
83
84
85 /*
86  * Client is sending a message.
87  */
88 void xmpp_send_message(char *message_to, char *message_body) {
89         char *recp = NULL;
90         struct CitContext *cptr;
91
92         if (message_body == NULL) return;
93         if (message_to == NULL) return;
94         if (IsEmptyStr(message_to)) return;
95         if (!CC->logged_in) return;
96
97         for (cptr = ContextList; cptr != NULL; cptr = cptr->next) {
98                 if (    (cptr->logged_in)
99                         && (cptr->can_receive_im)
100                         && (!strcasecmp(cptr->cs_principal_id, message_to))
101                 ) {
102                         recp = cptr->user.fullname;
103                 }
104         }
105
106         if (recp) {
107                 PerformXmsgHooks(CC->user.fullname, CC->cs_principal_id, recp, message_body);
108         }
109
110         free(XMPP->message_body);
111         XMPP->message_body = NULL;
112         XMPP->message_to[0] = 0;
113         time(&CC->lastidle);
114 }
115