# Module:	gtalk
# Date:		30th June 2006
# Author:	James Mills, prologic at shortcircuit dot net dot au

"""Google Talk Plugin

This plugin provides an interface and client to the Google
Talk servers using the XMPP protocol. This plugin enables
communication via Google Talk.

[gtalk]
user = kdb
password = foobar
"""

__ver__ = "0.1"
__author__ = "James Mills, prologic at shortcircuit dot net dot au"

from time import sleep

import xmpp

from circuits.lib.irc import Message
from circuits import listener, Event, Worker

from kdb.plugin import BasePlugin

class GTalk(BasePlugin, Worker):

	"""Google Talk Plugin

	This plugin provides an interface and client to the Google
	Talk servers using the XMPP protocol. This plugin enables
	communication via Google Talk.

	See: commands gtalk
	"""

	def __init__(self, *args, **kwargs):
		super(GTalk, self).__init__(*args, **kwargs)

		self._username = self.env.config.get("gtalk", "username", "kdbbot")
		self._password = self.env.config.get("gtalk", "password", "semaj2891")
		self._name = "kdb"

		self._client = cnx = xmpp.Client("gmail.com", debug=[])

		self.start()

	def cleanup(self):
		self._client.disconnect()
		self.stop()
	
	def sendMsg(self, to, message):
		self._client.send(xmpp.Message(to, message))

	def run(self):
		while self.running:
			if self._client.isConnected():
				if hasattr(self._client, "Process"):
					self._client.Process()
					sleep(0.01)
				else:
					sleep(1)
			else:
				try:
					self._client.connect(server=("gmail.com", 5223))
				except:
					sleep(60)
				self._client.auth(
						self._username,
						self._password,
						self._name)

				self._client.RegisterHandler(
						"message", self.messageHandler)
				self._client.sendInitPresence()
				sleep(1)

	def messageHandler(self, cnx, message):
		text = message.getBody()
		user = message.getFrom()

		if text is not None:
			self.env.log.debug("<%s> %s" % (user, text))

			if " " in text:
				command, args = text.split(" ", 1)
				command = command.upper()
			else:
				command, text = text, ""

			if command == "SUBSCRIBE":
				self._client.Roster.Authorize(user)
				reply = "Authorized."
			else:
				e = Message(str(user), self.bot.getNick(), text)
				r = self.iter(e, "message", self.channel)
				reply = "\n".join([x for x in r if x is not None])

			self.sendMsg(user, reply)

@@ -324,17 +324,17 @@
 __ = %220.
-1
+2
 %22%0A__auth
@@ -807,16 +807,26 @@
 __(self,
+ env, bot,
  *args, 
@@ -866,16 +866,26 @@
 _init__(
+env, bot, 
 *args, *
@@ -1079,14 +1079,8 @@
 nt =
- cnx =
  xmp
@@ -1532,34 +1532,8 @@
 ))%0A%09
-%09%09%09except:%0A%09%09%09%09%09sleep(60)%0A
 %09%09%09%09
@@ -1551,16 +1551,17 @@
 t.auth(%0A
+%09
 %09%09%09%09%09%09se
@@ -1576,24 +1576,25 @@
 name,%0A%09%09%09%09%09%09
+%09
 self._passwo
@@ -1599,24 +1599,25 @@
 word,%0A%09%09%09%09%09%09
+%09
 self._name)%0A
@@ -1616,17 +1616,17 @@
 ._name)%0A
-%0A
+%09
 %09%09%09%09self
@@ -1650,23 +1650,16 @@
 Handler(
-%0A%09%09%09%09%09%09
 %22message
@@ -1682,24 +1682,25 @@
 andler)%0A%09%09%09%09
+%09
 self._client
@@ -1719,16 +1719,61 @@
 sence()%0A
+%09%09%09%09except Exception, error:%0A%09%09%09%09%09sleep(60)%0A%0A
 %09%09%09%09slee
@@ -2008,38 +2008,8 @@
  1)%0A
-%09%09%09%09command = command.upper()%0A
 %09%09%09e
@@ -2043,16 +2043,46 @@
 xt, %22%22%0A%0A
+%09%09%09command = command.upper()%0A%0A
 %09%09%09if co
@@ -2174,24 +2174,53 @@
 .%22%0A%09%09%09else:%0A
+%09%09%09%09text = message.getBody()%0A
 %09%09%09%09e = Mess

Version 0.2: Resturctured connection code. Fixed command handling and automatic authorization of subscription requests.