# -*- Mode: Python; tab-width: 4 -*-
#	$Id: asynfing.py,v 1.1 1996/02/07 01:29:31 rushing Exp $
#	Author: Sam Rushing <rushing@nightmare.com>

import socket
import asyncore
import string

# simple demo of the asyncore dispatcher class.

class finger_client (asyncore.dispatcher_with_send):
	def __init__ (self, account, collect_fun, done_fun, long=1):
		self.name, self.host = tuple(string.splitfields (account, '@'))
		self.collect_fun = collect_fun
		self.done_fun = done_fun
		self.data = ''
		self.long = long
		if self.name == '':
			self.long = 0
		self.create_socket (socket.AF_INET, socket.SOCK_STREAM)
		asyncore.dispatcher.__init__ (self)

	def go (self):
		self.connect (self.host, 79)

	# once connected, send the account name
	def handle_connect (self):
		self.log ('connected')
		if self.long:
			# this requests 'long' output.
			self.send ('/w %s\r\n' % self.name)
		else:
			self.send ('%s\r\n' % self.name)

	# the other side closed, we're done.
	def handle_close (self):
		self.done_fun (1)
		self.close()

	# collect some more finger server output.
	def handle_read (self):
		more = self.recv(512)
		if more:
			self.collect_fun (more)

def demo_collect_fun (data):
	print data

def demo_done_fun (result):
	pass

def bogus_log_fun (*args):
	pass

def demo (who='asynfingdemo@squirl.nightmare.com'):
	f = finger_client (who, demo_collect_fun, demo_done_fun, long)
	f.log = bogus_log_fun
	f.go()
	asyncore.loop()

if __name__ == '__main__':
	import sys
	demo (sys.argv[1], 1)
