我如何在Java中开发“xdcc send”?

时间:2014-08-11 07:24:11

标签: java irc

我发现此代码与IRC服务器通信(见下文)。但是我没有找到如何发送命令下载或上传xdcc。

连接到IRC服务器并定位在频道中。我想发送一个像。

这样的命令

/ msg bot_name xdcc发送#number_of_file

提前感谢您的答案,示例和帮助。

import java.io.*;
import java.net.*;

public class HackBot {

    public static void main(String[] args) throws Exception {

        // The server to connect to and our details.
        String server = "irc.freenode.net";
        String nick = "simple_bot";
        String login = "simple_bot";

        // The channel which the bot will join.
        String channel = "#irchacks";

        // Connect directly to the IRC server.
        Socket socket = new Socket(server, 6667);
        BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(socket.getOutputStream( )));
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(socket.getInputStream( )));

        // Log on to the server.
        writer.write("NICK " + nick + "\r\n");
        writer.write("USER " + login + " 8 * : Java IRC Hacks Bot\r\n");
        writer.flush( );

        // Read lines from the server until it tells us we have connected.
        String line = null;
        while ((line = reader.readLine( )) != null) {
            if (line.indexOf("004") >= 0) {
                // We are now logged in.
                break;
            }
            else if (line.indexOf("433") >= 0) {
                System.out.println("Nickname is already in use.");
                return;
            }
        }

        // Join the channel.
        writer.write("JOIN " + channel + "\r\n");
        writer.flush( );

        // Keep reading lines from the server.
        while ((line = reader.readLine( )) != null) {
            if (line.toUpperCase( ).startsWith("PING ")) {
                // We must respond to PINGs to avoid being disconnected.
                writer.write("PONG " + line.substring(5) + "\r\n");
                writer.write("PRIVMSG " + channel + " :I got pinged!\r\n");
                writer.flush( );
            }
            else {
                // Print the raw line received by the bot.
                System.out.println(line);
            }
        }
    }

}

1 个答案:

答案 0 :(得分:0)

您的代码仅显示打开与IRC服务器的连接并保持连接所需的最低要求,实际上整个IRC协议要复杂得多,并且未在代码中实现。

xdcc send只是私下发送给IRC服务器的特定其他用户(通常是机器人)的普通IRC消息,因此您可以使用命令PRIVMSG发送它:

writer.write("PRIVMSG " + botNickName + " :xdcc send #" + numberOfPack + "\r\n");

其中botNickNamenumberOfPack是两个String变量,包含bot的昵称(即消息的接收者)和您感兴趣的包的数量(以字符串格式)。

然而,你必须考虑DCC是一个完全不同于IRC协议本身的协议:它在IRC上使用CTCP消息:

DCC SEND <filename> <ip> <port>

仅启动DCC会话,但随后有DCC协议以管理客户端到客户端的通信。因此,如果你真的想让DCC工作,你也应该实现它,但这不会是一个快速的工作。