Back to index

citadel  8.12
extnotify_main.c
Go to the documentation of this file.
00001 /*
00002  * extnotify_main.c
00003  * Mathew McBride
00004  *
00005  * This module implements an external pager hook for when notifcation
00006  * of a new email is wanted.
00007  *
00008  * Based on bits of serv_funambol
00009  * Contact: <matt@mcbridematt.dhs.org> / <matt@comalies>
00010  *
00011  * Copyright (c) 2008-2011
00012  *
00013  * This program is open source software; you can redistribute it and/or modify
00014  * it under the terms of the GNU General Public License as published by
00015  * the Free Software Foundation; either version 3 of the License, or
00016  * (at your option) any later version.
00017  *
00018  * This program is distributed in the hope that it will be useful,
00019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00021  * GNU General Public License for more details.
00022  *
00023  * You should have received a copy of the GNU General Public License
00024  * along with this program; if not, write to the Free Software
00025  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00026  */
00027 
00028 
00029 #include "sysdep.h"
00030 #include <stdlib.h>
00031 #include <unistd.h>
00032 #include <stdio.h>
00033 #include <fcntl.h>
00034 #include <signal.h>
00035 #include <pwd.h>
00036 #include <errno.h>
00037 #include <sys/types.h>
00038 
00039 #if TIME_WITH_SYS_TIME
00040 # include <sys/time.h>
00041 # include <time.h>
00042 #else
00043 # if HAVE_SYS_TIME_H
00044 #  include <sys/time.h>
00045 # else
00046 #  include <time.h>
00047 # endif
00048 #endif
00049 
00050 #include <sys/wait.h>
00051 #include <string.h>
00052 #include <limits.h>
00053 #include <sys/socket.h>
00054 #include <libcitadel.h>
00055 #include "citadel.h"
00056 #include "server.h"
00057 #include "citserver.h"
00058 #include "support.h"
00059 #include "config.h"
00060 #include "control.h"
00061 #include "user_ops.h"
00062 #include "database.h"
00063 #include "msgbase.h"
00064 #include "internet_addressing.h"
00065 #include "domain.h"
00066 #include "clientsocket.h"
00067 #include "event_client.h"
00068 #include "extnotify.h"
00069 #include "ctdl_module.h"
00070 
00071 struct CitContext extnotify_queue_CC;
00072 
00073 void ExtNotify_PutErrorMessage(NotifyContext *Ctx, StrBuf *ErrMsg)
00074 {
00075        int nNext;
00076        if (Ctx->NotifyErrors == NULL)
00077               Ctx->NotifyErrors = NewHash(1, Flathash);
00078 
00079        nNext = GetCount(Ctx->NotifyErrors) + 1;
00080        Put(Ctx->NotifyErrors,
00081            (char*)&nNext,
00082            sizeof(int),
00083            ErrMsg,
00084            HFreeStrBuf);
00085 }
00086 
00087 StrBuf* GetNHBuf(int i, int allocit, StrBuf **NotifyHostList)
00088 {
00089        if ((NotifyHostList[i] == NULL) && (allocit != 0))
00090               NotifyHostList[i] = NewStrBuf();
00091        return NotifyHostList[i];
00092 }
00093 
00094 
00095 int GetNotifyHosts(NotifyContext *Ctx)
00096 {
00097        char NotifyHostsBuf[SIZ];
00098        StrBuf *Host;
00099        StrBuf *File;
00100        StrBuf *NotifyBuf;
00101        int notify;
00102        const char *pchs, *pche;
00103        const char *NextHost = NULL;
00104 
00105        /* See if we have any Notification Hosts configured */
00106        Ctx->nNotifyHosts = get_hosts(NotifyHostsBuf, "notify");
00107        if (Ctx->nNotifyHosts < 1)
00108               return 0;
00109 
00110        Ctx->NotifyHostList = malloc(sizeof(StrBuf*) *
00111                                  2 *
00112                                  (Ctx->nNotifyHosts + 1));
00113        memset(Ctx->NotifyHostList, 0,
00114               sizeof(StrBuf*) * 2 * (Ctx->nNotifyHosts + 1));
00115 
00116        NotifyBuf = NewStrBufPlain(NotifyHostsBuf, -1);
00117        /* get all configured notifiers's */
00118        for (notify=0; notify<Ctx->nNotifyHosts; notify++) {
00119 
00120               Host = GetNHBuf(notify * 2, 1, Ctx->NotifyHostList);
00121               StrBufExtract_NextToken(Host, NotifyBuf, &NextHost, '|');
00122               pchs = ChrPtr(Host);
00123               pche = strchr(pchs, ':');
00124               if (pche == NULL) {
00125                      syslog(LOG_ERR,
00126                             "extnotify: filename of notification "
00127                             "template not found in %s.\n",
00128                             pchs);
00129                      continue;
00130               }
00131               File = GetNHBuf(notify * 2 + 1, 1, Ctx->NotifyHostList);
00132               StrBufPlain(File, pchs, pche - pchs);
00133               StrBufCutLeft(Host, pche - pchs + 1);
00134        }
00135        FreeStrBuf(&NotifyBuf);
00136        return Ctx->nNotifyHosts;
00137 }
00138 
00139 
00140 
00144 eNotifyType extNotify_getConfigMessage(char *username,
00145                                    char **PagerNumber,
00146                                    char **FreeMe)
00147 {
00148        struct ctdlroom qrbuf; // scratch for room
00149        struct ctdluser user; // ctdl user instance
00150        char configRoomName[ROOMNAMELEN];
00151        struct CtdlMessage *msg = NULL;
00152        struct cdbdata *cdbfr;
00153        long *msglist = NULL;
00154        int num_msgs = 0;
00155        int a;
00156        char *configMsg;
00157        char *pch;
00158 
00159        // Get the user
00160        CtdlGetUser(&user, username);
00161 
00162        CtdlMailboxName(configRoomName,
00163                      sizeof(configRoomName),
00164                      &user,
00165                      USERCONFIGROOM);
00166        // Fill qrbuf
00167        CtdlGetRoom(&qrbuf, configRoomName);
00168        /* Do something really, really stoopid here. Raid the room on ourselves,
00169         * loop through the messages manually and find it. I don't want
00170         * to use a CtdlForEachMessage callback here, as we would be
00171         * already in one */
00172        cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf.QRnumber, sizeof(long));
00173        if (cdbfr != NULL) {
00174               msglist = (long *) cdbfr->ptr;
00175               cdbfr->ptr = NULL;
00176                      /* CtdlForEachMessage() now owns this memory */
00177               num_msgs = cdbfr->len / sizeof(long);
00178               cdb_free(cdbfr);
00179        } else {
00180               syslog(LOG_DEBUG,
00181                      "extNotify_getConfigMessage: "
00182                      "No config messages found\n");
00183               return eNone; /* No messages at all?  No further action. */
00184        }
00185        for (a = 0; a < num_msgs; ++a) {
00186               msg = CtdlFetchMessage(msglist[a], 1);
00187               if (msg != NULL) {
00188                      if ((msg->cm_fields['U'] != NULL) &&
00189                          (strncasecmp(msg->cm_fields['U'],
00190                                     PAGER_CONFIG_MESSAGE,
00191                                     strlen(PAGER_CONFIG_MESSAGE)) == 0))
00192                      {
00193                             break;
00194                      }
00195                      CtdlFreeMessage(msg);
00196                      msg = NULL;
00197               }
00198        }
00199 
00200        free(msglist);
00201        if (msg == NULL)
00202               return eNone;
00203 
00204        // Do a simple string search to see if 'funambol' is selected as the
00205        // type. This string would be at the very top of the message contents.
00206 
00207        configMsg = msg->cm_fields['M'];
00208        msg->cm_fields['M'] = NULL;
00209        CtdlFreeMessage(msg);
00210 
00211        /* here we would find the pager number... */
00212        pch = strchr(configMsg, '\n');
00213        if (pch != NULL)
00214        {
00215               *pch = '\0';
00216               pch ++;
00217        }
00218 
00219        /* Check to see if:
00220         * 1. The user has configured paging / They have and disabled it
00221         * AND 2. There is an external pager program
00222         * 3. A Funambol server has been entered
00223         *
00224         */
00225        if (!strncasecmp(configMsg, "none", 4))
00226        {
00227               free(configMsg);
00228               return eNone;
00229        }
00230 
00231        if (!strncasecmp(configMsg, HKEY(PAGER_CONFIG_HTTP)))
00232        {
00233               free(configMsg);
00234               return eHttpMessages;
00235        }
00236        if (!strncasecmp(configMsg, HKEY(FUNAMBOL_CONFIG_TEXT)))
00237        {
00238               free(configMsg);
00239               return eFunambol;
00240        }
00241        else if (!strncasecmp(configMsg, HKEY(PAGER_CONFIG_SYSTEM)))
00242        {
00243               // whats the pager number?
00244               if (!pch || (*pch == '\0'))
00245               {
00246                      free(configMsg);
00247 
00248                      return eNone;
00249               }
00250               while (isspace(*pch))
00251                      pch ++;
00252               *PagerNumber = pch;
00253               while (isdigit(*pch) || (*pch == '+'))
00254                      pch++;
00255               *pch = '\0';
00256               *FreeMe = configMsg;
00257               return eTextMessage;
00258        }
00259 
00260        free(configMsg);
00261        return eNone;
00262 }
00263 
00264 
00265 /*
00266  * Process messages in the external notification queue
00267  */
00268 void process_notify(long NotifyMsgnum, void *usrdata)
00269 {
00270        NotifyContext *Ctx;
00271        long msgnum = 0;
00272        long todelete[1];
00273        char *pch;
00274        struct CtdlMessage *msg;
00275        eNotifyType Type;
00276        char remoteurl[SIZ];
00277        char *FreeMe = NULL;
00278        char *PagerNo;
00279 
00280        Ctx = (NotifyContext*) usrdata;
00281 
00282        msg = CtdlFetchMessage(NotifyMsgnum, 1);
00283        if ( msg->cm_fields['2'] != NULL)
00284        {
00285               Type = extNotify_getConfigMessage(
00286                      msg->cm_fields['2'],
00287                      &PagerNo,
00288                      &FreeMe);
00289 
00290               pch = strstr(msg->cm_fields['M'], "msgid|");
00291               if (pch != NULL)
00292                      msgnum = atol(pch + sizeof("msgid"));
00293 
00294               switch (Type)
00295               {
00296               case eFunambol:
00297                      snprintf(remoteurl, SIZ, "http://%s@%s:%d/%s",
00298                              config.c_funambol_auth,
00299                              config.c_funambol_host,
00300                              config.c_funambol_port,
00301                              FUNAMBOL_WS);
00302 
00303                      notify_http_server(remoteurl,
00304                                       file_funambol_msg,
00305                                       strlen(file_funambol_msg),/*GNA*/
00306                                       msg->cm_fields['2'],
00307                                       msg->cm_fields['I'],
00308                                       msgnum,
00309                                       NULL);
00310                      break;
00311               case eHttpMessages:
00312               {
00313                      int i = 0;
00314                      StrBuf *URL;
00315                      char URLBuf[SIZ];
00316                      StrBuf *File;
00317                      StrBuf *FileBuf = NewStrBuf();
00318 
00319                      for (i = 0; i < Ctx->nNotifyHosts; i++)
00320                      {
00321 
00322                             URL = GetNHBuf(i*2, 0, Ctx->NotifyHostList);
00323                             if (URL==NULL) break;
00324                             File = GetNHBuf(i*2 + 1, 0,
00325                                           Ctx->NotifyHostList);
00326                             if (File==NULL) break;
00327 
00328                             if (StrLength(File)>0)
00329                                    StrBufPrintf(FileBuf, "%s/%s",
00330                                                ctdl_shared_dir,
00331                                                ChrPtr(File));
00332                             else
00333                                    FlushStrBuf(FileBuf);
00334                             memcpy(URLBuf, ChrPtr(URL), StrLength(URL) + 1);
00335 
00336                             notify_http_server(URLBuf,
00337                                              ChrPtr(FileBuf),
00338                                              StrLength(FileBuf),
00339                                              msg->cm_fields['2'],
00340                                              msg->cm_fields['I'],
00341                                              msgnum,
00342                                              NULL);
00343                      }
00344                      FreeStrBuf(&FileBuf);
00345               }
00346               break;
00347               case eTextMessage:
00348               {
00349                      int commandSiz;
00350                      char *command;
00351 
00352                      commandSiz = sizeof(config.c_pager_program) +
00353                             strlen(PagerNo) +
00354                             strlen(msg->cm_fields['2']) + 5;
00355 
00356                      command = malloc(commandSiz);
00357 
00358                      snprintf(command,
00359                              commandSiz,
00360                              "%s %s -u %s",
00361                              config.c_pager_program,
00362                              PagerNo,
00363                              msg->cm_fields['2']);
00364 
00365                      system(command);
00366                      free(command);
00367               }
00368               break;
00369               case eNone:
00370                      break;
00371               }
00372        }
00373        if (FreeMe != NULL)
00374               free(FreeMe);
00375        CtdlFreeMessage(msg);
00376        todelete[0] = NotifyMsgnum;
00377        CtdlDeleteMessages(FNBL_QUEUE_ROOM, todelete, 1, "");
00378 }
00379 
00384 void do_extnotify_queue(void)
00385 {
00386        NotifyContext Ctx;
00387        static int doing_queue = 0;
00388        int i = 0;
00389 
00390        /*
00391         * This is a simple concurrency check to make sure only one queue run
00392         * is done at a time.  We could do this with a mutex, but since we
00393         * don't really require extremely fine granularity here, we'll do it
00394         * with a static variable instead.
00395         */
00396        if (IsEmptyStr(config.c_pager_program) &&
00397            IsEmptyStr(config.c_funambol_host))
00398        {
00399               syslog(LOG_ERR,
00400                      "No external notifiers configured on system/user\n");
00401               return;
00402        }
00403 
00404        if (doing_queue)
00405               return;
00406 
00407        doing_queue = 1;
00408 
00409        become_session(&extnotify_queue_CC);
00410 
00411        pthread_setspecific(MyConKey, (void *)&extnotify_queue_CC);
00412 
00413        /*
00414         * Go ahead and run the queue
00415         */
00416        syslog(LOG_DEBUG, "serv_extnotify: processing notify queue\n");
00417 
00418        memset(&Ctx, 0, sizeof(NotifyContext));
00419        if ((GetNotifyHosts(&Ctx) > 0) &&
00420            (CtdlGetRoom(&CC->room, FNBL_QUEUE_ROOM) != 0))
00421        {
00422               syslog(LOG_ERR, "Cannot find room <%s>\n", FNBL_QUEUE_ROOM);
00423               if (Ctx.nNotifyHosts > 0)
00424               {
00425                      for (i = 0; i < Ctx.nNotifyHosts * 2; i++)
00426                             FreeStrBuf(&Ctx.NotifyHostList[i]);
00427                      free(Ctx.NotifyHostList);
00428               }
00429               return;
00430        }
00431        CtdlForEachMessage(MSGS_ALL, 0L, NULL,
00432                         SPOOLMIME, NULL, process_notify, &Ctx);
00433        syslog(LOG_DEBUG, "serv_extnotify: queue run completed\n");
00434        doing_queue = 0;
00435        if (Ctx.nNotifyHosts > 0)
00436        {
00437               for (i = 0; i < Ctx.nNotifyHosts * 2; i++)
00438                      FreeStrBuf(&Ctx.NotifyHostList[i]);
00439               free(Ctx.NotifyHostList);
00440        }
00441 }
00442 
00443 
00444 
00445 /* Create the notify message queue. We use the exact same room
00446  * as the Funambol module.
00447  *
00448  * Run at server startup, creates FNBL_QUEUE_ROOM if it doesn't exist
00449  * and sets as system room.
00450  */
00451 void create_extnotify_queue(void) {
00452        struct ctdlroom qrbuf;
00453 
00454        CtdlCreateRoom(FNBL_QUEUE_ROOM, 3, "", 0, 1, 0, VIEW_QUEUE);
00455 
00456        CtdlFillSystemContext(&extnotify_queue_CC, "Extnotify");
00457 
00458        /*
00459         * Make sure it's set to be a "system room" so it doesn't show up
00460         * in the <K>nown rooms list for Aides.
00461         */
00462        if (CtdlGetRoomLock(&qrbuf, FNBL_QUEUE_ROOM) == 0) {
00463               qrbuf.QRflags2 |= QR2_SYSTEM;
00464               CtdlPutRoomLock(&qrbuf);
00465        }
00466 }
00467 
00468 
00469 CTDL_MODULE_INIT(extnotify)
00470 {
00471        if (!threading)
00472        {
00473               create_extnotify_queue();
00474               CtdlRegisterSessionHook(do_extnotify_queue, EVT_TIMER, PRIO_SEND + 10);
00475        }
00476        /* return our module name for the log */
00477        return "extnotify";
00478 }