#!/usr/bin/env python #Copyright Brian Brazil 2006 import os import re import sys import shutil chatrc = os.environ["HOME"] + "/.chatrc" configpath = os.environ["HOME"] + "/.chat-tmp-config" configdir = os.environ["HOME"] + "/.chat-tmp-dir" def getDefaultConfig(): return {"nick":os.getenv("USER"), "nickserv_pass":''} def parseConfig(fd): """Reads from the given file descriptor, returns a dict representing the config""" ans = {} linenum = 0 pattern = re.compile("^(\w+)\s*=\s*(.*)$") while True: line = fd.readline() linenum += 1 if "" == line: break; if '\n' == line[-1]: line = line[:-1] #Strip leading whitespace, ignore empty lines line = line.lstrip() if 0 == len(line): continue #Ignore comments if '#' == line[0]: continue data = re.match(pattern,line) if None == data: print "Error: line %i in invalid format" % linenum continue k = data.group(1) if "NICK" == k: ans['nick'] = data.group(2) elif "NICKSERV_PASS" == k: ans['nickserv_pass'] = data.group(2) else: print "Error: Unknown parameter '%s'" % k return ans def validateConfig(config): """Checks that config is reasonable. Returns false otherwise""" flag = True if None == re.compile("^[_a-zA-Z][_a-zA-Z0-9]{,8}$").match(config['nick']): flag = False print "Error: Invalid nickname" return flag def outputIrssiConfig(config): """Writes out an irssi config according to the specified config""" fd = open(configpath,'w') fd.write(""" servers = ( { address = "irc.netsoc.tcd.ie"; chatnet = "intersocs"; port = "6667"; autoconnect = "yes"; } ); chatnets = { intersocs = { type = "IRC"; }; }; channels = ( { name = "#netsoc"; chatnet = "intersocs"; autojoin = "yes"; } ); windows = { 2 = { items = ( { type = "CHANNEL"; chat_type = "IRC"; name = "#netsoc"; tag = "intersocs"; } ); }; 1 = { immortal = "yes"; name = "(status)"; level = "ALL"; }; }; liases = { J = "join"; WJOIN = "join -window"; WQUERY = "query -window"; LEAVE = "part"; BYE = "quit"; EXIT = "quit"; SIGNOFF = "quit"; DESCRIBE = "action"; DATE = "time"; HOST = "userhost"; LAST = "lastlog"; SAY = "msg *"; WI = "whois"; WII = "whois $0 $0"; WW = "whowas"; W = "who"; N = "names"; M = "msg"; T = "topic"; C = "clear"; CL = "clear"; K = "kick"; KB = "kickban"; KN = "knockout"; BANS = "ban"; B = "ban"; MUB = "unban *"; UB = "unban"; IG = "ignore"; UNIG = "unignore"; SB = "scrollback"; UMODE = "mode $N"; WC = "window close"; WN = "window new hide"; SV = "say Irssi $J ($V) - http://irssi.org/"; GOTO = "sb goto"; CHAT = "dcc chat"; RUN = "SCRIPT LOAD"; SBAR = "STATUSBAR"; INVITELIST = "mode $C +I"; Q = "QUERY"; }; default_color= "%r"; formats = { "fe-common/irc" = { action_public = "%0{pubaction $0}%0%w$1"; own_action = "%0{ownaction $0}%w$1"; }; }; settings = { core = { settings_autosave = "no"; """) fd.write('nick = "%s";\n' % config['nick']) fd.write(""" }; }; hilights = ( """) fd.write('{ text = "%s"; nick = "yes"; word = "yes"; fullword = "yes"; },\n' % config['nick']) fd.write("""); """) fd.close() shutil.rmtree(configdir,True) os.mkdir(configdir) fd = open(configdir + '/startup','w') fd.write("script load nickserv\n") if("" != config['nickserv_pass']): fd.write("nickserv addnet intersocs NickServ@hybrid-services.intersocs\n") fd.write("nickserv addnick intersocs %(nick)s %(nickserv_pass)s\n" % config) fd.close() def askUserForConfig(config): """Takes in current config, updates with new values""" print "Enter new value, or press enter to keep current value" print "A space will enter a blank value" nick = raw_input("Nickname (%s): " % config['nick']) if " " == nick: config['nick'] = '' elif "" != nick: config['nick'] = nick if '' == config['nickserv_pass']: val = '' else: val = '' p = raw_input("Nickserv password (%s): " % val) if " " == p: config['nickserv_pass'] = '' elif "" != p: config['nickserv_pass'] = p def writeOutChatrc(config): fd = open(chatrc,'w') fd.write("NICK = %(nick)s\n" % config) fd.write("NICKSERV_PASS = %(nickserv_pass)s\n" % config) if '__main__' == __name__: config = getDefaultConfig() try: config.update(parseConfig(open(chatrc,'r'))) except IOError: print "Warning: Couldn't open open '%s'" % chatrc if(-1 != sys.argv[0].find('chat-setup') or '--setup' in sys.argv[1:]): askUserForConfig(config) if not validateConfig(config): print "Errors occured while trying to validate config" sys.exit(1) writeOutChatrc(config) print "Your new chatrc has been saved. Run 'chat' to try it out" sys.exit(0) #Normal usage - start irssi if not validateConfig(config): print "Errors occured while trying to validate config" print "Check your .chatrc" sys.exit(1) outputIrssiConfig(config) os.execv('/usr/bin/irssi',('chat','--config='+configpath,'--home='+configdir))