我curl
在我的 Lua 程序中使用来获取新的邮件列表。我正在尝试发送这样的请求
local head_command = "curl --connect-timeout 30 -fsm 30"
local server = "imap.gmail.com"
local mail = "***"
local password = "***"
local port = 993
local request = "-X 'FETCH 86 BODY.PEEK[HEADER.FIELDS (SUBJECT)]'"
local curl = string.format("%s --url 'imaps://%s:%s/INBOX' -u %s:%q %s -k", head_command, server, port, mail, password, request)
helpers.async(curl, function(stdout, exit_code)
naughty.notify { text = stdout, timeout = 0 }
end )
但只有我得到的是
* 86 FETCH (BODY[HEADER.FIELDS (SUBJECT)] {54}
FETCH
对于任何命令我都会得到这样的响应。
如果直接在 url 中HEADER
指定,我可以收到消息,例如UID
imaps://%s:%s/INBOX;UID=86/;SECTION=HEADER
它输出这个
Delivered-To: [email protected]
Received: by 10.140.91.106 with SMTP id y97csp1976911qgd; Mon, 25 May 2015
18:50:08 -0700 (PDT)
X-Received: by 10.112.142.232 with SMTP id rz8mr19609586lbb.74.1432605008430;
Mon, 25 May 2015 18:50:08 -0700 (PDT)
Return-Path: <[email protected]>
Received: from f342.i.mail.ru (f342.i.mail.ru. [217.69.140.238]) by
mx.google.com with ESMTPS id b3si5030682lbd.117.2015.05.25.18.50.07 for
<[email protected]> (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256
bits=128/128); Mon, 25 May 2015 18:50:08 -0700 (PDT)
Received-SPF: pass (google.com: domain of [email protected] designates
217.69.140.238 as permitted sender) client-ip=217.69.140.238;
Authentication-Results: mx.google.com; spf=pass (google.com: domain of
[email protected] designates 217.69.140.238 as permitted sender)
[email protected]; dkim=pass [email protected]; dmarc=pass (p=NONE
dis=NONE) header.from=mail.ru
DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mail.ru;
s=mail2; h=Content-Type:Message-ID:Reply-To:Date:MIME-Version:To:From;
bh=vOFnL58jZvcsH24k/6XvHT0DhLLtNvAaCxTAZUvggn4=;
b=GJpRK8wJfOfrrlW6KycI6CDBUPTlQNBSNmAswE+5M67hdePFUeoHkakQdh8ziPtmOPsSWCwLs2spcqXjTkQ1Ny2raWBrbVaaFKPSxzdi7m6JGfwJ3Snkv1DMGBhGVCqBOIwVj7iCdnBEzYQhCMgZ6UvjDNv15PReV5MiJzECnG0=;
Received: from [194.125.255.143] (ident=mail) by f342.i.mail.ru with local
(envelope-from <[email protected]>) id 1Yx40N-0001gA-0m for
[email protected]; Tue, 26 May 2015 04:50:07 +0300
Received: from [194.125.255.143] by e.mail.ru with HTTP; Tue, 26 May 2015
04:50:06 +0300
From: =?UTF-8?B?0JzQsNGA0LjQvdCwINCR0YPQs9Cw0LXQstCw?= <[email protected]>
To: =?UTF-8?B?0JrQuNGA0LjQu9C7INCR0YPQs9Cw0LXQsg==?= <[email protected]>
MIME-Version: 1.0
X-Mailer: Mail.Ru Mailer 1.0
X-Originating-IP: [194.125.255.143]
Date: Tue, 26 May 2015 04:50:06 +0300
Reply-To: =?UTF-8?B?0JzQsNGA0LjQvdCwINCR0YPQs9Cw0LXQstCw?= <[email protected]>
X-Priority: 3 (Normal)
Message-ID: <[email protected]>
Content-Type: multipart/alternative;
boundary="--ALT--rUEeoQugXy3WMzk9UAxdG7pumUrVItQl1432605006"
X-Mras: Ok
X-Spam: undefined
但我怎样才能通过SUBJECT
这种方式获取消息呢?
答案1
我认为明白问题所在。 Gmail 分几步发送响应,curl 仅首先输出。我编写了可以检索消息主题的 lua 代码。像这样的东西
local socket = require "socket"
local ssl = require "ssl"
local format = string.format
local server = "imap.gmail.com"
local port = 993
local login = "[email protected]"
local password = "***"
local params = {
mode = "client",
protocol = "tlsv1",
verify = "none",
options = "all",
}
local conn = socket.connect(server, port)
conn = ssl.wrap(conn, params)
conn:dohandshake()
conn:settimeout(5)
conn:send(format("123 LOGIN %s %s", login, password) .. '\r\n')
conn:send("123 EXAMINE INBOX" .. '\r\n')
conn:send("123 FETCH 86 BODY.PEEK[HEADER.FIELDS (SUBJECT)]" .. '\r\n')
local result = ""
while result do
result = conn:receive()
if result then
print(result)
end
end
conn:send("LOGOUT")
conn:close()
它输出
* OK Gimap ready for requests from 176.114.188.238 q1mb64314748lfc
* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE ENABLE MOVE CONDSTORE ESEARCH UTF8=ACCEPT LIST-EXTENDED LIST-STATUS LITERAL- SPECIAL-USE APPENDLIMIT=35651584
123 OK [email protected] authenticated (Success)
* FLAGS (\Answered \Flagged \Draft \Deleted \Seen $NotPhishing $Phishing)
* OK [PERMANENTFLAGS ()] Flags permitted.
* OK [UIDVALIDITY 1] UIDs valid.
* 352 EXISTS
* 0 RECENT
* OK [UIDNEXT 1283] Predicted next UID.
* OK [HIGHESTMODSEQ 218991]
123 OK [READ-ONLY] INBOX selected. (Success)
* 86 FETCH (BODY[HEADER.FIELDS (SUBJECT)] {37}
Subject: =?UTF-8?B?UmVbMTJdOiA=?=
)
123 OK Success