All pastes #1476957 Raw Edit

IRC_Plugin.py

public python v1 · immutable
#1476957 ·published 2009-06-28 00:01 UTC
rendered paste body
# -*- coding: cp1252 -*-##########################################################################################################   This is a Plugin to get the IRC Channel Access Lists a site for Clan Checker for Soul Wars Unleashed.#       written by Aesus1##   Note that this script connects to an IRC server and requires internet connectivity##########################################################################################################main_log = "./Logs/Main_Log.txt"irc_log = "./Logs/IRC_Log.txt"## Function: _Log ======================================================================================================#   syntax: _Log(log_message, logFile = main_log)#         : log_message - The message plain text string to be written to the log file, date and# time added automatically#         : logFile - Optional file location - Default: main_log#   success: writes to the appropriate log#   failure: crashes the script ###=====================================================================================================================def _Log(log_message, logFile = main_log):    import os        if not os.path.isdir("./Logs/"):        os.mkdir("./Logs/")    import time    time_str = time.strftime("%m/%d/%y %I:%M:%S%p", time.localtime())    log_message = time_str + " - " + log_message        f = file(logFile, "a")    f.write(log_message + "\r\n")    f.close()#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#       IRC Funtions#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%###FUNCTION# ====================================================================================================================# Name...........: Make_IRC_Access_Lists# Description ...: Gets the IRC access lists and writes to a file# Syntax.........: Make_IRC_Access_Lists(Chan, sock)# Parameters ....: Chan - The chanel to get the list for, this includes the numbersign#                : sock - The active socket from the main IRC function# Return values .: Success - Returns a 1#                : Failure - Returns a -1##===============================================================================================================================def Make_IRC_Access_Lists(Chan, sock):    import os    import socket     import time    if not os.path.isdir("./Chan/"):        os.mkdir("./Chan/")    sock.send("privmsg chanserv access " + Chan + " list" +"\r\n")    temp_str = ""    x = -1    while 1:        time.sleep(1)        text=sock.recv(566566)        if not text:            break                if text != "":            _Log(text, irc_log)            print text            temp_str += text            if text.find(":End of access list.") != -1:                  ChanFile = file("./Chan/" + Chan + ".txt", 'w')                ChanFile.writelines(text + "\r\n")                ChanFile.close()                x = 1                break    return x###FUNCTION# ====================================================================================================================# Name...........: Get_IRC_Acces_List# Description ...: Gets the IRC access lists and writes to a log file# Syntax.........: Get_IRC_Access_Lists(IRC_Chan_List, IRC_Unames, IRC_Pword, IRC_Server, Port)# Parameters ....: IRC_Chan_List#                : IRC_Unames - a single name or list of names to use (must be in the same group or have the same password)#                : IRC_Pword - password for the irc nick(s)#                : IRC_Server - the actual server to connect to such as: irc.swiftirc.net#                : Port = 6667 - optional - The port to connect to, default is 6667# Return values .: Success - Returns a 1#                  Failure - Returns a 0##===============================================================================================================================def Get_IRC_Access_Lists(IRC_Chan_List, IRC_Unames, IRC_Pword, IRC_Server, Port = 6667):    import os    import socket     import time     import httplib    import types    from datetime import date    from AutoItFunctions import IsArray, UBound #StringTrimLeft, StringTrimRight, _ArraySearch    irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)     if type(IRC_Server) == types.ListType:        ServerNum = 0        SeverCount = len(IRC_Server)        Server = IRC_Server[0]    else:        ServerNum = 0        SeverCount = 1        Server = IRC_Server            if type(IRC_Unames) == types.ListType:        NickNum = 0        NickCount = len(IRC_Unames)        IRC_Nick = IRC_Unames[0]        else:        NickNum = 0        NickCount = 1        IRC_Nick = IRC_Unames    if type(IRC_Chan_List) == types.ListType:        ChanNum = 0        ChanCount = len(IRC_Chan_List)        IRC_Chan = IRC_Chan_List[0]    else:        ChanNum = 0        ChanCount = 1        IRC_Chan = IRC_Chan_List    irc.connect((Server, int(Port)))     user_id = 0 # place holder to know if the user is id'd or not    if not os.path.isdir("./Chan/"):        os.mkdir("./Chan/")             irc.send('USER py IRC_Server servname : Python Bot\r\n')     irc.send('NICK '+ str(IRC_Nick) +'\r\n')                 y = -1 # Create a return value    while 1:        time.sleep(1) #this is a 1 second delay needed to get the access lists without being broken up        #text=irc.recv(2040) # too low        # text=irc.recv(8192) # better but still too low        text=irc.recv(566566)# no idea what to set this value at, but this setting works        if not text:            break                if text != "":            print text            _Log(text, irc_log)                        #send ping response            if text.find('PING') != -1:                 irc.send('PONG ' + text.split() [1] + '\r\n')                        #This nickname is registered and protected.            if text.find('+ix') != -1:                irc.send('NS IDENTIFY '+ str(IRC_Pword) +'\r\n')            # error nick already in use            if text.find('433') != -1:                if NickCount > 1: # if user has more than 1 nick in the group change nick                    if NickCount > NickNum:                        NickNum += 1                        irc.send('NICK '+ str(IRC_Unames[NickNum]) +'\r\n')                    else: # there is no other user name left to use so send the ghost command                        irc.send("privmsg nickserv ghosts " + str(IRC_Unames[NickNum]) + " " + IRC_Pword +'\r\n')                else: # there is no otehr user name to use so send the ghost command                    irc.send("privmsg nickserv ghosts " + str(IRC_Unames) + " " + IRC_Pword +'\r\n')            # catch the password excepted response and send the first channel list request                    if text.find('Password accepted') != -1:                user_id = 1                if ChanNum < ChanCount:                    if ChanCount >= 1:                        for IRC_Chan in IRC_Chan_List:                            x = Make_IRC_Access_Lists(IRC_Chan, irc)                            ChanNum += 1                        break                         else:                        IRC_Chan = IRC_Chan_List                        x = Make_IRC_Access_Lists(IRC_Chan, irc)                        break    irc.close()    return x###FUNCTION# ====================================================================================================================# Name...........: ChanListCleanUp# Description ...: Calls the required functions to make the IRC access lists# Syntax.........: Get_Make_IRC_Acces_List()# Parameters ....: None# Return values .: Success - Returns a 1#                  Failure - Returns a 0# Author ........: Aesus1# Modified.......:# Remarks .......:# Related .......:# Link ..........:# Example .......: No##===============================================================================================================================def ChanListCleanUp(Channel):    import os    #import types    ChannelFile = "./Chan/" + Channel + ".txt"    if not os.path.isfile(ChannelFile):        return - 1    else:        f = open(ChannelFile)        access_list = f.read()        print access_list        f.close        access_list1 = access_list.split("\r\n")        if len(access_list1) == 1:            access_list = access_list.split("\n")        else:            access_list = access_list1               access_list2 = access_list[2:-3]        z = []        a = ""        for x in access_list2:            y = x.split()            if len(y) > 1:                irc_nick = y[len(y)- 1]                access_lvl = y[len(y)- 2]                a = (irc_nick, access_lvl)                z.append(a)    return z