Messager.php
5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
namespace Lib\Imap\Parse;
use Lib\Imap\Request\Msg;
/**
* 邮件列表
* @author:dc
* @time 2024/9/14 15:33
* Class Messager
* @package Lib\Imap\Parse
*/
class Messager implements \IteratorAggregate {
/**
* 原始数据
* @var array
*/
protected array $result = [];
/**
* 解析后的数据
* @var array
*/
protected array $item = [];
/**
* 读取消息的
* @var Msg
*/
protected Msg $msg;
public function __construct(array $result, Msg $msg)
{
$this->msg = $msg;
$k = 0;
foreach ($result as $item){
// 匹配出id和数据
if(preg_match("/\* (\d+) FETCH \(/",$item)){
$k++;
$this->result[$k] = $item;
}else{
if(isset($this->result[$k])){
$this->result[$k] .= $item;
}
}
}
$this->parse();
}
/**
* 实现 使用 foreach功能
* @return \ArrayIterator
* @author:dc
* @time 2024/9/18 10:43
*/
public function getIterator() {
return new \ArrayIterator($this->item);
}
/**
* 解析
* @author:dc
* @time 2024/9/14 17:35
*/
protected function parse(){
foreach ($this->result as $item){
$item = trim($item);
// 匹配出id和数据
if(preg_match("/\* (\d+) FETCH \(([\w\s\d\r\n\W]{1,})\)$/i",$item,$line)){
$this->parseFetch($line[1],$line[2]);
}
$line = null;
}
}
/**
* 解析头部信息,只能解析固定字段的属性
* @param int $msgno
* @param string $line
* UID 1568602720 INTERNALDATE "16-Sep-2019 10:58:40 +0800" FLAGS (\Seen) RFC822.HEADER {816}
Received: from localhost (unknown [10.110.4.165])
by mfast8 (Coremail) with SMTP id t8CowAA3Za5g+n5d1b4tCA--.53220S2;
Mon, 16 Sep 2019 10:58:40 +0800 (CST)
Date: Mon, 16 Sep 2019 10:58:40 +0800 (GMT+08:00)
From: =?utf-8?B?572R5piT6YKu566x5aSn5biI5Zui6Zif?= <developer.MailMaster@service.netease.com>
To: zhlong0616@163.com
Message-ID: <1803973093.340445.1568602720140.JavaMail.developer.MailMaster@service.netease.com>
Subject: =?utf-8?B?5qyi6L+O5L2/55So572R5piT6YKu566x5aSn5biI?=
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_Part_340444_1698792253.1568602720140"
X-CM-TRANSID:t8CowAA3Za5g+n5d1b4tCA--.53220S2
X-Coremail-Antispam: 1Uf129KBjDUn29KB7ZKAUJUUBvt529EdanIXcx71UUUUU7v73
VFW2AGmfu7bjvjm3AaLaJ3UbIYCTnIWIevJa73UjIFyTuYvj4RJUUUUUUUU
X-Originating-IP: [10.110.4.165]
RFC822.SIZE 5111
* @author:dc
* @time 2024/9/18 11:46
*/
protected function parseFetch(int $msgno, string $line) {
$this->item[$msgno] = new MessageItem($msgno, $this->msg);
$tokens = explode(' ',$line);
while ($tokens){
$key = trim(array_shift($tokens));
switch ($key){
case 'UID':{
$this->item[$msgno]->uid = array_shift($tokens);
break;
}
case 'FLAGS':{
$flags = [];
while (1){
$flags[] = array_shift($tokens);
if(substr(end($flags),-1)==')') break;
}
$this->item[$msgno]->flags = array_map(function ($v){
return strtolower(str_replace(['\\','(',')'],'',$v));
},$flags);
break;
}
case 'INTERNALDATE':{
// 内部时间
$date = '';
while (1){
$date .= array_shift($tokens);
if(substr($date,-1)=='"') break;
$date.= ' ';
}
$this->item[$msgno]->date = trim($date,'"');
break;
}
case 'RFC822.SIZE':{
$this->item[$msgno]->size = array_shift($tokens);
break;
}
case 'RFC822.HEADER':{
$str = trim(implode(' ',$tokens));
// 是否带有字符数数量
if(preg_match("/^\{(\d+)\}/",$str,$num)){
$len = $num[1]+strlen($num[0]);
$header = substr($str,0,$len);
// 剩下的字符串
$tokens = explode(' ',substr($str,$len,9999));
// 踢出长度的头
$header = substr($header,strlen($num[0]),strlen($header));
}else{
// 以3个\r\n为结束
$str = explode("\r\n\r\n\r\n",trim($str));
$header = array_shift($str);
// 剩余的字符串
$tokens = explode(' ',implode("\r\n\r\n\r\n",$str));
}
$this->item[$msgno]->header = new Header($header);
break;
}
default:{break;}
}
}
}
/**
* 循环结果
* @param \Closure $closure
* @author:dc
* @time 2024/9/18 11:41
*/
public function each(\Closure $closure):void {
array_map($closure,$this->item);
}
}