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 |
106
|
36 from tools.xml_tools import XMLUI |
101
|
37 import urllib |
|
38 |
102
|
39 from BeautifulSoup import BeautifulSoup |
106
|
40 import re |
101
|
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")) |
106
|
76 self.data=self.host.memory.getPrivate('plugin_cs_data') or {} #TODO: delete cookies/data after a while |
107
|
77 self.host.registerGeneralCB("plugin_CS_sendMessage", self.sendMessage) |
102
|
78 |
|
79 def erroCB(self, e, id): |
|
80 """Called when something is going wrong when contacting CS website""" |
107
|
81 #pdb.set_trace() |
102
|
82 message_data={"reason": "connection error", "message":_(u"Impossible to contact CS website, please check your login/password, connection or try again later")} |
|
83 self.host.bridge.actionResult("ERROR", id, message_data) |
|
84 |
101
|
85 def menuSelected(self, id, profile): |
|
86 """Called when the couchsurfing menu item is selected""" |
|
87 login = self.host.memory.getParamA("Login", "CouchSurfing", profile_key=profile) |
|
88 password = self.host.memory.getParamA("Password", "CouchSurfing", profile_key=profile) |
|
89 if not login or not password: |
|
90 message_data={"reason": "uncomplete", "message":_(u"You have to fill your CouchSurfing login & password in parameters before using this interface")} |
|
91 self.host.bridge.actionResult("ERROR", id, message_data) |
|
92 return |
|
93 |
|
94 post_data = urllib.urlencode({'auth_login[un]':login,'auth_login[pw]':password,'auth_login[action]':'Login...'}) |
|
95 |
106
|
96 if not self.data.has_key(profile): |
|
97 self.data[profile] = {'cookies':{}} |
|
98 else: |
|
99 self.data[profile]['cookies'] = {} |
101
|
100 |
|
101 |
|
102 |
|
103 |
106
|
104 |
102
|
105 #tmp |
107
|
106 """f = open('/home/goffi/tmp/CS_principale.html','r') |
102
|
107 html = f.read() |
107
|
108 self.__connectionCB(html, id, profile)""" |
102
|
109 |
107
|
110 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']) |
106
|
111 d.addCallback(self.__connectionCB, id, profile) |
107
|
112 d.addErrback(self.erroCB, id) |
102
|
113 |
|
114 |
106
|
115 #self.host.bridge.actionResult("SUPPRESS", id, {}) |
|
116 |
|
117 |
|
118 #pages parsing callbacks |
|
119 def savePage(self, name, html): |
|
120 f = open ('/home/goffi/tmp/CS_'+name+'.html','w') |
|
121 f.write(html) |
|
122 f.close() |
|
123 print "page [%s] sauvee" % name |
|
124 #pdb.set_trace() |
|
125 |
|
126 def __connectionCB(self, html, id, profile): |
|
127 print 'Response received' |
|
128 self.savePage('principale',html) |
|
129 soup = BeautifulSoup(html) |
|
130 self.data[profile]['user_nick'] = soup.find('a','item_link',href='/home.html').contents[0] |
|
131 self.data[profile]['user_name'] = soup.html.head.title.string.split(' - ')[1] |
|
132 #unread messages |
|
133 try: |
|
134 self.data[profile]['unread_messages'] = int(soup.find(lambda tag: tag.name=='div' and ('class','item_bubble') in tag.attrs and tag.find('a', href="/messages.html?message_status=inbox")).find(text=True)) |
|
135 except: |
|
136 self.data[profile]['unread_messages'] = 0 |
|
137 #unread couchrequest messages |
|
138 try: |
|
139 self.data[profile]['unread_CR_messages'] = int(soup.find(lambda tag: tag.name=='div' and ('class','item_bubble') in tag.attrs and tag.find('a', href="/couchmanager")).find(text=True)) |
|
140 except: |
|
141 self.data[profile]['unread_CR_messages'] = 0 |
|
142 |
|
143 #if we have already the list of friend, no need to make new requests |
|
144 if not self.data[profile].has_key('friends'): |
|
145 self.data[profile]['friends'] = {} |
|
146 """f = open('/home/goffi/tmp/CS_friends.html','r') |
|
147 html = f.read() |
|
148 self.__friendsPageCB(html, id, profile)""" |
|
149 d = getPage('http://www.couchsurfing.org/connections.html?type=myfriends&show=10000', agent=AGENT, cookies=self.data[profile]['cookies']) |
|
150 d.addCallback(self.__friendsPageCB, id=id, profile=profile) |
|
151 d.addErrback(self.erroCB, id) |
|
152 else: |
|
153 self.host.bridge.actionResult("XMLUI", id, {"type":"window", "xml":self.__buildUI(self.data[profile])}) |
|
154 |
|
155 def __buildUI(self, data): |
|
156 """Build the XML UI of the plugin |
|
157 @param data: data store for the profile""" |
|
158 user_nick = data['user_nick'] |
|
159 user_name = data['user_name'] |
|
160 unread_mess = data['unread_messages'] |
|
161 unread_CR_mess = data['unread_CR_messages'] |
|
162 friends_list = data['friends'].keys() |
|
163 friends_list.sort() |
107
|
164 interface = XMLUI('window','tabs', title='CouchSurfing management') |
106
|
165 interface.addCategory(_("Messages"), "vertical") |
107
|
166 interface.addText(_("G'day %(name)s, you have %(nb_message)i unread message%(plural_mess)s and %(unread_CR_mess)s unread couch request message%(plural_CR)s\nIf you want to send a message, select the recipient(s) in the list below") % {'name':user_name, 'nb_message':unread_mess, 'plural_mess':'s' if unread_mess>1 else '', 'unread_CR_mess': unread_CR_mess, 'plural_CR':'s' if unread_CR_mess>1 else ''}) |
|
167 interface.addList(friends_list, 'friends', style=['multi']) |
|
168 interface.changeLayout('pairs') |
|
169 interface.addLabel(_("Subject")) |
|
170 interface.addString('subject') |
|
171 interface.changeLayout('vertical') |
|
172 interface.addLabel(_("Message")) |
|
173 interface.addText("(use %name% for contact name and %firstname% for guessed first name)") |
|
174 interface.addTextBox('message') |
|
175 interface.addButton('plugin_CS_sendMessage', 'sendMessage', _('send'), fields_back=['friends','subject','message']) |
106
|
176 interface.addCategory(_("Events"), "vertical") |
|
177 interface.addCategory(_("Couch search"), "vertical") |
|
178 return interface.toXml() |
|
179 |
|
180 def __meetingPageCB(self, html): |
|
181 """Called when the meeting page has been received""" |
|
182 |
|
183 def __friendsPageCB(self, html, id, profile): |
|
184 """Called when the friends list page has been received""" |
|
185 self.savePage('friends',html) |
107
|
186 soup = BeautifulSoup(html.replace('"formtable width="400','"formtable" width="400"')) #CS html fix #TODO: report the bug to CS dev team |
106
|
187 friends = self.data[profile]['friends'] |
|
188 for _tr in soup.findAll('tr', {'class':re.compile("^msgRow*")}): #we parse the row with friends infos |
|
189 _nobr = _tr.find('nobr') #contain the friend name |
|
190 friend_name = unicode(_nobr.string) |
|
191 friend_link = u'http://www.couchsurfing.org'+_nobr.parent['href'] |
|
192 regex_href = re.compile(r'/connections\.html\?id=([^&]+)') |
|
193 a_tag = _tr.find('a',href=regex_href) |
|
194 friend_id = regex_href.search(unicode(a_tag)).groups()[0] |
|
195 |
|
196 debug(_("CS friend found: %(friend_name)s (id: %(friend_id)s, link: %(friend_link)s)") % {'friend_name':friend_name, 'friend_id':friend_id, 'friend_link':friend_link}) |
|
197 friends[friend_name] = {'link':friend_link,'id':friend_id} |
|
198 a = soup.find('td','barmiddle next').a #is there several pages ? |
|
199 if a: |
|
200 #yes, we parse the next page |
|
201 d = getPage('http://www.couchsurfing.org/'+str(a['href']), agent=AGENT, cookies=self.data[profile]['cookies']) |
|
202 d.addCallback(self.__friendsPageCB, id=id, profile=profile) |
|
203 d.addErrback(self.erroCB, id) |
|
204 else: |
|
205 #no, we show the result |
|
206 self.host.bridge.actionResult("XMLUI", id, {"type":"window", "xml":self.__buildUI(self.data[profile])}) |
|
207 #and save the data |
|
208 self.host.memory.setPrivate('plugin_cs_data', self.data) |
107
|
209 |
|
210 def __sendMessage(self, answer, subject, message, data, recipient_list, id, profile): |
|
211 """Send actually the message |
|
212 @param subject: subject of the message |
|
213 @param message: body of the message |
|
214 @param data: data of the profile |
|
215 @param recipient_list: list of friends names, names are removed once message is sent |
|
216 @param id: id of the action |
|
217 @param profile: profile who launched the action |
|
218 """ |
|
219 if answer: |
|
220 if not 'Here is a copy of the email that was sent' in answer: |
|
221 error(_("INTERNAL ERROR: no confirmation of message sent by CS, maybe the site has been modified ?")) |
|
222 #TODO: throw a warning to the frontend, saying that maybe the message has not been sent and to contact dev of this plugin |
|
223 #debug(_('HTML answer: %s') % answer) |
|
224 if recipient_list: |
|
225 recipient = recipient_list.pop() |
|
226 try: |
|
227 friend_id = data['friends'][recipient]['id'] |
|
228 except KeyError: |
|
229 error('INTERNAL ERROR: unknown friend') |
|
230 return #send an error to the frontend |
|
231 mess = message.replace('%name%',recipient).replace('%firstname%',recipient.split(' ')[0]) |
|
232 info(_('Sending message to %s') % recipient) |
|
233 debug(_("\nsubject: %(subject)s\nmessage: \n---\n%(message)s\n---\n\n") % {'subject':subject,'message':mess}) |
|
234 post_data = urllib.urlencode({'email[subject]':subject.encode('utf-8'),'email[body]':mess.encode('utf-8'),'email[id]':friend_id,'email[action]':'Send Message','email[replied_id]':'','email[couchsurf]':'','email[directions_to_add]':''}) |
|
235 d = getPage("http://www.couchsurfing.org/send_message.html", method='POST', postdata=post_data, headers={'Content-Type':'application/x-www-form-urlencoded'} , agent=AGENT, cookies=data['cookies']) |
|
236 d.addCallback(self.__sendMessage, subject, message, data, recipient_list, id, profile) |
|
237 d.addErrback(self.erroCB, id) |
|
238 else: |
|
239 interface = XMLUI('window', title=_('Message sent')) #TODO: create particular actionResult for alerts ? |
|
240 interface.addText(_('The message has been sent to every recipients')) |
|
241 self.host.bridge.actionResult("XMLUI", id, {"type":"window", "xml":interface.toXml()}) |
102
|
242 |
|
243 |
|
244 |
106
|
245 |
107
|
246 |
|
247 def sendMessage(self, id, data, profile): |
|
248 """Called to send a message to a friend |
|
249 @param data: dict with the following keys: |
|
250 friend: name of the recipient |
|
251 subject: subject of the message |
|
252 message: body of the message, with the following special keywords: |
|
253 - %name%: name of the friend |
|
254 - %firstname%: guessed first name of the friend (currently the first part of the name) |
|
255 """ |
|
256 if not data['friends']: |
|
257 message_data={"reason": "bad data", "message":_(u"There is not recipient selected for this message !")} |
|
258 self.host.bridge.actionResult("ERROR", id, message_data) |
|
259 return |
|
260 friends = data['friends'].split('\t') |
|
261 subject = data['subject'] |
|
262 message = data['message'] |
|
263 print "send message \o/ :) :) :)" |
|
264 info(_("sending message to %(friends)s with subject [%(subject)s]" % {'friends':friends, 'subject':subject})) |
|
265 self.__sendMessage(None, subject, message, self.data[profile], friends, id, profile) |
|
266 |