message.py
4.83 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
# 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/>.
from mailutils.c_api import message
from mailutils import envelope
from mailutils import header
from mailutils import body
from mailutils import attribute
from mailutils.error import MessageError
class Message:
__owner = False
def __init__ (self, msg=None):
if msg == None:
self.msg = message.MessageType ()
self.__owner = True
status = message.create (self.msg)
if status:
raise MessageError (status)
else:
self.msg = msg
def __del__ (self):
if self.__owner:
message.destroy (self.msg)
del self.msg
def __str__ (self):
try:
env = self.get_envelope ()
envelope = '%s %s' % (env.get_sender ().strip (),
env.get_date ().strip ())
except MessageError:
envelope = 'UNKNOWN'
return '<Message "%s" %d %d>' % (envelope, self.get_lines (),
self.get_size ())
def __getattr__ (self, name):
if name == 'header':
return self.get_header ()
elif name == 'body':
return self.get_body ()
elif name == 'envelope':
return self.get_envelope ()
elif name == 'attribute':
return self.get_attribute ()
elif name == 'size':
return self.get_size ()
elif name == 'lines':
return self.get_lines ()
else:
raise AttributeError, name
def __len__ (self):
return self.get_size ()
def is_multipart (self):
status, ismulti = message.is_multipart (self.msg)
if status:
raise MessageError (status)
return ismulti
def get_size (self):
status, size = message.size (self.msg)
if status:
raise MessageError (status)
return size
def get_lines (self):
status, lines = message.lines (self.msg)
if status:
raise MessageError (status)
return lines
def get_envelope (self):
status, env = message.get_envelope (self.msg)
if status:
raise MessageError (status)
return envelope.Envelope (env)
def get_header (self):
status, hdr = message.get_header (self.msg)
if status:
raise MessageError (status)
return header.Header (hdr)
def get_body (self):
status, bd = message.get_body (self.msg)
if status:
raise MessageError (status)
return body.Body (bd)
def get_attribute (self):
status, attr = message.get_attribute (self.msg)
if status:
raise MessageError (status)
return attribute.Attribute (attr)
def get_num_parts (self):
status, num_parts = message.get_num_parts (self.msg)
if status:
raise MessageError (status)
return num_parts
def get_part (self, npart):
status, part = message.get_part (self.msg, npart)
if status:
raise MessageError (status)
return Message (part)
def get_uid (self):
status, uid = message.get_uid (self.msg)
if status:
raise MessageError (status)
return uid
def get_uidl (self):
status, uidl = message.get_uidl (self.msg)
if status:
raise MessageError (status)
return uidl
def get_attachment_name (self, charset=None):
status, name, lang = message.get_attachment_name (self.msg, charset)
if status:
raise MessageError (status)
return name, lang
def save_attachment (self, filename=''):
status = message.save_attachment (self.msg, filename)
if status:
raise MessageError (status)
def unencapsulate (self):
status, msg = message.unencapsulate (self.msg)
if status:
raise MessageError (status)
return Message (msg)
def set_stream (self, stream):
status = message.set_stream (self.msg, stream.stm)
if status:
raise MessageError (status)