Sie sind auf Seite 1von 6

What Is IRC?

n IRC stands for “Internet Relay Chat”


n Created by Jarkko Oikarinen in 1988 and still
Java and IRC growing in popularity
n An IRC server allows people to chat in
channels (rooms or groups), or privately
Making a Java IRC Bot With
n People from all over the world can use it
The PircBot Framework
n IRC servers can be joined together to provide
vast networks with thousands of users

Copyright Paul Mutton, http://www.jibble.org/pircbot.php 1 Copyright Paul Mutton, http://www.jibble.org/pircbot.php 2

Using IRC Nicks, Channels and Operators

n A user runs a client program to connect to n Each user must have a unique nickname
the IRC server § Commonly referred to as a “nick”
n The client program allows you to send and § Must not contain certain characters, e.g. spaces
receive messages to and from other users n Channel names must start with # or &
n Some popular IRC clients are: - n Some users may be channel operators
§ mIRC § Can kick other users out of their channel
§ BitchX § Can “op” and “deop” other users in the channel
§ xchat § Can ban users from entering the channel

Copyright Paul Mutton, http://www.jibble.org/pircbot.php 3 Copyright Paul Mutton, http://www.jibble.org/pircbot.php 4

IRC Protocol What Is an IRC Bot?

n The IRC protocol is text-based n Bot is short for “robot”


n RFC 1459 defines how messages are sent n An IRC Bot is a special type of IRC client
from client to server and server to client n Does not require a human user
n TCP sockets are used for connecting n Often responds automatically to certain
n Some IRC servers will support extra events
commands that are not in RFC 1459 n One analogy is to think of an IRC Bot as a
n The protocol is asynchronous in nature normal IRC client, but where the human user
has been replaced by a program!

Copyright Paul Mutton, http://www.jibble.org/pircbot.php 5 Copyright Paul Mutton, http://www.jibble.org/pircbot.php 6

1
What Can IRC Bots Do? Using Bots Sensibly

n Tell people what the time is n Never annoy other users with your Bot
n Pass messages on to other users n Only place your Bot in channels where it may
n Display information from TV listings be of use or amusement
n Perform simple mathematics n Bots should only speak when spoken to!
n Send and receive files n Make the purpose of your Bot clear
n Monitor channels to generate statistics n Make it clear that you own your Bot
n ... anything you want! n Never try to pretend that it’s not a Bot!

Copyright Paul Mutton, http://www.jibble.org/pircbot.php 7 Copyright Paul Mutton, http://www.jibble.org/pircbot.php 8

What is PircBot? Where Can I Download PircBot?

n A framework for writing IRC Bots with Java n The PircBot homepage
§ Simplifies the task of writing an IRC Bot § http://www.jibble.org/pircbot.php
§ No need to worry about the underlying protocol • Documentation
changelog
§ Very simple Bots can be written within minutes! •

PircBot FAQ
Event-driven architecture

n
• Examples of some Bots that use PircBot
Can make a Bot that responds to certain events
§
n Download the zip file
§ Contains a file named pircbot.jar
§ Also contains a directory full of documentation

Copyright Paul Mutton, http://www.jibble.org/pircbot.php 9 Copyright Paul Mutton, http://www.jibble.org/pircbot.php 10

Extending PircBot An Example: SimpleBot

n To use PircBot, you must import its package


§ import org.jibble. pircbot.*; import org.jibble. pircbot.*;
n PircBot is an abstract class
§ You cannot instantiate it public class SimpleBot extends PircBot {
§ You must extend it and inherit its functionality public SimpleBot() {
§ You can override some of the methods in the setName(“SimpleBot”);
PircBot class to respond to certain events }
}

Copyright Paul Mutton, http://www.jibble.org/pircbot.php 11 Copyright Paul Mutton, http://www.jibble.org/pircbot.php 12

2
Connecting To an IRC Server Some Notes About SimpleBot
public static void main(String[] args) {
SimpleBot bot = new SimpleBot();
n SimpleBot.java
bot.setVerbose(true); § The setName method is inherited from the
try { PircBot class and sets the nick that will be used
bot.connect(“compsoc1.ukc.ac.uk”); when the Bot joins an IRC server
}
catch (Exception e) { n Connecting to an IRC server
System.out.println(“Can’t connect: ” + e); § setVerbose(true) causes everything to be printed
return;
out as it arrives from the IRC server
}
bot.joinChannel(“#bots”); n Each method in the PircBot class is fully
} described in the provided API documentation
Copyright Paul Mutton, http://www.jibble.org/pircbot.php 13 Copyright Paul Mutton, http://www.jibble.org/pircbot.php 14

Making SimpleBot Tell the Time Running Your IRC Bot


n In your SimpleBot class, override the n pircbot.jar contains the classes for PircBot
onMessage method: - n Add this file to your classpath when you
public void onMessage(String channel, String sender, compile or run your IRC Bot manually, e.g.
String login, String hostname,
String message) {
javac –classpath pircbot.jar;. *.java
if (message. equalsIgnoreCase(“time”)) { java –classpath pircbot.jar;. SimpleBot
String time = new java.util.Date().toString();
sendMessage(channel, sender + “: ” + time);
}
n Note: Unix users should use “:” instead of “;”
}

Copyright Paul Mutton, http://www.jibble.org/pircbot.php 15 Copyright Paul Mutton, http://www.jibble.org/pircbot.php 16

Other Built-in PircBot Features Rejoining a Channel When Kicked

n DCC send/receive files public void onKick(String channel, String kickerNick ,


String login, String hostname,
n DCC chat String recipientNick, String reason) {

n Coloured messages if (recipientNick.equalsIgnoreCase(getNick ())) {


n Maintain lists of joined channels and users joinChannel(channel);
}
n List all channels on a server
n Many event-driven methods that may be }

overridden n Note that we only attempt to rejoin the


§ onConnect, onDisconnect, onJoin, onOp, etc. channel if it was us that was kicked

Copyright Paul Mutton, http://www.jibble.org/pircbot.php 17 Copyright Paul Mutton, http://www.jibble.org/pircbot.php 18

3
Reconnecting to an IRC Server PircBot Ident Server
public void onDisconnect() {
n Some IRC servers require you to connect
while (!isConnected()) { from a machine that runs an Ident Server
try { n PircBot can emulate the functionality of an
reconnect();
} Ident Server if you do not already run one
catch (Exception e) { n Provides the IRC server with your Bot’s login
// Couldn’t reconnect.
// Pause for a short while before retrying? when it asks for it
}
}
bot.startIdentServer();
}

Copyright Paul Mutton, http://www.jibble.org/pircbot.php 19 Copyright Paul Mutton, http://www.jibble.org/pircbot.php 20

PircBot Flood Protection Colors and Formatting Examples

n Some IRC servers disconnect clients that String chan = “#bots”;


send too many messages too quickly.
sendMessage(chan, Colors.BOLD + “Hello!”);
n PircBot queues most outgoing messages. Hello!
n Queued messages are sent with a small
delay between them to prevent “flooding” sendMessage(chan, Colors.RED + “Red text”);
Red text
n You can get the current size of this queue by
calling the getOutgoingQueueSize() method sendMessage(chan, Colors.BLUE + “Blue text”);
Blue text

Copyright Paul Mutton, http://www.jibble.org/pircbot.php 21 Copyright Paul Mutton, http://www.jibble.org/pircbot.php 22

Further Text Formatting DCC Send File


File file = new File(“c:/stuff/elvis.mp3”);
sendMessage(chan, Colors.BOLD + Colors.RED + String nick = “Dave”;
“Bold and red”); int timeout = 120000;
Bold and red
dccSendFile(file, nick, timeout);
sendMessage(chan, Colors.BLUE + “Blue “ +
Colors.NORMAL + “normal”);
Blue normal n Target client must be able to establish a TCP
connection to your Bot to receive the file

Copyright Paul Mutton, http://www.jibble.org/pircbot.php 23 Copyright Paul Mutton, http://www.jibble.org/pircbot.php 24

4
User List Example Multiple Server Support in PircBot

n onUserList is called after we join a channel n An individual instance of a subclass of PircBot


n This example overrides the onUserList can only join one IRC server at a time
method and simply prints out each nick n Multiple server support can be achieved by
creating multiple instances
public void onUserList (String channel, User[] users) { § Create a class to control a Collection of PircBot
for (int i = 0; i < users.length; i++) { objects and allow them to interact
User user = users[i];
String nick = user.getNick ();
System.out.println(nick);
}
}

Copyright Paul Mutton, http://www.jibble.org/pircbot.php 25 Copyright Paul Mutton, http://www.jibble.org/pircbot.php 26

IRC Bots Based On PircBot (1) IRC Bots Based On PircBot (2)

n ComicBot n Monty
§ Creates comic strips out of things that people say § The first ever PircBot!
§ ‘Learns’ from what it sees other people saying
§ Dictionary and thesaurus lookup feature
§ Can remind people to do things after a set time
§ Shows TV schedule listings
§ Performs google searches
§ Calculates results of mathematical expressions
§ etc.
Copyright Paul Mutton, http://www.jibble.org/pircbot.php 27 Copyright Paul Mutton, http://www.jibble.org/pircbot.php 28

IRC Bots Based On PircBot (3) An IRC Client Based On PircBot

n SocialNetworkBot n ScreenIRC
§ Attempts to produce graphical representations of § IRC client with a Swing GUI
“who talks to who” on IRC channels § Can be ‘detached’ from a server and reconnected
without appearing to have ever left

Copyright Paul Mutton, http://www.jibble.org/pircbot.php 29 Copyright Paul Mutton, http://www.jibble.org/pircbot.php 30

5
Final Words...

n If you want to make your own IRC Bot that


uses PircBot, then remember that these
slides only provide a briefest glimpse into
what you may need to know
n Refer to the API documentation that is
included inside the zip file
n A good starting place is to read the
documentation for the PircBot class

Copyright Paul Mutton, http://www.jibble.org/pircbot.php 31

Das könnte Ihnen auch gefallen