Skip to content
Browse files

Fixed error handling

  • Loading branch information...
1 parent 554f46d commit e2aad98ffa6a5cc4fde0cf8c7ee9e85ac58a65ae @aallai committed
Showing with 105 additions and 41 deletions.
  1. +70 −8 smail.py
  2. +35 −33 users.db
View
78 smail.py
@@ -4,7 +4,7 @@
import pickle
import os
from config import config
-from core import *
+import core
users = None
@@ -13,16 +13,15 @@
def main(argv) :
setup()
-
- while True :
- menu = '''
+
+ menu = '''
Welcome!
1 Retreive messages
2 Send a message
3 Exit
-
'''
+ while True :
try :
action = int(raw_input(menu))
@@ -30,14 +29,77 @@ def main(argv) :
if action == 1 :
retreive_messages()
-
+ elif action == 2 :
+ send()
+ elif action == 3 :
+ sys.exit(1)
+ else :
+ bogus_input()
except ValueError :
bogus_input()
def bogus_input() :
- sys.err.write('\nEnter a valid choice from the menu.\n')
+ sys.stderr.write('\nEnter a choice from the menu.\n')
+
+
+def send() :
+
+ menu = '''
+1 Specify a plaintext file
+2 Type into prompt
+3 Return to menu
+'''
+
+ try :
+ action = int(raw_input(menu))
+ print
+
+ if action == 1 :
+ f = raw_input('file : ')
+
+ try :
+ data = open(f.strip(), 'r').read()
+ get_dest_and_send(data)
+ except IOError :
+ print "Can't open file."
+ send()
+
+ elif action == 2 :
+ print 'Enter EOF when done.'
+
+ data = ''
+ line = raw_input()
+
+ while line.strip() != 'EOF' :
+ data += line + '\n'
+ line = raw_input()
+
+ get_dest_and_send(data)
+
+ elif action == 3 :
+ return
+
+ else :
+ raise ValueError
+
+ except ValueError :
+ bogus_input()
+ send()
+
+def get_dest_and_send(data) :
+
+ try :
+
+ dest = raw_input('Enter the user you wish to send to : ')
+ core.send(users.local_user, users.remote_users[dest.strip()], data)
+
+ print 'Message sent'
+
+ except KeyError, e :
+ print e.message + ' is not in your list of users'
+ get_dest_and_send(data)
# check for config files and inbox folder, do some sanity checks
def setup() :
@@ -73,7 +135,7 @@ def retreive_messages() :
print 'Retreiving messages...'
- d = receive(users.local_user)
+ d = core.receive(users.local_user)
# only display new messages
new = { mid : data for mid, data in d.items() if mid not in os.listdir(config['inbox']) }
View
68 users.db
@@ -12,69 +12,71 @@ Rp4
(dp5
S'remote_users'
p6
-(lp7
+(dp7
+S'alex'
+p8
g0
(cusers
User
-p8
+p9
g2
-Ntp9
-Rp10
-(dp11
+Ntp10
+Rp11
+(dp12
S'mlist'
-p12
-(lp13
+p13
+(lp14
g0
(cmediums
EmailMedium
-p14
+p15
g2
-Ntp15
-Rp16
-(dp17
+Ntp16
+Rp17
+(dp18
S'mailbox_server'
-p18
+p19
g0
(cmediums
ImapServer
-p19
+p20
g2
-Ntp20
-Rp21
-(dp22
+Ntp21
+Rp22
+(dp23
S'passwd'
-p23
-S'assignment3'
p24
-sS'use_ssl'
+S'assignment3'
p25
+sS'use_ssl'
+p26
I01
sS'host'
-p26
-S'imap.gmail.com'
p27
-sS'port'
+S'imap.gmail.com'
p28
+sS'port'
+p29
I993
sS'user'
-p29
-S'comp535test@gmail.com'
p30
-sbsS'mtu'
+S'comp535test@gmail.com'
p31
+sbsS'mtu'
+p32
I1024
sS'address'
-p32
-S'comp535test@gmail.com'
p33
-sbasS'name'
+S'comp535test@gmail.com'
p34
-S'alex'
+sbasS'name'
p35
-sS'key'
+S'alex'
p36
-g35
-sbasS'local_user'
+sS'key'
p37
-g10
+g36
+sbssS'local_user'
+p38
+g11
sb.

0 comments on commit e2aad98

Please sign in to comment.
Something went wrong with that request. Please try again.