view plugins/plugin_misc_cs.py @ 101:783e9d6980ec

Couchsurfing plugin: first draft SàT core: adding additionnal menu method bridge: new methods getMenus, getMenuHelp and callMenu wix: new menu are added on startup CS plugin: login on CS
author Goffi <goffi@goffi.org>
date Sat, 19 Jun 2010 17:15:30 +0800
parents
children 94011f553cd0
line wrap: on
line source

#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
SAT plugin for managing xep-0045
Copyright (C) 2009, 2010  Jérôme Poisson (goffi@goffi.org)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""

from logging import debug, info, warning, error
from twisted.words.xish import domish
from twisted.internet import protocol, defer, threads, reactor
from twisted.words.protocols.jabber import client, jid, xmlstream
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 twisted.web.http_headers import Headers
import os.path
import pdb
import random

from zope.interface import implements

from wokkel import disco, iwokkel, data_form
from tools.xml_tools import XMLTools
#from twisted.web.iweb import IBodyProducer
import urllib




PLUGIN_INFO = {
"name": "CouchSurfing plugin",
"import_name": "CS",
"type": "Misc",
"protocols": [],
"dependencies": [],
"main": "CS_Plugin",
"handler": "no",
"description": _(u"""This plugin allow to manage your CouchSurfing account throught your SàT frontend""")
}

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'

class CS_Plugin():

    params = """
    <params>
    <individual>
    <category name="CouchSurfing">
        <param name="Login" type="string" />
        <param name="Password" type="password" />
    </category>
    </individual>
    </params>
    """

    def __init__(self, host):
        info(_("Plugin CS initialization"))
        self.host = host
        #parameters
        host.memory.importParams(CS_Plugin.params)
        #menu
        host.importMenu(_("Plugin"), "CouchSurfing", self.menuSelected, help_string = _("Launch CoushSurfing mangement interface"))
        self.session_cookies={} #TODO: delete cookies after a while

    def menuSelected(self, id, profile):
        """Called when the couchsurfing menu item is selected"""
        login = self.host.memory.getParamA("Login", "CouchSurfing", profile_key=profile)
        password = self.host.memory.getParamA("Password", "CouchSurfing", profile_key=profile)
        if not login or not password:
            message_data={"reason": "uncomplete", "message":_(u"You have to fill your CouchSurfing login & password in parameters before using this interface")}
            self.host.bridge.actionResult("ERROR", id, message_data)
            return

        post_data = urllib.urlencode({'auth_login[un]':login,'auth_login[pw]':password,'auth_login[action]':'Login...'}) 
        
        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=cookies)
        #d = getPage('file:///home/goffi/tmp/CS_principale.html', method='POST', postdata=post_data, headers={'Content-Type':'application/x-www-form-urlencoded'} , agent=AGENT, cookies=cookies)
        

        def connectionCB(html):
            print 'Response received'

            d = getPage('http://www.couchsurfing.org/messages.html?message_status=inbox', agent=AGENT, cookies=cookies)
            #d = getPage('file:///home/goffi/tmp/CS_inbox.html', agent=AGENT, cookies=cookies)
            def toto(html):
                print "cookies:",cookies
                pdb.set_trace()
            d.addBoth(toto)
        d.addCallback(connectionCB)


        self.host.bridge.actionResult("SUPPRESS", id, {})