source: publico/PloneImapClient/trunk/Cache.py @ 2665

Última Alteração nesse arquivo desde 2665 foi 2665, incluída por johnnysouza, 15 anos atrás

implementada parte de delMail

File size: 6.3 KB
Linha 
1# File: Cache.py
2#
3# Copyright (c) 2006 by Interlegis
4# Generator: ArchGenXML Version 1.4.0-RC2 svn/development
5#            http://plone.org/products/archgenxml
6#
7# GNU General Public Licence (GPL)
8#
9# This program is free software; you can redistribute it and/or modify it under
10# the terms of the GNU General Public License as published by the Free Software
11# Foundation; either version 2 of the License, or (at your option) any later
12# version.
13# This program is distributed in the hope that it will be useful, but WITHOUT
14# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
16# details.
17# You should have received a copy of the GNU General Public License along with
18# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19# Place, Suite 330, Boston, MA  02111-1307  USA
20#
21__author__  = '''STC Interlegis <stc@interlegis.gov.br>'''
22__docformat__ = 'plaintext'
23
24
25##code-section module-header #fill in your manual code here
26import Header
27import email
28##/code-section module-header
29
30
31
32
33class Cache:
34    ''' <p>cache das informacao do email de cada usuario</p>'''
35    __implements__ = ()
36
37    ##code-section class-header_Cache #fill in your manual code here
38    ##/code-section class-header_Cache
39
40
41    def __init__(self,*args,**kwargs):
42        self._init_attributes(*args,**kwargs)
43        self.atualizar()
44
45
46    def _init_attributes(self,*args,**kwargs):
47        #attributes
48        self._folders=None
49        self._headers=None
50        self._folderAtual=None
51        self._tool=None
52        self.user=None
53        self._message=None
54
55        # automatically set attributes where mutators exist
56        for key in kwargs.keys():
57            # camel case: variable -> setVariable
58            mutatorName = 'set'+key[0].upper()+key[1:]
59            mutator = getattr(self, mutatorName)
60            if mutator is not None and callable(mutator):
61                mutator(kwargs[key])
62
63
64    def _atualizar_headers(self,folder):
65        self.set_folderAtual(folder)
66        self._headers = []
67       
68        index = self._tool.getConecaoIndex()
69        self._tool.conecoes[index][1].select(folder)
70       
71        typ, headers  = self._tool.conecoes[index][1].fetch('1:*','(UID FLAGS BODY.PEEK[HEADER.FIELDS (FROM TO CC IN-REPLY-TO SUBJECT DATE)])')
72       
73        if typ == 'No':
74            return False
75
76        for i in xrange(0,len(headers),2):
77            info = headers[i][0] + headers[i+1]
78            header = Header.Header(info, headers[i][1])
79            self._headers.append(header)
80
81        return True
82       
83
84    def atualizar(self,):
85        index = self._tool.getConecaoIndex()
86        typ, lista = self._tool.conecoes[index][1].list()
87        folders = []
88        for i in lista:
89            if i.find(r'\Noselect') < 0:
90                if i[-1] == '"':
91                    name = i.split('"')[-2]
92                else:
93                    name = i.split(' ')[-1]
94                #
95                #
96                # PROBLEMA DE PERFORMANCE NO COMANDO STATUS
97                #
98                #
99                typ, [nmsg] = self._tool.conecoes[index][1].status(name,'(MESSAGES UNSEEN)')
100                # separa tudo
101                nmsg = nmsg.split('(')[-1].split(')')[0].split(' ')
102                nmsg[1] = int(nmsg[1])
103                nmsg[3] = int(nmsg[3])
104                # o numero de nao lidas deve ser menor ou igual ao total
105                if nmsg[1] < nmsg[3]:
106                    nlidas = nmsg[1]
107                    total = nmsg[3]
108                else:
109                    total = nmsg[1]
110                    nlidas = nmsg[3]
111                folders.append([name,nlidas,total])
112        self._folders = folders
113       
114        self._atualizar_headers('INBOX')
115
116
117    def get_headers(self, folder):
118        if folder == self.get_folderAtual():
119            return self._headers
120        else:
121            self._atualizar_headers(folder)
122            return self._headers
123
124
125    def getHeader(self, folder, UID):
126        UID = int(UID)
127        for i in xrange(len(self._headers)):
128            if self._headers[i].get_UID() == UID:
129               print self._headers[i].get_UID()
130               return self._headers[i];
131        return
132
133
134
135    def getMail(self,folder,UID):
136        if self._message:
137            if self._message[0] == UID:
138                return self._message[1]
139
140        index = self._tool.getConecaoIndex()
141        typ, data = self._tool.conecoes[index][1].select(folder)
142        typ, data = self._tool.conecoes[index][1].UID('fetch',UID,'(RFC822)')
143        msg = email.message_from_string(data[0][1])
144        self.markAsRead(folder,UID)
145        self._message = ( UID, msg )
146        return self._message[1]
147       
148
149    def delMail(self,folder,UID):
150        for i in xrange(len(self._headers)):
151            if str( self._headers[i].get_UID() ) == UID:
152                #deleta da cache
153                self._headers.__delitem__(i)
154                # se deletar da cache tem que mover para a lixeira no servidor
155                index = self._tool.getConecaoIndex()
156                #self._tool.conecoes[index][1].
157                return
158               
159        return
160
161
162    def set_message(self,value):
163        self._message=value
164
165    def get_message(self):
166        return self._message
167
168
169    def set_folderAtual(self,value):
170        self._folderAtual=value
171
172
173    def get_tool(self):
174        return self._tool
175
176
177    def markAsRead(self,folder,UID):
178        """
179        Marca um e-mail como lido
180        TODO implementar marcar como lida no servidor mas sem ler
181        """
182        for i in xrange(len(self._headers)):
183            if self._headers[i].get_UID() == UID:
184                flags = list(self._headers[i].get_flags())
185                if not r'\Seen' in flags:
186                    flags.append(r'\Seen')
187                    self._headers[i].set_flags(tuple(flags))
188                    return
189        return
190                   
191
192    def get_folders(self):
193        return self._folders
194
195
196    def getUser(self):
197        return self.user
198
199
200    def get_folderAtual(self):
201        return self._folderAtual
202
203
204    def setUser(self,value):
205        self.user=value
206
207
208    def set_folders(self,value):
209        self._folders=value
210
211
212    def set_tool(self,value):
213        self._tool=value
214
215
216##code-section module-footer #fill in your manual code here
217##/code-section module-footer
218
219
220
Note: Veja TracBrowser para ajuda no uso do navegador do trac.
 

The contents and data of this website are published under license:
Creative Commons 4.0 Brasil - Atribuir Fonte - Compartilhar Igual.