##
## config.py -- Configuration parsing.
##
## Copyleft 1997,1998 Teemu Kalvas <chery@s2.org>
##

## +method cmd_chars
## +method verbose
## +method motd
## +method login
## +method realname
## +method want_servers
## +method default_key
## +method known_key
## +method init          (p)  persistent storage object
## +method do            (p)  persistent storage object

import imp
import os
import pwd
import regex
import string
import sys

import idea

cmd_chars = '/'
verbose = 0
motd = 1
emacs_wrap = 0

_pw = pwd.getpwuid(os.getuid())
login = _pw[0]
realname = _pw[4]
_f = string.find(realname, ',')
if _f <> -1:
    realname = realname[:_f]

if os.environ.has_key('IRCSERVER'):
    server = os.environ['IRCSERVER']
else:
    server = 'irc.funet.fi'
if os.environ.has_key('IRCPORT'):
    port = os.environ['IRCPORT']
else:
    port = 6667
if os.environ.has_key('IRCNICK'):
    nick = os.environ['IRCNICK']
else:
    nick = login

def init(p):
    global ignore

    ignore = regex.compile('@')

def do(p):
    pass

def autoop(addr, chan):
    return 0

default_key = []

## Read user's configuration file and transfer entities from it to this
## module's namespace.

_userconfpath = os.path.expanduser('~/.clircrc.py')
if os.path.exists(_userconfpath):
    _userconffile = open(_userconfpath, 'r')
    _userconf = imp.load_source('userconf', _userconfpath, _userconffile)
    _config = sys.modules['config']
    for _attr in _userconf.__dict__.keys():
	if _attr[0] <> '_':
	    _config.__dict__[_attr] = _userconf.__dict__[_attr]

_d = locals()
if not _d.has_key('idea_default_version'):
    idea_default_version = 3
if not _d.has_key('want_servers'):
    want_servers = [(server, port, [nick], [])]

_dk = default_key
default_key = []
known_key = {}
for _k in _dk:
    _fp = idea.key_fingerprint(_k[1], 1)
    if not known_key.has_key(_fp):
	if type(_k[0]) == StringType:
            _tuple = regex.compile(_k[0]), \
                    idea.build_encryption_key(_k[1], 1), \
                    idea.build_encryption_key(_k[1], 2), \
                    idea.build_encryption_key(_k[1], 3)
            if len(_k) > 2:
                default_key.append(_tuple + (_k[2],))
            else:
                default_key.append(_tuple)
	else:
	    default_key.append(_k)
	known_key[_fp] = idea.build_decryption_key(_k[1], 1)
        known_key[idea.key_fingerprint(_k[1], 2)] = \
            idea.build_decryption_key(_k[1], 2)
        known_key[idea.key_fingerprint(_k[1], 3)] = \
            idea.build_decryption_key(_k[1], 3)

low_table = string.maketrans('ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]',
			     'abcdefghijklmnopqrstuvwxyz{|}')

def low(str):
    return string.translate(str, low_table)

version = 'clirc 0.0.8'

## End. ##
