alebot

API Documentation

Bot class

class alebot.Alebot(path=None, disableLog=False)[source]

The main bot class, where all the magic happens.

This class handles the socket and all incoming and outgoing data. This classes methods should be used for sending data.

It keeps an index of loaded plugins and helps with the management of requirements.

To interact witht he bot it supplies a hook function that can be used to register callbacks with the bot.

Please check out Hook and hook() to find out how hooking your plugins in works.

Please note that this bot does absolutely nothing by itself.

It won’t even answer pings or identify. But there are some system plugins to do that. Check the plugins folder.

config

Holds the bot configuration.

Hooks

Registered hooks

Plugins

Registered modules

classmethod Paths(path=None)[source]

The path storage. Use this to get the user & the systempath!

activate_hooks()[source]

Will instantiate all the loaded hooks.

call_hooks(event)[source]

Will check through all instantiated plugins and call the ones that match the given event.

collect_incoming_data(data)[source]

Collects the incoming data and adds it to the buffer.

Parameters:data – The received data from the server.

Please do not use this function manually! It is only to be used by asnchat!

configure_logging()[source]

Depending on the configuration the log level and eventual handlers for the logging have to be configured, which is what this function does.

connect()[source]

Creates a socket and kicks off the connection process.

found_terminator()[source]

As IRC is a line based protocol which means every command is in its own line, this function is called as soon as a line and thus a command has been completely received.

The line is read from the buffer and the buffer cleared.

The function only does very basic syntax correction, to clear up input that does not match the usual format.

I.e. a PING command does not follow the usual syntax.

Thusly the function will get the relevant parameter and fill it into the usual structure.

This function will call the func:call_hooks function with the extracted data.

classmethod get_plugin(name)[source]

This is mainly a helper for inter dependency between plugins. If one plugin requires another one, it can get to it with this function. If the plugin has not been loaded yet, it will try to load it. If it fails or does not exist, the requiring plugin will also fail to load.

handle_connect()[source]

As soon as the socket is connected, the (made up) event SOCK_CONNECTED will be called! Use it to identify yourself. This bot does NOTHING by itself.

classmethod hook(Hook)[source]

This method will register a hook with the bot. It is supposed to be used as a decorator, but can also be used as a normal function. Please see the Hook class documentation for an overview what a hook should look like.

This method will save the Hook class with the Alebot class until the __init__() function instantiates the hooks.

load_config()[source]

Will open the local config file config.json and load it as json. Will only accept a json object.

There are no required values. The file itself it optional. The bot will supply default values, if there are none specified.

Alebot itself makes use of the following options:

  • nick
  • ident
  • realname
  • server
  • port

Any additional option can be configured. Plugin developers are encouraged to specifiy plugin objects with own configuration, as long as they make sure to use a specific name to avoid conflicts.

classmethod load_plugin(name, path=[])[source]

Load a specific plugin. This will try to find a specific plugin, load it and save it to the Alebot class, so that it can be retrieved later on.

It will also make sure, that plugins are only loaded once.

classmethod load_plugins(path=None)[source]

Will load all the plugins from the plugin folder. It will only load them though! The plugins still have to register themselves with the hook() function, if they want to interact with the bot.

This function does not do anything yet. Plugins have to be in the the same file as the bot itself.

logger[source]

To enable access to the logger from both class- and object methods, this property is available.

paths[source]

Shortcut to class-level Paths storage.

save_config()[source]

Save the current configuration to file.

send_raw(data)[source]

Sends raw commands to the server. Only adds CLRF as a suffix.

Parameters:data – the IRC command and body to send, fully formatted as such.

Event class

class alebot.Event(name=None, user=None, target=None, body=None)[source]

The Event class is used to store data about the event and provide some helper methods that should save you some string manipulation.

name

The name attribute should always be given and will be a string, either as defined in the IRC RFC (event number or command) or one of the following custom events:

SOCK_CONNECTED: Sent as soon as the socket is connected.

Depending on the type of the event there might be one or me of the following attributes not empty (not None):

user

The user that caused the event. In case it is a channel or private message, it is the sender of the message, in case of a join, the joining person, in case of a kick, the kicking person and so on. If this is an actual user, and not a server, nick, ident and :attr`host` should be available, too.

If this is a user, it will be in the format of:

nick!ident@host

If it is a server, it will be in the form of:

subdomain.domain.zone
body

If the event has a message, reason or a similar thing, you will find it in body.

target

The target of the action, a channel if it is a channel message, the bot’s nick if it is a private one or anything else.

Hook class

class alebot.Hook(bot)[source]

This is just a possible implementation for a hook. Every hook that registers itself with the bot has to implement two functions:

__init__() match() call()

Read these functions documentation to see what they should do.

Every hook that actually is supposed to be used, has to be prefixed by the @Bot.hook decorator. Doing so automatically adds the hook to the bot. The hook will be instantiated once on startup and then kept in memory until the bot dies.

This means you can do some magic processing like reading config files and stuff in the beginning. As soon as your bot will be initialized there will be a bot instance present although you won’t be able to send data in the beginning.

You can check out the default alebot plugins for examples on how to write plugins.

If you want to log data, it is recommended to access the bot’s logger using self.bot.logger. It supports python’s usual logging infrastructure and thus functions like debug, info, warn and error.

call(event)[source]

In case that your match() function returned True this function will be called. It will again receive:

param event:an instance of the Event class

Now you are free to send data or do whatever you have to do.

match(event)[source]

This function is used to evaluate whether the hook wants to react to the event that is passed on. It has to accept one parameter:

param event:an instance of the Event class

Although theoretically possible I recommend to separate the evaluation of a match and the actual reaction for the code’s clearnesses sake.

This way it is very easy to determine what triggers this hook.

returns:Either True or False, depending on whether a match is given or not.

As this is a callback you have very high flexibility regarding your matching. You can match combinations of nick, ident, host, events, target and body.

Check out the Event class to see what event data is available.

You can also use a regex or vary matches based on the time or the weather. Whatever you want.

send_raw(data)[source]

This function is just a shortcut to func:Bot.send_raw.

Task class

class alebot.Task(hook, event)[source]

This class can be used to do stuff in the background. It can be used for everything that might take some time and should not block the bot in the meantime.

It expects the following parameters:

param hook:the current hook instance (the active plugin)
param event:the current event

You can basically overwrite the init event any pass less data, the example data her is just for convenience.

You will have to overwrite do() though. See the functions documentation for more information.

The task can be started using the start() function.

do()[source]

Override this with whatever your backgroundtask is supposed to do. Your function should take the Task instance as a parameter.

run()[source]

The run function is overwritten to call the do function and catch any exceptions. This way bot crashes should be avoided. So if you would like your bot to be stable, please do not overwrite this, but the do() function.

IRCCommandsMixin class

class alebot.IRCCommandsMixin[source]

This is just a helpful mixin to provide a few basic irc command wrappers to different classes.

join(channel)[source]

Join a channel.

param channel:the channel to join (with prefix)
msg(target, text)[source]

Send a message to a target.

param target:either a channel (with prefix) or a nick
param text:the contents of the message
part(channel, reason='Part.')[source]

Part a channel.

param channel:the channel to part (with prefix)
quit(reason='Quit.')[source]

Quit the server.

param reason:the reason for the quit (will be shown to other users in common channels)

System plugins

Default

class alebot.plugins.default.CommandHook(bot)[source]

This is a hook that can be subclassed in case you want to react to a message on a channel or in private. It will react to the bot’s current nickname followed by a colon and the command specified in the command attribute.

class alebot.plugins.default.CommandParamHook(bot)[source]

In case you want your command to take parameters, too.

class alebot.plugins.default.ConnectionReadyHook(bot)[source]

This is a hook that can be subclassed in case you want to react on a irc connection that is ready for commands. It waits for the end of the motd, or the message that there is no motd.

The match() function was implemented to listen to the correct events. You will just have to overwrite the :func`call` to actually do something.

class alebot.plugins.default.PingPong(bot)[source]

As the bot does nothing by itself, this plugin takes care of sending PONGs as answer to pings, as the bot won’t even do that.

It matches the PING event to do that.

class alebot.plugins.default.SocketConnectedHook(bot)[source]

As the bot does nothing itself, this plugin takes care of identifying the bot with the server. Yeah, seriously.

It uses the made up SOCK_CONNECTED event that is not even an actual IRC event..

Auth

class alebot.plugins.auth.AdminCommandHook(bot)[source]

Just a shortcut for simple commands that require admin permissions.

class alebot.plugins.auth.AdminCommandParamHook(bot)[source]

Just a shortcut for simple commands that require admin permissions.

class alebot.plugins.auth.AdminManagementHook(bot)[source]

Add and remove admins via irc commands.

class alebot.plugins.auth.AdminManager[source]

A wrapper around the config file to improve the auth config handling.

add_admin(nick)[source]

Add an admin to the list of admins.

check_config()[source]

Make sure that the config section exists.

delete_admin(nick)[source]

Remove an admin from the list of admins.

alebot.plugins.auth.admin_required(f)[source]

You can decorate the match functions of your Hook classes with this function. It will assure, that only auth admins can use the command.

After verifying this, your normal match will be excuted.

Admin

class alebot.plugins.admin.ReloadHook(bot)[source]

Reloads config and plugins on request.

class alebot.plugins.admin.SaveHook(bot)[source]

Save current config state to disk.

Channels

class alebot.plugins.channels.JoinOnConnect(bot)[source]

Join channels defined in the config file options channels on connection. If there are any definied, if not, it does not join any channels.