Last active 1450484348

xMail Python implementation

xmailv1.py Raw
1# xMail implementation v1 by blha303
2# MIT license
3# irc.esper.net #drtshock for questions
4import json, urllib2, urllib
5url = "http://xmail.turt2live.com/mail/index.php"
6from random import randrange
7
8def get_password(password):
9 return urllib2.urlopen("http://blha303.com.au/sha1.php?q=%s" % password).read()
10# An alternative for this, on machines with PHP installed, is:
11# return os.popen('echo "<?php echo sha1(\'%s\'); ?>" | php' % password).read()
12# That's all the above PHP file does.
13
14def get_json(url, data):
15 return json.loads(urllib2.urlopen(url, data).read())
16
17# xMail functions start here
18
19def register(username, password):
20 """username: new xmail username; password: yep"""
21 registerdata = urllib.urlencode({'mode': 'REGISTER', 'username': username, 'password': get_password(password), 'version': 'IRC'})
22 register = get_json(url, registerdata)
23 return register
24# Returns:
25# {u'status': u'OK', u'username': u'USERNAME REMOVED', u'loggedin': True, u'apikey': u'API KEY REMOVED', u'lastlogin': 1373054388, u'date': 1373054388, u'message': u'User registered'}
26
27def login(username, password):
28 """Initial login"""
29 logindata = urllib.urlencode({'mode': 'LOGIN', 'username': username, 'password': get_password(password), 'version': 'IRC'})
30 login = get_json(url, logindata)
31 return login
32# Returns:
33# {u'status': u'OK', u'username': u'USERNAME REMOVED', u'loggedin': True, u'apikey': u'API KEY REMOVED', u'lastlogin': u'1373009275', u'date': 1373009330, u'message': u'Logged in'}
34
35def check_login(username):
36 """Renews login, retrieves updated api key"""
37 checkdata = urllib.urlencode({'mode': 'CHECK_LOGIN', 'username': username, 'version': 'IRC'})
38 check = get_json(url, checkdata)
39 return check
40# Returns same as login(), used for verifying login status and retrieving updated API key
41
42def send(fromuser, to, message):
43 """fromuser: xmail username of sending user; to: xmail username of receiving user; message: yep"""
44 apikey = check_login(username)['apikey']
45 pid = randrange(1, 1001)
46 uid = randrange(1, 1001)
47 ident = "S"
48 version = "IRC"
49 senddata = urllib.urlencode({'mode': 'SEND', 'pid': pid, 'uid': uid, 'ident': ident, 'to': to, 'from': fromuser, 'message': message, 'apikey': apikey, 'version': version})
50 send = get_json(url, senddata)
51 return send
52# Returns:
53# {u'status': u'OK', u'message': u'Message sent!'}
54
55def inbox(username):
56 """Returns list of inbox entries. See below example"""
57 apikey = check_login(username)['apikey']
58 version = "IRC"
59 inboxdata = urllib.urlencode({'mode': 'INBOX', 'username': username, 'apikey': apikey, 'version': version})
60 # The one special case
61 inbox = urllib2.urlopen(url, inboxdata).read().split("\n")
62 inboxa = []
63 for i in inbox:
64 inboxa.append(i)
65 return inboxa
66# Returns list. If no unread messages, list length will be 1. Otherwise, length is number of unread items - 1
67
68def sent(username):
69 """Returns list of sent entries. See below example"""
70 apikey = check_login(username)['apikey']
71 version = "IRC"
72 inboxdata = urllib.urlencode({'mode': 'SENT', 'username': username, 'apikey': apikey, 'version': version})
73 # The one special case
74 inbox = urllib2.urlopen(url, inboxdata).read().split("\n")
75 inboxa = []
76 for i in inbox:
77 inboxa.append(i)
78 return inboxa
79# Returns list. If no unread messages, list length will be 1. Otherwise, length is number of unread items - 1
80
81def read(username):
82 """Returns list of read entries. See below example"""
83 apikey = check_login(username)['apikey']
84 version = "IRC"
85 inboxdata = urllib.urlencode({'mode': 'READ', 'username': username, 'apikey': apikey, 'version': version})
86 # The one special case
87 inbox = urllib2.urlopen(url, inboxdata).read().split("\n")
88 inboxa = []
89 for i in inbox:
90 inboxa.append(i)
91 return inboxa
92# Returns list. If no unread messages, list length will be 1. Otherwise, length is number of unread items - 1
93
94def exampleinboxusage(username):
95 inboxls = inbox(username)
96 if len(inboxls) > 1:
97 for i in inboxls:
98 i = json.loads(i)
99 try:
100 status = i['status']
101 unread = i['unread']
102 print "%s unread messages for %s:" % (unread, i['username'])
103 except KeyError:
104 id = i['id']
105 to = i['to']
106 fromuser = i['from']
107 message = i['message']
108 complex = i['complex']
109 attachments = i['attachments']
110 sentfrom = i['sentfrom']
111 sendtime = i['sendTime']
112 print "%s: %s - to %s from %s at %s (sent from %s)" % (id, message, to, fromuser, sendtime, sentfrom)
113 else:
114 for i in inboxls:
115 i = json.loads(i)
116 unread = i['unread']
117 print "%s unread messages for %s." % (unread, i['username'])
118
119def mark(username, id, read):
120 """username: xmail username; id: inbox[n]['id']; read: Boolean. true = read, false = unread"""
121 apikey = check_login(username)['apikey']
122 markdata = urllib.urlencode({'mode': 'MARK', 'pid': id, 'uid': randrange(1, 1001), 'username': username, 'apikey': apikey, 'read': read, 'version': 'IRC'})
123 mark = get_json(url, markdata)
124 return mark
125# Returns:
126# {u'status': u'OK', u'message': u'Updated Message'}
127
128def info():
129 """Returns server info"""
130 infodata = urllib.urlencode({'mode': 'INFO', 'version': 'IRC'})
131 info = get_json(url, infodata)
132 return info
133# Returns:
134# {u'status': u'OK', u'ip': u'IP address', u'version': u'XMAIL-1.1.1-OFFICIAL_SERVER', u'posturl': u'http://xmail.turt2live.com/mail', u'econaccount': u'@economy', u'message': u'xMail PHP Server', u'now': 1373012096}