def find_messages(sock, label): mailbox = imap_utf7.encode(label) label = imap_utf7.encode(label.encode('utf-8')) try: # process regular mailbox sock.select(mailbox) except sock.error: pass else: resp, data = sock.uid('SEARCH', None, '(ALL)') assert resp == 'OK' for uid in data[0].split(): # because we do select, this uid will be valid. yield uid try: # now process chats with that label sock.select("[Gmail]/Chats", True) except sock.error: # access to chats via IMAP is disabled most likely pass else: # resp, data = sock.uid('SEARCH', 'X-GM-RAW', 'label:%s' % label) sock.literal = label resp, data = sock.uid('SEARCH', 'CHARSET', 'UTF-8', 'X-GM-LABELS') assert resp == 'OK' for uid in data[0].split(): # because we do select, this uid will be valid. yield uid
def test_printableSingletons(self): """ The IMAP4 modified UTF-7 implementation encodes all printable characters which are in ASCII using the corresponding ASCII byte. """ # All printables represent themselves for o in range(0x20, 0x26) + range(0x27, 0x7f): self.failUnlessEqual(chr(o), encode(chr(o))) self.failUnlessEqual(chr(o), decode(chr(o))) self.failUnlessEqual(encode('&'), '&-') self.failUnlessEqual(decode('&-'), '&')
def test_printable_singletons(self): """ The IMAP4 modified UTF-7 implementation encodes all printable characters which are in ASCII using the corresponding ASCII byte. """ # All printables represent themselves for o in list(range(0x20, 0x26)) + list(range(0x27, 0x7f)): self.assertEqual(int2byte(o), encode(unichr(o))) self.assertEqual(unichr(o), decode(int2byte(o))) self.assertEqual(encode('&'), b'&-') self.assertEqual(encode('&'), b'&-') self.assertEqual(decode(b'&-'), '&')
def test_illegal_chars(self): not_valid_as_str = [ 'blah' + chr(0x80) + 'sne', chr(0xaa) + 'foo', 'blah' + chr(0xff) + 'sne'] for name in not_valid_as_str: self.assertRaises(FolderNameError, encode, name) unicode_names = [unicode(name, 'latin-1') for name in not_valid_as_str] for name in unicode_names: assert isinstance(encode(name), str)
def subdir(self, folder): """ return a subdir """ if folder in self.subdirs: return self.subdirs[folder] if folder: parts = folder.split(GMVaultExporter.GM_SEP) parent = GMVaultExporter.GM_SEP.join(parts[:-1]) self.subdir(parent) path = self.subdir_name(folder) path = imap_utf7.encode(path) else: if not self.root_is_maildir(): return path = '' abspath = os.path.join(self.path, path) sub = mailbox.Maildir(abspath, create = True) self.subdirs[folder] = sub return sub
def test_encode(self): for (input, output) in self.tests: encoded = encode(input) self.assertIsInstance(encoded, binary_type) self.assertEqual(encoded, output)
def get_message_list(self, item): mailbox = imap_utf7.encode(item.text()) self.statusbar.showMessage("Selecting mailbox...") typ, num_msg = self.imap_connection.select(mailbox) if typ != 'OK': self.statusbar.showMessage("Selecting mailbox was failed.") return num_msg = int(num_msg[0]) self.statusbar.showMessage("Searching in mailbox...") typ, msg_ids = self.imap_connection.search(None, 'ALL') if typ != 'OK': self.statusbar.showMessage("Searching in mailbox was failed.") return if msg_ids[0] == b'': self.statusbar.showMessage("Mailbox is empty") return # Get message id's list as newest is first msg_ids = msg_ids[0].decode().split() num_get_msg = min(len(msg_ids), self.message_per_page) self.statusbar.showMessage("Fetching messages...") typ, tmp_data_set = self.imap_connection.fetch( msg_ids[-1] + ":" + msg_ids[-num_get_msg], "(UID RFC822.HEADER)") if typ != 'OK': self.statusbar.showMessage("Getting messages failed.") return raw_msg_data_list = [] for i in range(num_get_msg): raw_msg_data_list.append(tmp_data_set[i * 2]) self.statusbar.showMessage("Finished fetching messages!") msg_list = [] # parse to get uid regex = re.compile('UID\ [0-9]+') for i in range(num_get_msg): tmp = raw_msg_data_list[i][0].decode( ) # '17502 (UID 17505 RFC822.HEADER {6198}' tmp = regex.search(tmp).group() # 'UID 17505' uid = int(tmp.split()[1]) # '17505' raw_msg_contents = raw_msg_data_list[i][1] msg_list.append({ "uid": uid, "contents": email.message_from_string(raw_msg_contents.decode()) }) mail_property = {} self.messages.setRowCount(self.message_per_page) for idx, msg in enumerate(msg_list): mail_property["from"] = decode_header(msg["contents"], 'From') mail_property["subject"] = decode_header(msg["contents"], 'Subject') self.messages.setItem(idx, 0, QTableWidgetItem(str(msg["uid"]))) self.messages.setItem(idx, 1, QTableWidgetItem(mail_property["from"])) self.messages.setItem(idx, 2, QTableWidgetItem(mail_property["subject"])) self.messages.setHorizontalHeaderLabels(["uid", "from", "subject"])
def u_to_utf7(text): """Convert to UTF-7""" return imap_utf7.encode(text)
def str_to_utf7(text): """Convert string to UTF-7""" return imap_utf7.encode(text)
def test_encode(self): for (input, output) in self.tests: encoded = encode(input) self.assertIsInstance(encoded, text_type) self.assertEqual(encoded, output)
def __call__(self, parser, namespace, values, option_string=None): setattr(namespace, self.dest, imap_utf7.encode(values))
def test_encode(self): for (input, output) in self.tests: self.assertEquals(encode(input), output)
def _normalise_folder(folder_name): if isinstance(folder_name, binary_type): folder_name = folder_name.decode('ascii') folder_name = imap_utf7.encode(folder_name) return _quote(folder_name)