import re
import socket
import subprocess
import time

from util import hook

socket.setdefaulttimeout(10)  # global setting


#autorejoin channels
@hook.event('KICK')
def rejoin(paraml, conn=None):
    if paraml[1] == conn.nick:
        if paraml[0].lower() in conn.channels:
            conn.join(paraml[0])


#join channels when invited
@hook.event('INVITE')
def invite(paraml, conn=None):
    conn.join(paraml[-1])


@hook.event('004')
def onjoin(paraml, conn=None):
    # identify to services
    nickserv_password = conn.conf.get('nickserv_password', '')
    nickserv_name = conn.conf.get('nickserv_name', 'nickserv')
    nickserv_command = conn.conf.get('nickserv_command', 'IDENTIFY %s')
    if nickserv_password:
        conn.msg(nickserv_name, nickserv_command % nickserv_password)
        time.sleep(1)

    # set mode on self
    mode = conn.conf.get('mode')
    if mode:
        conn.cmd('MODE', [conn.nick, mode])

    # join channels
    for channel in conn.channels:
        conn.join(channel)
        time.sleep(1)  # don't flood JOINs


@hook.regex(r'^\x01VERSION\x01$')
def version(inp, notice=None):
    p = subprocess.Popen(['hg', 'id', '-n'], stdout=subprocess.PIPE)
    stdout, _ = p.communicate()
    p.wait()

    ret = 'r' + stdout[:-2]
    notice('\x01VERSION skybot %s - http://bitbucket.org/Scaevolus/'
           'skybot/\x01' % ret)

@@ -1144,22 +1144,35 @@
 n(%5B'
-h
 g
+it
 ', '
-id', '-n
+describe', '--always
 '%5D, 
@@ -1255,25 +1255,22 @@
 t = 
-'r' + stdout%5B:-2%5D
+stdout.strip()
 %0A   
@@ -1313,31 +1313,23 @@
 p://
-bitbucket.org/Scaevolus
+github.com/rmmh
 /'%0A 

change VERSION string