Second revision of nemubot with thread for time related events, newyear script,
norme checker script, some commands
This commit is contained in:
commit
c3a7318489
101
nemubot.py
Executable file
101
nemubot.py
Executable file
|
@ -0,0 +1,101 @@
|
||||||
|
#!/usr/bin/python2.7
|
||||||
|
# coding=utf-8
|
||||||
|
|
||||||
|
#import signal
|
||||||
|
import sys
|
||||||
|
import socket
|
||||||
|
import string
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
import thread
|
||||||
|
|
||||||
|
import norme
|
||||||
|
import newyear
|
||||||
|
import ontime
|
||||||
|
|
||||||
|
if len(sys.argv) > 1:
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
|
HOST='192.168.0.242'
|
||||||
|
PORT=2770
|
||||||
|
#HOST='irc.rezosup.org'
|
||||||
|
#PORT=6667
|
||||||
|
NICK='nemubot'
|
||||||
|
IDENT='nemubot'
|
||||||
|
REALNAME='nemubot'
|
||||||
|
OWNER='nemunaire' #The bot owner's nick
|
||||||
|
#CHANLIST='#nemutest'
|
||||||
|
CHANLIST='#42sh #nemutest'
|
||||||
|
readbuffer='' #Here we store all the messages from server
|
||||||
|
|
||||||
|
s = socket.socket( ) #Create the socket
|
||||||
|
s.connect((HOST, PORT)) #Connect to server
|
||||||
|
s.send("PASS %s\r\n" % "McsuapTesbuf")
|
||||||
|
s.send("NICK %s\r\n" % NICK)
|
||||||
|
s.send("USER %s %s bla :%s\r\n" % (IDENT, HOST, REALNAME))
|
||||||
|
#s.send("JOIN %s\r\n" % CHANLIST)
|
||||||
|
|
||||||
|
print("Welcome on Nemubot. I operate on %s. My PID is %i" % (CHANLIST, os.getpid()))
|
||||||
|
|
||||||
|
def parsemsg(msg):
|
||||||
|
complete = msg[1:].split(':',1) #Parse the message into useful data
|
||||||
|
info = complete[0].split(' ')
|
||||||
|
msgpart = complete[1]
|
||||||
|
sender = info[0].split('!')
|
||||||
|
|
||||||
|
if CHANLIST.find(info[2]) != -1 and re.match(".*(norme|coding style).*", msgpart) is not None and re.match(".*(please|give|obtenir|now|plz|stp|svp|s'il (te|vous) pla.t|check).*", msgpart) is not None:
|
||||||
|
norme.launch (s, sender, msgpart)
|
||||||
|
|
||||||
|
elif msgpart[0] == '!' and CHANLIST.find(info[2]) != -1: #Treat all messages starting with '!' as command
|
||||||
|
cmd=msgpart[1:].split(' ')
|
||||||
|
if cmd[0] == 'new-year' or cmd[0] == 'newyear' or cmd[0] == 'ny':
|
||||||
|
newyear.launch (s, info[2], cmd)
|
||||||
|
|
||||||
|
|
||||||
|
elif msgpart[0] == '`' and sender[0] == OWNER and CHANLIST.find(info[2]) != -1: #Treat all messages starting with '`' as command
|
||||||
|
cmd=msgpart[1:].split(' ')
|
||||||
|
if cmd[0]=='op':
|
||||||
|
s.send("MODE %s +o %s\r\n" % (info[2], cmd[1]))
|
||||||
|
if cmd[0]=='deop':
|
||||||
|
s.send("MODE %s -o %s\r\n" % (info[2], cmd[1]))
|
||||||
|
if cmd[0]=='voice':
|
||||||
|
s.send('MODE '+info[2]+' +v '+cmd[1]+'n')
|
||||||
|
if cmd[0]=='devoice':
|
||||||
|
s.send('MODE '+info[2]+' -v '+cmd[1]+'n')
|
||||||
|
if cmd[0]=='restart':
|
||||||
|
print "Restarting thread"
|
||||||
|
thread.start_new_thread(ontime.startThread, (s,CHANLIST))
|
||||||
|
if cmd[0]=='stop':
|
||||||
|
print "Bye!"
|
||||||
|
s.send("PRIVMSG {0} :Bye!\r\n".format(info[2]))
|
||||||
|
sys.exit (0)
|
||||||
|
if cmd[0]=='sys':
|
||||||
|
syscmd(msgpart[1:],info[2])
|
||||||
|
|
||||||
|
if msgpart[0]=='-' and sender[0]==OWNER : #Treat msgs with - as explicit command to send to server
|
||||||
|
cmd=msgpart[1:]
|
||||||
|
#s.send(cmd+'n')
|
||||||
|
print 'cmd='+cmd
|
||||||
|
|
||||||
|
def read():
|
||||||
|
global s, readbuffer
|
||||||
|
while 1:
|
||||||
|
readbuffer = readbuffer + s.recv(1024) #recieve server messages
|
||||||
|
temp = readbuffer.split("\n")
|
||||||
|
readbuffer = temp.pop( )
|
||||||
|
#signal.signal(signal.SIGHUP, onSignal)
|
||||||
|
|
||||||
|
for line in temp:
|
||||||
|
print line
|
||||||
|
line = line.rstrip() #remove trailing 'rn'
|
||||||
|
|
||||||
|
if line.find('PRIVMSG') != -1: #Call a parsing function
|
||||||
|
parsemsg(line)
|
||||||
|
|
||||||
|
line = line.split()
|
||||||
|
|
||||||
|
if(line[0] == 'PING'): #If server pings then pong
|
||||||
|
s.send("PONG %s\r\n" % line[1])
|
||||||
|
|
||||||
|
thread.start_new_thread(ontime.startThread, (s,CHANLIST))
|
||||||
|
read()
|
67
newyear.py
Normal file
67
newyear.py
Normal file
|
@ -0,0 +1,67 @@
|
||||||
|
# coding=utf-8
|
||||||
|
import re
|
||||||
|
import os
|
||||||
|
from datetime import datetime
|
||||||
|
import time
|
||||||
|
|
||||||
|
def launch(s, chan, msgpart):
|
||||||
|
#What is the next year?
|
||||||
|
nyear = datetime.today().year + 1;
|
||||||
|
|
||||||
|
if msgpart != 0 and len(msgpart) > 1:
|
||||||
|
os.environ['TZ'] = msgpart[1]
|
||||||
|
time.tzset()
|
||||||
|
|
||||||
|
sentence_c = "PRIVMSG " + chan + " :"
|
||||||
|
|
||||||
|
#Calculate time before new year
|
||||||
|
if datetime.now() > datetime(nyear, 1, 1, 0, 0, 1):
|
||||||
|
sentence_c += "Nous faisons déjà la fête depuis{0}"
|
||||||
|
delta = datetime.now() - datetime(nyear, 1, 1, 0, 0, 1)
|
||||||
|
|
||||||
|
else:
|
||||||
|
sentence_c += "Il reste{0} avant la nouvelle année"
|
||||||
|
delta = datetime(nyear, 1, 1, 0, 0, 1) - datetime.now()
|
||||||
|
|
||||||
|
sec = delta.seconds
|
||||||
|
hours, remainder = divmod(sec, 3600)
|
||||||
|
minutes, seconds = divmod(remainder, 60)
|
||||||
|
|
||||||
|
sentence = ""
|
||||||
|
force = 0
|
||||||
|
|
||||||
|
if force or delta.days > 0:
|
||||||
|
force = 1
|
||||||
|
sentence += " {0} jour".format(delta.days)
|
||||||
|
|
||||||
|
if delta.days > 1:
|
||||||
|
sentence += "s"
|
||||||
|
sentence += ","
|
||||||
|
|
||||||
|
if force or hours > 0:
|
||||||
|
force = 1
|
||||||
|
sentence += " {0} heure".format(hours)
|
||||||
|
if hours > 1:
|
||||||
|
sentence += "s"
|
||||||
|
sentence += ","
|
||||||
|
|
||||||
|
if force or minutes > 0:
|
||||||
|
force = 1
|
||||||
|
sentence += " {0} minute".format(minutes)
|
||||||
|
if minutes > 1:
|
||||||
|
sentence += "s"
|
||||||
|
sentence += " et"
|
||||||
|
|
||||||
|
if force or seconds > 0:
|
||||||
|
force = 1
|
||||||
|
sentence += " {0} seconde".format(seconds)
|
||||||
|
if seconds > 1:
|
||||||
|
sentence += "s"
|
||||||
|
|
||||||
|
s.send(sentence_c.format(sentence) + "\r\n")
|
||||||
|
|
||||||
|
if msgpart != 0 and len(msgpart) > 1:
|
||||||
|
os.environ['TZ'] = "Europe/Paris"
|
||||||
|
|
||||||
|
def special(s, chan):
|
||||||
|
s.send("PRIVMSG {0} :Bonne année {1} !\r\n".format(chan, datetime.today().year))
|
44
norme.py
Normal file
44
norme.py
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
import re
|
||||||
|
import os
|
||||||
|
import subprocess
|
||||||
|
|
||||||
|
def launch(s, sender, msgpart):
|
||||||
|
result = re.match(".*((dans|in) (la |the )?bran?ch?e?|(dans|in) (la |the )?bran?c?he?) +([^ ]+).*", msgpart)
|
||||||
|
if result is not None:
|
||||||
|
branch = "acu/" + result.group(6);
|
||||||
|
else:
|
||||||
|
branch = "master"
|
||||||
|
|
||||||
|
result = re.match(".*(dans|in) (la |the |le fichier |le dossier )?([^b][^r][^ ]+).*", msgpart)
|
||||||
|
if result is not None and re.search("(^/|.*\.\..*)", result.group(3)) is None:
|
||||||
|
checkpath = result.group(3)
|
||||||
|
if not os.path.exists(checkpath):
|
||||||
|
if os.path.exists("./src/" + checkpath):
|
||||||
|
checkpath = "./src/" + checkpath
|
||||||
|
else:
|
||||||
|
dirList = os.listdir("./src/")
|
||||||
|
for f in dirList:
|
||||||
|
if os.path.exists("./src/" + os.path.basename(f) + "/" + checkpath):
|
||||||
|
checkpath = "./src/" + os.path.basename(f) + "/" + checkpath
|
||||||
|
break
|
||||||
|
|
||||||
|
if not os.path.exists(checkpath):
|
||||||
|
s.send("PRIVMSG %s :I don't find %s in branch %s\r\n" % (sender[0], checkpath, branch))
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
checkpath = "./src/"
|
||||||
|
|
||||||
|
if subprocess.call(["git", "checkout", branch]) == 0:
|
||||||
|
try:
|
||||||
|
#s.send("PRIVMSG %s :Let me check %s in %s\r\n" % (sender[0], branch, checkpath))
|
||||||
|
faults = subprocess.check_output(["/home/nemunaire/workspace_/moulinette/main.py", checkpath])
|
||||||
|
lines = faults.split('\n')
|
||||||
|
for l in lines:
|
||||||
|
if len(l) > 1:
|
||||||
|
#print l
|
||||||
|
#print sender
|
||||||
|
s.send("PRIVMSG %s :%s\r\n" % (sender[0], l))
|
||||||
|
except:
|
||||||
|
s.send("PRIVMSG %s :An error occurs, all is broken\r\n" % sender[0])
|
||||||
|
else:
|
||||||
|
s.send("PRIVMSG %s :I haven't the branch %s here.\r\n" % (sender[0], branch))
|
43
ontime.py
Normal file
43
ontime.py
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
# coding=utf-8
|
||||||
|
from datetime import datetime
|
||||||
|
import time
|
||||||
|
|
||||||
|
import newyear
|
||||||
|
|
||||||
|
def sync(sec):
|
||||||
|
print "Time thread synchronization..."
|
||||||
|
|
||||||
|
time.sleep(((60 + sec) - datetime.now().second - 1) % 60)
|
||||||
|
|
||||||
|
while datetime.now().second % sec != 0:
|
||||||
|
time.sleep (0.1)
|
||||||
|
|
||||||
|
time.sleep (0.4)
|
||||||
|
|
||||||
|
print "Synchonized on {0}={1} seconds...".format(sec, datetime.now().second)
|
||||||
|
|
||||||
|
|
||||||
|
def startThread(s, CHANLIST):
|
||||||
|
sync (60);
|
||||||
|
while 1:
|
||||||
|
minute = datetime.now ().minute
|
||||||
|
|
||||||
|
if minute == 0 and datetime.now ().hour == 0 and datetime.now ().day == 1 and datetime.now ().month == 1:
|
||||||
|
print("Happy new year!")
|
||||||
|
for chan in CHANLIST.split():
|
||||||
|
newyear.special (s, chan)
|
||||||
|
|
||||||
|
if minute == 18:
|
||||||
|
for chan in CHANLIST.split():
|
||||||
|
newyear.launch (s, chan, 0)
|
||||||
|
|
||||||
|
if minute == 42:
|
||||||
|
sync(42)
|
||||||
|
print("42!")
|
||||||
|
for chan in CHANLIST.split():
|
||||||
|
s.send("PRIVMSG %s :42 !\r\n" % chan)
|
||||||
|
|
||||||
|
if datetime.now ().second != 0:
|
||||||
|
sync(60)
|
||||||
|
else:
|
||||||
|
time.sleep(60)
|
Loading…
Reference in New Issue
Block a user