如何在python procmail管道脚本中获取UIDNEXT?

时间:2014-12-01 12:01:17

标签: python dovecot

我有这个用于将原始电子邮件插入db的python脚本。不要问我为什么要将原始邮件插入数据库。

import sys
from DB import *
import email

full_msg = sys.stdin.readlines()
j =  ''.join(full_msg)
msg = email.message_from_string(j)

sql = '''INSERT INTO Email(Id, Message) VALUES (NULL, %s)'''
db = DB()
db.query(sql, (msg, ))

如果我能获得该消息的uid会很棒,所以如果我例如从db中删除消息,我也可以删除我在imap服务器上的消息。

我不想登录到imap服务器,然后通过uid删除邮件,因为我不知道用户密码,因为它是加密的。

我正在考虑使用例如msg [' Message-Id'],然后在用户maildir中为该Message-Id grep文件并删除实际文件,但这听起来对我来说完全错误。

我在python中知道你在imaplib中有类似UIDNEXT的东西,但这是假设我已经登录了,我不是。

更新:

有了这个,我可以获取下一个uid,但我必须登录。如何在没有登录的情况下获得UIDNEXT? 顺便说一句,我使用postfix / dovecot和mysql。

import getpass, sys
from imapclient import IMAPClient

try:
    hostname, username = sys.argv[1:]
except ValueError:
    print 'usage %s hostname username' % sys.argv[0]

c = IMAPClient(hostname, ssl=True)

try:
    c.login(username, getpass.getpass())
except c.Error, e:
    print "Could not login in:", e
    sys.exit(1)

else:
    select_dict = c.select_folder('INBOX', readonly=True)
    for k, v in select_dict.items():
        if k == 'UIDNEXT':
            print '%s: %r' % (k,v)
    c.logout()

NEW UPDATE

dovecot-uidlist的样本

16762 W105493 S104093 :1417408077.2609_1.zumance
16763 S18340 W18608 :1417429204.3464_1.zumance

dovecot-uidlist uid的最后一行代码:

l = open("dovecot-uidlist").readlines()
print l[-1].split(" ")[0]

这是已完成的邮件管道脚本:

import sys
import email
import re
from DB import *
full_msg = sys.stdin.readlines()
j =  ''.join(full_msg)

msg = email.message_from_string(j)
match = re.search(r'[\w\.-]+@[\w\.-]+', msg['to'])

address = match.group(0)
address = address.split("@")

with open("/var/vmail/"+address[1]+"/"+address[0]+"/dovecot-uidlist", 'r') as f:
  first_line = f.readline()
  nextuid = first_line.split(" ")
  nextuid = re.findall(r'\d+', nextuid[2])

sql = '''INSERT INTO Email(Id, Message, Uid, Domain, Mbox) VALUES (NULL, %s, %s, %s, %s)'''
db = DB()
db.query(sql, (msg, nextuid[0], address[1], address[0],  ))

包含https://pregmatch.org/read/python-procmail

文件的博文

2 个答案:

答案 0 :(得分:3)

Dovecot维护文件dovecot-uidlist中UID和文件名之间的映射。该文件首先包含一个标题行,然后每个消息包含一行。

标题行如下所示:

1 1173189136 20221

第一个数字是版本,第二个数字是IMAP UIDVALIDITY,最后一个是将要使用的下一个UID。

之后,每条消息都有自己的行,如下所示:

20220 1035478339.27041_118.example.org:2,S

第一个单词是UID,下一个是文件名。

the dovecot wiki还有更多信息。

答案 1 :(得分:0)

由于解析dovecot-uidlist不起作用,因为在您使用电子邮件客户端检查电子邮件之前列表不会更新,我决定采用其他解决方案。该解决方案是dovecot pre-auth机制。在我的python procmail管道脚本中,我决定做这样的事情:

import subprocess
p = subprocess.Popen( "/usr/libexec/dovecot/imap -u "+user, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
p.stdin.write("b select INBOX\n")
p.stdin.write("e logout\n")
(stdoutdata, stderrdata) = p.communicate()
print stdoutdata

print stderrdata

stdoutdata给我的输出看起来像这样:

* PREAUTH [CAPABILITY IMAP4rev1 LITERAL+ SASL-IR LOGIN-REFERRALS ID ENABLE IDLE SORT SORT=DISPLAY THREAD=REFERENCES THREAD=REFS THREAD=ORDEREDSUBJECT MULTIAPPEND URL-PARTIAL CATENATE UNSELECT CHILDREN NAMESPACE UIDPLUS LIST-EXTENDED I18NLEVEL=1 CONDSTORE QRESYNC ESEARCH ESORT SEARCHRES WITHIN CONTEXT=SEARCH LIST-STATUS SPECIAL-USE BINARY MOVE] Logged in as nikola@pregmatch.org
* FLAGS (\Answered \Flagged \Deleted \Seen \Draft $NotJunk NotJunk $Forwarded)
* OK [PERMANENTFLAGS (\Answered \Flagged \Deleted \Seen \Draft $NotJunk NotJunk $Forwarded \*)] Flags permitted.
* 5574 EXISTS
* 0 RECENT
* OK [UIDVALIDITY 1412448500] UIDs valid
* OK [UIDNEXT 16875] Predicted next UID
* OK [HIGHESTMODSEQ 3051] Highest
b OK [READ-WRITE] Select completed (0.009 secs).
* BYE Logging out
e OK Logout completed.

现在我所要做的就是解析输出的这一部分:

* OK [UIDVALIDITY 1412448500] UIDs valid
* OK [UIDNEXT 16875] Predicted next UID

这位前认证解决了我的问题。我将在今天晚些时候(以及我的博客上)发布解析部分的完整解决方案。

解决方案:

import subprocess
pSub = subprocess.Popen( "/usr/libexec/dovecot/imap -u "+username+"@"+domain_parsed, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
pSub.stdin.write("b select INBOX\n")
pSub.stdin.write("e logout\n")
(stdoutdata, stderrdata) = pSub.communicate()
dovecotStream = open("/var/www/domain.com/scripts/server/dovecot","w")
dovecotStream.write(stdoutdata)
dovecotStream.close()


nextuidNo = []
with open("/var/www/domain.com/scripts/server/dovecot") as dovecotFile:
    dovecotFilelines = dovecotFile.read()
for dovecotFileline in dovecotFilelines.split('\n'):
    matchCheck = re.findall(r'\[UIDNEXT.+\]', dovecotFileline)
        if len(matchCheck):
            nextuidNo = re.findall(r'\d+', matchCheck[0])

print nextuidNo #this is list

完成博客文章:https://pregmatch.org/read/dovecot-preauth-python