forked from aaronsw/watchdog
/
contacts.py
159 lines (141 loc) · 5.78 KB
/
contacts.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
import time
import md5
import urllib, urllib2
from xml.dom import minidom
from BeautifulSoup import BeautifulSoup
import string
import demjson
import web
from settings import db, render, session
from utils import helpers, forms
def yahooLoginURL(email, url, token=None):
email = urllib.quote(email)
lines = open('/home/watchdog/certs/yauth', 'r').readlines()
appid = lines[0].rstrip()
secret = lines[1].rstrip()
ts = time.time()
appdata = email
yurl = 'https://api.login.yahoo.com'
purl = '%s?appid=%s&appdata=%s&ts=%s' % (url,appid, appdata, ts)
surl ='%s%s' % (purl, secret)
sig = md5.new(surl).hexdigest()
furl = '%s%s&sig=%s' % (yurl, purl, sig)
if token: furl = '%s&token=%s' % ( furl, token)
return furl
def gmailLoginURL(email):
url = 'https://www.google.com/accounts/AuthSubRequest?'
scope = urllib2.quote('http://www.google.com/m8/feeds/')
next = urllib2.quote('http://watchdog.net/authsub')
url += 'scope='+scope+'&session=1&secure=0&next='+ next
return url
class importcontacts:
def GET(self):
msg, msg_type = helpers.get_delete_msg()
return render.import_contacts(msg)
def POST(self):
i = web.input()
email = i.get('email')
form = forms.loadcontactsform()
if form.validates(i):
session.email = email
session.pid = i.pid
if i.provider == 'Yahoo':
ylogin_url = yahooLoginURL(email, '/WSLogin/V1/wslogin')
raise web.seeother(ylogin_url)
elif i.provider == 'Google':
glogin_url = gmailLoginURL(email)
raise web.seeother(glogin_url)
else:
import petition
share_obj = petition.share()
return share_obj.GET(form)
def save_contacts(email, contacts, provider):
user_id = helpers.get_loggedin_userid()
for c in contacts:
cname, cemail = c['name'], c['email']
vars = dict(user_id=user_id, uemail=email, cemail=cemail,
cname=cname, provider=provider)
e = db.select('contacts',
where='user_id=$user_id and uemail=$uemail and cemail=$cemail',
vars=vars)
if not e: n = db.insert('contacts', seqname=False, **vars)
else: db.update('contacts', cname=cname,
where='user_id=$user_id and uemail=$uemail and cemail=$cemail',
vars=vars)
class bbauth:
def get_contacts(self, contacts_json):
content = demjson.decode(contacts_json)
contacts = []
for c in content.get('contacts'):
fields = c['fields']
cemail = fields[0]['data']
cfname = ' '; clname = ' '
if len(fields) > 1:
cfname = fields[1].get('first', ' ')
clname = fields[1].get('last', ' ')
cname = u'%s %s' % (cfname, clname)
cname = cname.replace(''', ' ').strip()
contacts.append(dict(email=cemail, name=cname))
return contacts
def GET(self):
i = web.input()
appid = i.get('appid').rstrip()
appdata = i.get('appdata')
userhash = i.get('userhash')
ts = i.get('ts')
token = i.get('token')
email = session.email
#XXX: security verification etc..
url = yahooLoginURL(email, '/WSLogin/V1/wspwtoken_login', token)
resp = urllib2.urlopen(url)
content = resp.read()
soup = BeautifulSoup(content)
aurl = 'http://address.yahooapis.com/v1/searchContacts?format=json'
wssid = soup.findAll('wssid')[0].contents[0]
cookie =soup.findAll('cookie')[0].contents[0]
cookie = cookie.strip()
furl = aurl + '&fields=email,name&email.present=1&appid=%s&WSSID=%s' % (appid, wssid)
req = urllib2.Request(furl)
req.add_header('Cookie', cookie)
req.add_header('Content-Type', 'application/json')
response = urllib2.urlopen(req).read()
contacts = self.get_contacts(response)
save_contacts(email, contacts, provider='YAHOO')
raise web.seeother('/c/share?pid=%s' % (session.pid))
class authsub:
def get_contacts(self, contacts_feed):
ATOM_NS = 'http://www.w3.org/2005/Atom'
doc = minidom.parse(contacts_feed)
entries = doc.getElementsByTagNameNS(ATOM_NS, u'entry')
def get_text(elem):
text = ''
for node in elem.childNodes:
if node.nodeType == node.TEXT_NODE:
text += node.data
return text
contacts = []
for e in entries:
e_title = e.getElementsByTagNameNS(ATOM_NS, u'title')[0]
name = get_text(e_title)
email = e.getElementsByTagName('gd:email')[0].getAttribute('address')
contacts.append(dict(name=name, email=email))
return contacts
def GET(self):
i = web.input()
authToken = i.get('token')
email = session.email
emailq = urllib2.quote(email)
url = ("http://www.google.com/m8/feeds/contacts/%s/full?max-results=999" % emailq)
headers = { 'Authorization' : 'AuthSub token="%s"' % authToken.strip() }
request = urllib2.Request(url, None, headers)
response = urllib2.urlopen(request)
contacts = self.get_contacts(response)
save_contacts(email, contacts, provider='GOOGLE')
raise web.seeother('/c/share?pid=%s' % (session.pid))
class yauth:
def GET(self):
return """
Phrase: "# and nation nation moved yet so ship or onwhether so now conceived any the that"
File: "ydnlIEWXo.html"
Url to Check: "http://watchdog.net/ydnlIEWXo.html"
"""