logo

scripts

A bunch of scripts, some to be moved to their own repository
commit: f0496aa26084bace48f37aba272b504bf018262d
parent: 9d5ffa5591f02bfd79d59c602d8cbf562fb53b41
Author: lanodan <lanodan.delta@free.fr>
Date:   Sun, 13 Jul 2014 18:01:54 +0200

Regex and data input change

Diffstat:

MIRCBot/IRCBot.py35+++++++++++++++++------------------
1 file changed, 17 insertions(+), 18 deletions(-)

diff --git a/IRCBot/IRCBot.py b/IRCBot/IRCBot.py @@ -1,16 +1,16 @@ #!/usr/bin/env python -#@Author: Haelwenn Monnier (lanodan) <haelwenn.monnier@gmail.com> -#@License: CC-BY-SA +__Author__ = "Haelwenn Monnier (lanodan) <haelwenn.monnier@gmail.com>" +__License__ = "CC-BY-SA" import socket, sys, re, time, urllib, subprocess, ConfigParser, os tetris=0 -def get_element(textInput, elem): - idx1=textInput.find('<'+elem+'>') - idx2=textInput.find('</'+elem+'>') - return textInput[idx1+len('<'+elem+'>'):idx2].strip() +def get_element(dataInput, elem): + idx1=dataInput.find('<'+elem+'>') + idx2=dataInput.find('</'+elem+'>') + return dataInput[idx1+len('<'+elem+'>'):idx2].strip() # Output to the channel and console def printIrc(ircout): @@ -24,8 +24,7 @@ def log(line): log.write(line+'\n') log.close() -httpRegex = re.compile('https?://+') # http(s) regex -userRegex = re.compile(':[\w\d]') +urlRegex = re.compile('[hf]t+ps?://') # Load the config file (config.ini) config = ConfigParser.ConfigParser() @@ -53,31 +52,31 @@ printIrc(welcomeMsg) # Make a while loop to alway parse input while 1: - text=irc.recv(2048) # text is the socket input (I use text because input is already taken) - print text # print the input for debugging purpose + data=irc.recv(2048) # text is the socket input (I use text because input is already taken) + text=data.lower() # text is the socket input (I use text because input is already taken) + print data # print the input for debugging purpose # Below is a bunch of conditions to see if something the bot can deal with is found - if text.find('PING') != -1: + if text.find('ping') != -1: irc.send('PONG ' + text.split() [1] + '\n') # Return PONG back (sort of "I'm alive") if text.find(':!hi') !=-1: - t = text.split(':!hi') # split the message received at the :!hi part + t = data.split(':!hi') # split the message received at the :!hi part to = t[1].strip() # take the string after !hi printIrc('Ohayo-nyan '+str(to)+' ! [http://i.imgur.com/vzYFOkp.jpg]') # Say something crazy #TODO: Custom !hi is the config if text.find(':!say') != -1: - t = text.split(':!say') + t = data.split(':!say') out = t[1].strip() printIrc(str(out)) if text.find(':!action') != -1: - t = text.split(':!action') + t = data.split(':!action') out = t[1].strip() printIrc('\x01ACTION '+out+'\x01') # equivalent of /me - if httpRegex.search(text) is not None: # Use httpRegex on input - parse = re.findall('https?://[^\{\}\|\\\^\~\[\]\"\'\`\(\)\<\>\ ]+', text) # get only the link in parse + if urlRegex.search(text) is not None: # Use httpRegex on input + parse = re.findall('[HhFf][Tt]{1,2}[Pp][Ss]?://[^\{\}\|\\\^\~\[\]\"\'\`\(\)\<\>\ ]+', data) # parse URL try: url = str(parse[0]).rstrip() # Take the parsed link - print 'debug: '+url if (len(url) > 8) : # I assume a link is more than 8 characters long try: get = urllib.urlopen(url) # Open the link @@ -107,7 +106,7 @@ while 1: # Output the link to find the source code if text.find(':!source') != -1: printIrc(source) - if text.find('KICK '+channel+' '+nick) != -1: + if text.find('kick '+channel+' '+nick) != -1: time.sleep(5) irc.send('JOIN '+ channel +'\n') #re-join the chan printIrc(welcomeMsg) # Say hello