Silence logging in non-debug usecases. The raspii users gonna love this.
[citadel.git] / citadel / modules / extnotify / extnotify_main.c
index 51e4e13e55db56f13db479d9c555a7367843f290..097c9fef8f35a47f5879d0c33eadc191d34455f8 100644 (file)
@@ -8,7 +8,7 @@
  * Based on bits of serv_funambol
  * Contact: <matt@mcbridematt.dhs.org> / <matt@comalies>
  *
- * Copyright (c) 2008-2011
+ * Copyright (c) 2008-2015
  *
  * This program is open source software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  */
 
 
@@ -124,7 +120,7 @@ int GetNotifyHosts(NotifyContext *Ctx)
                if (pche == NULL) {
                        syslog(LOG_ERR,
                               "extnotify: filename of notification "
-                              "template not found in %s.\n",
+                              "template not found in %s.",
                               pchs);
                        continue;
                }
@@ -154,6 +150,7 @@ eNotifyType extNotify_getConfigMessage(char *username,
        int num_msgs = 0;
        int a;
        char *configMsg;
+       long clen;
        char *pch;
 
        // Get the user
@@ -177,22 +174,22 @@ eNotifyType extNotify_getConfigMessage(char *username,
                num_msgs = cdbfr->len / sizeof(long);
                cdb_free(cdbfr);
        } else {
-               syslog(LOG_DEBUG,
-                      "extNotify_getConfigMessage: "
-                      "No config messages found\n");
+               MARKM_syslog(LOG_DEBUG,
+                            "extNotify_getConfigMessage: "
+                            "No config messages found");
                return eNone;   /* No messages at all?  No further action. */
        }
        for (a = 0; a < num_msgs; ++a) {
-               msg = CtdlFetchMessage(msglist[a], 1);
+               msg = CtdlFetchMessage(msglist[a], 1, 1);
                if (msg != NULL) {
-                       if ((msg->cm_fields['U'] != NULL) &&
-                           (strncasecmp(msg->cm_fields['U'],
+                       if (!CM_IsEmpty(msg, eMsgSubject) &&
+                           (strncasecmp(msg->cm_fields[eMsgSubject],
                                         PAGER_CONFIG_MESSAGE,
                                         strlen(PAGER_CONFIG_MESSAGE)) == 0))
                        {
                                break;
                        }
-                       CtdlFreeMessage(msg);
+                       CM_Free(msg);
                        msg = NULL;
                }
        }
@@ -204,9 +201,8 @@ eNotifyType extNotify_getConfigMessage(char *username,
        // Do a simple string search to see if 'funambol' is selected as the
        // type. This string would be at the very top of the message contents.
 
-       configMsg = msg->cm_fields['M'];
-       msg->cm_fields['M'] = NULL;
-       CtdlFreeMessage(msg);
+       CM_GetAsField(msg, eMesageText, &configMsg, &clen);
+       CM_Free(msg);
 
        /* here we would find the pager number... */
        pch = strchr(configMsg, '\n');
@@ -279,15 +275,15 @@ void process_notify(long NotifyMsgnum, void *usrdata)
 
        Ctx = (NotifyContext*) usrdata;
 
-       msg = CtdlFetchMessage(NotifyMsgnum, 1);
-       if ( msg->cm_fields['2'] != NULL)
+       msg = CtdlFetchMessage(NotifyMsgnum, 1, 1);
+       if (!CM_IsEmpty(msg, eExtnotify))
        {
                Type = extNotify_getConfigMessage(
-                       msg->cm_fields['2'],
+                       msg->cm_fields[eExtnotify],
                        &PagerNo,
                        &FreeMe);
 
-               pch = strstr(msg->cm_fields['M'], "msgid|");
+               pch = strstr(msg->cm_fields[eMesageText], "msgid|");
                if (pch != NULL)
                        msgnum = atol(pch + sizeof("msgid"));
 
@@ -295,16 +291,16 @@ void process_notify(long NotifyMsgnum, void *usrdata)
                {
                case eFunambol:
                        snprintf(remoteurl, SIZ, "http://%s@%s:%d/%s",
-                                config.c_funambol_auth,
-                                config.c_funambol_host,
-                                config.c_funambol_port,
+                                CtdlGetConfigStr("c_funambol_auth"),
+                                CtdlGetConfigStr("c_funambol_host"),
+                                CtdlGetConfigInt("c_funambol_port"),
                                 FUNAMBOL_WS);
 
                        notify_http_server(remoteurl,
                                           file_funambol_msg,
                                           strlen(file_funambol_msg),/*GNA*/
-                                          msg->cm_fields['2'],
-                                          msg->cm_fields['I'],
+                                          msg->cm_fields[eExtnotify],
+                                          msg->cm_fields[emessageId],
                                           msgnum,
                                           NULL);
                        break;
@@ -336,8 +332,8 @@ void process_notify(long NotifyMsgnum, void *usrdata)
                                notify_http_server(URLBuf,
                                                   ChrPtr(FileBuf),
                                                   StrLength(FileBuf),
-                                                  msg->cm_fields['2'],
-                                                  msg->cm_fields['I'],
+                                                  msg->cm_fields[eExtnotify],
+                                                  msg->cm_fields[emessageId],
                                                   msgnum,
                                                   NULL);
                        }
@@ -349,18 +345,18 @@ void process_notify(long NotifyMsgnum, void *usrdata)
                        int commandSiz;
                        char *command;
 
-                       commandSiz = sizeof(config.c_pager_program) +
+                       commandSiz = sizeof(CtdlGetConfigStr("c_pager_program")) +
                                strlen(PagerNo) +
-                               strlen(msg->cm_fields['2']) + 5;
+                               msg->cm_lengths[eExtnotify] + 5;
 
                        command = malloc(commandSiz);
 
                        snprintf(command,
                                 commandSiz,
                                 "%s %s -u %s",
-                                config.c_pager_program,
+                                CtdlGetConfigStr("c_pager_program"),
                                 PagerNo,
-                                msg->cm_fields['2']);
+                                msg->cm_fields[eExtnotify]);
 
                        system(command);
                        free(command);
@@ -372,7 +368,7 @@ void process_notify(long NotifyMsgnum, void *usrdata)
        }
        if (FreeMe != NULL)
                free(FreeMe);
-       CtdlFreeMessage(msg);
+       CM_Free(msg);
        todelete[0] = NotifyMsgnum;
        CtdlDeleteMessages(FNBL_QUEUE_ROOM, todelete, 1, "");
 }
@@ -393,11 +389,11 @@ void do_extnotify_queue(void)
         * don't really require extremely fine granularity here, we'll do it
         * with a static variable instead.
         */
-       if (IsEmptyStr(config.c_pager_program) &&
-           IsEmptyStr(config.c_funambol_host))
+       if (IsEmptyStr(CtdlGetConfigStr("c_pager_program")) &&
+           IsEmptyStr(CtdlGetConfigStr("c_funambol_host")))
        {
-               syslog(LOG_ERR,
-                      "No external notifiers configured on system/user\n");
+               MARKM_syslog(LOG_DEBUG,
+                            "No external notifiers configured on system/user");
                return;
        }
 
@@ -413,13 +409,13 @@ void do_extnotify_queue(void)
        /*
         * Go ahead and run the queue
         */
-       syslog(LOG_DEBUG, "serv_extnotify: processing notify queue\n");
+       MARKM_syslog(LOG_DEBUG, "serv_extnotify: processing notify queue");
 
        memset(&Ctx, 0, sizeof(NotifyContext));
        if ((GetNotifyHosts(&Ctx) > 0) &&
            (CtdlGetRoom(&CC->room, FNBL_QUEUE_ROOM) != 0))
        {
-               syslog(LOG_ERR, "Cannot find room <%s>\n", FNBL_QUEUE_ROOM);
+               syslog(LOG_ERR, "Cannot find room <%s>", FNBL_QUEUE_ROOM);
                if (Ctx.nNotifyHosts > 0)
                {
                        for (i = 0; i < Ctx.nNotifyHosts * 2; i++)
@@ -430,7 +426,7 @@ void do_extnotify_queue(void)
        }
        CtdlForEachMessage(MSGS_ALL, 0L, NULL,
                           SPOOLMIME, NULL, process_notify, &Ctx);
-       syslog(LOG_DEBUG, "serv_extnotify: queue run completed\n");
+       MARKM_syslog(LOG_DEBUG, "serv_extnotify: queue run completed");
        doing_queue = 0;
        if (Ctx.nNotifyHosts > 0)
        {
@@ -451,7 +447,7 @@ void do_extnotify_queue(void)
 void create_extnotify_queue(void) {
        struct ctdlroom qrbuf;
 
-       CtdlCreateRoom(FNBL_QUEUE_ROOM, 3, "", 0, 1, 0, VIEW_MAILBOX);
+       CtdlCreateRoom(FNBL_QUEUE_ROOM, 3, "", 0, 1, 0, VIEW_QUEUE);
 
        CtdlFillSystemContext(&extnotify_queue_CC, "Extnotify");
 
@@ -465,13 +461,57 @@ void create_extnotify_queue(void) {
        }
 }
 
+int extnotify_after_mbox_save(struct CtdlMessage *msg,
+                             recptypes *recps)
+
+{
+       /* If this is private, local mail, make a copy in the
+        * recipient's mailbox and bump the reference count.
+        */
+       if (!IsEmptyStr(CtdlGetConfigStr("c_funambol_host")) || !IsEmptyStr(CtdlGetConfigStr("c_pager_program")))
+       {
+               /* Generate a instruction message for the Funambol notification
+                * server, in the same style as the SMTP queue
+                */
+               StrBuf *instr;
+               struct CtdlMessage *imsg;
+
+               instr = NewStrBufPlain(NULL, 1024);
+               StrBufPrintf(instr,
+                            "Content-type: "SPOOLMIME"\n"
+                            "\n"
+                            "msgid|%s\n"
+                            "submitted|%ld\n"
+                            "bounceto|%s\n",
+                            msg->cm_fields[eVltMsgNum],
+                            (long)time(NULL), //todo: time() is expensive!
+                            recps->bounce_to
+                       );
+                               
+               imsg = malloc(sizeof(struct CtdlMessage));
+               memset(imsg, 0, sizeof(struct CtdlMessage));
+               imsg->cm_magic = CTDLMESSAGE_MAGIC;
+               imsg->cm_anon_type = MES_NORMAL;
+               imsg->cm_format_type = FMT_RFC822;
+               CM_SetField(imsg, eMsgSubject, HKEY("QMSG"));
+               CM_SetField(imsg, eAuthor, HKEY("Citadel"));
+               CM_SetField(imsg, eJournal, HKEY("do not journal"));
+               CM_SetAsFieldSB(imsg, eMesageText, &instr);
+               CM_SetField(imsg, eExtnotify, recps->recp_local, strlen(recps->recp_local));
+               CtdlSubmitMsg(imsg, NULL, FNBL_QUEUE_ROOM, 0);
+               CM_Free(imsg);
+       }
+       return 0;
+}
 
 CTDL_MODULE_INIT(extnotify)
 {
        if (!threading)
        {
                create_extnotify_queue();
-               CtdlRegisterSessionHook(do_extnotify_queue, EVT_TIMER);
+               CtdlRegisterMessageHook(extnotify_after_mbox_save, EVT_AFTERUSRMBOXSAVE);
+
+               CtdlRegisterSessionHook(do_extnotify_queue, EVT_TIMER, PRIO_SEND + 10);
        }
        /* return our module name for the log */
        return "extnotify";