def PreDelivery(self, session, addrbook, store, folder, message):
        """ Original code from movetopublic.py with call to self.publicfolder().
		Change type modification. """

        props = message.GetProps([PR_RECEIVED_BY_EMAIL_ADDRESS_W], 0)
        if props[0].ulPropTag != PR_RECEIVED_BY_EMAIL_ADDRESS_W:
            self.logger.logError("!--- Not received by emailaddress")
            return MP_CONTINUE,

        recipient = props[0].Value.lower()
        if not recipient:
            self.logger.logError("!--- No recipient in props {}".format(props))
            return MP_CONTINUE,

        recipfolder = self.publicfolder(recipient)
        if not recipfolder:
            self.logger.logDebug(
                ("*--- No public folder for recipient {}".format(
                    recipient.encode('utf-8'))))
            return MP_CONTINUE,

        publicstore = GetPublicStore(session)
        if not publicstore:
            storeprops = store.GetProps([PR_MAILBOX_OWNER_ENTRYID], 0)
            if storeprops[0].ulPropTag == PR_MAILBOX_OWNER_ENTRYID:
                user = addrbook.OpenEntry(storeprops[0].Value, None, 0)
                userprops = user.GetProps([PR_EC_COMPANY_NAME_W], 0)
                if userprops[0].ulPropTag == PR_EC_COMPANY_NAME_W:
                    companyname = userprops[0].Value
                else:
                    companyname = None

                if not companyname:
                    self.logger.logError(("!--- Cannot open a public store."
                                          ' Use "kopano-storeadm -P"'
                                          " to create one if it is missing."))
                    return MP_CONTINUE,

                ema = store.QueryInterface(IID_IExchangeManageStore)
                publicstoreid = ema.CreateStoreEntryID(None, companyname,
                                                       MAPI_UNICODE)
                publicstore = session.OpenMsgStore(0, publicstoreid, None,
                                                   MDB_WRITE)

        publicfolders = publicstore.OpenEntry(
            publicstore.GetProps([PR_IPM_PUBLIC_FOLDERS_ENTRYID], 0)[0].Value,
            None, MAPI_MODIFY)

        folderlist = recipfolder.split('/')
        folder = publicfolders
        for foldername in folderlist:
            if len(foldername) > 0:
                if hexversion >= 0x03000000:
                    folder = folder.CreateFolder(
                        0, foldername, "Create by Move to Public plugin", None,
                        OPEN_IF_EXISTS | MAPI_UNICODE)
                else:
                    folder = folder.CreateFolder(
                        0, foldername, "Create by Move to Public plugin", None,
                        OPEN_IF_EXISTS)

        msgnew = folder.CreateMessage(None, 0)
        tags = message.GetPropList(MAPI_UNICODE)
        message.CopyProps(tags, 0, None, IID_IMessage, msgnew, 0)

        msgnew.SaveChanges(0)
        folderid = folder.GetProps([PR_ENTRYID], 0)[0].Value
        msgid = msgnew.GetProps([PR_ENTRYID], 0)[0].Value

        publicstore.NotifyNewMail(
            NEWMAIL_NOTIFICATION(msgid, folderid, 0, None, 0))

        self.logger.logInfo(
            ("*--- Message moved to public folder {}".format(recipfolder)))

        return MP_STOP_SUCCESS,
Exemple #2
0
    def PreDelivery(self, session, addrbook, store, folder, message):

        props = message.GetProps([PR_RECEIVED_BY_EMAIL_ADDRESS_W], 0)
        if props[0].ulPropTag != PR_RECEIVED_BY_EMAIL_ADDRESS_W:
            self.logger.logError("!--- No received by emailaddress")
            return MP_CONTINUE,

        recipient = props[0].Value.lower()

        if recipient not in self.rulelist:
            self.logger.logInfo("*--- No rule for recipient '%s'" %
                                recipient.encode('utf-8'))
            return MP_CONTINUE,

        publicstore = GetPublicStore(session)
        if publicstore is None:
            # check for company public
            companyname = None

            storeprops = store.GetProps([PR_MAILBOX_OWNER_ENTRYID], 0)
            if storeprops[0].ulPropTag == PR_MAILBOX_OWNER_ENTRYID:
                user = addrbook.OpenEntry(storeprops[0].Value, None, 0)
                userprops = user.GetProps([PR_EC_COMPANY_NAME_W], 0)
                if userprops[0].ulPropTag == PR_EC_COMPANY_NAME_W:
                    companyname = userprops[0].Value

                if companyname is None:
                    self.logger.logError("!--- Can not open a public store")
                    return MP_CONTINUE,

                ema = store.QueryInterface(IID_IExchangeManageStore)
                publicstoreid = ema.CreateStoreEntryID(None, companyname,
                                                       MAPI_UNICODE)
                publicstore = session.OpenMsgStore(0, publicstoreid, None,
                                                   MDB_WRITE)

        publicfolders = publicstore.OpenEntry(
            publicstore.GetProps([PR_IPM_PUBLIC_FOLDERS_ENTRYID], 0)[0].Value,
            None, MAPI_MODIFY)

        folderlist = self.rulelist[recipient].split('/')
        if len(folderlist) == 0:
            self.logger.logWarn(
                "!--- No folders in the rule of recipient '%s'" %
                recipient.encode('utf-8'))
            return MP_CONTINUE,

        folder = publicfolders
        for foldername in folderlist:
            if len(foldername) > 0:
                if hexversion >= 0x03000000:
                    folder = folder.CreateFolder(
                        0, foldername, "Create by Move to Public plugin", None,
                        OPEN_IF_EXISTS | MAPI_UNICODE)
                else:
                    folder = folder.CreateFolder(
                        0, foldername, "Create by Move to Public plugin", None,
                        OPEN_IF_EXISTS)

        msgnew = folder.CreateMessage(None, 0)
        tags = message.GetPropList(MAPI_UNICODE)
        message.CopyProps(tags, 0, None, IID_IMessage, msgnew, 0)

        msgnew.SaveChanges(0)
        folderid = folder.GetProps([PR_ENTRYID], 0)[0].Value
        msgid = msgnew.GetProps([PR_ENTRYID], 0)[0].Value

        publicstore.NotifyNewMail(
            NEWMAIL_NOTIFICATION(msgid, folderid, 0, None, 0))

        self.logger.logInfo("*--- Message moved to public folder '%s'" %
                            (self.rulelist[recipient].encode('utf-8')))

        return MP_STOP_SUCCESS,