Mercurial > libervia-backend
annotate src/plugins/deprecated_misc_cs.py @ 711:c9792d0be499
plugins (tools): collective games (with no opponent, like radiocol) were handled like other games
author | souliane <souliane@mailoo.org> |
---|---|
date | Sun, 17 Nov 2013 16:30:46 +0100 |
parents | 4f747d7fde8c |
children | bfabeedbf32e |
rev | line source |
---|---|
101 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
4 # SAT plugin for managing xep-0045 |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013 Jérôme Poisson (goffi@goffi.org) |
101 | 6 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
10 # (at your option) any later version. |
101 | 11 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
15 # GNU Affero General Public License for more details. |
101 | 16 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
101 | 19 |
20 from logging import debug, info, warning, error | |
21 from twisted.words.xish import domish | |
22 from twisted.internet import protocol, defer, threads, reactor | |
23 from twisted.words.protocols.jabber import client, jid, xmlstream | |
24 from twisted.words.protocols.jabber import error as jab_error | |
25 from twisted.words.protocols.jabber.xmlstream import IQ | |
26 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
|
27 from sat.memory.persistent import PersistentBinaryDict |
101 | 28 import os.path |
29 import pdb | |
30 | |
31 from zope.interface import implements | |
32 | |
33 from wokkel import disco, iwokkel, data_form | |
223 | 34 from sat.tools.xml_tools import XMLUI |
101 | 35 import urllib |
108
e24e080e6b16
CS plugin: unread messages can now be openned in external web browser
Goffi <goffi@goffi.org>
parents:
107
diff
changeset
|
36 import webbrowser |
101 | 37 |
102 | 38 from BeautifulSoup import BeautifulSoup |
106 | 39 import re |
101 | 40 |
41 PLUGIN_INFO = { | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
42 "name": "CouchSurfing plugin", |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
43 "import_name": "CS", |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
44 "type": "Misc", |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
45 "protocols": [], |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
46 "dependencies": [], |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
47 "main": "CS_Plugin", |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
48 "handler": "no", |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
49 "description": _(u"""This plugin allow to manage your CouchSurfing account throught your SàT frontend""") |
101 | 50 } |
51 | |
218
5c68a65548c3
Plugin CS: fixed forgotten debug stuff, CS plugin is now working again
Goffi <goffi@goffi.org>
parents:
109
diff
changeset
|
52 AGENT = 'Salut à Toi XMPP/CS Plugin' |
101 | 53 |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
54 |
588
beaf6bec2fcd
Remove every old-style class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
587
diff
changeset
|
55 class CS_Plugin(object): |
101 | 56 |
57 params = """ | |
58 <params> | |
59 <individual> | |
60 <category name="CouchSurfing"> | |
61 <param name="Login" type="string" /> | |
62 <param name="Password" type="password" /> | |
63 </category> | |
64 </individual> | |
65 </params> | |
66 """ | |
67 | |
68 def __init__(self, host): | |
69 info(_("Plugin CS initialization")) | |
70 self.host = host | |
71 #parameters | |
662
4f747d7fde8c
core: importParams renamed to updateParams: it now updates the parameter instead of appending children if it find an existing one.
Goffi <goffi@goffi.org>
parents:
609
diff
changeset
|
72 host.memory.updateParams(CS_Plugin.params) |
101 | 73 #menu |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
74 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
|
75 self.data = {} # TODO: delete cookies/data after a while |
107 | 76 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
|
77 self.host.registerGeneralCB("plugin_CS_showUnreadMessages", self.showUnreadMessages) |
102 | 78 |
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
|
79 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
|
80 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
|
81 |
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
|
82 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
|
83 if not self.data[profile]: |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
84 self.data[profile] = {'cookies': {}} |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
85 |
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
|
86 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
|
87 |
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 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
|
89 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
|
90 |
102 | 91 def erroCB(self, e, id): |
92 """Called when something is going wrong when contacting CS website""" | |
107 | 93 #pdb.set_trace() |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
94 message_data = {"reason": "connection error", "message": _(u"Impossible to contact CS website, please check your login/password, connection or try again later")} |
102 | 95 self.host.bridge.actionResult("ERROR", id, message_data) |
96 | |
101 | 97 def menuSelected(self, id, profile): |
98 """Called when the couchsurfing menu item is selected""" | |
99 login = self.host.memory.getParamA("Login", "CouchSurfing", profile_key=profile) | |
100 password = self.host.memory.getParamA("Password", "CouchSurfing", profile_key=profile) | |
101 if not login or not password: | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
102 message_data = {"reason": "uncomplete", "message": _(u"You have to fill your CouchSurfing login & password in parameters before using this interface")} |
101 | 103 self.host.bridge.actionResult("ERROR", id, message_data) |
104 return | |
105 | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
106 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
|
107 |
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
|
108 self.data[profile]['cookies'] = {} |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
109 |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
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 |
218
5c68a65548c3
Plugin CS: fixed forgotten debug stuff, CS plugin is now working again
Goffi <goffi@goffi.org>
parents:
109
diff
changeset
|
114 #self.host.bridge.actionResult("SUPPRESS", id, {}) |
106 | 115 |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
116 #pages parsing callbacks |
106 | 117 |
118 def savePage(self, name, html): | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
119 f = open('/tmp/CS_' + name + '.html', 'w') |
106 | 120 f.write(html) |
121 f.close() | |
122 print "page [%s] sauvee" % name | |
123 #pdb.set_trace() | |
124 | |
125 def __connectionCB(self, html, id, profile): | |
126 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
|
127 #self.savePage('principale',html) |
106 | 128 soup = BeautifulSoup(html) |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
129 self.data[profile]['user_nick'] = soup.find('a', 'item_link', href='/home.html').contents[0] |
106 | 130 self.data[profile]['user_name'] = soup.html.head.title.string.split(' - ')[1] |
131 #unread messages | |
132 try: | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
133 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 | 134 except: |
135 self.data[profile]['unread_messages'] = 0 | |
136 #unread couchrequest messages | |
137 try: | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
138 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 | 139 except: |
140 self.data[profile]['unread_CR_messages'] = 0 | |
141 | |
142 #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
|
143 if 'friends' not in self.data[profile]: |
106 | 144 self.data[profile]['friends'] = {} |
145 d = getPage('http://www.couchsurfing.org/connections.html?type=myfriends&show=10000', agent=AGENT, cookies=self.data[profile]['cookies']) | |
146 d.addCallback(self.__friendsPageCB, id=id, profile=profile) | |
147 d.addErrback(self.erroCB, id) | |
148 else: | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
149 self.host.bridge.actionResult("XMLUI", id, {"type": "window", "xml": self.__buildUI(self.data[profile])}) |
106 | 150 |
151 def __buildUI(self, data): | |
152 """Build the XML UI of the plugin | |
153 @param data: data store for the profile""" | |
154 user_nick = data['user_nick'] | |
155 user_name = data['user_name'] | |
156 unread_mess = data['unread_messages'] | |
157 unread_CR_mess = data['unread_CR_messages'] | |
158 friends_list = data['friends'].keys() | |
159 friends_list.sort() | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
160 interface = XMLUI('window', 'tabs', title='CouchSurfing management') |
106 | 161 interface.addCategory(_("Messages"), "vertical") |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
162 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
|
163 if unread_mess: |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
164 interface.addButton('plugin_CS_showUnreadMessages', 'showUnreadMessages', _('Show unread message%(plural)s in external web browser') % {'plural': 's' if unread_mess > 1 else ''}) |
107 | 165 interface.addList(friends_list, 'friends', style=['multi']) |
166 interface.changeLayout('pairs') | |
167 interface.addLabel(_("Subject")) | |
168 interface.addString('subject') | |
169 interface.changeLayout('vertical') | |
170 interface.addLabel(_("Message")) | |
171 interface.addText("(use %name% for contact name and %firstname% for guessed first name)") | |
172 interface.addTextBox('message') | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
173 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
|
174 #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
|
175 #interface.addCategory(_("Couch search"), "vertical") |
106 | 176 return interface.toXml() |
177 | |
178 def __meetingPageCB(self, html): | |
179 """Called when the meeting page has been received""" | |
180 | |
181 def __friendsPageCB(self, html, id, profile): | |
182 """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
|
183 self.savePage('friends', html) |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
184 soup = BeautifulSoup(html.replace('"formtable width="400', '"formtable" width="400"')) # CS html fix #TODO: report the bug to CS dev team |
106 | 185 friends = self.data[profile]['friends'] |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
186 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
|
187 _nobr = _tr.find('nobr') # contain the friend name |
106 | 188 friend_name = unicode(_nobr.string) |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
189 friend_link = u'http://www.couchsurfing.org' + _nobr.parent['href'] |
106 | 190 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
|
191 a_tag = _tr.find('a', href=regex_href) |
106 | 192 friend_id = regex_href.search(unicode(a_tag)).groups()[0] |
193 | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
194 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
|
195 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
|
196 a = soup.find('td', 'barmiddle next').a # is there several pages ? |
106 | 197 if a: |
198 #yes, we parse the next page | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
199 d = getPage('http://www.couchsurfing.org/' + str(a['href']), agent=AGENT, cookies=self.data[profile]['cookies']) |
106 | 200 d.addCallback(self.__friendsPageCB, id=id, profile=profile) |
201 d.addErrback(self.erroCB, id) | |
202 else: | |
203 #no, we show the result | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
204 self.host.bridge.actionResult("XMLUI", id, {"type": "window", "xml": self.__buildUI(self.data[profile])}) |
107 | 205 |
206 def __sendMessage(self, answer, subject, message, data, recipient_list, id, profile): | |
207 """Send actually the message | |
208 @param subject: subject of the message | |
209 @param message: body of the message | |
210 @param data: data of the profile | |
211 @param recipient_list: list of friends names, names are removed once message is sent | |
212 @param id: id of the action | |
213 @param profile: profile who launched the action | |
214 """ | |
215 if answer: | |
216 if not 'Here is a copy of the email that was sent' in answer: | |
217 error(_("INTERNAL ERROR: no confirmation of message sent by CS, maybe the site has been modified ?")) | |
218 #TODO: throw a warning to the frontend, saying that maybe the message has not been sent and to contact dev of this plugin | |
219 #debug(_('HTML answer: %s') % answer) | |
220 if recipient_list: | |
221 recipient = recipient_list.pop() | |
222 try: | |
223 friend_id = data['friends'][recipient]['id'] | |
224 except KeyError: | |
225 error('INTERNAL ERROR: unknown friend') | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
226 return # send an error to the frontend |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
227 mess = message.replace('%name%', recipient).replace('%firstname%', recipient.split(' ')[0]) |
107 | 228 info(_('Sending message to %s') % recipient) |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
229 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
|
230 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
|
231 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 | 232 d.addCallback(self.__sendMessage, subject, message, data, recipient_list, id, profile) |
233 d.addErrback(self.erroCB, id) | |
234 else: | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
235 interface = XMLUI('window', title=_('Message sent')) # TODO: create particular actionResult for alerts ? |
107 | 236 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
|
237 self.host.bridge.actionResult("XMLUI", id, {"type": "window", "xml": interface.toXml()}) |
102 | 238 |
107 | 239 def sendMessage(self, id, data, profile): |
240 """Called to send a message to a friend | |
241 @param data: dict with the following keys: | |
242 friend: name of the recipient | |
243 subject: subject of the message | |
244 message: body of the message, with the following special keywords: | |
245 - %name%: name of the friend | |
246 - %firstname%: guessed first name of the friend (currently the first part of the name) | |
247 """ | |
248 if not data['friends']: | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
249 message_data = {"reason": "bad data", "message": _(u"There is not recipient selected for this message !")} |
107 | 250 self.host.bridge.actionResult("ERROR", id, message_data) |
251 return | |
252 friends = data['friends'].split('\t') | |
253 subject = data['subject'] | |
254 message = data['message'] | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
255 info(_("sending message to %(friends)s with subject [%(subject)s]" % {'friends': friends, 'subject': subject})) |
107 | 256 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
|
257 |
108
e24e080e6b16
CS plugin: unread messages can now be openned in external web browser
Goffi <goffi@goffi.org>
parents:
107
diff
changeset
|
258 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
|
259 """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
|
260 #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
|
261 soup = BeautifulSoup(html) |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
262 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
|
263 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
|
264 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
|
265 |
e24e080e6b16
CS plugin: unread messages can now be openned in external web browser
Goffi <goffi@goffi.org>
parents:
107
diff
changeset
|
266 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
|
267 """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
|
268 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
|
269 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
|
270 d.addErrback(self.erroCB, id) |