-
Notifications
You must be signed in to change notification settings - Fork 1
/
wsgi.py
320 lines (286 loc) · 12.3 KB
/
wsgi.py
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# coding: utf-8
import datetime
import gevent.monkey; gevent.monkey.patch_all()
import gevent.pywsgi
import os
import re
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import cgi
import Cookie
import jinja2
import urllib2
import pymongo
PATH = os.path.dirname(os.path.realpath(__file__))
sys.path.append(PATH)
import config
import bot
con = pymongo.Connection()
db = con[config.SIRC_DB]
if config.SIRC_DB_USE_AUTH:
db.authenticate(config.SIRC_DB_USER, config.SIRC_DB_PASS)
def application(environ, start_response):
http_host = environ.get('HTTP_HOST', '')
url_scheme = environ.get('wsgi.url_scheme', '')
path = environ.get('PATH_INFO', '')
if not config.SIRC_URL.startswith('%s://%s' % (url_scheme, http_host)):
return redirect(start_response, path)
cookie = Cookie.SimpleCookie()
cookie.load(environ.get('HTTP_COOKIE', ''))
oauth_config = None
if config.OAUTH_PROVIDER in cookie:
oauth_provider = cookie[config.OAUTH_PROVIDER].value
if oauth_provider in config.OAUTH:
oauth_config = config.OAUTH[oauth_provider]
if path.startswith('/callback/'):
if oauth_config is None:
return error(start_response, message = 'callback')
return callback(environ, start_response, oauth_config, oauth_provider)
authorized = False
session = None
if config.SESSION_ID in cookie:
sessions = db.session.find({'session_id': cookie[config.SESSION_ID].value})
if sessions.count() > 0:
session = sessions[0]
if session is None:
referer = environ.get('HTTP_REFERER', '')
if oauth_config is None or referer != config.SIRC_URL:
return preauth(environ, start_response)
else:
return auth(environ, start_response, oauth_config)
db.session.update({
'session_id': session['session_id']
}, {
'$set': {'datetime': datetime.datetime.now()}
})
parameters = cgi.parse_qs(environ.get('QUERY_STRING', ''))
if path.startswith('/update/'):
return update(environ, start_response, session, parameters)
elif path.startswith('/downdate/'):
return downdate(environ, start_response, session, parameters)
elif path.startswith('/send/'):
return send(environ, start_response, session, parameters)
elif path.startswith('/delete/'):
return delete(environ, start_response, session, parameters)
elif path == '/':
return default(environ, start_response, session, parameters)
else:
return static(environ, start_response)
def preauth(environ, start_response):
context = {}
start_response('200 OK', [
('Content-Type', 'text/html; charset=utf-8')
])
context['OAUTH_PROVIDER'] = config.OAUTH_PROVIDER
context['oauth'] = config.OAUTH.keys()
return [render('preauth.html', context)]
def auth(environ, start_response, oauth_config):
(url, secret) = request_request_token(oauth_config)
start_response('200 OK', [
('Refresh', '0; url=%s' % url),
('Content-Type', 'text/html; charset=utf-8'),
('Set-Cookie', '%s=%s; path=/' % (config.TOKEN_SECRET, secret))
])
return ['<a href="%s">Login Required</a>' % url]
def callback(environ, start_response, oauth_config, oauth_provider):
parameters = cgi.parse_qs(environ.get('QUERY_STRING', ''))
if 'oauth_token' in parameters and \
'oauth_verifier' in parameters:
oauth_token = parameters['oauth_token'][0]
oauth_verifier = parameters['oauth_verifier'][0]
else:
return error(start_response, message = 'oauth')
cookie = Cookie.SimpleCookie()
cookie.load(environ.get('HTTP_COOKIE', ''))
if config.TOKEN_SECRET in cookie:
oauth_token_secret = cookie[config.TOKEN_SECRET].value
else:
return error(start_response, message = 'secret')
parameters = cgi.parse_qs(request_access_token(oauth_token, oauth_token_secret, oauth_verifier, oauth_config))
if oauth_provider == 'snucse' and 'account' in parameters:
account = parameters['account'][0]
elif oauth_provider == 'twitter' and 'screen_name' in parameters:
account = parameters['screen_name'][0]
else:
return error(start_response, message = 'account')
session_id = create_session_id()
shown_account = '%s%s' % (config.OAUTH[oauth_provider]['PREFIX'], account)
data = {
'session_id': session_id,
'account': shown_account,
'datetime': datetime.datetime.now()
}
db.session.insert(data)
start_response('200 OK', [
('Refresh', '0; url=/'),
('Content-Type', 'text/html; charset=utf-8'),
('Set-Cookie', '%s=deleted; path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT' % (config.TOKEN_SECRET)),
('Set-Cookie', '%s=%s; path=/' % (config.SESSION_ID, session_id))
])
return ['<a href="/">%s Go</a>' % account]
def update(environ, start_response, session, parameters):
context = {}
if 'channel' not in parameters:
return error(start_response, message = 'no channel')
last_update = datetime.datetime.now()
if 'last_update' in parameters:
last_update = parse_datetime(parameters['last_update'][0].decode('utf-8'))
if 'transition_id' not in parameters:
return error(start_response, message = 'no transition_id')
channel = parameters['channel'][0].lower()#.decode('utf-8').lower()
#channel is percent-encoded now
channel_encoded = urllib2.quote(channel)
transition_id = parameters['transition_id'][0].decode('utf-8')
if db[session['account']].find({'channel': channel}).count() == 0:
db[session['account']].insert({'channel': channel})
context['channel'] = channel
context['transition_id'] = transition_id
logs = []
for i in xrange(30):
logs = list(db[channel_encoded].find({
'datetime': {"$gt": last_update},
}, sort = [
('datetime', pymongo.ASCENDING)
]))
if len(logs) == 0:
gevent.sleep(1)
else:
if len(logs) > config.N_LINES:
logs = []
context['status'] = 'flooded'
break
for log in logs:
log['source'] = remove_invalid_utf8_char(log['source'])
log['message'] = remove_invalid_utf8_char(log['message'])
context['logs'] = logs
start_response('200 OK', [('Content-Type', 'text/xml; charset=utf-8')])
return [render('result.xml', context)]
def downdate(environ, start_response, session, parameters):
context = {}
if 'channel' not in parameters:
return error(start_response, message = 'no channel')
last_downdate = datetime.datetime.now()
if 'last_downdate' in parameters:
last_downdate = parse_datetime(parameters['last_downdate'][0].decode('utf-8'))
if 'transition_id' not in parameters:
return error(start_response, message = 'no transition_id')
channel = parameters['channel'][0].lower()#.decode('utf-8').lower()
channel_encoded = urllib2.quote(channel) #channel name is percent-encoded
transition_id = parameters['transition_id'][0].decode('utf-8')
context['channel'] = channel
context['transition_id'] = transition_id
logs = db[channel_encoded].find({
'datetime': {"$lt": last_downdate},
}, limit = config.N_LINES, sort = [
('datetime', pymongo.DESCENDING)
])
logs = list(logs)
for log in logs:
log['source'] = remove_invalid_utf8_char(log['source'])
log['message'] = remove_invalid_utf8_char(log['message'])
context['logs'] = logs
start_response('200 OK', [('Content-Type', 'text/xml; charset=utf-8')])
return [render('result.xml', context)]
def send(environ, start_response, session, parameters):
context = {}
if 'channel' not in parameters:
return error(start_response, message = 'no channel')
if 'message' not in parameters:
return error(start_response, message = 'no message')
#send db에 넣을 때는 percent-encode할 필요가 없다
channel = parameters['channel'][0].decode('utf-8').lower()
message = parameters['message'][0].decode('utf-8')
db.send.insert({
'account': session['account'],
'channel': channel,
'message': message
})
context['logs'] = [{
'source': config.BOT_NAME,
'message': '<%s> %s' % (remove_invalid_utf8_char(session['account']), remove_invalid_utf8_char(message)),
'datetime': datetime.datetime.now()
}]
start_response('200 OK', [('Content-Type', 'text/xml; charset=utf-8')])
return [render('result.xml', context)]
def delete(environ, start_response, session, parameters):
if 'channel' not in parameters:
return error(start_response, message = 'no channel')
channel = parameters['channel'][0].decode('utf-8').lower()
db[session['account']].remove({'channel': channel})
start_response('200 OK', [])
return []
def default(environ, start_response, session, parameters):
context = {}
context['account'] = session['account']
context['channels'] = list(db[session['account']].find(fields = ['channel']))
db.session.remove({'datetime': {'$lt': datetime.datetime.now() - datetime.timedelta(1)}})
start_response('200 OK', [('Content-Type', 'text/html; charset=utf-8')])
return [render('channel.html', context)]
def static(environ, start_response):
import mimetypes
path = environ.get('PATH_INFO', '')
assert '..' not in path
assert path.startswith('/')
file_path = os.path.join(PATH + '/static', os.path.normpath(path[1:]))
content_type, content_encoding = mimetypes.guess_type(file_path)
header = []
if content_type:
header.append(('Content-Type', content_type))
if content_encoding:
header.append(('Content-Encoding', content_encoding))
start_response('200 OK', header)
return open(file_path, 'rb')
def render(template, context):
return jinja2.Environment(loader=jinja2.FileSystemLoader(PATH + '/render')).get_template(template).render(context).encode('utf-8')
def redirect(start_response, path):
start_response('302 Found', [
('Location', config.SIRC_URL.strip('/') + path)
])
return []
def error(start_response, code = '404 Not Found', message = 'error'):
context = {}
context['message'] = message.decode('utf-8')
start_response(code, [('Content-Type', 'text/html; charset=utf-8')])
return [render('error.html', context)]
def create_session_id(): # TODO: 중복 체크
import string
import random
bag = string.ascii_uppercase + string.ascii_lowercase + string.digits
return ''.join(random.sample(bag * 24, 24))
def parse_datetime(s):
if '.' in s:
return datetime.datetime.strptime(s, '%Y-%m-%d %H:%M:%S.%f')
else:
return datetime.datetime.strptime(s, '%Y-%m-%d %H:%M:%S')
def request_request_token(oauth_config):
import oauth2
import httplib2
consumer = oauth2.Consumer(oauth_config['CONSUMER_KEY'], oauth_config['CONSUMER_SECRET'])
request = oauth2.Request.from_consumer_and_token(consumer, http_url=oauth_config['REQUEST_URL'], parameters={'oauth_callback': config.CALLBACK_URL})
request.sign_request(oauth2.SignatureMethod_HMAC_SHA1(), consumer, None)
response = httplib2.Http().request(request.to_url())
if response[0]['status'] != '200':
raise Exception(response)
token = oauth2.Token.from_string(response[1])
return ('%s?oauth_token=%s' % (oauth_config['AUTHORIZE_URL'], token.key), token.secret)
def request_access_token(oauth_token, oauth_token_secret, oauth_verifier, oauth_config):
import oauth2
import httplib2
token = oauth2.Token(oauth_token, oauth_token_secret)
token.set_verifier(oauth_verifier)
consumer = oauth2.Consumer(oauth_config['CONSUMER_KEY'], oauth_config['CONSUMER_SECRET'])
request = oauth2.Request.from_consumer_and_token(consumer, token=token, http_url=oauth_config['ACCESS_URL'], is_form_encoded=True)
request.sign_request(oauth2.SignatureMethod_HMAC_SHA1(), consumer, token)
response = httplib2.Http().request(request.to_url())
if response[0]['status'] != '200':
raise Exception(response)
return response[1]
pattern = re.compile("[\x00-\x1F]|[\x80-\x9F]", re.UNICODE)
def remove_invalid_utf8_char(s):
return pattern.sub(u'�', s)
if __name__ == '__main__':
bot = bot.SBot()
gevent.spawn(bot.start)
server = gevent.pywsgi.WSGIServer((config.SIRC_ADDRESS, config.SIRC_PORT), application)
server.serve_forever()