Mercurial > libervia-backend
diff plugins/plugin_misc_cs.py @ 107:5ae370c71803
CS: message sending is now working
- xmltools/xmlui: buttons can now send fields of the ui when used
- xmltools/xmlui: new textbox element, to write a large text (used for messages in CS plugin)
- xmltools/xmlui: list have now an attribute multi for selecting several options
- xmltools/xmlui: window title can now be specified in the xml (attribute title)
- CS_plugin: message sending interface & management
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 28 Jun 2010 15:18:59 +0800 |
parents | 138d82f64b6f |
children | e24e080e6b16 |
line wrap: on
line diff
--- a/plugins/plugin_misc_cs.py Sat Jun 26 15:33:16 2010 +0800 +++ b/plugins/plugin_misc_cs.py Mon Jun 28 15:18:59 2010 +0800 @@ -74,9 +74,11 @@ #menu host.importMenu(_("Plugin"), "CouchSurfing", self.menuSelected, help_string = _("Launch CoushSurfing mangement interface")) self.data=self.host.memory.getPrivate('plugin_cs_data') or {} #TODO: delete cookies/data after a while + self.host.registerGeneralCB("plugin_CS_sendMessage", self.sendMessage) def erroCB(self, e, id): """Called when something is going wrong when contacting CS website""" + #pdb.set_trace() message_data={"reason": "connection error", "message":_(u"Impossible to contact CS website, please check your login/password, connection or try again later")} self.host.bridge.actionResult("ERROR", id, message_data) @@ -101,13 +103,13 @@ #tmp - f = open('/home/goffi/tmp/CS_principale.html','r') + """f = open('/home/goffi/tmp/CS_principale.html','r') html = f.read() - self.__connectionCB(html, id, profile) + self.__connectionCB(html, id, profile)""" - """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']) + 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']) d.addCallback(self.__connectionCB, id, profile) - d.addErrback(self.erroCB, id)""" + d.addErrback(self.erroCB, id) #self.host.bridge.actionResult("SUPPRESS", id, {}) @@ -159,10 +161,18 @@ unread_CR_mess = data['unread_CR_messages'] friends_list = data['friends'].keys() friends_list.sort() - interface = XMLUI('window','tabs') + interface = XMLUI('window','tabs', title='CouchSurfing management') interface.addCategory(_("Messages"), "vertical") - 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") % {'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 ''}) - interface.addList(friends_list, 'friends') + 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 ''}) + interface.addList(friends_list, 'friends', style=['multi']) + interface.changeLayout('pairs') + interface.addLabel(_("Subject")) + interface.addString('subject') + interface.changeLayout('vertical') + interface.addLabel(_("Message")) + interface.addText("(use %name% for contact name and %firstname% for guessed first name)") + interface.addTextBox('message') + interface.addButton('plugin_CS_sendMessage', 'sendMessage', _('send'), fields_back=['friends','subject','message']) interface.addCategory(_("Events"), "vertical") interface.addCategory(_("Couch search"), "vertical") return interface.toXml() @@ -173,7 +183,7 @@ def __friendsPageCB(self, html, id, profile): """Called when the friends list page has been received""" self.savePage('friends',html) - soup = BeautifulSoup(html.replace('"formtable width="400','"formtable" width="400"')) + soup = BeautifulSoup(html.replace('"formtable width="400','"formtable" width="400"')) #CS html fix #TODO: report the bug to CS dev team friends = self.data[profile]['friends'] for _tr in soup.findAll('tr', {'class':re.compile("^msgRow*")}): #we parse the row with friends infos _nobr = _tr.find('nobr') #contain the friend name @@ -196,8 +206,61 @@ self.host.bridge.actionResult("XMLUI", id, {"type":"window", "xml":self.__buildUI(self.data[profile])}) #and save the data self.host.memory.setPrivate('plugin_cs_data', self.data) - + + def __sendMessage(self, answer, subject, message, data, recipient_list, id, profile): + """Send actually the message + @param subject: subject of the message + @param message: body of the message + @param data: data of the profile + @param recipient_list: list of friends names, names are removed once message is sent + @param id: id of the action + @param profile: profile who launched the action + """ + if answer: + if not 'Here is a copy of the email that was sent' in answer: + error(_("INTERNAL ERROR: no confirmation of message sent by CS, maybe the site has been modified ?")) + #TODO: throw a warning to the frontend, saying that maybe the message has not been sent and to contact dev of this plugin + #debug(_('HTML answer: %s') % answer) + if recipient_list: + recipient = recipient_list.pop() + try: + friend_id = data['friends'][recipient]['id'] + except KeyError: + error('INTERNAL ERROR: unknown friend') + return #send an error to the frontend + mess = message.replace('%name%',recipient).replace('%firstname%',recipient.split(' ')[0]) + info(_('Sending message to %s') % recipient) + debug(_("\nsubject: %(subject)s\nmessage: \n---\n%(message)s\n---\n\n") % {'subject':subject,'message':mess}) + 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]':''}) + 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']) + d.addCallback(self.__sendMessage, subject, message, data, recipient_list, id, profile) + d.addErrback(self.erroCB, id) + else: + interface = XMLUI('window', title=_('Message sent')) #TODO: create particular actionResult for alerts ? + interface.addText(_('The message has been sent to every recipients')) + self.host.bridge.actionResult("XMLUI", id, {"type":"window", "xml":interface.toXml()}) + + def sendMessage(self, id, data, profile): + """Called to send a message to a friend + @param data: dict with the following keys: + friend: name of the recipient + subject: subject of the message + message: body of the message, with the following special keywords: + - %name%: name of the friend + - %firstname%: guessed first name of the friend (currently the first part of the name) + """ + if not data['friends']: + message_data={"reason": "bad data", "message":_(u"There is not recipient selected for this message !")} + self.host.bridge.actionResult("ERROR", id, message_data) + return + friends = data['friends'].split('\t') + subject = data['subject'] + message = data['message'] + print "send message \o/ :) :) :)" + info(_("sending message to %(friends)s with subject [%(subject)s]" % {'friends':friends, 'subject':subject})) + self.__sendMessage(None, subject, message, self.data[profile], friends, id, profile) +