header.py
6.3 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
# GNU Mailutils -- a suite of utilities for electronic mail
# Copyright (C) 2009-2012, 2014-2017 Free Software Foundation, Inc.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 3 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General
# Public License along with this library. If not, see
# <http://www.gnu.org/licenses/>.
import types
from mailutils.c_api import header
from mailutils.error import *
class Header:
def __init__ (self, hdr):
self.hdr = hdr
def __del__ (self):
del self.hdr
def __getitem__ (self, item):
if isinstance (item, types.IntType):
return self.get_field_name (item), self.get_field_value (item)
else:
return self.get_value (item)
def __setitem__ (self, name, value):
self.set_value (name, value)
def __contains__ (self, name):
status, value = header.get_value (self.hdr, name)
if status == MU_ERR_NOENT:
return False
elif status:
raise HeaderError (status)
return True
def __getattr__ (self, name):
if name == 'size':
return self.get_size ()
elif name == 'lines':
return self.get_lines ()
else:
raise AttributeError, name
def __len__ (self):
return self.get_field_count ()
def __iter__ (self):
self.__count = 0
self.__len = self.get_field_count ()
return self
def next (self):
if self.__count >= self.__len:
self.__count = 0
raise StopIteration
else:
self.__count += 1
return self.__getitem__ (self.__count)
def has_key (self, name):
return self.__contains__ (name)
def get_size (self):
status, size = header.size (self.hdr)
if status:
raise HeaderError (status)
return size
def get_lines (self):
status, lines = header.lines (self.hdr)
if status:
raise HeaderError (status)
return lines
def get_value (self, name, default=None):
status, value = header.get_value (self.hdr, name)
if status == MU_ERR_NOENT:
if default != None:
return default
else:
raise KeyError (name)
elif status:
raise HeaderError (status)
return value
def get_value_n (self, name, n=1, default=None):
status, value = header.get_value_n (self.hdr, name, n)
if status == MU_ERR_NOENT:
if default != None:
return default
else:
raise KeyError (name)
elif status:
raise HeaderError (status)
return value
def set_value (self, name, value, replace=True):
status = header.set_value (self.hdr, name, value, replace)
if status:
raise HeaderError (status)
def get_field_count (self):
status, count = header.get_field_count (self.hdr)
if status:
raise HeaderError (status)
return count
def get_field_name (self, idx):
status, name = header.get_field_name (self.hdr, idx)
if status == MU_ERR_NOENT:
raise IndexError (idx)
elif status:
raise HeaderError (status)
return name
def get_field_value (self, idx):
status, value = header.get_field_value (self.hdr, idx)
if status == MU_ERR_NOENT:
raise IndexError (idx)
elif status:
raise HeaderError (status)
return value
MU_HEADER_UNIX_FROM = "From "
MU_HEADER_RETURN_PATH = "Return-Path"
MU_HEADER_RECEIVED = "Received"
MU_HEADER_DATE = "Date"
MU_HEADER_DCC = "Dcc"
MU_HEADER_FROM = "From"
MU_HEADER_SENDER = "Sender"
MU_HEADER_RESENT_FROM = "Resent-From"
MU_HEADER_SUBJECT = "Subject"
MU_HEADER_SENDER = "Sender"
MU_HEADER_RESENT_SENDER = "Resent-SENDER"
MU_HEADER_TO = "To"
MU_HEADER_RESENT_TO = "Resent-To"
MU_HEADER_CC = "Cc"
MU_HEADER_RESENT_CC = "Resent-Cc"
MU_HEADER_BCC = "Bcc"
MU_HEADER_RESENT_BCC = "Resent-Bcc"
MU_HEADER_REPLY_TO = "Reply-To"
MU_HEADER_RESENT_REPLY_TO = "Resent-Reply-To"
MU_HEADER_MESSAGE_ID = "Message-ID"
MU_HEADER_RESENT_MESSAGE_ID = "Resent-Message-ID"
MU_HEADER_IN_REPLY_TO = "In-Reply-To"
MU_HEADER_REFERENCE = "Reference"
MU_HEADER_REFERENCES = "References"
MU_HEADER_ENCRYPTED = "Encrypted"
MU_HEADER_PRECEDENCE = "Precedence"
MU_HEADER_STATUS = "Status"
MU_HEADER_CONTENT_LENGTH = "Content-Length"
MU_HEADER_CONTENT_LANGUAGE = "Content-Language"
MU_HEADER_CONTENT_TRANSFER_ENCODING = "Content-transfer-encoding"
MU_HEADER_CONTENT_ID = "Content-ID"
MU_HEADER_CONTENT_TYPE = "Content-Type"
MU_HEADER_CONTENT_DESCRIPTION = "Content-Description"
MU_HEADER_CONTENT_DISPOSITION = "Content-Disposition"
MU_HEADER_CONTENT_MD5 = "Content-MD5"
MU_HEADER_MIME_VERSION = "MIME-Version"
MU_HEADER_X_MAILER = "X-Mailer"
MU_HEADER_X_UIDL = "X-UIDL"
MU_HEADER_X_UID = "X-UID"
MU_HEADER_X_IMAPBASE = "X-IMAPbase"
MU_HEADER_ENV_SENDER = "X-Envelope-Sender"
MU_HEADER_ENV_DATE = "X-Envelope-Date"
MU_HEADER_FCC = "Fcc"
MU_HEADER_DELIVERY_DATE = "Delivery-date"
MU_HEADER_ENVELOPE_TO = "Envelope-to"
MU_HEADER_X_EXPIRE_TIMESTAMP = "X-Expire-Timestamp"
MU_HEADER_REPLACE = 0x01
MU_HEADER_BEFORE = 0x02