tmb_util package

Submodules

tmb_util.chanserv module

Chanserv utils

We provide helper interfaces for quiet/akick lists across multiple channels. Additionally we provide a way to temporarily quick/akick.

tmb_util.chanserv.ALL_PATTERNS = {'*host', '*host*', '*nick', '*nick*', '*user', '*user*', 'host', 'host*', 'nick', 'nick*', 'user', 'user*'}

All valid n!u@h patterns on which we can act

tmb_util.chanserv.DB_FNAME = 'chanserv.db'

sqlite3 database for us. Use db_fname() to get the real full path; this here is relative to the data directory

tmb_util.chanserv.DELETE_BAN_INTERVAL = 10

Interval, in seconds, with which we check for old bans that we should delete. Ideally we wouldn’t poll, but if we must, then ideally we wouldn’t poll so often. It has been lowered from hourly to this because the botabuse module issues very short mutes through us. Also now the joinspam module issues bans >1h but <1d.

tmb_util.chanserv.DELETE_BAN_TIMER_HOOK = None

Weechat timer hook on our reoccuriring event for deleting old bans

tmb_util.chanserv.INSERT_QUERY = '\nINSERT INTO bans (glob, chan, is_quiet, expire) VALUES (?, ?, ?, ?);\n'

Query string for inserting one ban (quiet or akick) into table

tmb_util.chanserv.PERMANENT_TS = 99999999999

For permanent bans, this is the timestamp at which the ban expires

tmb_util.chanserv.TEMP_BAN_DAYS = 30

How long a temporary ban should last, in days

tmb_util.chanserv.db_fname()
tmb_util.chanserv.handle_command(user, where, message)

Main tormodbot code calls into this when we’re enabled and the given tmb_util.userstr.UserStr has sent us a command stored in message (str), via where (str, either a “#channel” or our nick if PM). It has already been verified that the user is a master and that where is a proper place.

tmb_util.chanserv.initialize()

Called whenever we are (re)starting

tmb_util.chanserv.internal_handle_command(nick, chans, pats, reason, is_quiet=False, is_permanent=False, duration=30)

Internal version of handle_command() for our own code to issue akicks/quiets.

Parameters:
  • nick – Nick to akick/quiet. We’ll look up the n!u@h string.

  • chans – List of channels in which to akick/quiet. If ‘all’ appears in the list, it will be replaced with all moderated channels.

  • pats – List of hostmask patterns such as ‘nick’, ‘user*’, ‘host’, and ‘*host’.

  • reason – String reason for the akick/quiet. A reason must be provided.

  • is_quietTrue if this is a quiet/mute. False means this is a ban/akick.

  • is_permanent – Whether or not the akick/quiet is permanent. If True, overrides whatever duration is set to.

  • duration – The duration of the ban, in days. Gets overridden by is_permanent if it is True.

Returns:

True if we were able to do any akick/quiet, otherwise False.

tmb_util.chanserv.timer_cb()

tmb_util.cmdqueue module

tmb_util.cmdqueue.initialize(tb_size, tb_rate)

Initialize this queue system. tb_size is an int number of messages we can burst at once, and tb_rate is how often we earn a new token, as a float number of seconds (i.e. how fast we can send messages in steady-state).

tmb_util.cmdqueue.send(s)

Called from elsewhere to queue the sending of some command or message. We add it to our queue of messages and send it later s.t. we don’t flood the network.

tmb_util.cmdqueue.timer_cb()

tmb_util.lcsv module

tmb_util.lcsv.lcsv(str_or_list)

List of Comma-Separated Values.

Convert a str of comma-separated values to a list over the items, or convert such a list back to a comma-separated string.

This function does not understand quotes.

See the unit tests for examples of how lcsv works.

tmb_util.lcsv.test_empty()
tmb_util.lcsv.test_list_simple()
tmb_util.lcsv.test_list_with_confusing_quotes()
tmb_util.lcsv.test_str_simple()

tmb_util.msg module

Helper functions to make sending messages and notices a bit less fiddly in the string manipulation department. Provide either notice(…) or msg(…) with :

  • the nick or channel you’d like to talk to,

  • a format string (or just a str), and

  • any number of args/kwargs to pass to .format(…)

We provide join(…) so you can join a channel without having to remember to -noswitch. Simply pass the name of the channel you want to join.

We provide mode(…) so you can easily set the mode of a channel or nick. Provide it with:

  • the target channel or nick

  • the mode flags (e.g. ‘+R-M’) all as one string

  • any number of args for the flags

We provide kick(…) so you can kick someone from some channel. Provide it with:

  • the channel

  • the nick

  • a reason

Examples:

mode(‘#tor’, ‘-R’) mode(‘#tor’, ‘+o-o’, ‘pastly’, ‘arma’)

tmb_util.msg.join(what)

Join a channel

tmb_util.msg.kick(chan, nick, reason)

KICK the given nick from chan with some reason. The reason can be an empty string, in which case it is set to the nick.

tmb_util.msg.mode(what, flags, *a)

Set the given flags mode on what (chan or nick). Additional args contain values for flags. For example, +o needs a nick to receive op status.

tmb_util.msg.msg(who, s, *a, **kw)

Send a PRIVMSG to who (chan or nick). The message is s.format(*a, **kw)

tmb_util.msg.notice(who, s, *a, **kw)

Send a notice to who (chan or nick). The notice message is s.format(*a, **kw)

tmb_util.msg.reconnect(server)

Send the reconnect command with the given server

tmb_util.msg.voice(chan, nick)

Voice the given nick on the given channel

tmb_util.msg.voices(chan, nicks)

Give voice to the given nicks on the given channel

tmb_util.tokenbucket module

tmb_util.tokenbucket.token_bucket(size_, refill_rate_)

Return a function that implements a token bucket of sorts.

The returned function is intended to be called once immediately after every action that consumes a token.

The two arguments to this function are the size of the token bucket (maximum number of tokens it can hold), and the rate at which the bucket refills with tokens (number of seconds between each additional earned token).

Concerning the generated tocket bucket function:

The function relies on the caller to keep track of its state.

It takes one argument: its previous state It returns two values: - the amount of time that must be waited before doing another action; and - its new state

tmb_util.userlist module

tmb_util.userlist.D = {}

Our cache of data. Keys are channels (like #foo) and values are sets of UserStr.

tmb_util.userlist.REFRESH_TIMER_HOOK = None

Weechat timer hook on our reoccurring event on refreshing all nick!user@host that we know about.

tmb_util.userlist.REFRESH_TIMER_INTERVAL = 10

The next interval to use, in seconds

tmb_util.userlist.REFRESH_TIMER_INTERVAL_MAX = 86400

Target interval, in seconds, on our complete refresh of all nick!user@host strings we know about. When in steady state and not manually refreshing, we double our interval repeatedly until it is more than this, at which point we use this.

tmb_util.userlist.REFRESH_TIMER_INTERVAL_MIN = 10

The initial and minimum interval used, in seconds

tmb_util.userlist.connect_cb()

Called whenever we have connected to the server

tmb_util.userlist.initialize()
tmb_util.userlist.join_cb(user, chan)

Called on join events from UserStr user joining str chan

tmb_util.userlist.nick_to_user(nick)

See if we cann find a UserStr for the given nick. If so, return it. Else return None.

tmb_util.userlist.part_cb(user, chan)

Called on part events from UserStr user joining str chan

tmb_util.userlist.timer_cb()
tmb_util.userlist.user_in_chans(user)

Return all monitored channels that UserStr user is in.

tmb_util.userstr module

class tmb_util.userstr.UserStr(s)

Bases: object

Store a nick/user/host parsed from nick!user@host

property host
property nick
property user

tmb_util.wordwrap module

tmb_util.wordwrap.TEST_W = 10

Width to which to wrap lines in most (all?) tests

tmb_util.wordwrap.test_empty()
tmb_util.wordwrap.test_multi_para()
tmb_util.wordwrap.test_multi_whitespace_to_single()
tmb_util.wordwrap.test_single_para()
tmb_util.wordwrap.wrap_text(s, max_width)

Wrap the given multi-line string s such that each line is no more than max_width characters.

The input text can be more than one paragraph, in which case each paragraph is wrapped separately. Paragraphs are deliminated with blank lines:

This is one
very short paragraph.

And this is the start of another paragraph.

This function yields wrapped lines, each with a trailing newline. It may, in the case of the input containing multiple paragraphs, return lines that are simply a single newline character.

Module contents