##
## .clircrc.py for Teemu Kalvas <chery@s2.org>
##

import regex
import string
from types import *
import user

verbose = 1 # give verbose error messages
motd = 0 # do not print the server motd

# the extensive way to specify connections
want_servers = [(
    'irc-2.cs.hut.fi', 6667, # server, port
    ['cliini', 'oerliini', 'seeliini', 'cli', 'ilciin'], # list of nicks
    ['#cokers', '#polonhoitoalue', '#modeemi', '#tampere']), # list of channels
                (
    'hinaja.noitatieto.fi', 6667, # a second server
    ['cliini', 'cli'],
    ['#a'])]

# you could also instead of want_servers just specify
# nick = 'nick'
# server = 'server.address'
# port = port_number

# default encryption/decryption keys for addresses (regex/key -pairs)
default_key = [('.*', 'kala')]

# Subclass from user.User and replace the original with the new version.
# The same method can be used to customize server.Server and the other
# classes too, although these two are probably the ones that are interesting.
class NewUser(user.User):
    handle_ctrl_underscore = user.User.handle_keycmd_prev_window
    # ^_ moves to previous window
    handle_ctrl_backslash = user.User.handle_keycmd_next_window
    # ^\ moves to next window

    # adds a command /isop
    def handle_cmd_isop(user, p, tail):
        if autoop(tail, p.screen.current.name):
            p.screen.current.log('*** ' + tail + ' is auto-opped')
        else:
            p.screen.current.log('*** ' + tail + ' is not auto-opped')

    # adds some keyboard shortcuts (the function keys seem to be broken now)
    def handle_key_f9(user, p):
        for c in 'http://www.s2.org/~chery/':
            p.screen.edit_field.insert(ord(c))

    def handle_key_f10(user, p):
        for c in 'http://www.s2.org/polonhoitoalue/':
            p.screen.edit_field.insert(ord(c))

user.User = NewUser

# This is my rather bizarre regular expression -based auto-op system.
def _gen(obj):
    t = type(obj)
    if t == StringType:
	return obj
    elif t == ListType:
	go = map(_gen, obj)
	return '\(' + reduce(lambda x, y: x + '\|' + y, go[1:], go[0]) + '\)'
    elif t == TupleType:
	go = map(_gen, obj)
	return reduce(lambda x, y: x + y, go[1:], go[0])
    else:
	return ''

def gen(obj):
    return regex.compile(_gen(obj))

ops = {}
ops_all = gen(['.*@tjtk2\.trin\.cam\.ac\.uk',
               'chery@o68c\.erkki\.ton\.tut\.fi',
               ('sandra@', ['simpukka', 'kotilo'], '\.saunalahti\.fi'),
               ('.?sandra@', ['hilavitkutin\.pp\.', 'smtp',
                              'borg\.', ''],
                'sci\.fi')])
ops['#cokers'] = \
    gen([('.*@', [(['.*\.', ''], 'vvf'),
                  (['o68b\.erkki', 'a29c\.otanner', 'f298b\.w3',
                    (['a31b', 'a48b'], '\.mtalo'),
                    (['g156b', 'h167', 'd67'], '\.lanhovi')],
                   '\.ton\.tut')], '\.fi'),
         ([([(['pekangas', 'hali', 'sly', 'sunnu'], '@',
              ['assari', 'lehtori'], '\.cc'),
             (['k153997', 'sunnu', 'c142092'], '.*\.cs')],
            '\.tut'),
           ('jansu@borg\.sci'), '\.fi'])])
ops['#noitatieto'] = gen('.*@.*\.noitatieto\.fi')


def autoop(addr, chan):
    return ops_all.match(addr) > 0 or \
	   (ops.has_key(chan) and ops[chan].match(addr) > 0)

## End. ##
