]>
jfr.im git - irc/rizon/acid.git/blob - pyva/src/main/python/listbots/listbots.py
1 # psm_listbots.py: List pypsd bots but non-dynamically
9 from istring
import istring
11 class listbots(AcidPlugin
):
14 AcidPlugin
.__init
__(self
)
16 self
.name
= "listbots"
17 self
.log
= logging
.getLogger(__name__
)
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
)
26 self
.client
= self
.config
.get('listbots', 'nick')
31 def cmd_add(self
, source
, target
, pieces
):
32 if not pieces
or len(pieces
) < 2:
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
40 for i
, piece
in enumerate(pieces
):
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
)
55 self
.inter
.privmsg(self
.client
, target
, 'Added %s (%s).' % (pieces
[0], desc
))
59 def cmd_del(self
, source
, target
, pieces
):
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]))
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
)
74 self
.inter
.privmsg(self
.client
, target
, 'Deleted %s.' % pieces
[0])
78 def cmd_list(self
, source
, target
, pieces
):
82 self
.dbp
.execute('SELECT nick, description FROM pypsd_bots')
83 except Exception, err
:
84 self
.log
.exception('Error fetching listbots: (%s)' % err
)
87 self
.inter
.privmsg(self
.client
, target
, 'List of listed bots:')
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]))
96 self
.inter
.privmsg(self
.client
, target
, 'End of list of listed bots (%d bots).' % count
)
100 def onPrivmsg(self
, source
, target
, message
):
101 if target
!= self
.client
: # XXX uid
104 u
= self
.inter
.findUser(source
)
108 msg
= istring(message
.strip())
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
)
116 self
.dbp
.execute('SELECT nick, description FROM pypsd_bots')
117 except Exception, err
:
118 self
.log
.exception('Error fetching listbots: (%s)' % err
)
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(), ' ')
125 rows
= self
.dbp
.fetchall()
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]))
132 self
.inter
.notice(self
.client
, u
.getUID(), ' ')
134 def getCommands(self
):
138 'callback': self
.cmd_add
,
139 'usage': '<nick> <description> - adds a new bot to the list (or change its description) with the given description'
143 'callback': self
.cmd_del
,
144 'usage': '<nick> - removes a bot listed'
146 # For convenience, also alias list here
149 'callback': self
.cmd_list
,
150 'usage': '- displays all bots listed and their description'