Skip to content

Instantly share code, notes, and snippets.

@ThiefMaster
Created August 21, 2012 22:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ThiefMaster/3419896 to your computer and use it in GitHub Desktop.
Save ThiefMaster/3419896 to your computer and use it in GitHub Desktop.
Flask-IRC example
from flask.ext.irc import BotModule, CommandAborted
from exodus import login_required, db
auth = BotModule('Auth', __name__)
@auth.command('addhost')
def addhost(source, channel):
"""Show the URL to connect your host with your account.
This command shows you the URL to associate your hostname with an account.
Note that you should only add hosts which are static and unique to you, i.e.
not a shared host but only a fakehost containing your IRC account name.
You can remove a host on the "edit profile" page of the website.
"""
if g.user:
msg = 'You are already authenticated ($b%s$b)' % g.user.username
raise CommandAborted(msg)
yield 'To add your current host $b%s$b, open the following link:' % source.host
yield url_for('addirchost', host=source.host, _external=True)
@auth.command('delhost')
@login_required
def delhost(source, channel):
"""Remove your current host from your account.
This command removes your currenet host from your account, i.e. it logs you
out until you add your host again.
"""
for identity in g.user.identities:
if identity.type == 'irc' and identity.identifier == source.host:
g.user.identities.remove(identity)
db.session.commit()
app.logger.info('Identifier removed from %s: %s:%s' % (
g.user.username, identity.type, identity.identifier))
break
return 'Your current host $b%s$b has been removed' % source.host
from flask.ext.irc import Bot
from flask.ext.irc.modules.admin import admin
from exodus import app, priv_required
from exodus.model import Identity
app.config.setdefault('IRC_PERFORM', [])
app.config.setdefault('IRC_AUTH_CMD', None)
app.config.setdefault('IRC_AUTH_REPLY', None)
app.config.setdefault('IRC_AUTH_NICK', None)
app.config.setdefault('IRC_AUTH_DELAY', 5)
app.config.setdefault('IRC_CHANNELS', [])
app.config.setdefault('IRC_INVITEME_CMD', None)
bot = Bot(app, logger_name='irc')
is_authed = False
must_join = []
# Protect the administrative commands
admin.decorate(priv_required('ircbot.manage'))
def auth():
bot.send(app.config['IRC_AUTH_CMD'])
def authed():
global is_authed, must_join
is_authed = True
must_join = list(app.config['IRC_CHANNELS'])
bot.send_multi('JOIN %s', must_join)
@bot.on('001')
def on_welcome(msg):
for cmd in app.config['IRC_PERFORM']:
bot.send(cmd)
if app.config['IRC_AUTH_CMD']:
auth()
else:
authed()
@bot.on('471') # channel full
@bot.on('473') # invite only
@bot.on('474') # banned
@bot.on('475') # bad key
def on_cannotjoin(msg):
if app.config['IRC_INVITEME_CMD']:
bot.send(app.config['IRC_INVITEME_CMD'] % msg[1])
else:
def _rejoin():
bot.send('JOIN %s' % msg[1])
bot.after(5, _rejoin)
@bot.on('INVITE')
def on_invite(msg):
if msg[0] == bot.nick and msg[1] in must_join:
bot.send('JOIN %s' % msg[1])
@bot.on('NOTICE')
@bot.on('PRIVMSG')
def on_auth_response(msg):
auth_nick = app.config['IRC_AUTH_NICK']
if is_authed or (auth_nick and msg.source.nick != auth_nick):
return
if msg[1] == app.config['IRC_AUTH_REPLY']:
authed()
else:
bot.after(app.config['IRC_AUTH_DELAY'], auth)
@bot.on('JOIN')
def on_join(msg):
global must_join
if msg.source.nick != bot.nick or msg[0] not in must_join:
return
must_join.remove(msg[0])
if not must_join:
bot.trigger_ready()
@bot.event('disconnect')
def on_disconnect():
global is_authed
is_authed = False
@bot.event('before_command')
def on_before_command(msg, cmd):
g.user = None
identity = Identity.query.filter_by(type='irc', identifier=msg.source.host).first()
if identity:
g.user = identity.user
IRC_SERVER_BIND = '127.0.0.1'
IRC_SERVER_HOST = '127.0.0.1'
IRC_SERVER_PORT = 6667
IRC_NICK = 'FlaskBot'
IRC_USER = 'FlaskBot'
IRC_REALNAME = 'FlaskBot'
IRC_TRIGGER = '*'
IRC_DEBUG = True
IRC_MODULES = ['Admin', 'Auth', 'Misc']
IRC_PERFORM = ['UMODE +x']
IRC_AUTH_CMD = 'AUTHSERV AUTH <user> <password>'
IRC_AUTH_NICK = 'AuthServ'
IRC_AUTH_REPLY = 'I recognize you.'
IRC_AUTH_DELAY = 5
IRC_CHANNELS = ['#ircops']
IRC_INVITEME_CMD = 'CHANSERV INVITEME %s'
from flask.ext.irc import BotModule, CommandAborted
from exodus import priv_required
misc = BotModule('Misc', __name__)
@misc.command('raw', greedy=True)
@priv_required('ircbot.manage')
def raw(source, channel, msg):
"""Send raw data to the IRC server.
This command sends whatever arguments are provided to the IRC server.
"""
msg = msg.strip()
if not msg:
raise CommandAborted('Empty command')
misc.bot.send(msg)
@misc.command('say', greedy=True)
@priv_required('ircbot.manage')
def say(source, channel, target, msg):
"""Send a message to a user/channel.
This command sends a PRIVMSG to the given target.
"""
msg = msg.strip()
if not msg:
raise CommandAborted('Empty message')
misc.bot.send('PRIVMSG %s :%s' % (target, msg))
@misc.command('emote', greedy=True)
@priv_required('ircbot.manage')
def emote(source, channel, target, msg):
"""Send an emote message to a user/channel.
This command sends a CTCP ACTION to the given target.
"""
msg = msg.strip()
if not msg:
raise CommandAborted('Empty message')
misc.bot.send('PRIVMSG %s :\1ACTION %s\1' % (target, msg))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment