* More license declarations
[citadel.git] / citadel / modules / jabber / xmpp_messages.c
1 /*
2  * $Id$ 
3  *
4  * Handle messages sent and received using XMPP (Jabber) protocol
5  *
6  * Copyright (c) 2007-2009 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 jabber_output_incoming_messages(void) {
68
69         struct ExpressMessage *ptr;
70
71         while (CC->FirstExpressMessage != NULL) {
72
73                 begin_critical_section(S_SESSION_TABLE);
74                 ptr = CC->FirstExpressMessage;
75                 CC->FirstExpressMessage = CC->FirstExpressMessage->next;
76                 end_critical_section(S_SESSION_TABLE);
77
78                 cprintf("<message to=\"%s\" from=\"%s\" type=\"chat\">",
79                         XMPP->client_jid,
80                         ptr->sender_email);
81                 if (ptr->text != NULL) {
82                         striplt(ptr->text);
83                         cprintf("<body>%s</body>", ptr->text);
84                         free(ptr->text);
85                 }
86                 cprintf("</message>");
87                 free(ptr);
88         }
89 }
90
91 /*
92  * Client is sending a message.
93  */
94 void jabber_send_message(char *message_to, char *message_body) {
95         char *recp = NULL;
96         int message_sent = 0;
97         struct CitContext *cptr;
98
99         if (message_body == NULL) return;
100         if (message_to == NULL) return;
101         if (IsEmptyStr(message_to)) return;
102         if (!CC->logged_in) return;
103
104         for (cptr = ContextList; cptr != NULL; cptr = cptr->next) {
105                 if (    (cptr->logged_in)
106                         && (cptr->can_receive_im)
107                         && (!strcasecmp(cptr->cs_inet_email, message_to))
108                 ) {
109                         recp = cptr->user.fullname;
110                 }
111         }
112
113         if (recp) {
114                 message_sent = PerformXmsgHooks(CC->user.fullname, CC->cs_inet_email, recp, message_body);
115         }
116
117         free(XMPP->message_body);
118         XMPP->message_body = NULL;
119         XMPP->message_to[0] = 0;
120         time(&CC->lastidle);
121 }
122