101
|
1 #!/usr/bin/python |
|
2 # -*- coding: utf-8 -*- |
|
3 |
|
4 """ |
|
5 SAT plugin for managing xep-0045 |
|
6 Copyright (C) 2009, 2010 Jérôme Poisson (goffi@goffi.org) |
|
7 |
|
8 This program is free software: you can redistribute it and/or modify |
|
9 it under the terms of the GNU General Public License as published by |
|
10 the Free Software Foundation, either version 3 of the License, or |
|
11 (at your option) any later version. |
|
12 |
|
13 This program is distributed in the hope that it will be useful, |
|
14 but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
16 GNU General Public License for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
20 """ |
|
21 |
|
22 from logging import debug, info, warning, error |
|
23 from twisted.words.xish import domish |
|
24 from twisted.internet import protocol, defer, threads, reactor |
|
25 from twisted.words.protocols.jabber import client, jid, xmlstream |
|
26 from twisted.words.protocols.jabber import error as jab_error |
|
27 from twisted.words.protocols.jabber.xmlstream import IQ |
|
28 from twisted.web.client import getPage |
|
29 import os.path |
|
30 import pdb |
|
31 import random |
|
32 |
|
33 from zope.interface import implements |
|
34 |
|
35 from wokkel import disco, iwokkel, data_form |
102
|
36 from tools.xml_tools import dataForm2xml |
101
|
37 import urllib |
|
38 |
102
|
39 from BeautifulSoup import BeautifulSoup |
101
|
40 |
|
41 |
|
42 |
|
43 PLUGIN_INFO = { |
|
44 "name": "CouchSurfing plugin", |
|
45 "import_name": "CS", |
|
46 "type": "Misc", |
|
47 "protocols": [], |
|
48 "dependencies": [], |
|
49 "main": "CS_Plugin", |
|
50 "handler": "no", |
|
51 "description": _(u"""This plugin allow to manage your CouchSurfing account throught your SàT frontend""") |
|
52 } |
|
53 |
|
54 AGENT = 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.3) Gecko/20100423 Ubuntu/10.04 (lucid) Firefox/3.6.3' |
|
55 |
|
56 class CS_Plugin(): |
|
57 |
|
58 params = """ |
|
59 <params> |
|
60 <individual> |
|
61 <category name="CouchSurfing"> |
|
62 <param name="Login" type="string" /> |
|
63 <param name="Password" type="password" /> |
|
64 </category> |
|
65 </individual> |
|
66 </params> |
|
67 """ |
|
68 |
|
69 def __init__(self, host): |
|
70 info(_("Plugin CS initialization")) |
|
71 self.host = host |
|
72 #parameters |
|
73 host.memory.importParams(CS_Plugin.params) |
|
74 #menu |
|
75 host.importMenu(_("Plugin"), "CouchSurfing", self.menuSelected, help_string = _("Launch CoushSurfing mangement interface")) |
102
|
76 self.data={} #TODO: delete cookies/data after a while |
|
77 |
|
78 def erroCB(self, e, id): |
|
79 """Called when something is going wrong when contacting CS website""" |
|
80 message_data={"reason": "connection error", "message":_(u"Impossible to contact CS website, please check your login/password, connection or try again later")} |
|
81 self.host.bridge.actionResult("ERROR", id, message_data) |
|
82 |
101
|
83 |
|
84 def menuSelected(self, id, profile): |
|
85 """Called when the couchsurfing menu item is selected""" |
|
86 login = self.host.memory.getParamA("Login", "CouchSurfing", profile_key=profile) |
|
87 password = self.host.memory.getParamA("Password", "CouchSurfing", profile_key=profile) |
|
88 if not login or not password: |
|
89 message_data={"reason": "uncomplete", "message":_(u"You have to fill your CouchSurfing login & password in parameters before using this interface")} |
|
90 self.host.bridge.actionResult("ERROR", id, message_data) |
|
91 return |
|
92 |
|
93 post_data = urllib.urlencode({'auth_login[un]':login,'auth_login[pw]':password,'auth_login[action]':'Login...'}) |
|
94 |
102
|
95 self.data[profile] = {'cookies':{}} |
101
|
96 |
|
97 def connectionCB(html): |
|
98 print 'Response received' |
102
|
99 soup = self.data[profile]['soup'] = BeautifulSoup(html) |
|
100 user_nick = soup.find('a','item_link',href='/home.html').contents[0] |
|
101 user_name = soup.html.head.title.string.split(' - ')[1] |
|
102 unread_messages = int(soup.find('div','item_bubble').a.string) |
|
103 form_xml = """ |
|
104 <form> |
|
105 <elem type='text' value='Welcome %(name)s, you have %(nb_message)i unread messages'/> |
|
106 </form> |
|
107 """ % {'name':user_name, 'nb_message':unread_messages} |
|
108 self.host.bridge.actionResult("FORM", id, {"type":"window", "xml":form_xml}) |
101
|
109 |
|
110 |
|
111 |
102
|
112 #d = getPage('http://www.couchsurfing.org/messages.html?message_status=inbox', agent=AGENT, cookies=self.session_cookies[profile]) |
|
113 |
|
114 #tmp |
|
115 f = open('/home/goffi/tmp/CS_principale.html','r') |
|
116 html = f.read() |
|
117 connectionCB(html) |
|
118 |
|
119 """d = getPage('http://www.couchsurfing.org/login.html', method='POST', postdata=post_data, headers={'Content-Type':'application/x-www-form-urlencoded'} , agent=AGENT, cookies=self.data[profile]['cookies']) |
|
120 d.addCallback(connectionCB) |
|
121 d.addErrback(self.erroCB, id)""" |
|
122 |
|
123 |
|
124 #self.host.bridge.actionResult("SUPPRESS", id, {}) |
|
125 |
|
126 |
|
127 |