##
## user.py -- IRC server interface.
##
## Copyleft 1997 Teemu Kalvas <chery@s2.org>
##

## +class User              ()
##  -method read            (p)     persistent storage object
##  -method handle_XXX      (p)     persistent storage object
##  -method handle_cmd_XXX  (p,     persistent storage object
##                           tail)  arguments to the command
##  -method sock
##  -method out

import curses
import string
import sys

import config

class User:
    def __init__(self):
	self.sock = sys.stdin
	self.out = []

    def read(self, p):
	ch = p.screen.window.getch()
	if (ch >= 32 and ch < 127) or (ch >= 160 and ch < 256):
	    p.screen.edit_field.insert(ch)
	else:
	    handler = 'handle_none'
	    if ch >= curses.KEY_MIN and ch <= curses.KEY_MAX:
		handler = 'handle_' + curses.keyname(ch)
            elif ch > 0 and ch < 27:
                handler = 'handle_ctrl_' + chr(ch + 96)
            elif ch >= 27 and ch < 32:
                handler = 'handle_ctrl_' + \
                          ['leftbracket', 'backslash', 'rightbracket', 'caret',
                           'underscore'][ch - 27]
	    if config.__dict__.has_key(handler):
		config.__dict__[handler](self, p)
            elif User.__dict__.has_key(handler):
                User.__dict__[handler](self, p)

    def handle_key_backspace(self, p):
	p.screen.edit_field.del_char_left()

    def handle_key_end(self, p):
	self.handle_ctrl_e(p)

    def handle_key_enter(self, p):
	self.handle_ctrl_m(p)

    def handle_key_home(self, p):
	self.handle_ctrl_a(p)

    def handle_key_left(self, p):
	self.handle_ctrl_b(p)

    def handle_key_npage(self, p):
	self.handle_ctrl_n(p)

    def handle_key_ppage(self, p):
	self.handle_ctrl_p(p)

    def handle_key_right(self, p):
	self.handle_ctrl_f(p)

    def handle_ctrl_a(self, p):
	p.screen.edit_field.line_beg()

    def handle_ctrl_b(self, p):
	p.screen.edit_field.char_left()

    def handle_ctrl_d(self, p):
	p.screen.edit_field.del_char_right()

    def handle_ctrl_e(self, p):
	p.screen.edit_field.line_end()

    def handle_ctrl_f(self, p):
	p.screen.edit_field.char_right()

    def handle_ctrl_k(self, p):
	p.screen.edit_field.del_line_end()

    def handle_ctrl_l(self, p):
	p.screen.redraw()

    def handle_ctrl_m(self, p):
	screen = p.screen
	line = screen.edit_field.get_string()
	if len(line) > 0:
	    if line[0] not in config.cmd_chars:
		current = screen.current
		current.server.out.append('PRIVMSG ' + current.name + ' :' +
					  line)
		current.log('<' + current.server.nick + '> ' + line)
	    else:
		f = string.find(line, ' ')
		if f <> -1:
		    cmd, tail = line[1:f], line[f + 1:]
		else:
		    cmd, tail = line[1:], ''
		handler = 'handle_cmd_' + cmd
		if config.__dict__.has_key(handler):
		    config.__dict__[handler](self, p, tail)
		elif User.__dict__.has_key(handler):
		    User.__dict__[handler](self, p, tail)
	screen.edit_field.reset()

    def handle_ctrl_n(self, p):
	p.screen.current.page_down()

    def handle_ctrl_p(self, p):
	p.screen.current.page_up()

    def handle_ctrl_u(self, p):
	p.screen.edit_field.reset()

    def handle_ctrl_underscore(self, p):
	p.screen.prev()

    def handle_ctrl_backslash(self, p):
	p.screen.next()

    def handle_cmd_join(self, p, tail):
	p.screen.current.server.out.append('JOIN ' + tail)

    def handle_cmd_quit(self, p, tail):
	for server in p.servers:
	    server.out.append('QUIT :' + tail)
	p.quit = 1

    def handle_cmd_quote(self, p, tail):
	p.screen.current.server.out.append(tail)

    def handle_cmd_reload(self, p, tail):
	p.reload = 1

    def handle_cmd_state(self, p, tail):
	config.debug(p.servers[-1].channels)

## End. ##
