コード例 #1
0
ファイル: main.py プロジェクト: ogata/swmail
 def showmessage(self, uid):
     print('showmessage: ' + uid)
     buf = ''
     try:
         server = cherrypy.session['imap_server']
         user = cherrypy.session['imap_user']
         pwd = cherrypy.session['imap_pwd']
         folder = cherrypy.session['current_folder']
         print("folder: " + folder)
         imap = imaplib.IMAP4(server)
         imap.login(user, pwd)
         typ, [dat] = imap.select('"' + utf7imap.encoder(folder)[0].decode() + '"', True)
         if typ != 'OK':
             buf += html.escape(dat.decode())
         else:
             num_of_mails = int(dat.decode())
             if num_of_mails == 0:
                 pass
             else:
                 # typ, dat = imap.fetch(uid, '(BODY.PEEK[])')     # これでよいか?未読既読が変化すると嫌だ
                 typ, dat = imap.uid('FETCH', uid, '(BODY.PEEK[])')     # これでよいか?未読既読が変化すると嫌だ
                 assert typ == 'OK'
                 print('===================')
                 # pprint.pprint(dat)
                 # print(type(dat).__name__)
                 for x in dat:
                     if type(x).__name__ == 'tuple':
                         assert len(x) == 2
                         msg = email.parser.BytesParser().parsebytes(x[1], False)    # email.message.Message
                         '''
                         print(msg['subject'])
                         print(msg['date'])
                         print(msg['from'])
                         '''
                         buf += '<h1 data-dojo-type="dojox.mobile.Heading"'
                         buf += '    data-dojo-props="back:\'Messages\', moveTo:\'view3\', label:\'99 of 99\'">'
                         buf += '<ul data-dojo-type="dojox.mobile.TabBar" data-dojo-props=\'barType:"segmentedControl", selectOne:false\' style="float:right;">'
                         buf += '<li data-dojo-type="dojox.mobile.TabBarButton" data-dojo-props=\'icon:"mblDomButtonWhiteUpArrow"\'></li>'
                         buf += '<li data-dojo-type="dojox.mobile.TabBarButton" data-dojo-props=\'icon:"mblDomButtonWhiteDownArrow"\'></li>'
                         buf += '</ul>'
                         buf += '</h1>'
                         buf += '<ul data-dojo-type="dojox.mobile.EdgeToEdgeList" data-dojo-props=\'variableHeight:true\'>'
                         '''
                         buf += '<li data-dojo-type="dojox.mobile.ListItem" data-dojo-props=\'\'>From: </li>'
                         buf += '<li data-dojo-type="dojox.mobile.ListItem" data-dojo-props=\'\'>To: </li>'
                         '''
                         buf += '<li data-dojo-type="dojox.mobile.ListItem" data-dojo-props=\'\'>'
                         buf += '<div class="aaa2" >'
                         buf += myreplace('Subject: ' + html.escape(hoge(msg['subject']))) + '<br />'
                         buf += 'Date: ' + html.escape(msg['date']) + '<br />'
                         buf += myreplace('From: ' + html.escape(hoge(msg['from']))) + '<br />'
                         buf += 'To: ' + html.escape(msg['to']) + '<br />'
                         buf += '</div>'
                         buf += '</li>'
                         buf += '<li data-dojo-type="dojox.mobile.ListItem" data-dojo-props=\'\'>'
                         buf += '<div class="aaa2" style="font-family: monospace" >'
                         '''
                         print(msg.get_charset())
                         print(msg.get_content_type())
                         print(msg.get_content_charset())
                         '''
                         charset = msg.get_content_charset()
                         payload = msg.get_payload(decode=True)
                         payload = payload.decode(charset)
                         # print(type(payload))
                         # payload = html.escape(payload)
                         # payload = payload.replace('\r\n', '<br />\n')
                         # print(payload)
                         buf += '<pre>' + payload + '</pre>'
                         buf += '</div>'
                         buf += '</li>'
                         buf += '</ul>'
                         buf += ''
                     elif type(x).__name__ == 'bytes':
                         pass
                     else:
                         assert False
                 print('===================')
     except socket.gaierror as ex:
         return str(ex)
     except imaplib.IMAP4.error as ex:
         return str(ex)
     else:
         return buf
コード例 #2
0
ファイル: main.py プロジェクト: ogata/swmail
 def listmessages(self, folder):
     print('listmessages: ' + folder)
     cherrypy.session['current_folder'] = folder     # @@@
     buf = ''
     buf += '<h1 data-dojo-type="dojox.mobile.Heading"'
     buf += '    data-dojo-props="back:\'Folders\', moveTo:\'viewFolders\', label:\'Messages\' " >'
     buf += '<span data-dojo-type="dojox.mobile.ToolBarButton" data-dojo-props=\'label:"Refresh"\' style="float:right;" ></span>'
     buf += '</h1>'
     try:
         server = cherrypy.session['imap_server']
         user = cherrypy.session['imap_user']
         pwd = cherrypy.session['imap_pwd']
         imap = imaplib.IMAP4(server)
         imap.login(user, pwd)
         # typ, [dat] = imap.select('"' + folder + '"', True)
         typ, [dat] = imap.select('"' + utf7imap.encoder(folder)[0].decode() + '"', True)
         if typ != 'OK':
             buf += html.escape(dat.decode())
         else:
             num_of_mails = int(dat.decode())
             if num_of_mails == 0:
                 pass
             else:
                 msg_ids = "1:" + str(num_of_mails)
                 typ, dat = imap.fetch(msg_ids, '(BODY.PEEK[HEADER] UID FLAGS)')
                 assert typ == 'OK'
                 # pprint.pprint(dat)
                 buf += '<ul data-dojo-type="dojox.mobile.RoundRectList" '
                 buf += '    data-dojo-props="variableHeight:true" >'
                 for x in dat:
                     if type(x).__name__ == 'tuple':
                         assert len(x) == 2
                         info = x[0].decode()    # 1 (UID 1 FLAGS (\Seen) BODY[HEADER] {3800}
                         uid = info.split()[2]
                         # print(info)
                         # print(uid)
                         lines = x[1].decode()
                         headers = email.parser.Parser().parsestr(lines, True)
                         subject = headers['subject']
                         subject_decoded = hoge(subject)
                         date = headers['date']
                         frm = headers['from']
                         frm_decoded = hoge(frm)
                         buf += '<li data-dojo-type="dojox.mobile.ListItem" '
                         buf += 'data-dojo-props="moveTo:\'#\', onClick:actShowMessage, '
                         # buf += 'label: \'' + subject_decoded + '\'">'
                         buf += 'UID: \'' + uid + '\' '
                         buf += '" >'
                         buf += '<div class="aaa1" >'
                         buf += myreplace(html.escape(subject_decoded))
                         buf += '</div>'
                         buf += '<div class="aaa2" >'
                         buf += myreplace(html.escape(frm_decoded)) + '<br />'
                         buf += myreplace(html.escape(date)) + '<br />'
                         buf += '</div>'
                         buf += '</li>'
                     elif type(x).__name__ == 'bytes':
                         pass
                     else:
                         assert False
                 buf += '</ul>'
     except socket.gaierror as ex:
         return str(ex)
     except imaplib.IMAP4.error as ex:
         return str(ex)
     else:
         return buf