forked from tricknik/Thimbl-API
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprofile.ma
More file actions
58 lines (54 loc) · 1.91 KB
/
profile.ma
File metadata and controls
58 lines (54 loc) · 1.91 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
from twisted.internet import reactor, defer
from twisted.internet.task import deferLater
from twisted.protocols import basic
from twisted.internet.protocol import Protocol, ClientCreator
class FingerProtocol(basic.LineReceiver):
def __init__(self, user):
self.user = user
self.d = defer.Deferred()
def connectionMade(self):
self.transport.write("".join((self.user, "\r\n")))
self.buf = []
def dataReceived(self, data):
self.buf.append(data)
def connectionLost(self, reason):
self.gotData(''.join(self.buf))
def gotData(self, data):
self.d.callback(data)
def finger(user, host, port=79):
def connect():
def output(data):
s = data.split("Plan:")
if s:
plan = s.pop().strip()
request.setHeader("Access-Control-Allow-Origin", "*")
request.setHeader("Access-Control-Allow-Methods", "GET")
if "callback" in request.args:
callback = request.args['callback'][0]
jsonp = "%s(%s)" % (callback, plan)
request.setHeader("Content-Type", "application/javascript")
request.write(jsonp)
else:
request.setHeader("Content-Type", "application/json")
request.write(plan)
request.finish()
def error(fail):
request.finish()
def gotProtocol(p):
p.d.addCallback(output)
return p.d
c = ClientCreator(reactor, FingerProtocol, user)
d = c.connectTCP(host, port)
d.addCallbacks(gotProtocol, error)
d = deferLater(reactor, 0, connect)
return d
user = None
if "u" in request.args:
u = request.args["u"]
s = u[0].split("@")
if len(s) == 2:
user = s[0]
host = s[1]
reactor.callLater(0, finger, user, host)
if not user:
request.finish()