Ich suche ein Programm, das automatisch alle 59 Minuten einen Beitrag in einem IRC macht. Sinn der Sache ist, dort wird man gekickt, wenn man 60 Minuten idelt. Hättet ihr da eine Idee? Wäre dankbar für eure Hilfe.
25.05.2010 14:52
niwax
Hardcore-Coder
Beiträge: 3.822
Registriert seit: Dec 2009
public class Bot {
private final String server = "irc.euirc.net";
private final int port = 6667;
private final String nick = "euterhut";
private final String hostname = "IHaveNoIdeaWhatThisDoes";
private final String servername = "IHaveNoIdeaWhatThisDoes";
private final String realname = "Fritz";
private final String channel = "#kirschmet";
/**
* Connect to the given IRC server.
**/
private void connect() throws IOException {
System.out.println("Connecting...");
irc = new Socket(server, port);
out = new BufferedWriter(new OutputStreamWriter(irc.getOutputStream(), "UTF-8"));
in = new BufferedReader(new InputStreamReader(irc.getInputStream(), "UTF-8"));
out.write("NICK " + nick + "\n");
out.write("USER " + nick + " " + hostname + " " + servername + " " + realname + "\n");
out.flush();
joined = false;
running = true;
System.out.println("Connected.");
}
/**
* The most important method. Handles PING requests and everything else.
**/
private void communicate() throws IOException {
// loop forever
while(running) {
// New message coming in!
if(in.ready()) {
/* This is called every time a message arrives. */
/* NOTE: Could also be server messages, PINGs etc… */
String line = in.readLine();
// Respond to PING so that we don't get disconnected
if(isPing(line)) {
doPing(line.substring(5));
}
// Join if we haven't joined the channel yet
if(!joined && isConnected(line)) {
join();
}
// Output the line for debugging purposes
System.out.println(line);
}
/* This is called approximately all 100 milliseconds, probably less often. */
/* Adjust to your liking. */
/* Note that it has to be called very often to check for new PING requests. */
wait(100);
// SPAM THE CHANNEL FOR GREAT JUSTICE!
if(joined) say("Hello!");
}
}
/**
* Determines whether the given message is a confirmation
* that you have successfully connected to the IRC server.
*/
private boolean isConnected(String line) {
Pattern p = Pattern.compile("End of /MOTD command.", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(line);
return m.find();
}
/**
* Join the given channel.
*/
private void join() throws IOException {
System.out.println("Joining channel...");
out.write( "JOIN " + channel + "\n" );
out.flush();
joined = true;
System.out.println("Joined channel.");
}
/**
* Figure out if an incoming message is a PING request.
*/
private boolean isPing(String line) {
Pattern p = Pattern.compile("^PING", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(line);
return m.find();
}
/**
* Respond to a PING message. Necessary so you don't get disconnected.
*/
private void doPing(String receiver) throws IOException {
out.write("PONG " + receiver + "\n");
out.flush();
System.out.println("Responded to ping.");
}