]> jfr.im git - irc/rizon/acid.git/blame - pyva/pyva/src/main/python/listbots/listbots.py
Split pyva plugin into pyva.core and pyva.pyva
[irc/rizon/acid.git] / pyva / pyva / src / main / python / listbots / listbots.py
CommitLineData
685e346e
A
1# psm_listbots.py: List pypsd bots but non-dynamically
2# vim: set expandtab:
3
4import logging
5
6from pyva import *
7from core import *
8from plugin import *
9from istring import istring
10
11class listbots(AcidPlugin):
12
13 def __init__(self):
14 AcidPlugin.__init__(self)
15
16 self.name = "listbots"
17 self.log = logging.getLogger(__name__)
18
19 def start(self):
20 try:
21 self.dbp.execute("CREATE TABLE IF NOT EXISTS pypsd_bots (nick VARCHAR(30) NOT NULL PRIMARY KEY, description VARCHAR(200) NOT NULL) ENGINE=MyISAM;")
22 except Exception, err:
23 self.log.exception("Error creating table for listbots module: %s" % err)
24 return False
25
26 self.client = self.config.get('listbots', 'nick')
27
28 ###
29 # Admin commands
30 ###
31 def cmd_add(self, source, target, pieces):
32 if not pieces or len(pieces) < 2:
33 return False
34
35 # We won't sanity check for the nick's existence here, after all,
36 # they could be on another py instance or not be loaded right now
37 # for some reason.
38
39 desc = ""
40 for i, piece in enumerate(pieces):
41 if i == 0:
42 continue
43
44 if i != 1:
45 desc += ' '
46 desc += piece
47
48 try:
49 self.dbp.execute('INSERT INTO pypsd_bots(nick, description) VALUES(%s, %s) ON DUPLICATE KEY UPDATE description = %s',
50 (pieces[0], desc, desc))
51 except Exception, err:
52 self.log.exception('Error updating listbots: (%s)' % err)
53 return True
54
55 self.inter.privmsg(self.client, target, 'Added %s (%s).' % (pieces[0], desc))
56
57 return True
58
59 def cmd_del(self, source, target, pieces):
60 if not pieces:
61 return False
62
63 try:
64 self.dbp.execute('SELECT nick FROM pypsd_bots WHERE nick = %s', pieces[0])
65 if not self.dbp.fetchone():
66 self.inter.privmsg(self.client, target, 'Bot %s does not exist.' % (pieces[0]))
67 return True
68
69 self.dbp.execute('DELETE FROM pypsd_bots WHERE nick = %s', pieces[0])
70 except Exception, err:
71 self.log.exception('Error updating listbots: (%s)' % err)
72 return True
73
74 self.inter.privmsg(self.client, target, 'Deleted %s.' % pieces[0])
75
76 return True
77
78 def cmd_list(self, source, target, pieces):
79 count = 0
80
81 try:
82 self.dbp.execute('SELECT nick, description FROM pypsd_bots')
83 except Exception, err:
84 self.log.exception('Error fetching listbots: (%s)' % err)
85 return True
86
87 self.inter.privmsg(self.client, target, 'List of listed bots:')
88
89 for row in self.dbp.fetchall():
90 # Sanely, we can assume that hardly any nick will be longer than
91 # 9 chars, even if nicklen is 30. Or the list will look like crap,
92 # either of those work.
93 self.inter.privmsg(self.client, target, '%-9s - %s' % (row[0], row[1]))
94 count += 1
95
96 self.inter.privmsg(self.client, target, 'End of list of listed bots (%d bots).' % count)
97
98 return True
99
100 def onPrivmsg(self, source, target, message):
101 if target != self.client: # XXX uid
102 return
103
104 u = self.inter.findUser(source)
105 if not u:
106 return
107
108 msg = istring(message.strip())
109
110 if msg not in ('HELP', 'LIST', 'COMMANDS', 'SHOWCOMMANDS', 'INFO'):
111 self.inter.notice(self.client, u.getUID(),
112 'Unknown command, do /msg %s HELP' % self.client)
113 return
114
115 try:
116 self.dbp.execute('SELECT nick, description FROM pypsd_bots')
117 except Exception, err:
118 self.log.exception('Error fetching listbots: (%s)' % err)
119 return
120
121 self.inter.notice(self.client, u.getUID(), 'All commands listed here are only available to channel founders.')
122 self.inter.notice(self.client, u.getUID(), 'Once the bot joined, use .help for more information on any one bot.')
123 self.inter.notice(self.client, u.getUID(), ' ')
124
125 rows = self.dbp.fetchall()
126
127 for i, row in enumerate(rows):
128 self.inter.notice(self.client, u.getUID(), '\002%s\002: %s' % (row[0], row[1]))
129 self.inter.notice(self.client, u.getUID(), 'To request: \002/msg %s request \037#channel\037\002' % (row[0]))
130 self.inter.notice(self.client, u.getUID(), 'To remove: \002/msg %s remove \037#channel\037\002' % (row[0]))
131 if i < len(rows)-1:
132 self.inter.notice(self.client, u.getUID(), ' ')
133
134 def getCommands(self):
135 return (
136 ('add', {
137 'permission': 'B',
138 'callback': self.cmd_add,
139 'usage': '<nick> <description> - adds a new bot to the list (or change its description) with the given description'
140 }),
141 ('del', {
142 'permission': 'B',
143 'callback': self.cmd_del,
144 'usage': '<nick> - removes a bot listed'
145 }),
146 # For convenience, also alias list here
147 ('list', {
148 'permission': 'B',
149 'callback': self.cmd_list,
150 'usage': '- displays all bots listed and their description'
151 })
152 )
153