5
""" irc related commands. """
7
__copyright__ = 'this file is in the public domain'
8
__gendocfirst__ = ['reconnect', 'join']
9
__gendoclast__ = ['part', ]
12
from gozerbot.callbacks import callbacks
13
from gozerbot.fleet import fleet
14
from gozerbot.partyline import partyline
15
from gozerbot.commands import cmnds
16
from gozerbot.examples import examples
17
from gozerbot.plughelp import plughelp
18
from gozerbot.tests import tests
19
import gozerbot.threads.thr as thr
24
plughelp.add('irc', 'irc related commands')
28
def handle_broadcast(bot, ievent):
30
""" broadcast txt to all joined channels. """
33
ievent.missing('<txt>')
36
ievent.reply('broadcasting')
37
fleet.broadcast(ievent.rest)
38
partyline.say_broadcast(ievent.rest)
41
cmnds.add('broadcast', handle_broadcast, 'OPER')
42
examples.add('broadcast', 'send a message to all channels and dcc users', 'broadcast good morning')
43
tests.add('broadcast testing testing')
45
def handle_alternick(bot, ievent):
47
""" set alternative nick used if nick is already taken. """
52
ievent.reply('alternick is %s' % bot.state['alternick'])
55
ievent.reply('changing alternick to %s' % nick)
56
bot.state['alternick'] = nick
60
cmnds.add('alternick', handle_alternick, 'OPER')
61
examples.add('alternick', 'get/set alertnick' , '1) alternick 2) alternick gozerbot2')
62
tests.add('alternick testbot', 'testbot')
64
def dojoin(bot, ievent):
66
""" join <channel> [password]. """
69
channel = ievent.args[0]
71
ievent.missing("<channel> [password]")
75
password = ievent.args[1]
79
ievent.reply('joining %s' % channel)
80
bot.join(channel, password=password)
83
cmnds.add('join', dojoin, ['OPER', 'JOIN'])
84
examples.add('join', 'join <channel> [password]', '1) join #test 2) join #test mekker')
85
tests.add('join #dunkbots')
87
def delchan(bot, ievent):
89
""" delchan <channel> .. remove channel from bot.channels. """
92
chan = ievent.args[0].lower()
94
ievent.missing("<channel>")
98
bot.state['joinedchannels'].remove(chan)
104
del bot.channels.data[chan]
107
ievent.reply("no channel %s in database" % chan)
111
cmnds.add('delchan', delchan, 'OPER')
112
examples.add('delchan', 'delchan <channel> .. remove channel from bot.channels', 'delchan #mekker')
114
def dopart(bot, ievent):
116
""" part [<channel>]. """
119
chan = ievent.channel
123
ievent.reply('leaving %s chan' % chan)
127
cmnds.add('part', dopart, 'OPER')
128
examples.add('part', 'part [<channel>]', '1) part 2) part #test')
129
tests.add('part #mekker')
131
def handle_channels(bot, ievent):
133
""" channels .. show joined channels. """
135
chans = bot.state['joinedchannels']
138
ievent.reply("joined channels: ", chans, dot=True)
140
ievent.reply('no channels joined')
142
cmnds.add('channels', handle_channels, ['USER', 'WEB'])
143
examples.add('channels', 'show what channels the bot is on', 'channels')
144
tests.add('channels', '#dunkbots')
146
def handle_chat(bot, ievent):
148
""" chat .. start a bot initiated dcc chat session. """
150
if not bot.type == 'irc':
151
ievent.reply("chat only works on irc bots")
155
thr.start_new_thread(bot._dcclisten, (i.nick, i.userhost, i.channel))
156
ievent.reply('dcc chat request sent')
158
cmnds.add('chat', handle_chat, 'USER')
159
examples.add('chat', 'start a dcc chat session', 'chat')
160
tests.add('chat', 'sent')
162
def handle_cycle(bot, ievent):
164
""" cycle .. recycle channel. """
166
ievent.reply('cycling %s' % ievent.channel)
167
bot.part(ievent.channel)
169
key = bot.channels[ievent.channel.lower()]['key']
170
except (KeyError, TypeError):
173
bot.join(ievent.channel, password=key)
176
cmnds.add('cycle', handle_cycle, 'OPER')
177
examples.add('cycle', 'part/join channel', 'cycle')
180
def handle_jump(bot, ievent):
182
""" jump <server> <port> .. change server. """
185
ievent.reply('jump only works on irc bots')
187
if len(ievent.args) != 2:
188
ievent.missing('<server> <port>')
190
(server, port) = ievent.args
191
ievent.reply('changing to server %s' % server)
198
cmnds.add('jump', handle_jump, 'OPER')
199
examples.add('jump', 'jump <server> <port> .. switch server', 'jump localhost 6667')
201
def modecb(bot, ievent):
203
""" callback to detect change of channel key. """
205
if ievent.postfix.find('+k') != -1:
206
key = ievent.postfix.split('+k')[1]
207
bot.channels[ievent.channel.lower()]['key'] = key
209
callbacks.add('MODE', modecb)
211
def handle_nick(bot, ievent):
213
""" nick <nickname> .. change bot's nick. """
216
ievent.reply('nick works only on irc bots')
220
nick = ievent.args[0]
222
ievent.missing('<nickname>')
225
ievent.reply('changing nick to %s' % nick)
226
bot.donick(nick, setorig=1, save=1)
229
cmnds.add('nick', handle_nick, 'OPER', threaded=True)
230
examples.add('nick', 'nick <nickname> .. set nick of the bot', 'nick mekker')
231
tests.add('nick miep')
233
def handle_sendraw(bot, ievent):
235
""" sendraw <txt> .. send raw text to the server. """
237
ievent.reply('sending raw txt')
238
bot.sendraw(ievent.rest)
241
cmnds.add('sendraw', handle_sendraw, 'SENDRAW')
242
examples.add('sendraw', 'sendraw <txt> .. send raw string to the server', \
243
'sendraw PRIVMSG #test :yo!')
245
def handle_nicks(bot, ievent):
247
""" return nicks on channel. """
250
ievent.reply('nicks only works on irc bots')
254
chan = ievent.args[0]
256
chan = ievent.channel
258
queue = Queue.Queue()
259
# set callback for name info response
260
wait353 = bot.wait.register('353', chan, queue)
261
# 366 is end of names response list
262
wait366 = bot.wait.register('366', chan, queue)
264
ievent.reply('searching for nicks')
271
if qres.cmnd == '366':
274
result += "%s " % qres.txt
276
bot.wait.delete(wait353)
277
bot.wait.delete(wait366)
283
for i in ignorenicks:
291
ievent.reply("nicks on %s (%s): " % (chan, bot.server), res)
293
ievent.reply("can't get nicks of channel %s" % chan)
295
cmnds.add('nicks', handle_nicks, ['OPER', 'WEB'], threaded=True)
296
examples.add('nicks', 'show nicks on channel the command was given in', 'nicks')
297
tests.add('nicks', "can't")
299
def handle_silent(bot, ievent):
301
""" set silent mode of channel. """
304
channel = ievent.rest.split()[0].lower()
306
if ievent.cmnd == 'DCC':
308
channel = ievent.channel
310
ievent.reply('putting %s to silent mode' % channel)
313
bot.channels[channel]['silent'] = 1
314
except (KeyError, TypeError):
315
ievent.reply("no %s channel in database" % channel)
319
cmnds.add('silent', handle_silent, 'OPER')
320
examples.add('silent', 'set silent mode on channel the command was given in', 'silent')
321
tests.add('silent').add('loud')
323
def handle_loud(bot, ievent):
325
""" loud .. enable output to the channel. """
328
channel = ievent.rest.split()[0].lower()
330
if ievent.cmnd == 'DCC':
332
channel = ievent.channel
334
ievent.reply('putting %s into loud mode' % ievent.channel)
337
bot.channels[channel]['silent'] = 0
338
except (KeyError, TypeError):
339
ievent.reply("no %s channel in database" % channel)
344
cmnds.add('loud', handle_loud, 'OPER')
345
examples.add('loud', 'disable silent mode of channel command was given in', 'loud')
348
def handle_withnotice(bot, ievent):
350
""" withnotice .. make bot use notice in channel. """
353
channel = ievent.rest.split()[0].lower()
355
if ievent.cmnd == 'DCC':
357
channel = ievent.channel
359
ievent.reply('setting notice in %s' % channel)
362
bot.channels[channel]['notice'] = 1
363
except (KeyError, TypeError):
364
ievent.reply("no %s channel in database" % channel)
369
cmnds.add('withnotice', handle_withnotice, 'OPER')
370
examples.add('withnotice', 'make bot use notice on channel the command was given in', 'withnotice')
371
tests.add('withnotice').add('withprivmsg')
373
def handle_withprivmsg(bot, ievent):
375
""" withprivmsg .. make bot use privmsg in channel. """
378
channel = ievent.rest.split()[0].lower()
380
if ievent.cmnd == 'DCC':
382
channel = ievent.channel
384
ievent.reply('setting privmsg in %s' % ievent.channel)
387
bot.channels[channel]['notice'] = 0
388
except (KeyError, TypeError):
389
ievent.reply("no %s channel in database" % channel)
394
cmnds.add('withprivmsg', handle_withprivmsg, 'OPER')
395
examples.add('withprivmsg', 'make bot use privmsg on channel command was given in', 'withprivmsg')
396
tests.add('withprivmsg')
398
def handle_reconnect(bot, ievent):
400
""" reconnect .. reconnect to server. """
402
ievent.reply('reconnecting')
406
cmnds.add('reconnect', handle_reconnect, 'OPER', threaded=True)
407
examples.add('reconnect', 'reconnect to server', 'reconnect')
409
def handle_channelmode(bot, ievent):
411
""" show channel mode. """
413
if bot.type != 'irc':
414
ievent.reply('channelmode only works on irc bots')
418
chan = ievent.args[0].lower()
420
chan = ievent.channel.lower()
422
if not chan in bot.state['joinedchannels']:
423
ievent.reply("i'm not on channel %s" % chan)
426
ievent.reply('channel mode of %s is %s' % (chan, bot.channels.get(chan, 'mode')))
428
cmnds.add('channelmode', handle_channelmode, 'OPER')
429
examples.add('channelmode', 'show mode of channel', '1) channelmode 2) channelmode #test')
430
tests.add('channelmode --chan #dunkbots')
432
def handle_action(bot, ievent):
434
""" make the bot send an action string. """
437
channel, txt = ievent.rest.split(' ', 1)
439
ievent.missing('<channel> <txt>')
442
bot.action(channel, txt)
444
cmnds.add('action', handle_action, ['ACTION', 'OPER'], speed=1)
445
examples.add('action', 'send an action message', 'action #test yoo dudes')
446
tests.add('action #dunkbots mekker')
448
def handle_say(bot, ievent):
450
""" make the bot say something. """
453
channel, txt = ievent.rest.split(' ', 1)
455
ievent.missing('<channel> <txt>')
458
bot.say(channel, txt)
460
cmnds.add('say', handle_say, ['SAY', 'OPER'], speed=1)
461
examples.add('say', 'send txt to channel/user', 'say #test good morning')
462
tests.add('say #dunkbots mekkerbot')
464
def handle_server(bot, ievent):
466
""" show the server to which the bot is connected. """
468
ievent.reply(bot.server)
470
cmnds.add('server', handle_server, 'OPER')
471
examples.add('server', 'show server hostname of bot', 'server')
474
def handle_voice(bot, ievent):
478
if bot.type != 'irc':
479
ievent.reply('voice only works on irc bots')
482
if len(ievent.args)==0:
483
ievent.missing('<nickname>')
486
ievent.reply('setting voide on %s' % str(ievent.args))
488
for nick in sets.Set(ievent.args):
489
bot.voice(ievent.channel, nick)
493
cmnds.add('voice', handle_voice, 'OPER')
494
examples.add('voice', 'give voice to user', 'voice test')
495
tests.add('voice dunker')