Added Tell plugin to server management bot

This commit is contained in:
Rob Nelson
2013-12-20 11:09:15 -08:00
parent 2dec12e74b
commit 7657efd85e
6 changed files with 135 additions and 25 deletions

1
.gitignore vendored
View File

@@ -20,3 +20,4 @@ info.json
.metadata
/bot/config.yml
/bot/data

View File

@@ -26,12 +26,12 @@ class NudgePlugin(IPlugin):
thread.start_new_thread(self.nudge_listener, ())
def OnShaddap(self, event):
def OnShaddap(self, event, args):
self.dropNudges = True
self.bot.notice(event.source.nick, 'Now dropping nudges.')
return True
def OnSpeak(self, event):
def OnSpeak(self, event, args):
self.dropNudges = False
self.bot.notice(event.source.nick, 'No longer dropping nudges.')
return True
@@ -57,7 +57,7 @@ class NudgePlugin(IPlugin):
if truedata.get('key', '') != nudgeconfig['key']:
logging.info('Dropped nudge (BAD KEY): {0}'.format(repr(truedata)))
continue
if truedata.get("channel",None) is not None:
if truedata.get("channel", None) is not None:
to = truedata["channel"]
msg = 'AUTOMATIC ANNOUNCEMENT: [{0}] {1}'.format(truedata['id'], truedata["data"])

View File

@@ -4,7 +4,7 @@ Adapted from the Supybot plugin.
from vgstation.common.plugin import IPlugin, Plugin
import vgstation.common.config as globalConfig
import logging, random, re, time
#import restkit
# import restkit
from restkit import BasicAuth, Resource, RequestError
from restkit.errors import RequestFailed, ResourceNotFound
import simplejson as json
@@ -59,7 +59,7 @@ class RedminePlugin(IPlugin):
self.lastCheck = now
bugs = self.getAllBugs(project_id=self.project_id, sort='created_on:desc')
if bugs is None: return
#print(repr(bugs))
# print(repr(bugs))
lbc = ''
for bug in bugs['issues']:
if bug['created_on'] != self.data['last-bug-created']:
@@ -103,7 +103,7 @@ class RedminePlugin(IPlugin):
strings.append(msg)
except ResourceNotFound:
#strings.append("Unable to find redmine issue {0}.".format(id))
# strings.append("Unable to find redmine issue {0}.".format(id))
continue
return strings

84
bot/plugins/Tell.py Normal file
View File

@@ -0,0 +1,84 @@
# Inspired by Skilibliaasdadas's bot.
# nt, tell nickname something
# nt, received
from vgstation.common.plugin import IPlugin, Plugin
import vgstation.common.config as globalConfig
@Plugin
class TellPlugin(IPlugin):
def __init__(self, bot):
IPlugin.__init__(self, bot)
# Recipient => Messages ({from,message})
self.data = {}
self.LoadPluginData()
# {from,to,message}
self.lastMessages = []
self.RegisterCommand('tell', self.OnTell, help='Leave a message for someone.')
self.RegisterCommand('received', self.OnReceived, help='Bot will mark messages sent to you as read.')
self.RegisterCommand('messages', self.OnMessages, help='Rattle off the messages sent to you.')
self.RegisterCommand('belay', self.OnBelay, help='Remove last message you sent.')
def OnTell(self, event, args):
channel = event.target
nick = event.source.nick
if len(args) < 3:
self.bot.notice(nick,'The format is: bot, tell NICKNAME MESSAGE TO SEND')
msg = ' '.join(args[2:])
to = args[1]
message = {'from':nick, 'to':to, 'message':msg}
self.lastMessages += [message]
if to not in self.data:
self.data[to] = []
else:
if len(self.data[to]) == 5:
self.bot.privmsg(channel, '{to} has too many messages. They need to use the received command before I can add more.'.format(**message))
return True
self.data[to] += [message]
self.SavePluginData()
self.bot.privmsg(channel, 'Your message has been sent. It will be displayed the next time {to} joins or uses messages.'.format(**message))
return True
def OnReceived(self, event, args):
channel = event.target
nick = event.source.nick
self.data[nick] = []
self.SavePluginData()
self.bot.privmsg(channel, 'Your messages have been cleared.')
return True
def OnBelay(self, event, args):
channel = event.target
nick = event.source.nick
lm = None
for m in self.lastMessages:
if m['from'] == nick:
lm = m
if lm is not None:
self.data[lm['to']].remove(lm)
self.bot.privmsg(channel, 'Your message to {to} was removed.'.format(**lm))
self.SavePluginData()
return True
def OnJoin(self, channel, nick):
self.SendMessages(channel, nick)
return False # Let other plugins use it.
def OnMessages(self, event, args):
channel = event.target
nick = event.source.nick
self.SendMessages(channel,nick)
return True
def SendMessages(self,channel,nick):
if nick in self.data:
if len(self.data[nick]) > 0:
self.bot.privmsg(channel, '{0}, you have {1} messages. Say "{2}, received" to clear them.'.format(nick, len(self.data[nick]), globalConfig.get('names', ['nt'])[0]))
for message in self.data[nick]:
self.bot.privmsg(channel, '{from}: {message}'.format(**message))

View File

@@ -16,10 +16,30 @@ class Bot(irc.bot.SingleServerIRCBot):
self.command = {}
self.plugins = []
for i in ["ping"]:
self.connection.add_global_handler(i, getattr(self, "on_" + i),0)
self.welcomeReceived=False
self.connection.add_global_handler(i, getattr(self, "on_" + i), 0)
self.welcomeReceived = False
def on_ping(self,c,e):
self.messageQueue = []
self.connection.execute_every(1, self.SendQueuedMessage)
def SendQueuedMessage(self):
if len(self.messageQueue) == 0: return
msg = self.messageQueue[0]
msgtype, target, message = msg
logging.info('{0} -> {1}: {2}'.format(msgtype, target, self.stripUnprintable(message)))
if msgtype == 'PRIVMSG':
self.connection.privmsg(target, message)
elif msgtype == 'NOTICE':
self.connection.notice(target, message)
self.messageQueue.remove(msg)
def on_join(self, c, e):
ch = e.target
nick = e.source.nick
for plugin in self.plugins:
if plugin.OnJoin(ch, nick): break
def on_ping(self, c, e):
for plugin in self.plugins:
if plugin.OnPing(): break
@@ -27,7 +47,7 @@ class Bot(irc.bot.SingleServerIRCBot):
c.nick(c.get_nickname() + "_")
def on_welcome(self, c, e):
self.welcomeReceived=True
self.welcomeReceived = True
for channel, channelconfig in self.chanconfig.items():
password = channelconfig.get('password', None)
logging.info('Joining {0}...'.format(channel))
@@ -37,15 +57,15 @@ class Bot(irc.bot.SingleServerIRCBot):
c.join(channel, password)
def on_privmsg(self, c, e):
msg=e.arguments[0]
msg=self.stripUnprintable(msg)
msg = e.arguments[0]
msg = self.stripUnprintable(msg)
logging.info('PRIVMSG: <{0}> {1}'.format(e.source.nick, msg))
self.do_command(e, e.arguments[0])
def on_pubmsg(self, c, e):
#logging.info(msg.source)
msg=e.arguments[0]
msg=self.stripUnprintable(msg)
# logging.info(msg.source)
msg = e.arguments[0]
msg = self.stripUnprintable(msg)
logging.info('PUBMSG: <{0}:{1}> {2}'.format(e.source.nick, e.target, msg))
if ',' in msg:
args = msg.split(',', 1)
@@ -63,39 +83,41 @@ class Bot(irc.bot.SingleServerIRCBot):
def on_dccchat(self, c, e):
return
def stripUnprintable(self,msg):
def stripUnprintable(self, msg):
return filter(lambda x: x in string.printable, msg)
def notice(self, nick, message):
logging.info('NOTICE -> {0}: {1}'.format(nick, self.stripUnprintable(message)))
self.connection.notice(nick, message)
self.messageQueue += [('NOTICE', nick, message)]
# self.connection.notice(nick, message)
def privmsg(self, nick, message):
logging.info('PRIVMSG -> {0}: {1}'.format(nick, self.stripUnprintable(message)))
self.connection.privmsg(nick, message)
self.messageQueue += [('PRIVMSG', nick, message)]
# self.connection.privmsg(nick, message)
def do_command(self, e, cmd):
nick = e.source.nick
channel = nick
if e.target:
channel = e.target
args = cmd.split(' ')
cmd = args[0]
c = self.connection
if cmd == 'help':
self.privmsg(nick, '-- VGBot 1.0 Help: (All commands are accessed with {0}, <command> [args]'.format(c.get_nickname()))
for name in self.command:
for name in sorted(self.command.keys()):
self.privmsg(nick, ' {0}: {1}'.format(name, self.command[name].get('help', 'No help= argument for this command.')))
elif cmd == 'version':
self.notice(channel, 'VGBot 1.0 - By N3X15') # pls to not change
elif cmd in self.command:
self.command[cmd]['handler'](e)
self.command[cmd]['handler'](e, args)
else:
self.notice(channel, 'I don\'t know that command, sorry. Say "{0}, help" for available commands.'.format(c.get_nickname()))
def sendToAllFlagged(self, flag, msg):
for channel, chandata in self.chanconfig.items():
if chandata.get(flag, False)==True:
if chandata.get(flag, False) == True:
self.privmsg(channel, msg)
def haveJoined(self,channel):
def haveJoined(self, channel):
return channel in self.channels

View File

@@ -47,7 +47,7 @@ class IPlugin(object):
self.datadir = os.path.join('data')
if not os.path.isdir(self.datadir):
os.makedirs(self.datadir)
self.datafile = os.path.join(self.datadir, 'redmine.yml')
self.datafile = os.path.join(self.datadir, self.__class__.__name__+'.yml')
if os.path.isfile(self.datafile):
with open(self.datafile, 'r') as stream:
self.data = yaml.load(stream)
@@ -68,3 +68,6 @@ class IPlugin(object):
def OnPing(self):
return False # Not handled
def OnJoin(self,channel,nick):
return False # Not handled