e579c7cfe602b35853208d78d02931ffd7337790
[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 by Art Cancro
7  * This code is released under the terms of the GNU General Public License.
8  *
9  */
10
11 #include "sysdep.h"
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <stdio.h>
15 #include <fcntl.h>
16 #include <signal.h>
17 #include <pwd.h>
18 #include <errno.h>
19 #include <sys/types.h>
20
21 #if TIME_WITH_SYS_TIME
22 # include <sys/time.h>
23 # include <time.h>
24 #else
25 # if HAVE_SYS_TIME_H
26 #  include <sys/time.h>
27 # else
28 #  include <time.h>
29 # endif
30 #endif
31
32 #include <sys/wait.h>
33 #include <string.h>
34 #include <limits.h>
35 #include <ctype.h>
36 #include <libcitadel.h>
37 #include "citadel.h"
38 #include "server.h"
39 #include "citserver.h"
40 #include "support.h"
41 #include "config.h"
42 #include "internet_addressing.h"
43 #include "md5.h"
44 #include "ctdl_module.h"
45
46 #ifdef HAVE_EXPAT
47 #include <expat.h>
48 #include "serv_xmpp.h"
49
50
51 /*
52  * This function is called by the XMPP service's async loop.
53  * If the client session has instant messages waiting, it outputs
54  * unsolicited XML stanzas containing them.
55  */
56 void jabber_output_incoming_messages(void) {
57
58         struct ExpressMessage *ptr;
59
60         while (CC->FirstExpressMessage != NULL) {
61
62                 begin_critical_section(S_SESSION_TABLE);
63                 ptr = CC->FirstExpressMessage;
64                 CC->FirstExpressMessage = CC->FirstExpressMessage->next;
65                 end_critical_section(S_SESSION_TABLE);
66
67                 cprintf("<message to=\"%s\" from=\"%s\" type=\"chat\">",
68                         XMPP->client_jid,
69                         ptr->sender_email);
70                 if (ptr->text != NULL) {
71                         striplt(ptr->text);
72                         cprintf("<body>%s</body>", ptr->text);
73                         free(ptr->text);
74                 }
75                 cprintf("</message>");
76                 free(ptr);
77         }
78 }
79
80 /*
81  * Client is sending a message.
82  */
83 void jabber_send_message(char *message_to, char *message_body) {
84         char *recp = NULL;
85         int message_sent = 0;
86         struct CitContext *cptr;
87
88         if (message_body == NULL) return;
89         if (message_to == NULL) return;
90         if (IsEmptyStr(message_to)) return;
91         if (!CC->logged_in) return;
92
93         for (cptr = ContextList; cptr != NULL; cptr = cptr->next) {
94                 if (cptr->logged_in) {
95                         if (!strcasecmp(cptr->cs_inet_email, message_to)) {
96                                 recp = cptr->user.fullname;
97                         }
98                 }
99         }
100
101         if (recp) {
102                 message_sent = PerformXmsgHooks(CC->user.fullname, CC->cs_inet_email, recp, message_body);
103         }
104
105         free(XMPP->message_body);
106         XMPP->message_body = NULL;
107         XMPP->message_to[0] = 0;
108 }
109
110
111
112 #endif  /* HAVE_EXPAT */