more places where we can use cm_lengths;
[citadel.git] / citadel / modules / extnotify / extnotify_main.c
index a519548faee6271acc0b03252c6e7d1f82544115..8eafc036a61b09e39fbbd54583cc2768dd59e10c 100644 (file)
@@ -154,6 +154,7 @@ eNotifyType extNotify_getConfigMessage(char *username,
        int num_msgs = 0;
        int a;
        char *configMsg;
+       long clen;
        char *pch;
 
        // Get the user
@@ -185,14 +186,14 @@ eNotifyType extNotify_getConfigMessage(char *username,
        for (a = 0; a < num_msgs; ++a) {
                msg = CtdlFetchMessage(msglist[a], 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 +205,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');
@@ -280,14 +280,14 @@ void process_notify(long NotifyMsgnum, void *usrdata)
        Ctx = (NotifyContext*) usrdata;
 
        msg = CtdlFetchMessage(NotifyMsgnum, 1);
-       if ( msg->cm_fields['2'] != NULL)
+       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"));
 
@@ -303,8 +303,8 @@ void process_notify(long NotifyMsgnum, void *usrdata)
                        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 +336,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);
                        }
@@ -351,7 +351,7 @@ void process_notify(long NotifyMsgnum, void *usrdata)
 
                        commandSiz = sizeof(config.c_pager_program) +
                                strlen(PagerNo) +
-                               strlen(msg->cm_fields['2']) + 5;
+                               msg->cm_lengths[eExtnotify] + 5;
 
                        command = malloc(commandSiz);
 
@@ -360,7 +360,7 @@ void process_notify(long NotifyMsgnum, void *usrdata)
                                 "%s %s -u %s",
                                 config.c_pager_program,
                                 PagerNo,
-                                msg->cm_fields['2']);
+                                msg->cm_fields[eExtnotify]);
 
                        system(command);
                        free(command);
@@ -372,7 +372,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, "");
 }
@@ -465,12 +465,56 @@ 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(config.c_funambol_host) || !IsEmptyStr(config.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();
+               CtdlRegisterMessageHook(extnotify_after_mbox_save, EVT_AFTERUSRMBOXSAVE);
+
                CtdlRegisterSessionHook(do_extnotify_queue, EVT_TIMER, PRIO_SEND + 10);
        }
        /* return our module name for the log */