annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
101
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
3
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
4 """
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
5 SAT plugin for managing xep-0045
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
6 Copyright (C) 2009, 2010 Jérôme Poisson (goffi@goffi.org)
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
7
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
8 This program is free software: you can redistribute it and/or modify
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
9 it under the terms of the GNU General Public License as published by
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
10 the Free Software Foundation, either version 3 of the License, or
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
11 (at your option) any later version.
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
12
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful,
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
16 GNU General Public License for more details.
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
17
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
20 """
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
21
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from logging import debug, info, warning, error
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from twisted.words.xish import domish
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from twisted.internet import protocol, defer, threads, reactor
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from twisted.words.protocols.jabber import client, jid, xmlstream
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from twisted.words.protocols.jabber import error as jab_error
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from twisted.words.protocols.jabber.xmlstream import IQ
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from twisted.web.client import getPage
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
29 #from twisted.web.http_headers import Headers
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
30 import os.path
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
31 import pdb
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
32 import random
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
33
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
34 from zope.interface import implements
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
35
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
36 from wokkel import disco, iwokkel, data_form
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
37 from tools.xml_tools import XMLTools
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
38 #from twisted.web.iweb import IBodyProducer
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
39 import urllib
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
40
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
41
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
42
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
43
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
44 PLUGIN_INFO = {
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
45 "name": "CouchSurfing plugin",
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
46 "import_name": "CS",
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
47 "type": "Misc",
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
48 "protocols": [],
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
49 "dependencies": [],
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
50 "main": "CS_Plugin",
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
51 "handler": "no",
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
52 "description": _(u"""This plugin allow to manage your CouchSurfing account throught your SàT frontend""")
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
53 }
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
54
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
55 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'
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
56
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
57 class CS_Plugin():
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
58
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
59 params = """
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
60 <params>
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
61 <individual>
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
62 <category name="CouchSurfing">
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
63 <param name="Login" type="string" />
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
64 <param name="Password" type="password" />
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
65 </category>
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
66 </individual>
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
67 </params>
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
68 """
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
69
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
70 def __init__(self, host):
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
71 info(_("Plugin CS initialization"))
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
72 self.host = host
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
73 #parameters
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
74 host.memory.importParams(CS_Plugin.params)
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
75 #menu
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
76 host.importMenu(_("Plugin"), "CouchSurfing", self.menuSelected, help_string = _("Launch CoushSurfing mangement interface"))
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
77 self.session_cookies={} #TODO: delete cookies after a while
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
78
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
79 def menuSelected(self, id, profile):
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
80 """Called when the couchsurfing menu item is selected"""
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
81 login = self.host.memory.getParamA("Login", "CouchSurfing", profile_key=profile)
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
82 password = self.host.memory.getParamA("Password", "CouchSurfing", profile_key=profile)
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
83 if not login or not password:
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
84 message_data={"reason": "uncomplete", "message":_(u"You have to fill your CouchSurfing login & password in parameters before using this interface")}
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
85 self.host.bridge.actionResult("ERROR", id, message_data)
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
86 return
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
87
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
88 post_data = urllib.urlencode({'auth_login[un]':login,'auth_login[pw]':password,'auth_login[action]':'Login...'})
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
89
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
90 cookies = {}
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
91 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)
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
92 #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)
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
93
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
94
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
95 def connectionCB(html):
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
96 print 'Response received'
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
97
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
98 d = getPage('http://www.couchsurfing.org/messages.html?message_status=inbox', agent=AGENT, cookies=cookies)
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
99 #d = getPage('file:///home/goffi/tmp/CS_inbox.html', agent=AGENT, cookies=cookies)
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
100 def toto(html):
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
101 print "cookies:",cookies
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
102 pdb.set_trace()
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
103 d.addBoth(toto)
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
104 d.addCallback(connectionCB)
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
105
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
106
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
107 self.host.bridge.actionResult("SUPPRESS", id, {})
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
108
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
109
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
110