view libgaim/gaim-url-handler @ 14520:82b59abcaee4

[gaim-migrate @ 17240] SF Patch #1492561 from Lars T. Mikkelsen "This patch adds gaim-url-handler, a URL handler for most protocols in Gaim. The gaim-url-handler is similar to (and based on) gaim-remote, however, it uses the native URL format of each protocol. Furthermore, the patch includes a GConf schema, which allows Gnome applications to use the URL handler." With this and gaim-send/gaim-send-async, we should be able to drop gaim-remote. Please let me know if you object, or I'll remove gaim-remote shortly. For the record, I know this doesn't support 100% of the ChatZilla IRC URL spec, and I haven't investigated the XMPP URL spec beyond a casual first look. We can tweak these things later -- I figured it was important to get the bulk of the code in place. committer: Tailor Script <tailor@pidgin.im>
author Richard Laager <rlaager@wiktel.com>
date Mon, 11 Sep 2006 02:30:59 +0000
parents
children e3a640372b6e
line wrap: on
line source

#!/usr/bin/python

import dbus
import re
import sys
import time
import urllib

obj = dbus.SessionBus().get_object("net.sf.gaim.GaimService", "/net/sf/gaim/GaimObject")
gaim = dbus.Interface(obj, "net.sf.gaim.GaimInterface")

class CheckedObject:
    def __init__(self, obj):
        self.obj = obj

    def __getattr__(self, attr):
        return CheckedAttribute(self, attr)

class CheckedAttribute:
    def __init__(self, cobj, attr):
        self.cobj = cobj
        self.attr = attr

    def __call__(self, *args):
        result = self.cobj.obj.__getattr__(self.attr)(*args)
        if result == 0:
            raise "Error: " + self.attr + " " + str(args) + " returned " + str(result)
        return result

cgaim = CheckedObject(gaim)

def extendlist(list, length, fill):
    if len(list) < length:
        return list + [fill] * (length - len(list))
    else:
        return list

def convert(value):
    try:
        return int(value)
    except:
        return value

def findaccount(protocolname, accountname=""):
    try:
        # prefer connected accounts
        account = cgaim.GaimAccountsFindConnected(accountname, protocolname)
        return account
    except:
        # try to get any account and connect it
        account = cgaim.GaimAccountsFindAny(accountname, protocolname)
        gaim.GaimAccountSetStatusVargs(account, "online", 1)
        gaim.GaimAccountConnect(account)
        return account

def goim(account, screenname, message=None):
    # XXX: 1 == GAIM_CONV_TYPE_IM
    conversation = cgaim.GaimConversationNew(1, account, screenname)
    if message:
        gaim.GaimConvSendConfirm(conversation, message)

def gochat(account, params, message=None):
    connection = cgaim.GaimAccountGetConnection(account)
    gaim.ServJoinChat(connection, params)

    if message != None:
    	for i in range(20):
            # XXX: 2 == GAIM_CONV_TYPE_CHAT
            conversation = gaim.GaimFindConversationWithAccount(2, params.get("channel", params.get("room")), account)
            if conversation:
                gaim.GaimConvSendConfirm(conversation, message)
                break
            else:
                time.sleep(0.5)

def addbuddy(account, screenname, group="", alias=""):
    cgaim.GaimBlistRequestAddBuddy(account, screenname, group, alias)


def aim(uri):
    protocol = "prpl-oscar"
    match = re.match(r"^(aim|icq):([^?]*)(\?(.*))", uri)
    if not match:
        print "Invalid aim URI: %s" % uri
        return

    command = urllib.unquote(match.group(2))
    paramstring = match.group(4)
    params = {}
    if paramstring:
        for param in paramstring.split("&"):
            key, value = extendlist(param.split("=", 1), 2, "")
            params[key] = urllib.unquote(value)
    accountname = params.get("account", "")
    screenname = params.get("screenname", "")

    account = findaccount(protocol, accountname)

    if command == "goim":
        goim(account, screenname, params.get("message"))
    elif command == "gochat":
        gochat(account, params)
    elif command == "addbuddy":
        addbuddy(account, screenname, params.get("group", ""))

def gg(uri):
    protocol = "prpl-gg"
    match = re.match(r"^gg:(.*)", uri)
    if not match:
        print "Invalid gg URI: %s" % uri
        return

    screenname = urllib.unquote(match.group(1))
    account = findaccount(protocol)
    goim(account, screenname)

def icq(uri):
    aim(uri)

def irc(uri):
    protocol = "prpl-irc"
    match = re.match(r"^irc:(//([^/]*)/)?([^?]*)(\?(.*))?", uri)
    if not match:
        print "Invalid irc URI: %s" % uri
        return

    server = urllib.unquote(match.group(2)) or ""
    target = match.group(3) or ""
    query = match.group(5) or ""

    modifiers = {}
    if target:
        for modifier in target.split(",")[1:]:
            modifiers[modifier] = True

    isnick = modifiers.has_key("isnick")

    paramstring = match.group(5)
    params = {}
    if paramstring:
        for param in paramstring.split("&"):
            key, value = extendlist(param.split("=", 1), 2, "")
            params[key] = urllib.unquote(value)

    account = findaccount(protocol)

    if (target != ""):
        if (isnick):
            goim(account, urllib.unquote(target.split(",")[0]), params.get("msg"))
	else:
            channel = urllib.unquote(target.split(",")[0])
            if channel[0] != "#":
                channel = "#" + channel
            gochat(account, {"server": server, "channel": channel, "password": params.get("key", "")}, params.get("msg"))

def msnim(uri):
    protocol = "prpl-msn"
    match = re.match(r"^msnim:([^?]*)(\?(.*))", uri)
    if not match:
        print "Invalid msnim URI: %s" % uri
        return

    command = urllib.unquote(match.group(1))
    paramstring = match.group(3)
    params = {}
    if paramstring:
        for param in paramstring.split("&"):
            key, value = extendlist(param.split("=", 1), 2, "")
            params[key] = urllib.unquote(value)
    screenname = params.get("contact", "")

    account = findaccount(protocol)

    if command == "chat":
        goim(account, screenname)
    elif command == "add":
        addbuddy(account, screenname)

def sip(uri):
    protocol = "prpl-simple"
    match = re.match(r"^sip:(.*)", uri)
    if not match:
        print "Invalid sip URI: %s" % uri
        return

    screenname = urllib.unquote(match.group(1))
    account = findaccount(protocol)
    goim(account, screenname)

def xmpp(uri):
    protocol = "prpl-jabber"
    match = re.match(r"^xmpp:(//([^/?#]*))?(/?([^?#]*))(\?([^;#]*)(;([^#]*))?)?(#(.*))?", uri)
    if not match:
        print "Invalid xmpp URI: %s" % uri
        return

    accountname = urllib.unquote(match.group(2)) or ""
    screenname = urllib.unquote(match.group(4))
    command = urllib.unquote(match.group(6))
    paramstring = match.group(8)
    params = {}
    if paramstring:
        for param in paramstring.split(";"):
            key, value = extendlist(param.split("=", 1), 2, "")
            params[key] = urllib.unquote(value)

    account = findaccount(protocol, accountname)

    if command == "message":
        goim(account, screenname, params.get("body"))
    elif command == "join":
        room, server = screenname.split("@")
        gochat(account, {"room": room, "server": server})
    elif command == "roster":
        addbuddy(account, screenname, params.get("group", ""), params.get("name", ""))
    else:
        goim(account, screenname)

def ymsgr(uri):
    protocol = "prpl-yahoo"
    match = re.match(r"^ymsgr:([^?]*)(\?([^&]*)(&(.*))?)", uri)
    if not match:
        print "Invalid ymsgr URI: %s" % uri
        return

    command = urllib.unquote(match.group(1))
    screenname = urllib.unquote(match.group(3))
    paramstring = match.group(5)
    params = {}
    if paramstring:
        for param in paramstring.split("&"):
            key, value = extendlist(param.split("=", 1), 2, "")
            params[key] = urllib.unquote(value)

    account = findaccount(protocol)

    if command == "sendIM":
        goim(account, screenname, params.get("m"))
    elif command == "chat":
        gochat(account, {"room": screenname})
    elif command == "addfriend":
        addbuddy(account, screenname)


def main(argv=sys.argv):
    if len(argv) != 2:
        print "Usage: %s URI" % argv[0]
        print "Example: %s \"xmpp:romeo@montague.net?message\"" % argv[0]
        return

    uri = argv[1]
    type = uri.split(":")[0]

    if type == "aim":
        aim(uri)
    elif type == "gg":
        gg(uri)
    elif type == "icq":
        icq(uri)
    elif type == "irc":
        irc(uri)
    elif type == "msnim":
        msnim(uri)
    elif type == "sip":
        sip(uri)
    elif type == "xmpp":
        xmpp(uri)
    elif type == "ymsgr":
        ymsgr(uri)
    else:
        print "Unkown protocol: %s" % type

if __name__ == "__main__":
    main()