changeset 440:48277946348b

plugin CS: use of PersistentBinaryData to store profile's information, and clean up on profile disconnection. /!\ The plugin doesn't work anymore, probably due to some change on CS website.
author Goffi <goffi@goffi.org>
date Sat, 03 Dec 2011 19:52:04 +0100
parents 866dbb0d7d87
children a6640bb8ee95
files src/plugins/plugin_misc_cs.py
diffstat 1 files changed, 14 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/src/plugins/plugin_misc_cs.py	Sat Dec 03 15:50:09 2011 +0100
+++ b/src/plugins/plugin_misc_cs.py	Sat Dec 03 19:52:04 2011 +0100
@@ -26,6 +26,7 @@
 from twisted.words.protocols.jabber import error as jab_error
 from twisted.words.protocols.jabber.xmlstream import IQ
 from twisted.web.client import getPage
+from sat.memory.persistent import PersistentBinaryDict
 import os.path
 import pdb
 
@@ -73,10 +74,21 @@
         host.memory.importParams(CS_Plugin.params)
         #menu
         host.importMenu(_("Plugin"), "CouchSurfing", self.menuSelected, help_string = _("Launch CoushSurfing management interface"))
-        self.data=self.host.memory.getPrivate('plugin_cs_data') or {} #TODO: delete cookies/data after a while
+        self.data={} #TODO: delete cookies/data after a while
         self.host.registerGeneralCB("plugin_CS_sendMessage", self.sendMessage)
         self.host.registerGeneralCB("plugin_CS_showUnreadMessages", self.showUnreadMessages)
 
+    def profileConnected(self, profile):
+        self.data[profile] = PersistentBinaryDict("plugin_CS", profile)
+        def dataLoaded(ignore):
+            if not self.data[profile]:
+                self.data[profile] = {'cookies':{}}
+                
+        self.data[profile].load().addCallback(dataLoaded)
+
+    def profileDisconnected(self, profile):
+        del self.data[profile]
+
     def erroCB(self, e, id):
         """Called when something is going wrong when contacting CS website"""
         #pdb.set_trace()
@@ -94,10 +106,7 @@
 
         post_data = urllib.urlencode({'auth_login[un]':login,'auth_login[pw]':password,'auth_login[action]':'Login...'}) 
         
-        if not self.data.has_key(profile):
-            self.data[profile] = {'cookies':{}}
-        else:
-            self.data[profile]['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)
@@ -194,8 +203,6 @@
         else:
             #no, we show the result
             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