Package ProcImap :: Package Utils :: Module Server
[hide private]
[frames] | no frames]

Source Code for Module ProcImap.Utils.Server

  1  ############################################################################ 
  2  #    Copyright (C) 2008 by Michael Goerz                                   # 
  3  #    http://www.physik.fu-berlin.de/~goerz                                 # 
  4  #                                                                          # 
  5  #    This program is free software; you can redistribute it and#or modify  # 
  6  #    it under the terms of the GNU General Public License as published by  # 
  7  #    the Free Software Foundation; either version 3 of the License, or     # 
  8  #    (at your option) any later version.                                   # 
  9  #                                                                          # 
 10  #    This program is distributed in the hope that it will be useful,       # 
 11  #    but WITHOUT ANY WARRANTY; without even the implied warranty of        # 
 12  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         # 
 13  #    GNU General Public License for more details.                          # 
 14  #                                                                          # 
 15  #    You should have received a copy of the GNU General Public License     # 
 16  #    along with this program; if not, write to the                         # 
 17  #    Free Software Foundation, Inc.,                                       # 
 18  #    59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             # 
 19  ############################################################################ 
 20   
 21  """ This module contains functions that work directly on an online 
 22      ImapServer/ImapMailbox. 
 23  """ 
 24  import email 
 25  from ProcImap.Utils.Processing import put_through_pager 
 26   
 27  DEFAULT_PAGER = 'less' 
 28   
 29   
30 -def display(mailbox, uid, pager=DEFAULT_PAGER, headerfields=None):
31 """ Display a stripped down version of the message with UID in 32 a pager. The displayed message will contain the the header 33 fields set in 'headerfields', and the first BODY section 34 (which is usually the human-readable part) 35 36 headerfields defaults to ['Date', 'From', 'To', 'Subject'] 37 contrary to the declaration. 38 """ 39 header = mailbox.get_header(uid) 40 result = '' 41 # get body 42 body = '' 43 (code, data) = mailbox._server.uid('fetch', uid, '(BODY[1])') 44 if code == 'OK': 45 body = data[0][1] 46 # build result 47 if headerfields is None: 48 headerfields = ['Date', 'From', 'To', 'Subject'] 49 for field in headerfields: 50 if header.has_key(field): 51 result += "%s: %s\n" % (field, header[field]) 52 result += "\n" 53 result += body 54 put_through_pager(result, pager)
55 56
57 -def summary(mailbox, uids, printout=True, printuid=True):
58 """ generates lines showing some basic information about the messages 59 with the supplied uids. Non-existing UIDs in the list are 60 silently ignored. 61 62 If printout is True, the lines are printed out as they 63 are generated, and the function returns nothing, otherwise, 64 nothing is printed out and the function returns a list of 65 generated lines. 66 67 The summary contains 68 1) an index (the uid if printuid=True) 69 2) the from name (or from address), truncated 70 3) the date of the message 71 4) the subject, truncated 72 73 Each line has the indicated fields truncated so that it is at 74 most 79 characters wide. 75 """ 76 counter = 0 77 result = [] # array of lines 78 if isinstance(uids, (str, int)): 79 uids = [uids] 80 for uid in uids: 81 try: 82 header = mailbox.get_header(uid) 83 except: # unspecified on purpose, might be ProcImap.imaplib2.error 84 continue 85 counter += 1 86 index = counter 87 if printuid: 88 index = str(uid) 89 index = "%2s" % index 90 (from_name, address) = email.utils.parseaddr(header['From']) 91 if from_name == '': 92 from_name = address 93 date = str(header['Date']) 94 datetuple = email.utils.parsedate_tz(date) 95 date = date[:16].ljust(16, " ") 96 if datetuple is not None: 97 date = "%02i/%02i/%04i %02i:%02i" \ 98 % tuple([datetuple[i] for i in (1,2,0,3,4)]) 99 subject = str(header['Subject']) 100 subject = ' '.join([s for (s, c) in \ 101 email.header.decode_header(subject)]) 102 length_from = 25-len(index) # width of ... 103 length_subject = 35 # ... truncated strings 104 generated_line = "%s %s %s %s" \ 105 % (index, 106 from_name[:length_from].ljust(length_from, " "), 107 date, 108 subject[:length_subject].ljust(length_subject, " ")) 109 if printout: 110 print generated_line 111 else: 112 result.append(generated_line) 113 if not printout: 114 return result
115