from imbox.imap import ImapTransport
from imbox.parser import parse_email
from imbox.query import build_search_query

class Imbox(object):

	def __init__(self, hostname, username=None, password=None, ssl=True):

		server = ImapTransport(hostname, ssl=ssl)
		self.connection = server.connect(username, password)

	def fetch_by_uid(self, uid):
		message, data = self.connection.uid('fetch', uid, '(BODY.PEEK[])') # Don't mark the messages as read, save bandwidth with PEEK
		raw_email = data[0][1]

		email_object = parse_email(raw_email)

		return email_object

	def fetch_list(self, data):
		uid_list = data[0].split()

		for uid in uid_list:
			yield self.fetch_by_uid(uid)

	def messages(self, *args, **kwargs):

		# Check for folder argument
		folder = kwargs.get('folder', False)
		
		if folder:
			self.connection.select(folder)

		query = build_search_query(**kwargs)

		message, data = self.connection.uid('search', None, query)


		return self.fetch_list(data)
		

@@ -133,17 +133,20 @@
 ject):%0A%0A
-%09
+    
 def __in
@@ -208,18 +208,29 @@
 True):%0A%0A
-%09%09
+        self.
 server =
@@ -263,18 +263,300 @@
 sl=ssl)%0A
-%09%09
+        self.username = username%0A        self.password = password%0A%0A    def __enter__(self):%0A        self.connect()%0A%0A    def __exit__(self, type, value, traceback):%0A        self.logout()%0A%0A    def connect(self):%0A        username = self.username%0A        password = self.password%0A        
 self.con
@@ -565,16 +565,21 @@
 ction = 
+self.
 server.c
@@ -602,25 +602,265 @@
  password)%0A%0A
-%09
+    def logout(self):%0A        self.connection.logout()%0A%0A    def query_uids(self, **kwargs):%0A        query = build_search_query(**kwargs)%0A%0A        message, data = self.connection.uid('search', None, query)%0A        return data%5B0%5D.split()%0A%0A    
 def fetch_by
@@ -876,18 +876,24 @@
 , uid):%0A
-%09%09
+        
 message,
@@ -1011,18 +1011,24 @@
 th PEEK%0A
-%09%09
+        
 raw_emai
@@ -1043,18 +1043,24 @@
 %5B0%5D%5B1%5D%0A%0A
-%09%09
+        
 email_ob
@@ -1090,18 +1090,24 @@
 email)%0A%0A
-%09%09
+        
 return e
@@ -1119,17 +1119,20 @@
 object%0A%0A
-%09
+    
 def fetc
@@ -1148,76 +1148,117 @@
 lf, 
-data):%0A%09%09uid_list = data%5B0%5D.split()%0A%0A%09%09for uid in uid_list:%0A%09%09%09yield
+**kwargs):%0A        uid_list = self.query_uids(**kwargs)%0A%0A        for uid in uid_list:%0A            yield (uid,
  sel
@@ -1280,137 +1280,305 @@
 uid)
+)
 %0A%0A
-%09
+    
 def m
-essages(self, *args, **kwargs):%0A%0A%09%09# Check for folder argument%0A%09%09folder = kwargs.get('folder', False)%0A%09%09%0A%09%09if 
+ark_seen(self, uid):%0A        self.connection.uid('STORE', uid, '+FLAGS', '%5C%5CSeen')%0A%0A    def delete(self, uid):%0A        mov, data = self.connection.uid('STORE', uid, '+FLAGS', '(%5C%5CDeleted)')%0A        self.connection.expunge()%0A%0A    def copy(self, uid, destination_
 folder
+)
 :%0A
-%09%09%09
+        return 
 self
@@ -1593,129 +1593,334 @@
 ion.
-select(folder)%0A%0A%09%09query = build_search_query(**kwargs)%0A%0A%09%09message, data = self.connection.uid('search', None, query)%0A%0A%0A%09%09
+uid('COPY', uid, destination_folder)%0A%0A    def move(self, uid, destination_folder):%0A        if self.copy(uid, destination_folder):%0A            self.delete(uid)%0A%0A    def messages(self, *args, **kwargs):%0A        folder = kwargs.get('folder', False)%0A        %0A        if folder:%0A            self.connection.select(folder)%0A%0A        
 retu
@@ -1942,13 +1942,23 @@
 ist(
-data)%0A%09%09
+**kwargs)%0A        
 %0A

Add some convenient operation