version = "1.0"
sysinfo_version = "1.0"
__module_name__ = "lemmings IRC Script"
__module_version__ = version
__module_description__ = "Python module for xchat2"

###################################################################
#     Trilliji and FuzzyTheBear's Hacked up Onion Script          #
#                                                                 #
#   Come an join us sometime on irc.freenode.com #lemmings        #
#                                                                 #
#   This script was originally written by Onion under the terms   #
#   of the GNU Public License, v2 or later and Copyleft 2003      #
#   onioN. It was modified and extended by FuzzyTheBear and       #
#   and Trilliji.                                                 #
#                                                                 #
###################################################################
import xchat
import string
import os
import sys
import time

def sysinfo(word, word_el, userdata):
    XCString = ''
    fd_osinfo = os.popen("uname -a","r")
    for data_osinfo in fd_osinfo.readlines():
        temp_osinfo=string.split(data_osinfo)
        info_os=temp_osinfo[0]+" "+temp_osinfo[2]
    fd_osinfo.close()
        
    fd_cpuinfo = os.popen("cat /proc/cpuinfo","r")
    cpu_count=0
    info_cpu = {}
    info_clock = {}
    for data_cpuinfo in fd_cpuinfo.readlines():
        if string.find(data_cpuinfo,'model name') > -1 :
           temp_cpu=string.replace(data_cpuinfo,'\n','')
           temp_cpu=string.replace(temp_cpu,'model name','')
           info_cpu[cpu_count]=string.replace(temp_cpu,': ','')
	if string.find(data_cpuinfo,'cpu MHz') > -1 :
           temp_clock=string.split(string.replace(data_cpuinfo,'\n',''))
           info_clock[cpu_count]=temp_clock[3]
           cpu_count=cpu_count+1
    fd_cpuinfo.close()
       
    fd_uptimeinfo = os.popen("uptime","r")
    for data_uptimeinfo in fd_uptimeinfo.readlines():
        temp_uptimeinfo=string.split(data_uptimeinfo)
        info_uptime=string.replace(temp_uptimeinfo[2],',','')
    fd_osinfo.close()

    fd_meminfo = os.popen("cat /proc/meminfo","r")
    for data_meminfo in fd_meminfo.readlines():
        if string.find(data_meminfo,'MemTotal') > -1 :
           temp_mem1=string.replace(data_meminfo,'\n','')
           info_mem1=string.replace(temp_mem1,'MemTotal:      ','')
        if string.find(data_meminfo,'MemFree') > -1 :
           temp_mem2=string.replace(data_meminfo,'\n','')
           info_mem2=string.replace(temp_mem2,'MemFree:       ','')
    fd_meminfo.close()

    fd_vgainfo = os.popen("cat /proc/pci","r")
    for data_vgainfo in fd_vgainfo.readlines():
        if string.find(data_vgainfo,'VGA') > -1 :
           temp_vga=string.split(data_vgainfo,':')
           info_vga=string.replace(temp_vga[1],'\n','')
           if string.find(data_vgainfo,'Display') > -1 :
              temp_vga=string.replace(data_vgainfo,'\n','')
              info_vga=string.replace(temp_vga,'    ','')
    fd_vgainfo.close()
			
    fd_xinfo = os.popen("xdpyinfo","r")
    for data_xinfo in fd_xinfo.readlines():
        if string.find(data_xinfo,'vendor string') > -1 :
           temp_xvender=string.replace(data_xinfo,'\n','')
           info_xvender=string.replace(temp_xvender,'vendor string:    ','')
        if string.find(data_xinfo,'XFree86 version') > -1 :
           temp_xversion=string.split(string.replace(data_xinfo,'\n',''))
           info_xversion=temp_xversion[2]
        if string.find(data_xinfo,'dimensions') > -1 :
           temp_xresolution=string.replace(data_xinfo,'\n','')
           temp_xresolution=string.replace(temp_xresolution,'  dimensions:    ','')
           info_xresolution=temp_xresolution
        if string.find(data_xinfo,'depth of root window') > -1 :
           temp_xdepth=string.split(string.replace(data_xinfo,'\n',''))
           info_xdepth=temp_xdepth[4]
    fd_xinfo.close()

    info_sound=""
    fd_soundinfo = os.popen("cat /proc/pci","r")
    for data_soundinfo in fd_soundinfo.readlines():
        if string.find(data_soundinfo,'audio') > -1 :
           temp_sound=string.replace(data_soundinfo,'\n','')
           info_sound=string.replace(temp_sound,'    Multimedia audio controller: ','')

    fd_soundinfo = os.popen("cat /proc/isapnp","r")
    for data_soundinfo in fd_soundinfo.readlines():
        if string.find(data_soundinfo,'Audio') > -1 :
           temp_sound=string.replace(data_soundinfo,'\n','')
           info_sound=string.replace(temp_sound,'    Multimedia audio controller: ','')
    fd_soundinfo.close()

    XCString = ('Kernel: %s\x039' % (info_os))
    count=0
    for count in info_cpu :
        XCString = '%s CPU[%s]: %s@%s MHz' % ( XCString, count, info_cpu[count], info_clock[count] ) 
        count=count+1
        XCString = '%s\x0311 Mem/Use: %s/%s' % (XCString, info_mem1, info_mem2)
        XCString = '%s\x038 Vid:%s \x0313XInfo:%s @ %s/%s bit' \
                    % (XCString, info_vga, info_xversion, info_xresolution, info_xdepth )

    if info_sound<>"" :
       XCString = '%s\x0310 Sound:%s' % ( XCString, info_sound )

    return XCString

def sysinfo_cb(word, word_el, userdata):
    XCString = "msg %s %s" % (xchat.get_info('channel'),sysinfo( word, word_el, userdata))
    xchat.command(XCString)
    return xchat.EAT_ALL

#This section has a few lines commented out.Hack to taste,
def crap_cb( word, word_eol, userdata):
    xchat.command("me Points to the amazingly high crap-o-meter's reading.") 
    xchat.command("me        0   2   4   6  \x034 8   10")
    xchat.command("me \x038                            /")
    xchat.command("me \x038                          /")
#   xchat.command("me \x038                        /")
#   xchat.command("me \x038                      /")
    xchat.command("me            \x038crap-o-meter")
    return xchat.EAT_ALL

def bulllow_cb( word, word_eol, userdata):
    xchat.command('me \x038Points \x039out \x038The \x037Amazing \x039Crap-\x038O-\x034Meter \x039reading.')
    xchat.command("msg %s \x039 ` ` 1 ` ` 2 ` ` 3 ` ` 4 \x038 ` ` 5 ` ` 6 ` ` 7 \x034 ` ` 8 ` ` 9 ` ` 10   " % (xchat.get_info('channel')))
    xchat.command("msg %s              \x039,9 |" % (xchat.get_info('channel')))
    xchat.command("msg %s \x039             safe        \x038         normal     \x034         boots" % (xchat.get_info('channel')))       
    return xchat.EAT_ALL

def bullmed_cb( word, word_eol, userdata):
    xchat.command('me \x038Points \x039out \x038The \x037Amazing \x039Crap-\x038O-\x034Meter \x039reading.')
    xchat.command("msg %s \x039 ` ` 1 ` ` 2 ` ` 3 ` ` 4 \x038 ` ` 5 ` ` 6 ` ` 7 \x034 ` ` 8 ` ` 9 ` ` 10   " % (xchat.get_info('channel')))
    xchat.command("msg %s                                         \x038,8 |" % (xchat.get_info('channel')))
    xchat.command("msg %s \x039             safe        \x038         normal     \x034         boots" % (xchat.get_info('channel')))       
    return xchat.EAT_ALL

def bullhigh_cb( word, word_eol, userdata):
    xchat.command('me \x038Points \x039out \x038The \x037Amazing \x039Crap-\x038O-\x034Meter \x039reading.')
    xchat.command("msg %s \x039 ` ` 1 ` ` 2 ` ` 3 ` ` 4 \x038 ` ` 5 ` ` 6 ` ` 7 \x034 ` ` 8 ` ` 9 ` ` 10   " % (xchat.get_info('channel')))
    xchat.command("msg %s                                                                 \x034,4 |" % (xchat.get_info('channel')))
    xchat.command("msg %s \x039             safe        \x038         normal     \x034         boots" % (xchat.get_info('channel')))       
    return xchat.EAT_ALL

def fog_cb( word, word_eol, userdata):
    xchat.command("me Lost in a fog so thick you can cut it in chunks and make igloos with it.")
    return xchat.EAT_ALL

def littlemeter_cb(word, word_el, userdata):
    if len(word) < 2:
       xchat.prnt('Usage:')
       xchat.prnt('    caption> Meter Text')
       xchat.prnt('    level> Location on scale')
    else:
       level = int(word[1])
       skipped = 0
       caption = ''
       for eachword in word:
           if skipped < 2:
              skipped = skipped + 1
           else:
              caption = caption + ' ' + eachword 
       if level > 10:
          caption = caption + ' * Off Scale *'
          level = 10 
       xchat.command('msg %s \x03,3|0    |     |     \x03,8|     |     5     |     \x03,5|     |     |   10'
                     %xchat.get_info('channel'))
       if level < 4:
          color = "\x03,3"
       elif level < 7:
          color = "\x03,8"
       else:
          color = "\x03,5"
       level = level * 6
       startcaption = (60 - len(caption))/2
#       xchat.prnt('Level %d'%level)
#       xchat.prnt('Startcaption:%d'%startcaption)
       captionlocation = 0
       for i in range(60):
           if i == level:
              color = color + '\x03,15'
           if captionlocation < len(caption):
              if i >= startcaption:
#                 xchat.prnt('Character:%s'%caption[captionlocation])
                 color = color + caption[captionlocation]
                 captionlocation = captionlocation + 1
              else:
                 color = color + ' '
           else:
              color = color + ' '
#           xchat.prnt('CaptionLocation:%d Index:%d'%(captionlocation,i))
#           xchat.prnt(color)
       color = 'msg %s %s'%(xchat.get_info('channel'),color)
       xchat.command(color)
       return xchat.EAT_ALL 
 
def tiny_cb(word, word_el, userdata):
    if len(word) < 2:
       xchat.prnt('Usage:')
       xchat.prnt('    caption> Meter Text')
       xchat.prnt('    level> Location on scale')
    else:
       level = int(word[1])
       skipped = 0
       caption = ''
       for eachword in word:
           if skipped < 2:
              skipped = skipped + 1
           else:
              caption = caption + ' ' + eachword 
       if level > 10:
          caption = caption + ' * Off Scale *'
          level = 10 
       if level < 4:
          color = "\x031,3"
       elif level < 7:
          color = "\x031,8"
       else:
          color = "\x031,4"
       level = level * 6
       startcaption = (60 - len(caption))/2
#       xchat.prnt('Level %d'%level)
#       xchat.prnt('Startcaption:%d'%startcaption)
       captionlocation = 0
       for i in range(60):
           if i == level:
              color = color + '\x03,15'
           if captionlocation < len(caption):
              if i >= startcaption:
#                 xchat.prnt('Character:%s'%caption[captionlocation])
                 color = color + caption[captionlocation]
                 captionlocation = captionlocation + 1
              else:
                 color = color + ' '
           else:
              color = color + ' '
#           xchat.prnt('CaptionLocation:%d Index:%d'%(captionlocation,i))
#           xchat.prnt(color)
       color = 'msg %s %s'%(xchat.get_info('channel'),color)
       xchat.command(color)
       return xchat.EAT_ALL      
       
def localinfo_cb(word, word_el, userdata):
    XCString = sysinfo( word, word_el, userdata)
    XCString = string.replace(XCString, "\t", "")
    xchat.prnt(XCString)
    return xchat.EAT_ALL  

def herring_cb(word, word_eol, userdata):
    if len(word) < 2:
       xchat.prnt("Tell me who to wack!")
    else:
       xchat.command("me dumps basket of herrings over %s's head" % (word[1]))
    return xchat.EAT_ALL

def ferret_cb(word, word_eol, userdata):
    if len(word) < 2:
       xchat.prnt("Tell me who to drop!")
    else:
       xchat.command("me drops a rabid ferret down %s's pants" % (word[1]))
    return xchat.EAT_ALL

def mackerel_cb(word, word_eol, userdata):
    if len(word) < 2:
       xchat.prnt("Tell me who to dump on!")
    else:
       xchat.command("me whacks %s over head with a mackerel" % (word[1]))
    return xchat.EAT_ALL

def lemming_cb( word, word_eol, userdata):
    xchat.command( \
    "me \x039Follow me to the sea  \x038:=  \x0311:=  \x034:=  \x037:=  \x036:=  \x0313:=  \x030:= ")
    return xchat.EAT_ALL

def squirrel_cb( word, word_eol, userdata ):
    if len(word) < 2:
       xchat.prnt("Tell me who to slap!")
    else:
       xchat.command("me slaps %s with a wet squirrel" % (word[1]))
    return xchat.EAT_ALL

def greet_cb( word, word_eol, userdata ):
    if len(word) < 2:
       xchat.prnt("Tell me who to greet!")
    else:
       xchat.command("msg %s Welcome to %s %s" % 
         (xchat.get_info('channel'),xchat.get_info('channel'), word[1]))
    return xchat.EAT_ALL

def opnkick_cb( word, word_eol, userdata ):
    if len(word) < 2:
       xchat.prnt('Tell me who to kick!')
    else:
       xchat.command('msg ChanServ op %s' % (xchat.get_info('channel')))
       time.sleep(2)
       xchat.command('kick %s' % ( word[1] ))
       xchat.command('deop %s' % (xchat.get_info('nick')))
    return xchat.EAT_ALL

def newtopic_cb( word, word_eol, userdata ):
    if len(word) < 2:
       xchat.prnt('What should the Topic be?')
    else:
       xchat.command('msg ChanServ op %s' % (xchat.get_info('channel')))
       time.sleep(2)
       skipone=1
       topic = ''
       for eachword in word:
           if skipone:
              skipone=0
           else:
              topic = topic + ' ' + eachword 
       xchat.command('topic %s' % ( topic ))
       xchat.command('deop %s' % (xchat.get_info('nick')))
    return xchat.EAT_ALL

def automsg_cb( word, word_eol, userdata ):
    xchat.prnt(repr(word))
    if len(word) < 2:
       xchat.prnt('What should the message be?')
    else:           
       skipcount = 0
       message = ''
       for eachword in word:
          if skipcount < 3:
             skipcount = skipcount + 1
          else:
             message = message + ' ' + eachword 
       xchat.command('timer -refnum 1 -repeat %s %s me %s' % ( word[1], word[2], message ))
    return xchat.EAT_ALL


def credits_cb(word, word_eol, userdata):
    xchat.prnt('\x0307 Trilliji and FuzzyTheBear\x27s Hacked Up Onion Script')
    xchat.prnt('\x0309 Copyleft 2003 onioN - Under terms of GNU Public License v2 or later')
    xchat.prnt('\x0309 Copyleft 2003 Trilliji - Under terms of GNU Public License v2 or later')
    xchat.prnt('\x0309 Copyleft 2003 FuzzyTheBear - Under terms of GNU Public License v2 or later')
    xchat.prnt('\x0313 Come an visit FuzzyTheBear and Trilliji - irc.freenode.net #lemmings')
    xchat.prnt('\x0313 If there is something you would like let us know') 
    return xchat.EAT_ALL

def tst_cb(word, word_eol, userdata):
    xchat.command("msg %s \x034,2 This is a test" % (xchat.get_info('channel')))
    return xchat.EAT_ALL
 
def reverse_cb(word, word_eol, userdata):
       skipone=1
       topic = ''
       for eachword in word:
           if skipone:
              skipone=0
           else:
              topic = topic + ' ' + eachword

       newtopic = ''
       for i in range( len(topic) ):
           j=len(topic) - i -1 
           newtopic = newtopic + topic[j]
       xchat.command("msg %s %s"%(xchat.get_info('channel'),newtopic))
       return xchat.EAT_ALL

xchat.hook_command("rev",reverse_cb,help="/rev <text>")
xchat.hook_command("crap", crap_cb, help="/crap <No Args>")
xchat.hook_command("bulllow", bulllow_cb, help="/crap <No Args>")
xchat.hook_command("bullmed", bullmed_cb, help="/crap <No Args>")
xchat.hook_command("bullhigh", bullhigh_cb, help="/crap <No Args>")
xchat.hook_command("fog", fog_cb, help="/fog <No Agrs>")
xchat.hook_command("sysinfo", sysinfo_cb, help="/sysinfo <No Args>")
xchat.hook_command("localinfo", localinfo_cb, help="/localinfo <No Args>")
xchat.hook_command("mackerel", mackerel_cb, help="/mackerel <List of folks>")
xchat.hook_command("herring", herring_cb, help="/herring <List of folks>")
xchat.hook_command("ferret", ferret_cb, help="/ferret <List of folks>")
xchat.hook_command("lemming", lemming_cb, help="/lemming <No Args>")
xchat.hook_command("squirrel",squirrel_cb, help="/squirrel <who>")
xchat.hook_command("greet", greet_cb, help="/greet <who>")
xchat.hook_command("opnkick",opnkick_cb, help="/opnkick <who>")
xchat.hook_command("test",tst_cb, help="/test <No Args>")
xchat.hook_command("newtopic",newtopic_cb,help="/newtopic <Topic>")
xchat.hook_command("automsg",automsg_cb,help="/automsg <repeat> <delay> <Message>")
xchat.hook_command("credits",credits_cb,help="/credits <No Args>")
xchat.hook_command("littlemeter",littlemeter_cb,help="/littlemeter <rating> <caption>")
xchat.hook_command("tiny",tiny_cb,help="/tiny <rating> <caption>")

xchat.prnt("lemmings module Version %s load complete! See /credits" % (version))
