-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbosp.py
More file actions
executable file
·247 lines (216 loc) · 6.27 KB
/
bosp.py
File metadata and controls
executable file
·247 lines (216 loc) · 6.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#! /usr/bin/python3
import bottle
import pymongo
from bson import ObjectId
from time import time,ctime
import re
from hashlib import sha1
from random import random
dbinfo={
'host': 'localhost',
'port': 27017,
'database': 'bosp',
'dbuname': '',
'dbpasswd': '',
'rcpt_coll': 'rcpts',
'user_coll': 'users',
}
email_pattern= re.compile(r'^\S{1,128}@(\b\w+\b.)+\b\w{2,4}\b\.?$')
class FormDataError(Exception):
"""docstring for FormDataError"""
def __init__(self, key, estring=''):
self.key= key
super(FormDataError, self).__init__(key +' ' + estring)
class InvalidUser(Exception):
"""docstring for InvalidUser"""
def __init__(self, user, estring):
self.user = user
super(InvalidUser, self).__init__(user+' '+estring)
def valid_email(addr):
return True if email_pattern.match(addr) else False
def readForm():
original=bottle.request.forms.decode()
original=dict(original, **bottle.request.cookies.decode())
finald={}
tmp=[]
for key in original:
if key.endswith('_xs'):
finald[key]= original.getall(key)
else:
finald[key]= original[key]
return finald
def checkForm(argd, keyset):
retd={}
try:
for key in keyset:
try:
retd[key]= argd[key]
except KeyError:
raise FormDataError(key,' is absent.')
try:
assert retd[key] or retd[key] =='' or retd[key]==[] #<==== Maybe use Regular Expression later!
except AssertionError:
raise FormDataError(key,' is invalid.')
return retd
except FormDataError as e:
local={
'type':'danger',
'name': 'dataerr',
'title': 'Form Data Error !',
'content': '<strong>%s</strong>' % e ,
}
raise bottle.HTTPError(406,msg= bottle.template('alert.tpl',**local))
class BOSPAdmin(object):
"""docstring for BOSPAdmin"""
def __init__(self, dbinfo=dbinfo):
super(BOSPAdmin, self).__init__()
self.dbinfo = dbinfo
self.resources= {}
self._init_db()
def get_mongo_conn(self):
return self.resources['conn']
def get_mongo_db(self):
return self.resources['db']
def _init_db(self):
self.resources['conn']= pymongo.MongoClient(self.dbinfo['host'],self.dbinfo['port'])
self.resources['db']= self.resources['conn'][self.dbinfo['database']]
self.resources['users']= self.resources['db'][self.dbinfo['user_coll']]
self.resources['rcpt']= self.resources['db'][self.dbinfo['rcpt_coll']]
def ExM_add_rcpt(self, argd):
argd= checkForm(argd,{'from','to','_id'})
if valid_email(argd['from']) and valid_email(argd['to']):
self.resources['rcpt'].remove({'bospUser': argd['_id']})
self.resources['rcpt'].insert({'_id':argd['from'],'to':argd['to'],'bospUser': argd['_id']})
else:
raise ValueError('Invalid Email Address.')
def ExM_get_rcpt(self, uid):
return [x for x in self.resources['rcpt'].find({'bospUser':uid})]
def add_user(self, argd):
argd= checkForm(argd,{'passwd','_id'})
argd['since']= time()
argd['session']= ''
self.resources['users'].insert(argd)
return argd
def get_user(self, argd, session= True):
if session:
argd= checkForm(argd,{'session',})
return self.resources['users'].find_one(argd)
else:
argd= checkForm(argd,{'_id','passwd','salt'})
usr= self.resources['users'].find_one({'_id':argd['_id']})
return usr if sha1((usr['passwd']+str(argd['salt'])).encode()).hexdigest() == argd['passwd'] else None
def start_session(self, uid):
session= sha1(str(random()).encode('ascii')).hexdigest()
self.resources['users'].update({'_id':uid},{'$set':{'session':session}})
return session
bosp= BOSPAdmin()
@bottle.error(406)
@bottle.error(404)
@bottle.error(403)
@bottle.error(401)
def showError(err):
msg= '400 Failure!'
try:
msg=err['msg']
except KeyError:
pass
return msg
@bottle.get('/')
def welcome():
usr= None
try:
usr= bosp.get_user(readForm())
except bottle.HTTPError:
pass
local= {
'user': usr,
}
return bottle.template('welcome.tpl', data= local)
@bottle.get('/signup')
def signupForm():
usr= None
try:
usr= bosp.get_user(readForm())
except bottle.HTTPError:
pass
local= {
'user': usr,
}
return bottle.template('signup.tpl', data= local)
@bottle.post('/signup')
def signupAction():
try:
usr= bosp.add_user(readForm())
except pymongo.errors.DuplicateKeyError:
local={
'type':'danger',
'name': 'dataerr',
'title': '',
'content': '<strong>Duplicated Email address, use another one.</strong>' ,
}
raise bottle.HTTPError(406,msg= bottle.template('alert.tpl',**local))
else:
bottle.response.set_cookie('session',bosp.start_session(usr['_id']))
return '<script type="text/javascript">window.location="/";</script>'
@bottle.post('/signin')
def signinAction():
try:
usr= bosp.get_user(readForm(),session= False)
bottle.response.set_cookie('session',bosp.start_session(usr['_id']))
except (KeyError, FormDataError, TypeError):
local={
'type':'danger',
'name': 'dataerr',
'title': '',
'content': '<strong>Invalid email/password.</strong>' ,
}
raise bottle.HTTPError(406,msg= bottle.template('alert.tpl',**local))
return '<script type="text/javascript">window.location="/";</script>'
@bottle.get('/signout')
def signoutAction():
usr= bosp.get_user(readForm())
if usr:
bosp.start_session(usr['_id'])
else:
pass
bottle.response.delete_cookie('session')
bottle.redirect('/')
@bottle.get('/ExM/')
def ExMForm():
usr= bosp.get_user(readForm())
rcpt= bosp.ExM_get_rcpt(usr['_id'])
local= {
'user': usr,
'ExM': rcpt[0] if rcpt else rcpt
}
return bottle.template('ExM.tpl', data= local)
@bottle.post('/ExM/set')
def ExMForm():
argd=readForm()
usr= bosp.get_user(argd)
try:
bosp.ExM_add_rcpt(dict(argd,_id=usr['_id']))
except pymongo.errors.DuplicateKeyError:
local={
'type':'danger',
'name': 'dataerr',
'title': 'This Address Is Taken',
'content': '<strong>%s</strong> already registered by another user. Please come up with another one.' % argd['from'],
}
raise bottle.HTTPError(406,msg= bottle.template('alert.tpl', **local))
else:
local={
'type':'success',
'name': 'success',
'title': 'All set',
'content': '<strong>%s</strong> ==> <strong>%s</strong>.' % (argd['from'], argd['to']) ,
}
return bottle.template('alert.tpl', **local)
@bottle.route('/static/<filepath:path>')
def statics(filepath):
return bottle.static_file(filepath, root='./static/')
if __name__ == '__main__':
bottle.debug(True)
bottle.run(host='0.0.0.0', port=8080)
else:
application=bottle.app()