]> jfr.im git - erebus.git/blob - modules/bashorg.py
bashorg - add aliases bashorg and bash.org
[erebus.git] / modules / bashorg.py
1 # Erebus IRC bot - Author: Erebus Team
2 # vim: fileencoding=utf-8
3 # bash.org quoter
4 # This file is released into the public domain; see http://unlicense.org/
5
6 # Note: this basically requires nofakelag since quotes can be many lines
7
8 # The config value [bashorg] path must point to a folder with
9 # files named like 0030507.txt for quote #30507 ("%07d.txt")
10
11 # module info
12 modinfo = {
13 'author': 'Erebus Team',
14 'license': 'public domain',
15 'compatible': [0], # compatible module API versions
16 'depends': [], # other modules required to work properly?
17 'softdeps': ['help'], # modules which are preferred but not required
18 }
19 # note: softdeps will be loaded before this module, IF not disabled in the configuration (autoload.module = 0) (and if it exists)
20 # however, if it is disabled it will be silently ignored, and if it is unloaded at runtime it won't cause this one to unload.
21 #
22 # basically, softdeps are things this module will use if available, but does not require (no errors will occur if it's not loaded)
23 # for example, @lib.help() will attempt to use the help module, but swallow errors if it is not loaded
24
25 # preamble
26 import modlib
27 lib = modlib.modlib(__name__)
28 modstart = lib.modstart
29 modstop = lib.modstop
30
31 # module code
32 import os, random
33
34 @lib.hook(('bash','bashorg','bash.org'), needchan=False, wantchan=True)
35 @lib.help('[<number>]', 'gives you a bash.org quote')
36 def bash(bot, user, chan, realtarget, *args):
37 path = bot.parent.cfg.get('bashorg', 'path')
38 target = chan if chan else user
39 try:
40 if len(args) == 0:
41 filename = random.choice(os.listdir(path))
42 number = int(filename[:-4])
43 which = os.path.join(path, filename)
44 else:
45 number = int(args[0])
46 filename = "%07d.txt" % (number)
47 which = os.path.join(path, filename)
48 if not os.path.isfile(which):
49 return "That quote doesn't exist!"
50 except ValueError:
51 return "Invalid quote number."
52
53 fh = open(which, 'r')
54 target.msg("--- bash.org #%d" % (number))
55 for line in fh:
56 target.msg(line)