# Copyright (c) 2007 Arne Brodowski, http://www.arnebrodowski.de # All rights reserved. # # Parts are based on code from http://snipplr.com/view/2584/python-rest/ # written by felipec # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # # * Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # * Redistributions in binary form must reproduce the above # copyright notice, this list of conditions and the following # disclaimer in the documentation and/or other materials provided # with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. __module_name__ = "XChat Twitter Bridge" __module_version__ = "0.2" __module_description__ = "bridge some events to twitter" __module_author__ = "Arne Brodowski" import xchat import httplib import urlparse import urllib import base64 class RConnection: def __init__(self, base_url, username, password): self.base_url = base_url self.username = username self.password = password self.url = urlparse.urlparse(base_url) def request_get(self, resource, args = None): self.request(resource, "get", args) def request_post(self, resource, args = None): self.request(resource, "post", args) def request(self, resource, method = "get", args = None): params = None path = resource headers = {} if args: path += "?" + urllib.urlencode(args) if self.username and self.password: encoded = base64.encodestring("%s:%s" % (self.username, self.password))[:-1] headers["Authorization"] = "Basic %s" % encoded headers["X-Twitter-Client"] = "X-Chat" headers['Host'] = "twitter.com" if(self.url[0] == 'https'): conn = httplib.HTTPSConnection(self.url[1]) else: conn = httplib.HTTPConnection(self.url[1]) req = conn.request(method.upper(), "/" + path, None, headers) r = conn.getresponse() #twitter stuff class TConnection: url = "https://twitter.com" def __init__(self, username = None, password = None): self.conn = RConnection(self.url, username, password) def update(self, message): res = self.conn.request_post("statuses/update.xml", {"status": message, "source": "X-Chat"}) #x-chat specific stuff def action_event(word, word_eol, userdata): c = TConnection("TWITTER-USERNAME", "TWITTER-PASSWORD") c.update(word_eol[1][:140]) return xchat.EAT_NONE xchat.hook_command("me", action_event) print "XChat Twitter Bridge v%s loaded" % __module_version__