xmailv1.py
· 5.4 KiB · Python
Raw
# xMail implementation v1 by blha303
# MIT license
# irc.esper.net #drtshock for questions
import json, urllib2, urllib
url = "http://xmail.turt2live.com/mail/index.php"
from random import randrange
def get_password(password):
return urllib2.urlopen("http://blha303.com.au/sha1.php?q=%s" % password).read()
# An alternative for this, on machines with PHP installed, is:
# return os.popen('echo "<?php echo sha1(\'%s\'); ?>" | php' % password).read()
# That's all the above PHP file does.
def get_json(url, data):
return json.loads(urllib2.urlopen(url, data).read())
# xMail functions start here
def register(username, password):
"""username: new xmail username; password: yep"""
registerdata = urllib.urlencode({'mode': 'REGISTER', 'username': username, 'password': get_password(password), 'version': 'IRC'})
register = get_json(url, registerdata)
return register
# Returns:
# {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'}
def login(username, password):
"""Initial login"""
logindata = urllib.urlencode({'mode': 'LOGIN', 'username': username, 'password': get_password(password), 'version': 'IRC'})
login = get_json(url, logindata)
return login
# Returns:
# {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'}
def check_login(username):
"""Renews login, retrieves updated api key"""
checkdata = urllib.urlencode({'mode': 'CHECK_LOGIN', 'username': username, 'version': 'IRC'})
check = get_json(url, checkdata)
return check
# Returns same as login(), used for verifying login status and retrieving updated API key
def send(fromuser, to, message):
"""fromuser: xmail username of sending user; to: xmail username of receiving user; message: yep"""
apikey = check_login(username)['apikey']
pid = randrange(1, 1001)
uid = randrange(1, 1001)
ident = "S"
version = "IRC"
senddata = urllib.urlencode({'mode': 'SEND', 'pid': pid, 'uid': uid, 'ident': ident, 'to': to, 'from': fromuser, 'message': message, 'apikey': apikey, 'version': version})
send = get_json(url, senddata)
return send
# Returns:
# {u'status': u'OK', u'message': u'Message sent!'}
def inbox(username):
"""Returns list of inbox entries. See below example"""
apikey = check_login(username)['apikey']
version = "IRC"
inboxdata = urllib.urlencode({'mode': 'INBOX', 'username': username, 'apikey': apikey, 'version': version})
# The one special case
inbox = urllib2.urlopen(url, inboxdata).read().split("\n")
inboxa = []
for i in inbox:
inboxa.append(i)
return inboxa
# Returns list. If no unread messages, list length will be 1. Otherwise, length is number of unread items - 1
def sent(username):
"""Returns list of sent entries. See below example"""
apikey = check_login(username)['apikey']
version = "IRC"
inboxdata = urllib.urlencode({'mode': 'SENT', 'username': username, 'apikey': apikey, 'version': version})
# The one special case
inbox = urllib2.urlopen(url, inboxdata).read().split("\n")
inboxa = []
for i in inbox:
inboxa.append(i)
return inboxa
# Returns list. If no unread messages, list length will be 1. Otherwise, length is number of unread items - 1
def read(username):
"""Returns list of read entries. See below example"""
apikey = check_login(username)['apikey']
version = "IRC"
inboxdata = urllib.urlencode({'mode': 'READ', 'username': username, 'apikey': apikey, 'version': version})
# The one special case
inbox = urllib2.urlopen(url, inboxdata).read().split("\n")
inboxa = []
for i in inbox:
inboxa.append(i)
return inboxa
# Returns list. If no unread messages, list length will be 1. Otherwise, length is number of unread items - 1
def exampleinboxusage(username):
inboxls = inbox(username)
if len(inboxls) > 1:
for i in inboxls:
i = json.loads(i)
try:
status = i['status']
unread = i['unread']
print "%s unread messages for %s:" % (unread, i['username'])
except KeyError:
id = i['id']
to = i['to']
fromuser = i['from']
message = i['message']
complex = i['complex']
attachments = i['attachments']
sentfrom = i['sentfrom']
sendtime = i['sendTime']
print "%s: %s - to %s from %s at %s (sent from %s)" % (id, message, to, fromuser, sendtime, sentfrom)
else:
for i in inboxls:
i = json.loads(i)
unread = i['unread']
print "%s unread messages for %s." % (unread, i['username'])
def mark(username, id, read):
"""username: xmail username; id: inbox[n]['id']; read: Boolean. true = read, false = unread"""
apikey = check_login(username)['apikey']
markdata = urllib.urlencode({'mode': 'MARK', 'pid': id, 'uid': randrange(1, 1001), 'username': username, 'apikey': apikey, 'read': read, 'version': 'IRC'})
mark = get_json(url, markdata)
return mark
# Returns:
# {u'status': u'OK', u'message': u'Updated Message'}
def info():
"""Returns server info"""
infodata = urllib.urlencode({'mode': 'INFO', 'version': 'IRC'})
info = get_json(url, infodata)
return info
# Returns:
# {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}
| 1 | # xMail implementation v1 by blha303 |
| 2 | # MIT license |
| 3 | # irc.esper.net #drtshock for questions |
| 4 | import json, urllib2, urllib |
| 5 | url = "http://xmail.turt2live.com/mail/index.php" |
| 6 | from random import randrange |
| 7 | |
| 8 | def 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 | |
| 14 | def get_json(url, data): |
| 15 | return json.loads(urllib2.urlopen(url, data).read()) |
| 16 | |
| 17 | # xMail functions start here |
| 18 | |
| 19 | def 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 | |
| 27 | def 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 | |
| 35 | def 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 | |
| 42 | def 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 | |
| 55 | def 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 | |
| 68 | def 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 | |
| 81 | def 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 | |
| 94 | def 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 | |
| 119 | def 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 | |
| 128 | def 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} |