raumgirl/plugins/Raumstatus/plugin.py

101 lines
3.3 KiB
Python
Raw Permalink Normal View History

2015-10-23 14:46:09 +00:00
###
# Copyright (c) 2011, xleave
# All rights reserved.
#
#
###
from threading import Event, Thread
import subprocess
import supybot.utils as utils
from supybot.commands import *
import supybot.plugins as plugins
import supybot.ircutils as ircutils
import supybot.callbacks as callbacks
import supybot.ircmsgs as ircmsgs
import re
raum_in_str = re.compile(".*(raum:|r:).*", re.I).match
raumstatus_is_open = re.compile("(raum: |r: )(auf|offen|open)", re.I).match
raumstatus_is_closed = re.compile("(raum: |r: )(zu|geschlossen|closed)", re.I).match
raumstatus_replace = re.compile("((raum: |r: ))[^ ]*", re.I).sub
class Raumstatus(callbacks.Plugin):
threaded = True
foo = "2"
def __init__(self, irc):
self.__parent = super(Raumstatus, self)
self.__parent.__init__(irc)
self.meinTimer = RepeatTimer(2, self.Bla, 0, [irc])
self.meinTimer.daemon = True
def start(self, irc, msg, args):
irc.reply("starting timer")
self.meinTimer.start()
irc.reply("MUST DESTROY UNIVERSE!!!")
start = wrap(start)
def stop(self, irc, msg, args):
irc.reply("stopping timer")
self.meinTimer.cancel()
irc.reply("FUCKED")
stop = wrap(stop)
def Bla(self, irc):
c = currentTopic = irc.state.getTopic(self.registryValue('channel'))
# import pdb;pdb.set_trace()
# c = currentTopic = irc.state.channels["#ccc.do"].topic
# raise Exception("%s %s %s %s"%("fuck you", type(c), dir(c), c))
# print "blabla %s " % self.registryValue('target')
ret = subprocess.call("ping -c6 -i .2 %s" % self.registryValue('target'),
shell=True,
stdout=open('/dev/null','w'),
stderr=subprocess.STDOUT)
x = raum_in_str(c)
if ret == 0:
if not x:
currentTopic = "Raum: auf | %s"%currentTopic
else:
if not raumstatus_is_open(currentTopic):
currentTopic = raumstatus_replace('\\1auf', currentTopic)
else:
if not x:
currentTopic = "Raum: zu | %s"%currentTopic
else:
if not raumstatus_is_closed(currentTopic):
currentTopic = raumstatus_replace('\\1zu', currentTopic)
if c != currentTopic:
irc.queueMsg(ircmsgs.topic(self.registryValue('channel'), currentTopic[:159]))
# irc.sendMsg(ircmsgs.privmsg("#ccc.do", "reset topic"))
class RepeatTimer(Thread):
def __init__(self, interval, function, iterations=0, args=[], kwargs={}):
Thread.__init__(self)
self.interval = interval
self.function = function
self.iterations = iterations
self.args = args
self.kwargs = kwargs
self.finished = Event()
def run(self):
count = 0
while not self.finished.isSet() and (self.iterations <= 0 or count < self.iterations):
self.finished.wait(self.interval)
if not self.finished.isSet():
self.function(*self.args, **self.kwargs)
count += 1
def cancel(self):
self.finished.set()
Class = Raumstatus
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: