source: npl/mailserver/zarafa7/Datux-spamhook.patch @ c5c522c

gcc484ntopperl-5.22
Last change on this file since c5c522c was c5c522c, checked in by Edwin Eefting <edwin@datux.nl>, 8 years ago

initial commit, transferred from cleaned syn3 svn tree

  • Property mode set to 100644
File size: 8.7 KB
  • provider/libserver/ZarafaCmd.cpp

    diff -ru zarafa-7.1.4/provider/libserver/ZarafaCmd.cpp zarafa-7.1.4.spamhook/provider/libserver/ZarafaCmd.cpp
    old new  
    8484#include "StreamUtil.h"
    8585#include "CommonUtil.h"
    8686#include "StorageUtil.h"
     87#include "UnixUtil.h"
    8788
    8889#include "ZarafaICS.h"
    8990
     
    76557656        bool             bMoved;
    76567657}COPYITEM;
    76577658
     7659//SPAM HOOK
     7660//This function parses an e-mail to the /etc/zarafa/userscripts/junklearn script. With 2 arguments:
     7661//ham or spam
     7662//message id
     7663//and pipes the mail header to the script.
     7664//This script wil be inhaled by MoveObjects();
     7665/////////////////////////////////////////////////////////////////////////////////////////////////
     7666int SpamHook(ECDatabase *lpDatabase,int ulId, unsigned int ulDestFolderId)
     7667{
     7668
     7669                ALLOC_DBRESULT();
     7670                ECRESULT er = erSuccess;
     7671                std::string shScriptPath = g_lpSessionManager->GetConfig()->GetSetting("junklearn_script");
     7672                string shMailStatus;
     7673                entryId* junkFolderEntryId;
     7674                entryId* wasteBucketEntryId;
     7675                int shNumRows;
     7676
     7677                //dont do anything if the junklearn script doesnt exist:
     7678                int fCheck=open(shScriptPath.c_str(), O_RDONLY);
     7679                if (fCheck==0) {
     7680                        g_lpSessionManager->GetLogger()->Log(EC_LOGLEVEL_INFO,"SpamHook: skipping, script "+shScriptPath+" not found");
     7681                        er=erSuccess;
     7682                        goto exit;
     7683                }
     7684                close(fCheck);
     7685
     7686                //Get store object ID via message object id
     7687                unsigned int storeId;
     7688                er = g_lpSessionManager->GetCacheManager()->GetStore(ulId,&storeId,NULL);
     7689                if(er != erSuccess)
     7690                {
     7691                        g_lpSessionManager->GetLogger()->Log(EC_LOGLEVEL_WARNING,"SpamHook: error while retrieve source folder.");
     7692                        goto exit;
     7693                }
     7694
     7695                //get deleted items folder entry id
     7696                strQuery="SELECT val_binary FROM properties WHERE hierarchyid="+stringify(storeId)+" AND tag="+stringify(PROP_ID(PR_IPM_WASTEBASKET_ENTRYID));
     7697                er = lpDatabase->DoSelect(strQuery, &lpDBResult);
     7698                if(er != erSuccess) {
     7699                        g_lpSessionManager->GetLogger()->Log(EC_LOGLEVEL_WARNING,"SpamHook: error while retrieve wastebasket entryid from DB.");
     7700                        goto exit;
     7701                }
     7702                lpDBRow = lpDatabase->FetchRow(lpDBResult);
     7703                lpDBLen = lpDatabase->FetchRowLengths(lpDBResult);
     7704                shNumRows=lpDatabase->GetNumRows(lpDBResult);
     7705                if(shNumRows<1)
     7706                {
     7707                        g_lpSessionManager->GetLogger()->Log(EC_LOGLEVEL_WARNING,"SpamHook: error while retrieve wastebasket entryid, empty DB result.");
     7708                        goto exit;
     7709                }
     7710
     7711                //Convert 'deleted items' entryid to objectid.
     7712                wasteBucketEntryId = new entryId[0];
     7713                wasteBucketEntryId->__ptr=(unsigned char*)lpDBRow[0];
     7714                wasteBucketEntryId->__size=lpDBLen[0];
     7715                unsigned int wasteBucketFolderId;
     7716                er=g_lpSessionManager->GetCacheManager()->GetObjectFromEntryId(wasteBucketEntryId,&wasteBucketFolderId);
     7717                delete wasteBucketEntryId;
     7718                if(er!=erSuccess)
     7719                {
     7720                        g_lpSessionManager->GetLogger()->Log(EC_LOGLEVEL_WARNING,"SpamHook: error while retrieve wastebasket entryid, converting to objectID.");
     7721                        goto exit;
     7722                }
     7723
     7724                //Get 'junk folder' entryId.
     7725                FREE_DBRESULT();
     7726                strQuery="SELECT val_binary  FROM receivefolder LEFT JOIN mvproperties ON receivefolder.objid=mvproperties.hierarchyid WHERE receivefolder.storeid="+stringify(storeId)+" AND receivefolder.messageclass='IPC' AND mvproperties.tag="+stringify(PROP_ID(PR_ADDITIONAL_REN_ENTRYIDS))+" AND mvproperties.orderid=4";
     7727                er = lpDatabase->DoSelect(strQuery, &lpDBResult);
     7728                if(er != erSuccess) {
     7729                        g_lpSessionManager->GetLogger()->Log(EC_LOGLEVEL_WARNING,"SpamHook: error while retrieve junkfolder entryids from DB.");
     7730                        goto exit;
     7731                }
     7732                lpDBRow = lpDatabase->FetchRow(lpDBResult);
     7733                lpDBLen = lpDatabase->FetchRowLengths(lpDBResult);
     7734                shNumRows=lpDatabase->GetNumRows(lpDBResult);
     7735                if(shNumRows<1)
     7736                {
     7737                        g_lpSessionManager->GetLogger()->Log(EC_LOGLEVEL_WARNING,"SpamHook: error while retrieve junkfolder entryid, empty DB result.");
     7738                        goto exit;
     7739                }
     7740
     7741                //Convert 'junk folder' entryid to objectid.
     7742                junkFolderEntryId = new entryId[0];
     7743                junkFolderEntryId->__ptr=(unsigned char*)lpDBRow[0];
     7744                junkFolderEntryId->__size=lpDBLen[0];
     7745                unsigned int junkFolderId;
     7746                er=g_lpSessionManager->GetCacheManager()->GetObjectFromEntryId(junkFolderEntryId,&junkFolderId);
     7747                delete junkFolderEntryId;
     7748                if(er!=erSuccess)
     7749                {
     7750                        g_lpSessionManager->GetLogger()->Log(EC_LOGLEVEL_WARNING,"SpamHook: error while retrieve junkfolder entryid, converting to objectID.");
     7751                        goto exit;
     7752                }
     7753                       
     7754                //Get source folder object ID. (Actually we should check if mail came from subfolders in the 'deleted items folder', which I think never happens.)
     7755                unsigned int srcFolderId;
     7756                er=g_lpSessionManager->GetCacheManager()->GetParent(ulId,&srcFolderId);
     7757                if(er!=erSuccess)
     7758                {
     7759                        g_lpSessionManager->GetLogger()->Log(EC_LOGLEVEL_WARNING,"SpamHook: error while retrieve src folder id.");
     7760                        goto exit;
     7761                }
     7762
     7763                //now we can determine if object being moved from or to the junkfolder
     7764                //if destination folder is junk, mark as spam
     7765                if(ulDestFolderId==junkFolderId)
     7766                        shMailStatus="spam";
     7767                else
     7768                {
     7769                        //if destination folder is not TRASH and de source folder is JUNK, mark as ham
     7770                        if(ulDestFolderId!=wasteBucketFolderId  &&  srcFolderId==junkFolderId)
     7771                                shMailStatus="ham";
     7772                        else
     7773                                //its just a normal movement, so do nothing.
     7774                                goto exit;
     7775                }
     7776
     7777                //Get the mail from the DB.
     7778                FREE_DBRESULT();
     7779                strQuery="SELECT val_string FROM properties WHERE tag="+stringify(PROP_ID(PR_TRANSPORT_MESSAGE_HEADERS))+" AND hierarchyid= "+stringify(ulId);
     7780                er = lpDatabase->DoSelect(strQuery, &lpDBResult);
     7781                if(er != erSuccess) {
     7782                        g_lpSessionManager->GetLogger()->Log(EC_LOGLEVEL_WARNING,"SpamHook: db error while retrieve mail header.");
     7783                        goto exit;
     7784                }
     7785
     7786                lpDBRow = lpDatabase->FetchRow(lpDBResult);
     7787                shNumRows=lpDatabase->GetNumRows(lpDBResult);
     7788
     7789                if(shNumRows<=0) {
     7790                        g_lpSessionManager->GetLogger()->Log(EC_LOGLEVEL_WARNING,"SpamHook: warning mail header empty or this object is no e-mail");
     7791                        goto exit;
     7792                }
     7793
     7794                {
     7795                        //now its time to open the spamhook script and pass the mail to it:
     7796                        shScriptPath=shScriptPath+" "+shMailStatus+" "+stringify(ulId);
     7797                        int ulFpWrite = -1;
     7798                        int ulFpRead = -1;
     7799                        int ulCommandRetval;
     7800
     7801                        //we asume failure, unless we make it all the way though the script
     7802                        er=ZARAFA_E_UNKNOWN;
     7803
     7804                        pid_t ulCommandPid = unix_popen_rw(g_lpSessionManager->GetLogger(), shScriptPath.c_str(), &ulFpWrite, &ulFpRead, NULL, NULL, true, false);
     7805                        if (ulCommandPid <= 0) {
     7806                                g_lpSessionManager->GetLogger()->Log(EC_LOGLEVEL_WARNING,"SpamHook: error opening subprocess.");
     7807                                goto exit;
     7808                        }
     7809
     7810                        //pass the data to the subprocess:
     7811                        write(ulFpWrite, lpDBRow[0], strlen(lpDBRow[0]));
     7812                        ulCommandRetval=unix_pclose(ulFpRead, ulFpWrite, ulCommandPid);
     7813
     7814                        //subprocess is done, check results
     7815                        if (ulCommandRetval<0) {
     7816                                g_lpSessionManager->GetLogger()->Log(EC_LOGLEVEL_WARNING,"SpamHook: error "+shScriptPath+" exits with: "+stringify(WEXITSTATUS(ulCommandRetval)));
     7817                                goto exit;
     7818                        }
     7819                }
     7820
     7821                g_lpSessionManager->GetLogger()->Log(EC_LOGLEVEL_INFO,"SpamHook:  "+shScriptPath+" successfully executed.");
     7822                er=erSuccess;
     7823
     7824        exit:
     7825                // Free database results
     7826                FREE_DBRESULT();
     7827
     7828                return er;
     7829}
     7830
     7831
    76587832// Move one or more messages and/or moved a softdeleted message to a normal message
    76597833ECRESULT MoveObjects(ECSession *lpSession, ECDatabase *lpDatabase, ECListInt* lplObjectIds, unsigned int ulDestFolderId, unsigned int ulSyncId)
    76607834{
     
    86418815
    86428816        // @note The object type checking wille be done in MoveObjects or CopyObject
    86438817
     8818//SPAMHOOK
     8819/////////////////////////////////////
     8820        //Iterate over all mail ids and initiate spamhook.
     8821        for(iObjectId = lObjectIds.begin(); iObjectId != lObjectIds.end(); iObjectId++)
     8822        {
     8823                SpamHook(lpDatabase,*iObjectId,ulDestFolderId);
     8824        }
     8825//SPAMHOOK END
     8826////////////////////////////////////
     8827       
    86448828        //check copy or a move
    86458829        if(ulFlags & FOLDER_MOVE ) { // A move
    86468830                er = MoveObjects(lpecSession, lpDatabase, &lObjectIds, ulDestFolderId, ulSyncId);
  • provider/server/ECServer.cpp

    diff -ru zarafa-7.1.4/provider/server/ECServer.cpp zarafa-7.1.4.spamhook/provider/server/ECServer.cpp
    old new  
    963963                { "deletegroup_script",                 "/etc/zarafa/userscripts/deletegroup", CONFIGSETTING_RELOADABLE},
    964964                { "createcompany_script",               "/etc/zarafa/userscripts/createcompany", CONFIGSETTING_RELOADABLE },
    965965                { "deletecompany_script",               "/etc/zarafa/userscripts/deletecompany", CONFIGSETTING_RELOADABLE },
     966                { "junklearn_script",                   "/etc/zarafa/userscripts/junklearn", CONFIGSETTING_RELOADABLE },
    966967                { "user_safe_mode",                             "no", CONFIGSETTING_RELOADABLE },
    967968
    968969                // Storename format
Note: See TracBrowser for help on using the repository browser.