~ubuntu-branches/ubuntu/utopic/gozerbot/utopic

« back to all changes in this revision

Viewing changes to gozerplugs/plugs/links.py

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Malcolm
  • Date: 2009-09-14 09:00:29 UTC
  • mfrom: (1.1.4 upstream) (3.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20090914090029-uval0ekt72kmklxw
Tags: 0.9.1.3-3
Changed dependency on python-setuptools to python-pkg-resources
(Closes: #546435) 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# plugs/links.py
2
 
#
3
 
#
4
 
 
5
 
""" link <item> to <otheritem> """
6
 
 
7
 
__copyright__ = 'this file is in the public domain'
8
 
 
9
 
from gozerbot.commands import cmnds
10
 
from gozerbot.redispatcher import rebefore
11
 
from gozerbot.plughelp import plughelp
12
 
from gozerbot.examples import examples
13
 
from gozerbot.pdol import Pdol
14
 
from gozerbot.datadir import datadir
15
 
import os
16
 
 
17
 
plughelp.add('links', 'alias items to other items .. this is only used in \
18
 
karma for now')
19
 
 
20
 
links = Pdol(datadir + os.sep + 'links')
21
 
 
22
 
def handle_linksadd(bot, ievent):
23
 
    """ links-add <item> = <otheritem> """
24
 
    try:
25
 
        (item, otheritem) = ievent.groups
26
 
    except ValueError:
27
 
        ievent.missing('<item> = <otheritem>')
28
 
        return
29
 
    links.add(item, otheritem)
30
 
    links.save()
31
 
    ievent.reply('%s link added' % otheritem)
32
 
 
33
 
rebefore.add(5, 'links-add (.*?) = (.*)', handle_linksadd, 'USER')
34
 
examples.add('links-add', 'links-add <from> = <to> .. add a link', \
35
 
'links-add mekker = miep bla')
36
 
 
37
 
def handle_linksdel(bot, ievent):
38
 
    """ links-del <item> = <otheritem> """
39
 
    try:
40
 
        (item, linkto) = ievent.groups
41
 
    except ValueError:
42
 
        ievent.missing('<item> = <link>')
43
 
        return
44
 
    itemlist = links[item]
45
 
    if not itemlist:
46
 
        ievent.reply('no links available for %s' % item)
47
 
        return
48
 
    for i in range(len(itemlist)-1, -1, -1):
49
 
        if itemlist[i] == linkto:
50
 
            del itemlist[i]
51
 
            links.save()
52
 
            ievent.reply('%s deleted' % linkto)
53
 
            return
54
 
            
55
 
rebefore.add(5, 'links-del (.*?) = (.*)', handle_linksdel, ['LINKS', 'OPER'])
56
 
examples.add('links-del', 'links-del <from> = <to> .. delete a link', \
57
 
'links-del mekker = miep bla')
58
 
 
59
 
def handle_linkslist(bot, ievent):
60
 
    """ links-list <item> .. show link of <item> """
61
 
    if not ievent.rest:
62
 
        ievent.missing('<item>')
63
 
        return
64
 
    result = links[ievent.rest]
65
 
    if result:
66
 
        ievent.reply("links for %s: " % ievent.rest, result)
67
 
    else:
68
 
        ievent.reply('no links found for %s' % ievent.rest)
69
 
 
70
 
cmnds.add('links-list', handle_linkslist, 'USER')
71
 
examples.add('links-list', 'links-list <item> .. show link of <item>', \
72
 
'links-list mekker')