Back to index

citadel  8.12
xmpp_messages.c
Go to the documentation of this file.
00001 /*
00002  * Handle messages sent and received using XMPP (Jabber) protocol
00003  *
00004  * Copyright (c) 2007-2010 by Art Cancro
00005  *
00006  * This program is open source software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License version 3.
00008  * 
00009  * 
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * 
00017  * 
00018  * 
00019  *
00020  */
00021 
00022 #include "sysdep.h"
00023 #include <stdlib.h>
00024 #include <unistd.h>
00025 #include <stdio.h>
00026 #include <fcntl.h>
00027 #include <signal.h>
00028 #include <pwd.h>
00029 #include <errno.h>
00030 #include <sys/types.h>
00031 
00032 #if TIME_WITH_SYS_TIME
00033 # include <sys/time.h>
00034 # include <time.h>
00035 #else
00036 # if HAVE_SYS_TIME_H
00037 #  include <sys/time.h>
00038 # else
00039 #  include <time.h>
00040 # endif
00041 #endif
00042 
00043 #include <sys/wait.h>
00044 #include <string.h>
00045 #include <limits.h>
00046 #include <ctype.h>
00047 #include <expat.h>
00048 #include <libcitadel.h>
00049 #include "citadel.h"
00050 #include "server.h"
00051 #include "citserver.h"
00052 #include "support.h"
00053 #include "config.h"
00054 #include "internet_addressing.h"
00055 #include "md5.h"
00056 #include "ctdl_module.h"
00057 #include "serv_xmpp.h"
00058 
00059 
00060 /*
00061  * This function is called by the XMPP service's async loop.
00062  * If the client session has instant messages waiting, it outputs
00063  * unsolicited XML stanzas containing them.
00064  */
00065 void xmpp_output_incoming_messages(void) {
00066 
00067        struct ExpressMessage *ptr;
00068        char xmlbuf1[4096];
00069        char xmlbuf2[4096];
00070 
00071        while (CC->FirstExpressMessage != NULL) {
00072 
00073               begin_critical_section(S_SESSION_TABLE);
00074               ptr = CC->FirstExpressMessage;
00075               CC->FirstExpressMessage = CC->FirstExpressMessage->next;
00076               end_critical_section(S_SESSION_TABLE);
00077 
00078               cprintf("<message to=\"%s\" from=\"%s\" type=\"chat\">",
00079                      xmlesc(xmlbuf1, XMPP->client_jid, sizeof xmlbuf1),
00080                      xmlesc(xmlbuf2, ptr->sender_email, sizeof xmlbuf2)
00081               );
00082               if (ptr->text != NULL) {
00083                      striplt(ptr->text);
00084                      cprintf("<body>%s</body>", xmlesc(xmlbuf1, ptr->text, sizeof xmlbuf1));
00085                      free(ptr->text);
00086               }
00087               cprintf("</message>");
00088               free(ptr);
00089        }
00090 }
00091 
00092 /*
00093  * Client is sending a message.
00094  */
00095 void xmpp_send_message(char *message_to, char *message_body) {
00096        char *recp = NULL;
00097        struct CitContext *cptr;
00098 
00099        if (message_body == NULL) return;
00100        if (message_to == NULL) return;
00101        if (IsEmptyStr(message_to)) return;
00102        if (!CC->logged_in) return;
00103 
00104        for (cptr = ContextList; cptr != NULL; cptr = cptr->next) {
00105               if (   (cptr->logged_in)
00106                      && (cptr->can_receive_im)
00107                      && (!strcasecmp(cptr->cs_inet_email, message_to))
00108               ) {
00109                      recp = cptr->user.fullname;
00110               }
00111        }
00112 
00113        if (recp) {
00114               PerformXmsgHooks(CC->user.fullname, CC->cs_inet_email, recp, message_body);
00115        }
00116 
00117        free(XMPP->message_body);
00118        XMPP->message_body = NULL;
00119        XMPP->message_to[0] = 0;
00120        time(&CC->lastidle);
00121 }
00122