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

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

implementado suporte a mensagens nao multipart

File size: 5.5 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 markAsRead(self,folder,UID):
65        """
66        Marca um e-mail como lido
67        TODO implementar marcar como lida no servidor mas sem ler
68        """
69        for i in xrange(len(self._headers)):
70            if self._headers[i].get_UID() == UID:
71                flags = self._headers[i].get_flags()
72                if not r'\Seen' in flags:
73                    flags.append(r'\Seen')
74                    self._headers[i].set_flags(flags)
75                    return
76        return
77                   
78
79
80    def _atualizar_headers(self,folder):
81        self.set_folderAtual(folder)
82        self._headers = []
83       
84        index = self._tool.getConecaoIndex()
85        self._tool.conecoes[index][1].select(folder)
86       
87        typ, headers  = self._tool.conecoes[index][1].fetch('1:*','(UID FLAGS BODY.PEEK[HEADER.FIELDS (FROM TO CC IN-REPLY-TO SUBJECT DATE)])')
88       
89        if typ == 'No':
90            return False
91
92        for i in xrange(0,len(headers),2):
93            info = headers[i][0] + headers[i+1]
94            header = Header.Header(info, headers[i][1])
95            self._headers.append(header)
96
97        return True
98       
99
100    def atualizar(self,):
101        index = self._tool.getConecaoIndex()
102        typ, lista = self._tool.conecoes[index][1].list()
103        folders = []
104        for i in lista:
105            if i.find(r'\Noselect') < 0:
106                if i[-1] == '"':
107                    name = i.split('"')[-2]
108                else:
109                    name = i.split(' ')[-1]
110                #
111                #
112                # PROBLEMA DE PERFORMANCE NO COMANDO STATUS
113                #
114                #
115                typ, [nmsg] = self._tool.conecoes[index][1].status(name,'(MESSAGES UNSEEN)')
116                # separa tudo
117                nmsg = nmsg.split('(')[-1].split(')')[0].split(' ')
118                nmsg[1] = int(nmsg[1])
119                nmsg[3] = int(nmsg[3])
120                # o numero de nao lidas deve ser menor ou igual ao total
121                if nmsg[1] < nmsg[3]:
122                    nlidas = nmsg[1]
123                    total = nmsg[3]
124                else:
125                    total = nmsg[1]
126                    nlidas = nmsg[3]
127                folders.append([name,nlidas,total])
128        self._folders = folders
129       
130        self._atualizar_headers('INBOX')
131
132
133
134
135    def set_headers(self,value):
136        self._headers=value
137
138
139    def set_folderAtual(self,value):
140        self._folderAtual=value
141
142
143    def get_headers(self, folder):
144        if folder == self.get_folderAtual():
145            return self._headers
146        else:
147            self._atualizar_headers(folder)
148            return self._headers
149
150
151    def get_tool(self):
152        return self._tool
153
154
155    def set_tool(self,value):
156        self._tool=value
157
158
159    def get_folders(self):
160        return self._folders
161
162
163    def getUser(self):
164        return self.user
165
166
167    def get_folderAtual(self):
168        return self._folderAtual
169
170
171    def setUser(self,value):
172        self.user=value
173
174
175    def set_folders(self,value):
176        self._folders=value
177
178    def getMail(self,folder,UID):
179        if self._message:
180            if self._message[0] == UID:
181                return self._message[1]
182
183        index = self._tool.getConecaoIndex()
184        typ, data = self._tool.conecoes[index][1].select(folder)
185        typ, data = self._tool.conecoes[index][1].UID('fetch',UID,'(RFC822)')
186        msg = email.message_from_string(data[0][1])
187        self.markAsRead(folder,UID)
188        self._message = ( UID, msg )
189        return self._message[1]
190       
191
192
193##code-section module-footer #fill in your manual code here
194##/code-section module-footer
195
196
197
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.