Mercurial > libervia-backend
annotate src/plugins/deprecated_misc_cs.py @ 606:21ddafccf32d
installation: added forgotten imaging dependency
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 23 Feb 2013 17:50:58 +0100 |
parents | e629371a28d3 |
children | 84a6e83157c2 |
rev | line source |
---|---|
101 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 SAT plugin for managing xep-0045 | |
572 | 6 Copyright (C) 2009, 2010, 2011, 2012, 2013 Jérôme Poisson (goffi@goffi.org) |
101 | 7 |
8 This program is free software: you can redistribute it and/or modify | |
480
2a072735e459
Licence modification: the full project is now under AGPL v3+ instead of GPL v3+
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
9 it under the terms of the GNU Affero General Public License as published by |
101 | 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 | |
480
2a072735e459
Licence modification: the full project is now under AGPL v3+ instead of GPL v3+
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
16 GNU Affero General Public License for more details. |
101 | 17 |
480
2a072735e459
Licence modification: the full project is now under AGPL v3+ instead of GPL v3+
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
18 You should have received a copy of the GNU Affero General Public License |
101 | 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 | |
440
48277946348b
plugin CS: use of PersistentBinaryData to store profile's information, and clean up on profile disconnection.
Goffi <goffi@goffi.org>
parents:
437
diff
changeset
|
29 from sat.memory.persistent import PersistentBinaryDict |
101 | 30 import os.path |
31 import pdb | |
32 | |
33 from zope.interface import implements | |
34 | |
35 from wokkel import disco, iwokkel, data_form | |
223 | 36 from sat.tools.xml_tools import XMLUI |
101 | 37 import urllib |
108
e24e080e6b16
CS plugin: unread messages can now be openned in external web browser
Goffi <goffi@goffi.org>
parents:
107
diff
changeset
|
38 import webbrowser |
101 | 39 |
102 | 40 from BeautifulSoup import BeautifulSoup |
106 | 41 import re |
101 | 42 |
43 PLUGIN_INFO = { | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
44 "name": "CouchSurfing plugin", |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
45 "import_name": "CS", |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
46 "type": "Misc", |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
47 "protocols": [], |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
48 "dependencies": [], |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
49 "main": "CS_Plugin", |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
50 "handler": "no", |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
51 "description": _(u"""This plugin allow to manage your CouchSurfing account throught your SàT frontend""") |
101 | 52 } |
53 | |
218
5c68a65548c3
Plugin CS: fixed forgotten debug stuff, CS plugin is now working again
Goffi <goffi@goffi.org>
parents:
109
diff
changeset
|
54 AGENT = 'Salut à Toi XMPP/CS Plugin' |
101 | 55 |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
56 |
588
beaf6bec2fcd
Remove every old-style class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
587
diff
changeset
|
57 class CS_Plugin(object): |
101 | 58 |
59 params = """ | |
60 <params> | |
61 <individual> | |
62 <category name="CouchSurfing"> | |
63 <param name="Login" type="string" /> | |
64 <param name="Password" type="password" /> | |
65 </category> | |
66 </individual> | |
67 </params> | |
68 """ | |
69 | |
70 def __init__(self, host): | |
71 info(_("Plugin CS initialization")) | |
72 self.host = host | |
73 #parameters | |
74 host.memory.importParams(CS_Plugin.params) | |
75 #menu | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
76 host.importMenu(_("Plugin"), "CouchSurfing", self.menuSelected, help_string=_("Launch CoushSurfing management interface")) |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
77 self.data = {} # TODO: delete cookies/data after a while |
107 | 78 self.host.registerGeneralCB("plugin_CS_sendMessage", self.sendMessage) |
108
e24e080e6b16
CS plugin: unread messages can now be openned in external web browser
Goffi <goffi@goffi.org>
parents:
107
diff
changeset
|
79 self.host.registerGeneralCB("plugin_CS_showUnreadMessages", self.showUnreadMessages) |
102 | 80 |
440
48277946348b
plugin CS: use of PersistentBinaryData to store profile's information, and clean up on profile disconnection.
Goffi <goffi@goffi.org>
parents:
437
diff
changeset
|
81 def profileConnected(self, profile): |
48277946348b
plugin CS: use of PersistentBinaryData to store profile's information, and clean up on profile disconnection.
Goffi <goffi@goffi.org>
parents:
437
diff
changeset
|
82 self.data[profile] = PersistentBinaryDict("plugin_CS", profile) |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
83 |
440
48277946348b
plugin CS: use of PersistentBinaryData to store profile's information, and clean up on profile disconnection.
Goffi <goffi@goffi.org>
parents:
437
diff
changeset
|
84 def dataLoaded(ignore): |
48277946348b
plugin CS: use of PersistentBinaryData to store profile's information, and clean up on profile disconnection.
Goffi <goffi@goffi.org>
parents:
437
diff
changeset
|
85 if not self.data[profile]: |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
86 self.data[profile] = {'cookies': {}} |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
87 |
440
48277946348b
plugin CS: use of PersistentBinaryData to store profile's information, and clean up on profile disconnection.
Goffi <goffi@goffi.org>
parents:
437
diff
changeset
|
88 self.data[profile].load().addCallback(dataLoaded) |
48277946348b
plugin CS: use of PersistentBinaryData to store profile's information, and clean up on profile disconnection.
Goffi <goffi@goffi.org>
parents:
437
diff
changeset
|
89 |
48277946348b
plugin CS: use of PersistentBinaryData to store profile's information, and clean up on profile disconnection.
Goffi <goffi@goffi.org>
parents:
437
diff
changeset
|
90 def profileDisconnected(self, profile): |
48277946348b
plugin CS: use of PersistentBinaryData to store profile's information, and clean up on profile disconnection.
Goffi <goffi@goffi.org>
parents:
437
diff
changeset
|
91 del self.data[profile] |
48277946348b
plugin CS: use of PersistentBinaryData to store profile's information, and clean up on profile disconnection.
Goffi <goffi@goffi.org>
parents:
437
diff
changeset
|
92 |
102 | 93 def erroCB(self, e, id): |
94 """Called when something is going wrong when contacting CS website""" | |
107 | 95 #pdb.set_trace() |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
96 message_data = {"reason": "connection error", "message": _(u"Impossible to contact CS website, please check your login/password, connection or try again later")} |
102 | 97 self.host.bridge.actionResult("ERROR", id, message_data) |
98 | |
101 | 99 def menuSelected(self, id, profile): |
100 """Called when the couchsurfing menu item is selected""" | |
101 login = self.host.memory.getParamA("Login", "CouchSurfing", profile_key=profile) | |
102 password = self.host.memory.getParamA("Password", "CouchSurfing", profile_key=profile) | |
103 if not login or not password: | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
104 message_data = {"reason": "uncomplete", "message": _(u"You have to fill your CouchSurfing login & password in parameters before using this interface")} |
101 | 105 self.host.bridge.actionResult("ERROR", id, message_data) |
106 return | |
107 | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
108 post_data = urllib.urlencode({'auth_login[un]': login, 'auth_login[pw]': password, 'auth_login[action]': 'Login...'}) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
109 |
440
48277946348b
plugin CS: use of PersistentBinaryData to store profile's information, and clean up on profile disconnection.
Goffi <goffi@goffi.org>
parents:
437
diff
changeset
|
110 self.data[profile]['cookies'] = {} |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
111 |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
112 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 | 113 d.addCallback(self.__connectionCB, id, profile) |
107 | 114 d.addErrback(self.erroCB, id) |
102 | 115 |
218
5c68a65548c3
Plugin CS: fixed forgotten debug stuff, CS plugin is now working again
Goffi <goffi@goffi.org>
parents:
109
diff
changeset
|
116 #self.host.bridge.actionResult("SUPPRESS", id, {}) |
106 | 117 |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
118 #pages parsing callbacks |
106 | 119 |
120 def savePage(self, name, html): | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
121 f = open('/tmp/CS_' + name + '.html', 'w') |
106 | 122 f.write(html) |
123 f.close() | |
124 print "page [%s] sauvee" % name | |
125 #pdb.set_trace() | |
126 | |
127 def __connectionCB(self, html, id, profile): | |
128 print 'Response received' | |
218
5c68a65548c3
Plugin CS: fixed forgotten debug stuff, CS plugin is now working again
Goffi <goffi@goffi.org>
parents:
109
diff
changeset
|
129 #self.savePage('principale',html) |
106 | 130 soup = BeautifulSoup(html) |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
131 self.data[profile]['user_nick'] = soup.find('a', 'item_link', href='/home.html').contents[0] |
106 | 132 self.data[profile]['user_name'] = soup.html.head.title.string.split(' - ')[1] |
133 #unread messages | |
134 try: | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
135 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)) |
106 | 136 except: |
137 self.data[profile]['unread_messages'] = 0 | |
138 #unread couchrequest messages | |
139 try: | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
140 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)) |
106 | 141 except: |
142 self.data[profile]['unread_CR_messages'] = 0 | |
143 | |
144 #if we have already the list of friend, no need to make new requests | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
145 if 'friends' not in self.data[profile]: |
106 | 146 self.data[profile]['friends'] = {} |
147 d = getPage('http://www.couchsurfing.org/connections.html?type=myfriends&show=10000', agent=AGENT, cookies=self.data[profile]['cookies']) | |
148 d.addCallback(self.__friendsPageCB, id=id, profile=profile) | |
149 d.addErrback(self.erroCB, id) | |
150 else: | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
151 self.host.bridge.actionResult("XMLUI", id, {"type": "window", "xml": self.__buildUI(self.data[profile])}) |
106 | 152 |
153 def __buildUI(self, data): | |
154 """Build the XML UI of the plugin | |
155 @param data: data store for the profile""" | |
156 user_nick = data['user_nick'] | |
157 user_name = data['user_name'] | |
158 unread_mess = data['unread_messages'] | |
159 unread_CR_mess = data['unread_CR_messages'] | |
160 friends_list = data['friends'].keys() | |
161 friends_list.sort() | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
162 interface = XMLUI('window', 'tabs', title='CouchSurfing management') |
106 | 163 interface.addCategory(_("Messages"), "vertical") |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
164 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 ''}) |
108
e24e080e6b16
CS plugin: unread messages can now be openned in external web browser
Goffi <goffi@goffi.org>
parents:
107
diff
changeset
|
165 if unread_mess: |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
166 interface.addButton('plugin_CS_showUnreadMessages', 'showUnreadMessages', _('Show unread message%(plural)s in external web browser') % {'plural': 's' if unread_mess > 1 else ''}) |
107 | 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') | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
175 interface.addButton('plugin_CS_sendMessage', 'sendMessage', _('send'), fields_back=['friends', 'subject', 'message']) |
109
18b0cf49a6f1
Plugin CS minor changes (Events & Couch Search tabs temporarly removed, open_new_tab user to open link in browser)
Goffi <goffi@goffi.org>
parents:
108
diff
changeset
|
176 #interface.addCategory(_("Events"), "vertical") #TODO: coming soon, hopefuly :) |
18b0cf49a6f1
Plugin CS minor changes (Events & Couch Search tabs temporarly removed, open_new_tab user to open link in browser)
Goffi <goffi@goffi.org>
parents:
108
diff
changeset
|
177 #interface.addCategory(_("Couch search"), "vertical") |
106 | 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""" | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
185 self.savePage('friends', html) |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
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'] |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
188 for _tr in soup.findAll('tr', {'class': re.compile("^msgRow*")}): # we parse the row with friends infos |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
189 _nobr = _tr.find('nobr') # contain the friend name |
106 | 190 friend_name = unicode(_nobr.string) |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
191 friend_link = u'http://www.couchsurfing.org' + _nobr.parent['href'] |
106 | 192 regex_href = re.compile(r'/connections\.html\?id=([^&]+)') |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
193 a_tag = _tr.find('a', href=regex_href) |
106 | 194 friend_id = regex_href.search(unicode(a_tag)).groups()[0] |
195 | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
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}) |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
197 friends[friend_name] = {'link': friend_link, 'id': friend_id} |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
198 a = soup.find('td', 'barmiddle next').a # is there several pages ? |
106 | 199 if a: |
200 #yes, we parse the next page | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
201 d = getPage('http://www.couchsurfing.org/' + str(a['href']), agent=AGENT, cookies=self.data[profile]['cookies']) |
106 | 202 d.addCallback(self.__friendsPageCB, id=id, profile=profile) |
203 d.addErrback(self.erroCB, id) | |
204 else: | |
205 #no, we show the result | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
206 self.host.bridge.actionResult("XMLUI", id, {"type": "window", "xml": self.__buildUI(self.data[profile])}) |
107 | 207 |
208 def __sendMessage(self, answer, subject, message, data, recipient_list, id, profile): | |
209 """Send actually the message | |
210 @param subject: subject of the message | |
211 @param message: body of the message | |
212 @param data: data of the profile | |
213 @param recipient_list: list of friends names, names are removed once message is sent | |
214 @param id: id of the action | |
215 @param profile: profile who launched the action | |
216 """ | |
217 if answer: | |
218 if not 'Here is a copy of the email that was sent' in answer: | |
219 error(_("INTERNAL ERROR: no confirmation of message sent by CS, maybe the site has been modified ?")) | |
220 #TODO: throw a warning to the frontend, saying that maybe the message has not been sent and to contact dev of this plugin | |
221 #debug(_('HTML answer: %s') % answer) | |
222 if recipient_list: | |
223 recipient = recipient_list.pop() | |
224 try: | |
225 friend_id = data['friends'][recipient]['id'] | |
226 except KeyError: | |
227 error('INTERNAL ERROR: unknown friend') | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
228 return # send an error to the frontend |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
229 mess = message.replace('%name%', recipient).replace('%firstname%', recipient.split(' ')[0]) |
107 | 230 info(_('Sending message to %s') % recipient) |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
231 debug(_("\nsubject: %(subject)s\nmessage: \n---\n%(message)s\n---\n\n") % {'subject': subject, 'message': mess}) |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
232 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]': ''}) |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
233 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']) |
107 | 234 d.addCallback(self.__sendMessage, subject, message, data, recipient_list, id, profile) |
235 d.addErrback(self.erroCB, id) | |
236 else: | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
237 interface = XMLUI('window', title=_('Message sent')) # TODO: create particular actionResult for alerts ? |
107 | 238 interface.addText(_('The message has been sent to every recipients')) |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
239 self.host.bridge.actionResult("XMLUI", id, {"type": "window", "xml": interface.toXml()}) |
102 | 240 |
107 | 241 def sendMessage(self, id, data, profile): |
242 """Called to send a message to a friend | |
243 @param data: dict with the following keys: | |
244 friend: name of the recipient | |
245 subject: subject of the message | |
246 message: body of the message, with the following special keywords: | |
247 - %name%: name of the friend | |
248 - %firstname%: guessed first name of the friend (currently the first part of the name) | |
249 """ | |
250 if not data['friends']: | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
251 message_data = {"reason": "bad data", "message": _(u"There is not recipient selected for this message !")} |
107 | 252 self.host.bridge.actionResult("ERROR", id, message_data) |
253 return | |
254 friends = data['friends'].split('\t') | |
255 subject = data['subject'] | |
256 message = data['message'] | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
257 info(_("sending message to %(friends)s with subject [%(subject)s]" % {'friends': friends, 'subject': subject})) |
107 | 258 self.__sendMessage(None, subject, message, self.data[profile], friends, id, profile) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
259 |
108
e24e080e6b16
CS plugin: unread messages can now be openned in external web browser
Goffi <goffi@goffi.org>
parents:
107
diff
changeset
|
260 def __showUnreadMessages2(self, html, id, profile): |
e24e080e6b16
CS plugin: unread messages can now be openned in external web browser
Goffi <goffi@goffi.org>
parents:
107
diff
changeset
|
261 """Called when the inbox page has been received""" |
e24e080e6b16
CS plugin: unread messages can now be openned in external web browser
Goffi <goffi@goffi.org>
parents:
107
diff
changeset
|
262 #FIXME: that's really too fragile, only works if the unread messages are in the first page, and it would be too resources consuming for the website to DL each time all pages. In addition, the show attribute doesn't work as expected. |
e24e080e6b16
CS plugin: unread messages can now be openned in external web browser
Goffi <goffi@goffi.org>
parents:
107
diff
changeset
|
263 soup = BeautifulSoup(html) |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
264 for tag in soup.findAll(lambda tag: tag.name == 'strong' and tag.a and tag.a['href'].startswith('messages.html?message_status=inbox')): |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
265 link = "http://www.couchsurfing.org/" + str(tag.a['href']) |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
266 webbrowser.open_new_tab(link) # TODO: the web browser need to already have CS cookies (i.e. already be opened & logged on CS, or be permanently loggued), a warning to the user should be sent/or a balloon-tip |
108
e24e080e6b16
CS plugin: unread messages can now be openned in external web browser
Goffi <goffi@goffi.org>
parents:
107
diff
changeset
|
267 |
e24e080e6b16
CS plugin: unread messages can now be openned in external web browser
Goffi <goffi@goffi.org>
parents:
107
diff
changeset
|
268 def showUnreadMessages(self, id, data, profile): |
e24e080e6b16
CS plugin: unread messages can now be openned in external web browser
Goffi <goffi@goffi.org>
parents:
107
diff
changeset
|
269 """Called when user want to see all unread messages in the external browser""" |
e24e080e6b16
CS plugin: unread messages can now be openned in external web browser
Goffi <goffi@goffi.org>
parents:
107
diff
changeset
|
270 d = getPage("http://www.couchsurfing.org/messages.html?message_status=inbox&show=10000", agent=AGENT, cookies=self.data[profile]['cookies']) |
e24e080e6b16
CS plugin: unread messages can now be openned in external web browser
Goffi <goffi@goffi.org>
parents:
107
diff
changeset
|
271 d.addCallback(self.__showUnreadMessages2, id, profile) |
e24e080e6b16
CS plugin: unread messages can now be openned in external web browser
Goffi <goffi@goffi.org>
parents:
107
diff
changeset
|
272 d.addErrback(self.erroCB, id) |