Software Freedom Law Center

root/trunk/antimatter/tim/Bot-BasicBot/README

Revision 53, 19.8 kB (checked in by bkuhn, 9 months ago)
  • Added SFLC's internally developed tim bot released under AGPLv3
Line 
1 NAME
2     Bot::BasicBot - simple irc bot baseclass
3
4 SYNOPSIS
5       # with all defaults
6       my $bot = Bot::BasicBot->new( channels => ["#bottest"] );
7       $bot->run();
8
9       # with all known options
10       my $bot = Bot::BasicBot->new(
11
12         server => "irc.example.com",
13         port   => "6667",
14         channels => ["#bottest"],
15    
16         nick      => "basicbot",
17         alt_nicks => ["bbot", "simplebot"],
18         username  => "bot",
19         name      => "Yet Another Bot",
20    
21         ignore_list => [qw(dipsy dadadodo laotse)],
22
23         charset => "utf-8", # charset the bot assumes the channel is using
24
25       );
26       $bot->run();
27
28 DESCRIPTION
29     Basic bot system designed to make it easy to do simple bots, optionally
30     forking longer processes (like searches) concurrently in the background.
31
32     There are several examples of bots using Bot::BasicBot in the examples/
33     folder in the Bot::BasicBot tarball. If you installed Bot::BasicBot
34     through CPAN, see http://jerakeen.org/programming/Bot-BasicBot for more
35     docs and examples.
36
37     A quick summary, though - You want to define your own package that
38     subclasses Bot::BasicBot, override various methods (documented below),
39     then call new() and run() on it.
40
41 STARTING THE BOT
42   new( key => value, .. )
43     Creates a new instance of the class. Name value pairs may be passed
44     which will have the same effect as calling the method of that name with
45     the value supplied. Returns a Bot::BasicBot object, that you can call
46     'run' on later.
47
48     eg:
49
50       my $bot = Bot::BasicBot->new( nick => 'superbot', channels => [ '#superheroes' ] );
51
52   run()
53     Runs the bot. Hands the control over to the POE core.
54
55 METHODS TO OVERRIDE
56     In your Bot::BasicBot subclass, you want to override some of the
57     following methods to define how your bot works. These are all object
58     methods - the (implicit) first parameter to all of them will be the bot
59     object.
60
61   init()
62     called when the bot is created, as part of new(). Override to provide
63     your own init. Return a true value for a successful init, or undef if
64     you failed, in which case new() will die.
65
66   said($args)
67     This is the main method that you'll want to override in your subclass -
68     it's the one called by default whenever someone says anything that we
69     can hear, either in a public channel or to us in private that we
70     shouldn't ignore.
71
72     You'll be passed a hashref that contains the arguments described below.
73     Feel free to alter the values of this hash - it won't be used later on.
74
75     who Who said it (the nick that said it)
76
77     channel
78         The channel in which they said it. Has special value "msg" if it was
79         in a message. Actually, you can send a message to many channels at
80         once in the IRC spec, but no-one actually does this so this is just
81         the first one in the list.
82
83     body
84         The body of the message (i.e. the actual text)
85
86     address
87         The text that indicates how we were addressed. Contains the string
88         "msg" for private messages, otherwise contains the string off the
89         text that was stripped off the front of the message if we were
90         addressed, e.g. "Nick: ". Obviously this can be simply checked for
91         truth if you just want to know if you were addressed or not.
92
93     You should return what you want to say. This can either be a simple
94     string (which will be sent back to whoever was talking to you as a
95     message or in public depending on how they were talking) or a hashref
96     that contains values that are compatible with say (just changing the
97     body and returning the structure you were passed works very well.)
98
99     Returning undef will cause nothing to be said.
100
101   emoted( $args )
102     This is a secondary method that you may wish to override. It gets called
103     when someone in channel 'emotes', instead of talking. In its default
104     configuration, it will simply pass anything emoted on channel through to
105     the "said" handler.
106
107     "emoted" receives the same data hash as "said".
108
109   chanjoin( $mess )
110     Called when someone joins a channel. $mess is an object similar to a
111     said() message, $mess->{who} is the nick of the user who joined,
112     $mess->{channel} is the channel they joined.
113
114     This is a do-nothing implementation, override this in your subclass.
115
116   chanpart( $mess )
117     Called when someone leaves a channel. $mess is an object similar to a
118     said() message, $mess->{who} is the nick of the user who left,
119     $mess->{channel} is the channel they left.
120
121     This is a do-nothing implementation, override this in your subclass.
122
123   got_names( $mess )
124     Whenever we have been given a definitive list of 'who is in the
125     channel', this function will be called. As usual, $mess is a hash.
126     $mess->{channel} will be the channel we have information for,
127     $mess->{names} is a hashref, where the keys are the nicks of the users,
128     and the values are more hashes, containing the two keys 'op' and
129     'voice', indicating if the user is a chanop or voiced respectively.
130
131     The reply value is ignored.
132
133     Normally, I wouldn't override this method - instead, just use the names
134     call when you want to know who's in the channel. Override this only if
135     you want to be able to do something as soon as possible. Also be aware
136     that the names list can be changed by other events - kicks, joins, etc,
137     and this method won't be called when that happens.
138
139   topic( $mess )
140     Called when the topic of the channel changes. $mess->{channel} is the
141     channel the topic was set in, $mess->{who} is the nick of the user who
142     changed the channel, and $mess->{topic} will be the new topic of the
143     channel.
144
145   nick_change( $mess )
146     When a user changes nicks, this will be called. $mess looks like
147
148       { from => "old_nick",
149         to => "new_nick",
150       }
151
152   kicked( $mess )
153     Called when a user is kicked from the channel. $mess looks like:
154
155       { channel => "#channel",
156         who => "nick",
157         kicked => "kicked",
158         reason => "reason",
159       }
160
161     The reply value is ignored.
162
163   tick()
164     This is an event called every regularly. The function should return the
165     amount of time until the tick event should next be called. The default
166     tick is called 5 seconds after the bot starts, and the default
167     implementation returns '0', which disables the tick. Override this and
168     return non-zero values to have an ongoing tick event.
169
170     Use this function if you want the bot to do something periodically, and
171     don't want to mess with 'real' POE things.
172
173     Call the schedule_tick event to schedule a tick event without waiting
174     for the next tick.
175
176   help
177     This is the other method that you should override. This is the text that
178     the bot will respond to if someone simply says help to it. This should
179     be considered a special case which you should not attempt to process
180     yourself. Saying help to a bot should have no side effects whatsoever
181     apart from returning this text.
182
183   connected
184     An optional method to override, gets called after we have connected to
185     the server
186
187 BOT METHODS
188     There are a few methods you can call on the bot object to do things.
189     These are as follows:
190
191   schedule_tick(time)
192     Causes the tick event to be called in 'time' seconds (or 5 seconds if
193     time is left unspecified). Note that if the tick event is due to be
194     called already, this will override it, you can't schedule multiple
195     future events with this funtction.
196
197   forkit
198     This method allows you to fork arbitrary background processes. They will
199     run concurrently with the main bot, returning their output to a handler
200     routine. You should call "forkit" in response to specific events in your
201     "said" routine, particularly for longer running processes like searches,
202     which will block the bot from receiving or sending on channel whilst
203     they take place if you don't fork them.
204
205     "forkit" takes the following arguments:
206
207     run A coderef to the routine which you want to run. Bear in mind that
208         the routine doesn't automatically get the text of the query - you'll
209         need to pass it in "arguments" (see below) if you want to use it at
210         all.
211
212         Apart from that, your "run" routine just needs to print its output
213         to "STDOUT", and it will be passed on to your designated handler.
214
215     handler
216         Optional. A method name within your current package which we can
217         return the routine's data to. Defaults to the built-in method
218         "say_fork_return" (which simply sends data to channel).
219
220     body
221         Optional. Use this to pass on the body of the incoming message that
222         triggered you to fork this process. Useful for interactive proceses
223         such as searches, so that you can act on specific terms in the
224         user's instructions.
225
226     who The nick of who you want any response to reach (optional inside a
227         channel.)
228
229     channel
230         Where you want to say it to them in. This may be the special channel
231         "msg" if you want to speak to them directly
232
233     address
234         Optional. Setting this to a true value causes the person to be
235         addressed (i.e. to have "Nick: " prepended to the front of returned
236         message text if the response is going to a public forum.
237
238     arguments
239         Optional. This should be an anonymous array of values, which will be
240         passed to your "run" routine. Bear in mind that this is not
241         intelligent - it will blindly spew arguments at "run" in the order
242         that you specify them, and it is the responsibility of your "run"
243         routine to pick them up and make sense of them.
244
245   say( key => value, .. )
246     Say something to someone. You should pass the following arguments:
247
248     who The nick of who you are saying this to (optional inside a channel.)
249
250     channel
251         Where you want to say it to them in. This may be the special channel
252         "msg" if you want to speak to them directly
253
254     body
255         The body of the message. I.e. what you want to say.
256
257     address
258         Optional. Setting this to a true value causes the person to be
259         addressed (i.e. to have "Nick: " prepended to the front of the
260         message text if this message is going to a pulbic forum.
261
262     You can also make non-OO calls to "say", which will be interpreted as
263     coming from a process spawned by "forkit". The routine will serialise
264     any data it is sent, and throw it to STDOUT, where POE::Wheel::Run can
265     pass it on to a handler.
266
267   emote( key => value, .. )
268     "emote" will return data to channel, but emoted (as if you'd said "/me
269     writes a spiffy new bot" in most clients). It takes the same arguments
270     as "say", listed above.
271
272   reply($mess, $body)
273     Reply to a message $mess. Will reply to an incoming message with the
274     text '$body', in a privmsg if $mess was a privmsg, in channel if not,
275     and prefixes if $mess was prefixed. Mostly a shortcut method - it's
276     roughly equivalent to $mess->{body} = $body; $self->say($mess);
277
278   channel_data
279 ATTRIBUTES
280     Get or set methods. Changing most of these values when connected won't
281     cause sideffects. e.g. changing the server will not cause a disconnect
282     and a reconnect to another server.
283
284     Attributes that accept multiple values always return lists and either
285     accept an arrayref or a complete list as an argument.
286
287     The usual way of calling these is as keys to the hash passed to the
288     'new' method.
289
290   server
291     The server we're going to connect to. Defaults to "irc.perl.org".
292
293   port
294     The port we're going to use. Defaults to "6667"
295
296   nick
297     The nick we're going to use. Defaults to five random letters and numbers
298     followed by the word "bot"
299
300   alt_nicks
301     Alternate nicks that this bot will be known by. These are not nicks that
302     the bot will try if it's main nick is taken, but rather other nicks that
303     the bot will recognise if it is addressed in a public channel as the
304     nick. This is useful for bots that are replacements for other
305     bots...e.g, your bot can answer to the name "infobot: " even though it
306     isn't really.
307
308   username
309     The username we'll claim to have at our ip/domain. By default this will
310     be the same as our nick.
311
312   name
313     The name that the bot will identify itself as. Defaults to "$nick bot"
314     where $nick is the nick that the bot uses.
315
316   channels
317     The channels we're going to connect to.
318
319   quit_message
320     The quit message. Defaults to "Bye".
321
322   ignore_list
323     The list of irc nicks to ignore public messages from (normally other
324     bots.) Useful for stopping bot cascades.
325
326   charset
327     IRC has no defined character set for putting high-bit chars into
328     channel. In general, people tend to assume latin-1, but in case your
329     channel thinks differently, the bot can be told about different
330     charsets.
331
332     This feature requires perl 5.8+, I'm not fannying about with charsets
333     under any other version of perl.
334
335   flood
336     Set to '1' to disable the built-in flood protection of
337     POE::Compoent::IRC
338
339 STATES
340     These are the POE states that we register in order to listen for IRC
341     events. For the most part you don't need to worry about these, unless
342     you want to override them to do something clever.
343
344   start_state
345     Called when we start. Used to fire a "connect to irc server event"
346
347   reconnect
348     Connects the bot to the IRC server. Called 1 second after the 'start'
349     event.
350
351     in an ideal world, this will never get called again - we schedule it for
352     'x' seconds in the future, and whenever we see a server ping we reset
353     this counter again. This means that it'll get run if we haven't seen
354     anything from the server for a while, so we can assume that something
355     bad has happened. At that point we shotgun the IRC session and restart
356     everything, so we reconnect to the server.
357
358     This is by far the most reliable way I have found of ensuring that a bot
359     will reconnect to a server after it's lost a network connection for some
360     reason.
361
362     By default, the timeout is 300 seconds. It can be set by changing
363     $Bot::BasicBot::RECONNECT_TIMEOUT.
364
365   stop_state
366     Called when we're stopping. Shutdown the bot correctly.
367
368   irc_001_state
369     Called when we connect to the irc server. This is used to tell the irc
370     server that we'd quite like to join the channels.
371
372     We also ignore ourselves. We don't want to hear what we have to say.
373
374   irc_disconnected_state
375     Called if we are disconnected from the server. Logs the error and
376     schedules a reconnect event.
377
378   irc_error_state
379     Called if there is an irc server error. Logs the error and schedules a
380     reconnect event.
381
382   irc_kicked_state
383     Called on kick. If we're kicked then it's best to do nothing. Bots are
384     normally called in wrapper that restarts them if we die, which may end
385     us up in a busy loop. Anyway, if we're not wanted, the best thing to do
386     would be to hang around off channel.
387
388   irc_join_state
389     Called if someone joins. Used for nick tracking
390
391   irc_nick_state
392     Called if someone changes nick. Used for nick tracking.
393
394   irc_mode_state
395   irc_said_state
396     Called if we recieve a private or public message. This formats it into a
397     nicer format and calls 'said'
398
399   irc_emoted_state
400     Called if someone "emotes" on channel, rather than directly saying
401     something. Currently passes the emote striaght to "irc_said_state" which
402     deals with it as if it was a spoken phrase.
403
404   irc_received_state
405     Called by "irc_said_state" and "irc_emoted_state" in order to format
406     channel input into a more copable-with format.
407
408   irc_ping_state
409     The most reliable way I've found of doing auto-server-rejoin is to
410     listen for pings. Every ping we get, we put off rejoining the server for
411     another few mins. If we haven't heard a ping in a while, the rejoin code
412     will get called.
413
414     Recently, I've adapted this for servers that don't send pings very
415     often, and reset the counter any time _anything_ interesting happens.
416
417     You can change the amount of time the bot waits between events before
418     calling a reconnect event by changing $Bot::BasicBot::RECONNECT_TIMEOUT
419     to a value in seconds. The default is '500'.
420
421   irc_chanjoin_state
422     Called if someone joins a channel.
423
424   irc_chanpart_state
425     Called if someone parts a channel.
426
427   irc_chan_received_state
428     Called by "irc_chanjoin_state" and "irc_chanpart_state" in order to
429     format channel joins and parts into a more copable-with format.
430
431   fork_close_state
432     Called whenever a process forked by POE::Wheel::Run (in "forkit")
433     terminates, and allows us to delete the object and associated data from
434     memory.
435
436   fork_error_state
437     Called if a process forked by POE::Wheel::Run (in "forkit") hits an
438     error condition for any reason. Does nothing, but can be overloaded in
439     derived classes to be more useful
440
441   tick_state
442     the POE state for the tick event. Reschedules a tick event for the
443     future if the tick method returned a value.
444
445   names_state
446   names_done_state
447   topic_raw_state
448   topic_state
449 OTHER METHODS
450   AUTOLOAD
451     Bot::BasicBot implements AUTOLOAD for sending arbitrary states to the
452     underlying POE::Component::IRC compoment. So for a $bot object, sending
453
454         $bot->foo("bar");
455
456     is equivalent to
457
458         $poe_kernel->post(BASICBOT_ALIAS, "foo", "bar");
459
460   log
461     Logs the message. This method merely prints to STDERR - If you want
462     smarter logging, override this method - it will have simple text strings
463     passed in @_.
464
465   ignore_nick($nick)
466     Return true if this nick should be ignored. Ignores anything in the
467     ignore list
468
469   nick_strip
470     Takes a nick and hostname (of the form "nick!hostname") and returns just
471     the nick
472
473   charset_decode( foo, bar, baz )
474     Converts a string of bytes into a perl string, using the bot's charset.
475     (under perls before 5.8, just returns the thing it's passed.
476
477     Takes a list of strings, returns a list of strings, this is useful in
478     the contexts that I tend to be calling it from. Bytes that cannot be
479     decoded are converted to '?' symbols - see
480     http://search.cpan.org/~dankogai/Encode-2.09/Encode.pm#Handling_Malforme
481     d_Data
482
483   charset_encode( foo, bar, baz )
484     Converts a list of perl strings into a list of byte sequences, using the
485     bot's charset. See charset_decode.
486
487 AUTHOR
488     Tom Insam <tom@jerakeen.org>
489
490     This program is free software; you can redistribute it and/or modify it
491     under the same terms as Perl itself.
492
493 CREDITS
494     The initial version of Bot::BasicBot was written by Mark Fowler, and
495     many thanks are due to him.
496
497     Nice code for dealing with emotes thanks to Jo Walsh.
498
499     Various patches from Tom Insam, including much improved rejoining,
500     AUTOLOAD stuff, better interactive help, and a few API tidies.
501
502     Maintainership for a while was in the hands of Simon Kent
503     <simon@hitherto.net>. Don't know what he did. :-)
504
505     I recieved patches for tracking joins and parts from Silver, sat on them
506     for two months, and have finally applied them. Thanks, dude. He also
507     sent me changes for the tick event API, which made sense.
508
509 SYSTEM REQUIREMENTS
510     Bot::BasicBot is based on POE, and really needs the latest version as of
511     writing (0.22), since POE::Wheel::Run (used for forking) is still under
512     development, and the interface recently changed. With earlier versions
513     of POE, forking will not work, and the makefile process will carp if you
514     have < 0.22. Sorry.
515
516     You also need POE::Component::IRC.
517
518 BUGS
519     During the make, make test make install process, POE will moan about its
520     kernel not being run. I'll try and gag it in future releases, but hey,
521     release early, release often, and it's not a fatal error. It just looks
522     untidy.
523
524     Don't call your bot "0".
525
526     Nick tracking blatantly doesn't work yet. In Progress.
527
528     "fork_error_state" handlers sometimes seem to cause the bot to segfault.
529     I'm not yet sure if this is a POE::Wheel::Run problem, or a problem in
530     our implementation.
531
532 SEE ALSO
533     POE, POE::Component::IRC
534
535     Possibly Infobot, at http://www.infobot.org
536
Note: See TracBrowser for help on using the browser.

SFLC Main Page

[frdm] Support SFLC