Commit d05cecef authored by liujiahua123123's avatar liujiahua123123

Merge remote-tracking branch 'origin/master'

parents 2f963381 7a5f7de8
...@@ -3,37 +3,48 @@ ...@@ -3,37 +3,48 @@
一个以<b>TIM QQ协议</b>驱动的JAVA(+Kotlin) QQ机器人服务端核心 一个以<b>TIM QQ协议</b>驱动的JAVA(+Kotlin) QQ机器人服务端核心
我们坚持免费与开源 我们坚持免费与开源
项目处于快速开发阶段, 现在已经可以接受和发送群聊/好友消息. 项目处于快速开发阶段
协议来自网络上开源项目 协议来自网络上开源项目
一切开发旨在学习, 请勿用于非法用途 一切开发旨在学习, 请勿用于非法用途
<br> ### 抢先体验
现在你可以使用 Mirai 内置的一些测试qq号体验 Mirai, 但我们现在还不建议你使用自己的 qq 号登录
1. Clone
2. Import as Maven project
3. Run [MiraiMain](mirai-core/src/main/java/net/mamoe/mirai/MiraiMain.java#L7)
A JAVA(+Kotlin) powered open-source project under GPL license<br> #### 事件 Hook (Kotlin)
It use protocols from <i>TIM QQ</i>, that is, it won't be affected by the close of <i>Smart QQ</i><br> ![event hook.png](.github/event%20hook.png)
The project is all for <b>learning proposes</b> and still in <b>developing stage</b><br> ![AYWVE86P](.github/A%7DYWVE860U%28%25YQD%24R1GB1%5BP.png)
#### 图片测试
**现在可以接受图片消息**(并解析为消息链):
![JsssF](.github/J%5DCE%29IK4BU08%28EO~UVLJ%7B%5BF.png)
![](.README_images/68f8fec9.png)
不过我们还正在努力做发送图片
### 代码结构 ### 代码结构
Network部分使用 Kotlin 完成(因为kt有对 unsigned byte 的支持). Network部分使用 Kotlin 完成(因为kt有对 unsigned byte 的支持).
与插件相关性强(或其他在二次开发中容易接触)的部分使用 Java 完成, 与插件相关性强(或其他在二次开发中容易接触)的部分尽量使用 Java 完成,
同时也会针对kotlin提供优化的方法调用. 例如对'+'操作符的重载: `String+BufferedImage+QQ.At+Face+URL+String+File` 将会被自动处理为String消息. 若使用 Kotlin, 我们会通过 Java interface 实现或 javadoc 帮助未接触过 Kotlin 的开发者.
即使你完全不了解 Kotlin, 你也可以正常开发.
### TODO ## TODO
- [x] 事件(Event)模块 - [x] 事件(Event)模块
- [ ] 插件(Plugin)模块 - [ ] 插件(Plugin)模块 **(Working on)**
- [x] Network - Touch - [x] Network - Touch
- [X] Network - Login - [X] Network - Login
- [X] Network - Session - [X] Network - Session
- [ ] Network - Verification Code **(Working on)** - [X] Network - Verification Code
- [X] Network - Message Receiving - [X] Network - Message Receiving
- [X] Network - Message Sending - [X] Network - Message Sending
- [ ] Network - Events **(Working on)** - [ ] Network - Events **(Working on)**
- [ ] Robot - Friend/group list - [ ] Bot - Friend/group list
- [ ] Robot - Actions(joining group, adding friend, etc.) - [ ] Bot - Actions(joining group, adding friend, etc.)
- [ ] Message Section **(Working on)** - [ ] Message Section **(Working on)**
- [ ] Contact - [ ] Contact
- [ ] UI **(Working on)** - [ ] UI
<br> <br>
...@@ -42,18 +53,24 @@ Network部分使用 Kotlin 完成(因为kt有对 unsigned byte 的支持). ...@@ -42,18 +53,24 @@ Network部分使用 Kotlin 完成(因为kt有对 unsigned byte 的支持).
- Java 11 或更高 - Java 11 或更高
- Kotlin 1.3 或更高 - Kotlin 1.3 或更高
### 插件开发 ### 插件开发
``` php ``` text
to be continued to be continued
... ...
``` ```
<br>
A JAVA(+Kotlin) powered open-source project under GPL license<br>
It use protocols from <i>TIM QQ</i>, that is, it won't be affected by the close of <i>Smart QQ</i><br>
The project is all for <b>learning proposes</b> and still in <b>developing stage</b><br>
## Usage ## Usage
### Requirements ### Requirements
- Java 11 or higher - Java 11 or higher
- Kotlin 1.3 or higher - Kotlin 1.3 or higher
### Plugin Development ### Plugin Development
``` php ``` text
to be continued to be continued
... ...
``` ```
......
This diff is collapsed.
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent> <parent>
<artifactId>mirai</artifactId>
<groupId>net.mamoe</groupId> <groupId>net.mamoe</groupId>
<artifactId>mirai</artifactId>
<version>1.0</version> <version>1.0</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mirai-native</artifactId> <artifactId>mirai-api</artifactId>
<version>1.0</version> <version>1.0</version>
<dependencies>
</dependencies>
<build>
<resources>
<resource>
<directory>/src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
<plugins>
</plugins>
</build>
</project> </project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>net.mamoe</groupId>
<artifactId>mirai</artifactId>
<version>1.0</version>
</parent>
<artifactId>mirai-console</artifactId>
<version>1.0</version>
<dependencies>
</dependencies>
<build>
<resources>
<resource>
<directory>/src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
<plugins>
</plugins>
</build>
</project>
\ No newline at end of file
...@@ -9,10 +9,6 @@ ...@@ -9,10 +9,6 @@
<packaging>jar</packaging> <packaging>jar</packaging>
<properties>
<kotlin.version>1.3.41</kotlin.version>
</properties>
<parent> <parent>
<groupId>net.mamoe</groupId> <groupId>net.mamoe</groupId>
<artifactId>mirai</artifactId> <artifactId>mirai</artifactId>
...@@ -21,7 +17,6 @@ ...@@ -21,7 +17,6 @@
</parent> </parent>
<dependencies> <dependencies>
<!-- https://mvnrepository.com/artifact/org.jetbrains.kotlinx/kotlinx-coroutines-core -->
<dependency> <dependency>
<groupId>org.jetbrains.kotlinx</groupId> <groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-coroutines-core</artifactId> <artifactId>kotlinx-coroutines-core</artifactId>
...@@ -31,42 +26,36 @@ ...@@ -31,42 +26,36 @@
<groupId>io.netty</groupId> <groupId>io.netty</groupId>
<artifactId>netty-all</artifactId> <artifactId>netty-all</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>net.java.dev.jna</groupId> <groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId> <artifactId>jna</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.jetbrains.kotlin</groupId> <groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId> <artifactId>kotlin-stdlib</artifactId>
<version>${kotlin.version}</version>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency> <dependency>
<groupId>org.apache.logging.log4j</groupId> <groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId> <artifactId>log4j-core</artifactId>
<version>2.12.1</version>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.18</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.yaml</groupId> <groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId> <artifactId>snakeyaml</artifactId>
<version>1.18</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.jetbrains.kotlin</groupId> <groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId> <artifactId>kotlin-reflect</artifactId>
<version>1.3.41</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
</dependencies> </dependencies>
<build> <build>
<resources> <resources>
<resource> <resource>
<directory>/src/main/resources</directory> <directory>/src/main/resources</directory>
...@@ -94,63 +83,16 @@ ...@@ -94,63 +83,16 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId> <artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>shaded</shadedClassifierName>
</configuration>
</plugin> </plugin>
<plugin> <plugin>
<groupId>org.jetbrains.kotlin</groupId> <groupId>org.projectlombok</groupId>
<artifactId>kotlin-maven-plugin</artifactId> <artifactId>lombok-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<jvmTarget>1.8</jvmTarget>
</configuration>
</plugin> </plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.jetbrains.kotlin</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>kotlin-maven-plugin</artifactId>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
......
...@@ -3,47 +3,59 @@ package net.mamoe.mirai; ...@@ -3,47 +3,59 @@ package net.mamoe.mirai;
import lombok.Getter; import lombok.Getter;
import net.mamoe.mirai.contact.Group; import net.mamoe.mirai.contact.Group;
import net.mamoe.mirai.contact.QQ; import net.mamoe.mirai.contact.QQ;
import net.mamoe.mirai.network.RobotNetworkHandler; import net.mamoe.mirai.network.BotNetworkHandler;
import net.mamoe.mirai.utils.BotAccount;
import net.mamoe.mirai.utils.ContactList; import net.mamoe.mirai.utils.ContactList;
import net.mamoe.mirai.utils.RobotAccount;
import net.mamoe.mirai.utils.config.MiraiConfigSection; import net.mamoe.mirai.utils.config.MiraiConfigSection;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.io.Closeable; import java.io.Closeable;
import java.util.*; import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
/** /**
* Mirai 的机器人. 一个机器人实例登录一个 QQ 账号. * Mirai 的机器人. 一个机器人实例登录一个 QQ 账号.
* Mirai 为多账号设计, 可同时维护多个机器人账号. * Mirai 为多账号设计, 可同时维护多个机器人.
* <br> * <br>
* {@link Robot} 由 2 个模块组成. * {@link Bot} 由 2 个模块组成.
* {@linkplain ContactSystem 联系人管理}: 可通过 {@link Robot#contacts} 访问 * {@linkplain ContactSystem 联系人管理}: 可通过 {@link Bot#contacts} 访问
* {@linkplain RobotNetworkHandler 网络处理器}: 可通过 {@link Robot#network} 访问 * {@linkplain BotNetworkHandler 网络处理器}: 可通过 {@link Bot#network} 访问
* <br> * <br>
* 另外地, 若你需要得到机器人的 QQ 账号, 请访问 {@link Robot#account} * 若你需要得到机器人的 QQ 账号, 请访问 {@link Bot#account}
* 若你需要得到服务器上所有机器人列表, 请访问 {@link Robot#instances} * 若你需要得到服务器上所有机器人列表, 请访问 {@link Bot#instances}
* *
* @author Him188moe * @author Him188moe
* @author NatrualHG * @author NatrualHG
* @see net.mamoe.mirai.contact.Contact * @see net.mamoe.mirai.contact.Contact
* *
* <p> * <p>
* Robot that is the base of the whole program. * Bot that is the base of the whole program.
* It contains a {@link ContactSystem}, which manage contacts such as {@link QQ} and {@link Group}. * It contains a {@link ContactSystem}, which manage contacts such as {@link QQ} and {@link Group}.
*/ */
public final class Robot implements Closeable { public final class Bot implements Closeable {
public static final List<Robot> instances = Collections.synchronizedList(new LinkedList<>()); public static final List<Bot> instances = Collections.synchronizedList(new LinkedList<>());
public final RobotAccount account; {
instances.add(this);
}
public final int id = _id.getAndAdd(1);
public final BotAccount account;
public final ContactSystem contacts = new ContactSystem(); public final ContactSystem contacts = new ContactSystem();
public final RobotNetworkHandler network; public final BotNetworkHandler network;
@Override
public String toString() {
return String.format("Bot{id=%d,qq=%d}", id, this.account.qqNumber);
}
/** /**
* Robot 联系人管理. * Bot 联系人管理.
* *
* @see Robot#contacts * @see Bot#contacts
*/ */
public final class ContactSystem { public final class ContactSystem {
private final ContactList<Group> groups = new ContactList<>(); private final ContactList<Group> groups = new ContactList<>();
...@@ -55,14 +67,14 @@ public final class Robot implements Closeable { ...@@ -55,14 +67,14 @@ public final class Robot implements Closeable {
public QQ getQQ(long qqNumber) { public QQ getQQ(long qqNumber) {
if (!this.qqs.containsKey(qqNumber)) { if (!this.qqs.containsKey(qqNumber)) {
this.qqs.put(qqNumber, new QQ(Robot.this, qqNumber)); this.qqs.put(qqNumber, new QQ(Bot.this, qqNumber));
} }
return this.qqs.get(qqNumber); return this.qqs.get(qqNumber);
} }
public Group getGroupByNumber(long groupNumber) { public Group getGroupByNumber(long groupNumber) {
if (!this.groups.containsKey(groupNumber)) { if (!this.groups.containsKey(groupNumber)) {
this.groups.put(groupNumber, new Group(Robot.this, groupNumber)); this.groups.put(groupNumber, new Group(Bot.this, groupNumber));
} }
return groups.get(groupNumber); return groups.get(groupNumber);
} }
...@@ -83,9 +95,9 @@ public final class Robot implements Closeable { ...@@ -83,9 +95,9 @@ public final class Robot implements Closeable {
return owners.contains(ownerName); return owners.contains(ownerName);
} }
public Robot(MiraiConfigSection<Object> data) throws Throwable { public Bot(MiraiConfigSection<Object> data) throws Throwable {
this( this(
new RobotAccount( new BotAccount(
data.getLongOrThrow("account", () -> new IllegalArgumentException("account")), data.getLongOrThrow("account", () -> new IllegalArgumentException("account")),
data.getStringOrThrow("password", () -> new IllegalArgumentException("password")) data.getStringOrThrow("password", () -> new IllegalArgumentException("password"))
), ),
...@@ -93,12 +105,12 @@ public final class Robot implements Closeable { ...@@ -93,12 +105,12 @@ public final class Robot implements Closeable {
); );
} }
public Robot(@NotNull RobotAccount account, @NotNull List<String> owners) { public Bot(@NotNull BotAccount account, @NotNull List<String> owners) {
Objects.requireNonNull(account); Objects.requireNonNull(account);
Objects.requireNonNull(owners); Objects.requireNonNull(owners);
this.account = account; this.account = account;
this.owners = Collections.unmodifiableList(owners); this.owners = Collections.unmodifiableList(owners);
this.network = new RobotNetworkHandler(this); this.network = new BotNetworkHandler(this);
} }
...@@ -109,5 +121,12 @@ public final class Robot implements Closeable { ...@@ -109,5 +121,12 @@ public final class Robot implements Closeable {
this.contacts.qqs.clear(); this.contacts.qqs.clear();
} }
public void addFriend(long qq) {
}
/* PRIVATE */
private static final AtomicInteger _id = new AtomicInteger(0);
} }
...@@ -6,8 +6,10 @@ import net.mamoe.mirai.event.events.server.ServerDisabledEvent; ...@@ -6,8 +6,10 @@ import net.mamoe.mirai.event.events.server.ServerDisabledEvent;
import net.mamoe.mirai.event.events.server.ServerEnabledEvent; import net.mamoe.mirai.event.events.server.ServerEnabledEvent;
import net.mamoe.mirai.network.packet.login.LoginState; import net.mamoe.mirai.network.packet.login.LoginState;
import net.mamoe.mirai.task.MiraiTaskManager; import net.mamoe.mirai.task.MiraiTaskManager;
import net.mamoe.mirai.utils.BotAccount;
import net.mamoe.mirai.utils.LoggerTextFormat; import net.mamoe.mirai.utils.LoggerTextFormat;
import net.mamoe.mirai.utils.MiraiLogger; import net.mamoe.mirai.utils.MiraiLogger;
import net.mamoe.mirai.utils.MiraiLoggerKt;
import net.mamoe.mirai.utils.config.MiraiConfig; import net.mamoe.mirai.utils.config.MiraiConfig;
import net.mamoe.mirai.utils.config.MiraiConfigSection; import net.mamoe.mirai.utils.config.MiraiConfigSection;
import net.mamoe.mirai.utils.setting.MiraiSettingListSection; import net.mamoe.mirai.utils.setting.MiraiSettingListSection;
...@@ -16,7 +18,9 @@ import net.mamoe.mirai.utils.setting.MiraiSettings; ...@@ -16,7 +18,9 @@ import net.mamoe.mirai.utils.setting.MiraiSettings;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.List;
import java.util.Scanner; import java.util.Scanner;
import java.util.concurrent.ExecutionException;
/** /**
* @author NaturalHG * @author NaturalHG
...@@ -150,9 +154,8 @@ public class MiraiServer { ...@@ -150,9 +154,8 @@ public class MiraiServer {
MiraiConfigSection<Object> section = new MiraiConfigSection<>(); MiraiConfigSection<Object> section = new MiraiConfigSection<>();
System.out.println("/");
Scanner scanner = new Scanner(System.in); Scanner scanner = new Scanner(System.in);
getLogger().info("Input a " + LoggerTextFormat.RED + " QQ number " + LoggerTextFormat.GREEN + "for default robotNetworkHandler"); getLogger().info("Input a " + LoggerTextFormat.RED + " QQ number " + LoggerTextFormat.GREEN + "for default botNetworkHandler");
getLogger().info("输入用于默认机器人的QQ号"); getLogger().info("输入用于默认机器人的QQ号");
long qqNumber = scanner.nextLong(); long qqNumber = scanner.nextLong();
getLogger().info("Input the password for that QQ account"); getLogger().info("Input the password for that QQ account");
...@@ -173,30 +176,60 @@ public class MiraiServer { ...@@ -173,30 +176,60 @@ public class MiraiServer {
getLogger().info(LoggerTextFormat.GREEN + "Server enabled; Welcome to Mirai"); getLogger().info(LoggerTextFormat.GREEN + "Server enabled; Welcome to Mirai");
getLogger().info("Mirai Version=" + MiraiServer.MIRAI_VERSION + " QQ Version=" + MiraiServer.QQ_VERSION); getLogger().info("Mirai Version=" + MiraiServer.MIRAI_VERSION + " QQ Version=" + MiraiServer.QQ_VERSION);
getLogger().info("Initializing [Robot]s"); getLogger().info("Initializing [Bot]s");
try {
getAvailableBot();
} catch (ExecutionException | InterruptedException e) {
e.printStackTrace();
}
/*
this.qqs.keySet().stream().map(key -> this.qqs.getSection(key)).forEach(section -> { this.qqs.keySet().stream().map(key -> this.qqs.getSection(key)).forEach(section -> {
getLogger().info("Initializing [Robot] " + section.getString("account")); getLogger().info("Initializing [Bot] " + section.getString("account"));
try { try {
Robot robot = new Robot(section); Bot bot = new Bot(section);
var state = robot.network.tryLogin$mirai_core().get(); var state = bot.network.tryLogin$mirai_core().get();
//robot.network.tryLogin$mirai_core().whenComplete((state, e) -> { //bot.network.tryLogin$mirai_core().whenComplete((state, e) -> {
if (state == LoginState.SUCCEED) { if (state == LoginState.SUCCESS) {
Robot.instances.add(robot); Bot.instances.add(bot);
getLogger().success(" Login Succeed"); getLogger().success(" Login Succeed");
} else { } else {
getLogger().error(" Login Failed with error " + state); getLogger().error(" Login Failed with error " + state);
robot.close(); bot.close();
} }
// }).get(); // }).get();
} catch (Throwable e) { } catch (Throwable e) {
e.printStackTrace(); e.printStackTrace();
getLogger().error("Could not load QQ robots config!"); getLogger().error("Could not load QQ bots config!");
System.exit(1); System.exit(1);
} }
}); });*/
} }
String qqList = "3150499752----1234567890\n" +
"3119292829----987654321\n" +
"2399148773----12345678910\n" +
"3145561616----987654321\n" +
"2374150554----12345678910\n" +
"2375307394----12345678910\n" +
"2401645747----12345678910\n" +
"1515419818----1234567890\n" +
"3107367848----987654321\n";
private Bot getAvailableBot() throws ExecutionException, InterruptedException {
for (String it : qqList.split("\n")) {
var strings = it.split("----");
var bot = new Bot(new BotAccount(Long.parseLong(strings[0]), strings[1]), List.of());
if (bot.network.tryLogin$mirai_core().get() == LoginState.SUCCESS) {
MiraiLoggerKt.success(bot, "Login succeed");
return bot;
}
}
throw new RuntimeException();
}
} }
package net.mamoe.mirai.contact package net.mamoe.mirai.contact
import net.mamoe.mirai.Robot import net.mamoe.mirai.Bot
import net.mamoe.mirai.message.Message import net.mamoe.mirai.message.Message
import net.mamoe.mirai.message.defaults.MessageChain
import net.mamoe.mirai.message.defaults.PlainText import net.mamoe.mirai.message.defaults.PlainText
/** /**
* A contact is a [QQ] or a [Group] for one particular [Robot] instance only. * A contact is a [QQ] or a [Group] for one particular [Bot] instance only.
* *
* @param robot Owner [Robot] * @param bot Owner [Bot]
* @author Him188moe * @author Him188moe
*/ */
abstract class Contact(val robot: Robot, val number: Long) { abstract class Contact internal constructor(val bot: Bot, val number: Long) {
/** /**
* Async * Async
*/ */
abstract fun sendMessage(message: Message) abstract fun sendMessage(message: MessageChain)
fun sendMessage(message: Message) {
if (message is MessageChain) {
return sendMessage(message)
}
return sendMessage(message.toChain())
}
fun sendMessage(message: String) { fun sendMessage(message: String) {
this.sendMessage(PlainText(message)) this.sendMessage(PlainText(message))
......
package net.mamoe.mirai.contact package net.mamoe.mirai.contact
import net.mamoe.mirai.Robot import net.mamoe.mirai.Bot
import net.mamoe.mirai.message.Message import net.mamoe.mirai.contact.Group.Companion.groupNumberToId
import net.mamoe.mirai.message.defaults.MessageChain
import net.mamoe.mirai.utils.ContactList import net.mamoe.mirai.utils.ContactList
import java.io.Closeable import java.io.Closeable
class Group(robot: Robot, number: Long) : Contact(robot, number), Closeable { /**
* 群
*
* Java 获取 groupNumber: `group.getNumber()`
* Java 获取所属 bot: `group.getBot()`
* Java 获取群成员列表: `group.getMembers()`
* Java 获取 groupId: `group.getGroupId()`
*
* Java 调用 [groupNumberToId] : `Group.groupNumberToId(number)`
*/
class Group(bot: Bot, number: Long) : Contact(bot, number), Closeable {
val groupId = groupNumberToId(number) val groupId = groupNumberToId(number)
val members = ContactList<QQ>() val members = ContactList<QQ>()
override fun sendMessage(message: Message) { override fun sendMessage(message: MessageChain) {
robot.network.messageHandler.sendGroupMessage(this, message) bot.network.messageHandler.sendGroupMessage(this, message)
} }
override fun sendXMLMessage(message: String) { override fun sendXMLMessage(message: String) {
...@@ -23,7 +34,7 @@ class Group(robot: Robot, number: Long) : Contact(robot, number), Closeable { ...@@ -23,7 +34,7 @@ class Group(robot: Robot, number: Long) : Contact(robot, number), Closeable {
companion object { companion object {
@JvmStatic @JvmStatic
fun groupNumberToId(number: Long): Long { fun groupNumberToId(number: Long): Long {//求你别出错
val left: Long = number.toString().let { val left: Long = number.toString().let {
if (it.length < 6) { if (it.length < 6) {
return@groupNumberToId number return@groupNumberToId number
...@@ -61,7 +72,7 @@ class Group(robot: Robot, number: Long) : Contact(robot, number), Closeable { ...@@ -61,7 +72,7 @@ class Group(robot: Robot, number: Long) : Contact(robot, number), Closeable {
} }
@JvmStatic @JvmStatic
fun groupIdToNumber(id: Long): Long { fun groupIdToNumber(id: Long): Long {//求你别出错
var left: Long = id.toString().let { var left: Long = id.toString().let {
if (it.length < 6) { if (it.length < 6) {
return@groupIdToNumber id return@groupIdToNumber id
......
package net.mamoe.mirai.contact package net.mamoe.mirai.contact
import net.mamoe.mirai.Robot import net.mamoe.mirai.Bot
import net.mamoe.mirai.message.Message import net.mamoe.mirai.message.Message
import net.mamoe.mirai.message.defaults.At import net.mamoe.mirai.message.defaults.At
import net.mamoe.mirai.message.defaults.MessageChain
/** /**
* QQ 账号. * QQ 账号.
* 注意: 一个 [QQ] 实例并不是独立的, 它属于一个 [Robot]. * 注意: 一个 [QQ] 实例并不是独立的, 它属于一个 [Bot].
*
* Java 获取 qq 号: `qq.getNumber()`
* Java 获取所属 bot: `qq.getBot()`
* *
* A QQ instance helps you to receive message from or send message to. * A QQ instance helps you to receive message from or send message to.
* Notice that, one QQ instance belong to one [Robot], that is, QQ instances from different [Robot] are NOT the same. * Notice that, one QQ instance belong to one [Bot], that is, QQ instances from different [Bot] are NOT the same.
* *
* @author Him188moe * @author Him188moe
*/ */
class QQ(robot: Robot, number: Long) : Contact(robot, number) { class QQ(bot: Bot, number: Long) : Contact(bot, number) {
override fun sendMessage(message: Message) { override fun sendMessage(message: MessageChain) {
robot.network.messageHandler.sendFriendMessage(this, message) bot.network.messageHandler.sendFriendMessage(this, message)
} }
override fun sendXMLMessage(message: String) { override fun sendXMLMessage(message: String) {
......
...@@ -62,6 +62,6 @@ fun <C : KClass<E>, E : MiraiEvent> C.hookWhile(hook: (E) -> Boolean) { ...@@ -62,6 +62,6 @@ fun <C : KClass<E>, E : MiraiEvent> C.hookWhile(hook: (E) -> Boolean) {
private class MiraiEventHookSimple<E : MiraiEvent>(clazz: Class<E>, val hook: (E) -> Boolean) : MiraiEventHook<E>(clazz) { private class MiraiEventHookSimple<E : MiraiEvent>(clazz: Class<E>, val hook: (E) -> Boolean) : MiraiEventHook<E>(clazz) {
override fun accept(event: MiraiEvent?): Boolean { override fun accept(event: MiraiEvent?): Boolean {
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
return hook.invoke(event as E) return !hook.invoke(event as E)
} }
} }
\ No newline at end of file
package net.mamoe.mirai.event.events.robot; package net.mamoe.mirai.event.events.bot;
import net.mamoe.mirai.Robot; import net.mamoe.mirai.Bot;
import net.mamoe.mirai.event.MiraiEvent; import net.mamoe.mirai.event.MiraiEvent;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.Objects; import java.util.Objects;
public abstract class RobotEvent extends MiraiEvent { public abstract class BotEvent extends MiraiEvent {
private final Robot robot; private final Bot bot;
public RobotEvent(@NotNull Robot robot) { public BotEvent(@NotNull Bot bot) {
this.robot = Objects.requireNonNull(robot); this.bot = Objects.requireNonNull(bot);
} }
@NotNull @NotNull
public Robot getRobot() { public Bot getBot() {
return robot; return bot;
} }
} }
package net.mamoe.mirai.event.events.robot package net.mamoe.mirai.event.events.bot
import net.mamoe.mirai.Robot import net.mamoe.mirai.Bot
import net.mamoe.mirai.event.MiraiEvent import net.mamoe.mirai.event.MiraiEvent
/** /**
* @author Him188moe * @author Him188moe
*/ */
class RobotLoginEvent(val robot: Robot) : MiraiEvent() class BotLoginEvent(val bot: Bot) : MiraiEvent()
class RobotLogoutEvent(val robot: Robot) : MiraiEvent() class BotLogoutEvent(val bot: Bot) : MiraiEvent()
class RobotMessageReceivedEvent(val robot: Robot, val type: Type, val message: String) : MiraiEvent() { class BotMessageReceivedEvent(val bot: Bot, val type: Type, val message: String) : MiraiEvent() {
enum class Type { enum class Type {
FRIEND, FRIEND,
GROUP GROUP
......
package net.mamoe.mirai.event.events.bot;
import net.mamoe.mirai.Bot;
public final class BotLoginSucceedEvent extends BotEvent {
public BotLoginSucceedEvent(Bot bot) {
super(bot);
}
}
package net.mamoe.mirai.event.events.group; package net.mamoe.mirai.event.events.group;
import net.mamoe.mirai.Robot; import net.mamoe.mirai.Bot;
import net.mamoe.mirai.contact.Group; import net.mamoe.mirai.contact.Group;
import net.mamoe.mirai.event.events.robot.RobotEvent; import net.mamoe.mirai.event.events.bot.BotEvent;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/** /**
* @author Him188moe * @author Him188moe
*/ */
public abstract class GroupEvent extends RobotEvent { public abstract class GroupEvent extends BotEvent {
private final Group group; private final Group group;
public GroupEvent(Robot robot, Group group) { public GroupEvent(Bot bot, Group group) {
super(robot); super(bot);
this.group = group; this.group = group;
} }
......
package net.mamoe.mirai.event.events.group; package net.mamoe.mirai.event.events.group;
import net.mamoe.mirai.Robot; import net.mamoe.mirai.Bot;
import net.mamoe.mirai.contact.Group; import net.mamoe.mirai.contact.Group;
import net.mamoe.mirai.contact.QQ; import net.mamoe.mirai.contact.QQ;
import net.mamoe.mirai.message.defaults.MessageChain; import net.mamoe.mirai.message.defaults.MessageChain;
...@@ -14,8 +14,8 @@ public final class GroupMessageEvent extends GroupEvent { ...@@ -14,8 +14,8 @@ public final class GroupMessageEvent extends GroupEvent {
private final MessageChain messageChain; private final MessageChain messageChain;
private final String messageString; private final String messageString;
public GroupMessageEvent(@NotNull Robot robot, @NotNull Group group, @NotNull QQ sender, @NotNull MessageChain messageChain) { public GroupMessageEvent(@NotNull Bot bot, @NotNull Group group, @NotNull QQ sender, @NotNull MessageChain messageChain) {
super(robot, group); super(bot, group);
this.sender = sender; this.sender = sender;
this.messageChain = messageChain; this.messageChain = messageChain;
this.messageString = messageChain.toString(); this.messageString = messageChain.toString();
......
package net.mamoe.mirai.event.events.network; package net.mamoe.mirai.event.events.network;
import net.mamoe.mirai.Bot;
import net.mamoe.mirai.event.Cancellable; import net.mamoe.mirai.event.Cancellable;
import net.mamoe.mirai.network.packet.ClientPacket; import net.mamoe.mirai.network.packet.ClientPacket;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/** /**
* Packet 已经 {@link ClientPacket#encode()}, 即将被发送 * Packet 已经 encoded, 即将被发送
* *
* @author Him188moe * @author Him188moe
*/ */
public final class BeforePacketSendEvent extends ClientPacketEvent implements Cancellable { public final class BeforePacketSendEvent extends ClientPacketEvent implements Cancellable {
public BeforePacketSendEvent(@NotNull ClientPacket packet) { public BeforePacketSendEvent(@NotNull Bot bot, @NotNull ClientPacket packet) {
super(packet); super(bot, packet);
} }
} }
package net.mamoe.mirai.event.events.network; package net.mamoe.mirai.event.events.network;
import net.mamoe.mirai.Bot;
import net.mamoe.mirai.network.packet.ClientPacket; import net.mamoe.mirai.network.packet.ClientPacket;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
...@@ -7,8 +8,8 @@ import org.jetbrains.annotations.NotNull; ...@@ -7,8 +8,8 @@ import org.jetbrains.annotations.NotNull;
* @author Him188moe * @author Him188moe
*/ */
public abstract class ClientPacketEvent extends PacketEvent { public abstract class ClientPacketEvent extends PacketEvent {
public ClientPacketEvent(@NotNull ClientPacket packet) { public ClientPacketEvent(@NotNull Bot bot, @NotNull ClientPacket packet) {
super(packet); super(bot, packet);
} }
@Override @Override
......
package net.mamoe.mirai.event.events.network; package net.mamoe.mirai.event.events.network;
import net.mamoe.mirai.event.MiraiEvent; import net.mamoe.mirai.Bot;
import net.mamoe.mirai.event.events.bot.BotEvent;
import net.mamoe.mirai.network.packet.Packet; import net.mamoe.mirai.network.packet.Packet;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
...@@ -9,10 +10,11 @@ import java.util.Objects; ...@@ -9,10 +10,11 @@ import java.util.Objects;
/** /**
* @author Him188moe * @author Him188moe
*/ */
public abstract class PacketEvent extends MiraiEvent { public abstract class PacketEvent extends BotEvent {
private final Packet packet; private final Packet packet;
public PacketEvent(@NotNull Packet packet) { public PacketEvent(@NotNull Bot bot, @NotNull Packet packet) {
super(bot);
this.packet = Objects.requireNonNull(packet); this.packet = Objects.requireNonNull(packet);
} }
......
package net.mamoe.mirai.event.events.network; package net.mamoe.mirai.event.events.network;
import net.mamoe.mirai.Bot;
import net.mamoe.mirai.network.packet.ClientPacket; import net.mamoe.mirai.network.packet.ClientPacket;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
...@@ -9,7 +10,7 @@ import org.jetbrains.annotations.NotNull; ...@@ -9,7 +10,7 @@ import org.jetbrains.annotations.NotNull;
* @author Him188moe * @author Him188moe
*/ */
public final class PacketSentEvent extends ClientPacketEvent { public final class PacketSentEvent extends ClientPacketEvent {
public PacketSentEvent(@NotNull ClientPacket packet) { public PacketSentEvent(@NotNull Bot bot, @NotNull ClientPacket packet) {
super(packet); super(bot, packet);
} }
} }
package net.mamoe.mirai.event.events.network; package net.mamoe.mirai.event.events.network;
import net.mamoe.mirai.Bot;
import net.mamoe.mirai.network.packet.ServerPacket; import net.mamoe.mirai.network.packet.ServerPacket;
/** /**
* @author Him188moe * @author Him188moe
*/ */
public abstract class ServerPacketEvent extends PacketEvent { public abstract class ServerPacketEvent extends PacketEvent {
public ServerPacketEvent(ServerPacket packet) { public ServerPacketEvent(Bot bot, ServerPacket packet) {
super(packet); super(bot, packet);
} }
@Override @Override
......
package net.mamoe.mirai.event.events.network; package net.mamoe.mirai.event.events.network;
import net.mamoe.mirai.Bot;
import net.mamoe.mirai.event.Cancellable; import net.mamoe.mirai.event.Cancellable;
import net.mamoe.mirai.network.packet.ServerPacket; import net.mamoe.mirai.network.packet.ServerPacket;
import net.mamoe.mirai.network.packet.ServerVerificationCodePacket; import net.mamoe.mirai.network.packet.ServerVerificationCodePacket;
...@@ -11,7 +12,7 @@ import net.mamoe.mirai.network.packet.ServerVerificationCodePacket; ...@@ -11,7 +12,7 @@ import net.mamoe.mirai.network.packet.ServerVerificationCodePacket;
* @author Him188moe * @author Him188moe
*/ */
public final class ServerPacketReceivedEvent extends ServerPacketEvent implements Cancellable { public final class ServerPacketReceivedEvent extends ServerPacketEvent implements Cancellable {
public ServerPacketReceivedEvent(ServerPacket packet) { public ServerPacketReceivedEvent(Bot bot, ServerPacket packet) {
super(packet); super(bot, packet);
} }
} }
package net.mamoe.mirai.event.events.qq; package net.mamoe.mirai.event.events.qq;
import net.mamoe.mirai.Robot; import net.mamoe.mirai.Bot;
import net.mamoe.mirai.contact.QQ; import net.mamoe.mirai.contact.QQ;
import net.mamoe.mirai.event.events.robot.RobotEvent; import net.mamoe.mirai.event.events.bot.BotEvent;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.Objects; import java.util.Objects;
...@@ -10,11 +10,11 @@ import java.util.Objects; ...@@ -10,11 +10,11 @@ import java.util.Objects;
/** /**
* @author Him188moe * @author Him188moe
*/ */
public abstract class FriendEvent extends RobotEvent { public abstract class FriendEvent extends BotEvent {
private final QQ qq; private final QQ qq;
public FriendEvent(@NotNull Robot robot, @NotNull QQ qq) { public FriendEvent(@NotNull Bot bot, @NotNull QQ qq) {
super(robot); super(bot);
this.qq = Objects.requireNonNull(qq); this.qq = Objects.requireNonNull(qq);
} }
......
package net.mamoe.mirai.event.events.qq; package net.mamoe.mirai.event.events.qq;
import net.mamoe.mirai.Robot; import net.mamoe.mirai.Bot;
import net.mamoe.mirai.contact.QQ; import net.mamoe.mirai.contact.QQ;
import net.mamoe.mirai.message.defaults.MessageChain; import net.mamoe.mirai.message.defaults.MessageChain;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
...@@ -12,19 +12,14 @@ import java.util.Objects; ...@@ -12,19 +12,14 @@ import java.util.Objects;
*/ */
public final class FriendMessageEvent extends FriendEvent { public final class FriendMessageEvent extends FriendEvent {
private final MessageChain messageChain; private final MessageChain messageChain;
private final String messageString;
public FriendMessageEvent(@NotNull Robot robot, @NotNull QQ sender, @NotNull MessageChain messageChain) { public FriendMessageEvent(@NotNull Bot bot, @NotNull QQ sender, @NotNull MessageChain messageChain) {
super(robot, sender); super(bot, sender);
this.messageChain = Objects.requireNonNull(messageChain); this.messageChain = Objects.requireNonNull(messageChain);
this.messageString = messageChain.toString();
} }
public String getMessageString() { @NotNull
return messageString; public MessageChain message() {
}
public MessageChain getMessageChain() {
return messageChain; return messageChain;
} }
} }
package net.mamoe.mirai.event.events.robot;
import net.mamoe.mirai.Robot;
public final class RobotLoginSucceedEvent extends RobotEvent {
public RobotLoginSucceedEvent(Robot robot) {
super(robot);
}
}
...@@ -4,8 +4,147 @@ package net.mamoe.mirai.message; ...@@ -4,8 +4,147 @@ package net.mamoe.mirai.message;
* @author Him188moe * @author Him188moe
*/ */
public enum FaceID { public enum FaceID {
// TODO: 2019/9/1 Face_jingya(0),
Face_piezui(1),
Face_se(2),
Face_fadai(3),
Face_deyi(4),
Face_liulei(5),
Face_haixiu(6),
Face_bizui(7),
Face_shui(8),
Face_daku(9),
Face_ganga(10),
Face_fanu(11),
Face_tiaopi(12),
Face_ciya(13),
Face_weixiao(14),
Face_nanguo(15),
Face_ku(16),
Face_zhuakuang(18),
Face_tu(19),
Face_touxiao(20),
Face_keai(21),
Face_baiyan(22),
Face_aoman(23),
Face_ji_e(24),
Face_kun(25),
Face_jingkong(26),
Face_liuhan(27),
Face_hanxiao(28),
Face_dabing(29),
Face_fendou(30),
Face_zhouma(31),
Face_yiwen(32),
Face_yun(34),
Face_zhemo(35),
Face_shuai(36),
Face_kulou(37),
Face_qiaoda(38),
Face_zaijian(39),
Face_fadou(41),
Face_aiqing(42),
Face_tiaotiao(43),
Face_zhutou(46),
Face_yongbao(49),
Face_dan_gao(53),
Face_shandian(54),
Face_zhadan(55),
Face_dao(56),
Face_zuqiu(57),
Face_bianbian(59),
Face_kafei(60),
Face_fan(61),
Face_meigui(63),
Face_diaoxie(64),
Face_aixin(66),
Face_xinsui(67),
Face_liwu(69),
Face_taiyang(74),
Face_yueliang(75),
Face_qiang(76),
Face_ruo(77),
Face_woshou(78),
Face_shengli(79),
Face_feiwen(85),
Face_naohuo(86),
Face_xigua(89),
Face_lenghan(96),
Face_cahan(97),
Face_koubi(98),
Face_guzhang(99),
Face_qiudale(100),
Face_huaixiao(101),
Face_zuohengheng(102),
Face_youhengheng(103),
Face_haqian(104),
Face_bishi(105),
Face_weiqu(106),
Face_kuaikule(107),
Face_yinxian(108),
Face_qinqin(109),
Face_xia(110),
Face_kelian(111),
Face_caidao(112),
Face_pijiu(113),
Face_lanqiu(114),
Face_pingpang(115),
Face_shiai(116),
Face_piaochong(117),
Face_baoquan(118),
Face_gouyin(119),
Face_quantou(120),
Face_chajin(121),
Face_aini(122),
Face_bu(123),
Face_hao(124),
Face_zhuanquan(125),
Face_ketou(126),
Face_huitou(127),
Face_tiaosheng(128),
Face_huishou(129),
Face_jidong(130),
Face_jiewu(131),
Face_xianwen(132),
Face_zuotaiji(133),
Face_youtaiji(134),
Face_shuangxi(136),
Face_bianpao(137),
Face_denglong(138),
Face_facai(139),
Face_K_ge(140),
Face_gouwu(141),
Face_youjian(142),
Face_shuai_qi(143),
Face_hecai(144),
Face_qidao(145),
Face_baojin(146),
Face_bangbangtang(147),
Face_he_nai(148),
Face_xiamian(149),
Face_xiangjiao(150),
Face_feiji(151),
Face_kaiche(152),
Face_gaotiezuochetou(153),
Face_chexiang(154),
Face_gaotieyouchetou(155),
Face_duoyun(156),
Face_xiayu(157),
Face_chaopiao(158),
Face_xiongmao(159),
Face_dengpao(160),
Face_fengche(161),
Face_naozhong(162),
Face_dasan(163),
Face_caiqiu(164),
Face_zuanjie(165),
Face_shafa(166),
Face_zhijin(167),
Face_yao(168),
Face_shouqiang(169),
Face_qingwa(170),
// TODO: 2019/9/1 添加更多表情
; ;
...@@ -18,4 +157,15 @@ public enum FaceID { ...@@ -18,4 +157,15 @@ public enum FaceID {
public int getId() { public int getId() {
return id; return id;
} }
public static FaceID ofId(int id) {
for (FaceID value : FaceID.values()) {
if (value.id == id) {
return value;
}
}
return null;
}
} }
package net.mamoe.mirai.message;
import net.mamoe.mirai.contact.Contact;
import net.mamoe.mirai.contact.QQ;
import net.mamoe.mirai.message.defaults.At;
import net.mamoe.mirai.message.defaults.Image;
import net.mamoe.mirai.message.defaults.MessageChain;
import net.mamoe.mirai.message.defaults.PlainText;
import org.jetbrains.annotations.NotNull;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Objects;
/**
* 可发送的或从服务器接收的消息.
* 采用这样的消息模式是因为 QQ 的消息多元化, 一条消息中可包含 {@linkplain PlainText 纯文本}, {@linkplain Image 图片} 等.
*
* @author Him188moe
* @see Contact#sendMessage(Message) 发送这个消息
* @see MessageKt 若你使用 kotlin, 请查看针对 kotlin 的优化实现
*/
public abstract class Message {
@Override
public abstract String toString();
/**
* 把这个消息连接到另一个消息的头部. 相当于字符串相加
* <p>
* Connects this Message to the head of another Message.
* That is, another message becomes the tail of this message.
* This method does similar to {@link String#concat(String)}
* <p>
* E.g.:
* PlainText a = new PlainText("Hello ");
* PlainText b = new PlainText("world");
* PlainText c = a.concat(b);
* <p>
* the text of c is "Hello world"
*
* @param tail tail
* @return message connected
*/
public Message concat(@NotNull Message tail) {
return new MessageChain(this, Objects.requireNonNull(tail));
}
public final Message concat(String tail) {
return concat(new PlainText(tail));
}
public Message withImage(String imageId) {
// TODO: 2019/9/1
return this;
}
public Message withImage(BufferedImage image) {
// TODO: 2019/9/1
return this;
}
public Message withImage(File image) {
// TODO: 2019/9/1
return this;
}
public Message withAt(@NotNull QQ target) {
this.concat(target.at());
return this;
}
public Message withAt(int target) {
this.concat(new At(target));
return this;
}
}
package net.mamoe.mirai.message
import net.mamoe.mirai.contact.Contact
import net.mamoe.mirai.contact.QQ
import net.mamoe.mirai.message.defaults.At
import net.mamoe.mirai.message.defaults.Image
import net.mamoe.mirai.message.defaults.MessageChain
import net.mamoe.mirai.message.defaults.PlainText
import java.awt.image.BufferedImage
import java.io.File
import java.util.*
/**
* 可发送的或从服务器接收的消息.
* 采用这样的消息模式是因为 QQ 的消息多元化, 一条消息中可包含 [纯文本][PlainText], [图片][Image] 等.
*
* #### 在 Kotlin 使用 [Message]
* 这与使用 [String] 的使用非常类似.
*
* 比较 [Message] 与 [String] (使用 infix [Message.valueEquals]):
* `if(message valueEquals "你好") qq.sendMessage(message)`
*
* 连接 [Message] 与 [Message], [String], [BufferedImage] (使用 operator [Message.plus]):
* ```
* message = PlainText("Hello ")
* qq.sendMessage(message + "world")
* ```
*
* @author Him188moe
* @see Contact.sendMessage
*/
abstract class Message {
internal abstract val type: Int
private var toStringCache: String? = null
private val cacheLock = object : Any() {}
internal abstract fun toStringImpl(): String
/**
* 得到用户层的文本消息. 如:
* - [PlainText] 得到 消息内容
* - [Image] 得到 "{ID}.png"
* - [At] 得到 "[@qq]"
*/
final override fun toString(): String {
synchronized(cacheLock) {
if (toStringCache != null) {
return toStringCache!!
}
this.toStringCache = toStringImpl()
return toStringCache!!
}
}
internal fun clearToStringCache() {
synchronized(cacheLock) {
toStringCache = null
}
}
/**
* 得到类似 "PlainText(内容)", "Image(ID)"
*/
open fun toObjectString(): String {
return this.javaClass.simpleName + String.format("(%s)", this.toString())
}
/**
* 转换为数据包使用的 byte array
*/
abstract fun toByteArray(): ByteArray
/**
* 比较两个 Message 的内容是否相等. 如:
* - [PlainText] 比较 [PlainText.text]
* - [Image] 比较 [Image.imageID]
*/
abstract infix fun valueEquals(another: Message): Boolean
/**
* 将这个消息的 [toString] 与 [another] 比较
*/
infix fun valueEquals(another: String): Boolean = this.toString() == another
/**
* 把这个消息连接到另一个消息的头部. 相当于字符串相加
*
*
* Connects this Message to the head of another Message.
* That is, another message becomes the tail of this message.
* This method does similar to [String.concat]
*
*
* E.g.:
* PlainText a = new PlainText("Hello ");
* PlainText b = new PlainText("world");
* PlainText c = a.concat(b);
*
*
* the text of c is "Hello world"
*
* @param tail tail
* @return message connected
*/
open fun concat(tail: Message): Message {
return MessageChain(this, Objects.requireNonNull(tail))
}
fun concat(tail: String): Message {
return concat(PlainText(tail))
}
fun withImage(imageId: String): Message {
// TODO: 2019/9/1
return this
}
fun withImage(image: BufferedImage): Message {
// TODO: 2019/9/1
return this
}
fun withImage(image: File): Message {
// TODO: 2019/9/1
return this
}
fun withAt(target: QQ): Message {
this.concat(target.at())
return this
}
fun withAt(target: Int): Message {
this.concat(At(target.toLong()))
return this
}
open fun toChain(): MessageChain {
return MessageChain(this)
}
/* For Kotlin */
/**
* 实现使用 '+' 操作符连接 [Message] 与 [Message]
*/
infix operator fun plus(another: Message): Message = this.concat(another)
/**
* 实现使用 '+' 操作符连接 [Message] 与 [String]
*/
infix operator fun plus(another: String): Message = this.concat(another)
/**
* 实现使用 '+' 操作符连接 [Message] 与 [Number]
*/
infix operator fun plus(another: Number): Message = this.concat(another.toString())
/**
* 连接 [String] 与 [Message]
*/
fun String.concat(another: Message): Message = PlainText(this).concat(another)
override fun hashCode(): Int {
return javaClass.hashCode()
}
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is Message) return false
if (type != other.type) return false
return this.toString() == other.toString()
}
}
\ No newline at end of file
package net.mamoe.mirai.message
/**
* [Message] 在数据包中的 id([UByte])
*
* Java 调用方式:
* MessageId.at
*
* @author Him188moe
*/
object MessageId {
const val AT: Int = 0x00//todo 不知道是多少
const val FACE: Int = 0x00//todo 不知道是多少
const val TEXT: Int = 0x01
const val IMAGE: Int = 0x06
const val CHAIN: Int = 0xff//仅用于 equals. Packet 中不存在 Chain 概念
}
\ No newline at end of file
@file:JvmName("MessageKt")
package net.mamoe.mirai.message
import net.mamoe.mirai.message.defaults.PlainText
/**
* 实现使用 '+' 操作符连接 [Message] 与 [Message]
*/
infix operator fun Message.plus(another: Message): Message = this.concat(another)
/**
* 实现使用 '+' 操作符连接 [Message] 与 [String]
*/
infix operator fun Message.plus(another: String): Message = this.concat(another)
/**
* 连接 [String] 与 [Message]
*/
infix fun String.concat(another: Message): Message = PlainText(this).concat(another)
\ No newline at end of file
package net.mamoe.mirai.message.defaults;
import net.mamoe.mirai.contact.QQ;
import net.mamoe.mirai.message.Message;
import org.jetbrains.annotations.NotNull;
import java.util.Objects;
/**
* At 一个人的消息.
*
* @author Him188moe
*/
public final class At extends Message {
private final long target;
public At(@NotNull QQ target) {
this(Objects.requireNonNull(target).getNumber());
}
public At(long target) {
this.target = target;
}
public long getTarget() {
return target;
}
@Override
public String toString() {
// TODO: 2019/9/4 At.toString
throw new UnsupportedOperationException();
}
}
package net.mamoe.mirai.message.defaults
import net.mamoe.mirai.contact.QQ
import net.mamoe.mirai.message.Message
import net.mamoe.mirai.message.MessageId
/**
* At 一个人
*
* @author Him188moe
*/
class At(val target: Long) : Message() {
override val type: Int = MessageId.AT
constructor(target: QQ) : this(target.number)
override fun toStringImpl(): String = "[@$target]"
override fun toByteArray(): ByteArray {
TODO()
}
override fun valueEquals(another: Message): Boolean {
if (another !is At) {
return false
}
return another.target == this.target
}
}
package net.mamoe.mirai.message.defaults;
import net.mamoe.mirai.message.FaceID;
import net.mamoe.mirai.message.Message;
/**
* QQ 自带表情
*
* @author Him188moe
*/
public final class Face extends Message {
private final FaceID id;
public Face(FaceID id) {
this.id = id;
}
public FaceID getId() {
return id;
}
@Override
public String toString() {
// TODO: 2019/9/1
throw new UnsupportedOperationException();
}
}
package net.mamoe.mirai.message.defaults
import net.mamoe.mirai.message.FaceID
import net.mamoe.mirai.message.Message
import net.mamoe.mirai.message.MessageId
/**
* QQ 自带表情
*
* @author Him188moe
*/
class Face(val id: FaceID?) : Message() {
override val type: Int = MessageId.FACE
override fun toStringImpl(): String {
return if (id == null) {
"[face?]"
} else String.format("[face%d]", id.id)
}
override fun toByteArray(): ByteArray {
TODO()
}
override fun valueEquals(another: Message): Boolean {
if (another !is Face) {
return false
}
return this.id == another.id
}
}
package net.mamoe.mirai.message.defaults;
import net.mamoe.mirai.message.Message;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
/**
* @author Him188moe
*/
public final class Image extends Message {
public Image(InputStream inputStream) {
}
public Image(BufferedImage image) {
}
public Image(File imageFile) throws FileNotFoundException {
this(new FileInputStream(imageFile));
}
public Image(URL url) throws IOException {
this(ImageIO.read(url));
}
/**
* {xxxxx}.jpg
*
* @param imageID
*/
public Image(String imageID) {
}
@Override
public String toString() {
return null;
}
}
package net.mamoe.mirai.message.defaults
import net.mamoe.mirai.message.Message
import net.mamoe.mirai.message.MessageId
import java.awt.image.BufferedImage
import java.io.*
import java.net.URL
import javax.imageio.ImageIO
/**
* @author Him188moe
*/
class Image : Message {
override val type: Int = MessageId.IMAGE
private var imageID: String? = null
constructor(inputStream: InputStream) {
}
constructor(image: BufferedImage) {
}
@Throws(FileNotFoundException::class)
constructor(imageFile: File) : this(FileInputStream(imageFile)) {
}
@Throws(IOException::class)
constructor(url: URL) : this(ImageIO.read(url)) {
}
/**
* {xxxxx}.jpg
*
* @param imageID
*/
constructor(imageID: String) {
this.imageID = imageID
}
override fun toStringImpl(): String {
return imageID!!
}
override fun toByteArray(): ByteArray {
TODO()
}
override fun valueEquals(another: Message): Boolean {
if (another !is Image) {
return false
}
return this.imageID == another.imageID
}
}
\ No newline at end of file
package net.mamoe.mirai.message.defaults;
import net.mamoe.mirai.message.Message;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* @author Him188moe
*/
public final class MessageChain extends Message {
private LinkedList<Message> list = new LinkedList<>();
public MessageChain(@NotNull Message head, @NotNull Message tail) {
Objects.requireNonNull(head);
Objects.requireNonNull(tail);
list.add(head);
list.add(tail);
}
public MessageChain(@NotNull Message message) {
Objects.requireNonNull(message);
list.add(message);
}
/**
* @return An unmodifiable list
*/
public List<Message> toList() {
return List.copyOf(list);
}
public Stream<Message> stream() {
return new ArrayList<>(list).stream();
}
@Override
public synchronized String toString() {
return this.list.stream().map(Message::toString).collect(Collectors.joining(""));
}
@Override
public synchronized Message concat(@NotNull Message tail) {
this.list.add(tail);
return this;
}
}
package net.mamoe.mirai.message.defaults
import net.mamoe.mirai.message.Message
import net.mamoe.mirai.message.MessageId
import net.mamoe.mirai.utils.lazyEncode
import org.intellij.lang.annotations.MagicConstant
import java.util.*
import java.util.stream.Collectors
import java.util.stream.Stream
class MessageChain : Message {
override val type: Int = MessageId.CHAIN
internal val list = LinkedList<Message>()
constructor(head: Message, tail: Message) {
Objects.requireNonNull(head)
Objects.requireNonNull(tail)
list.add(head)
list.add(tail)
}
constructor(message: Message) {
Objects.requireNonNull(message)
list.add(message)
}
constructor()
fun toList(): List<Message> {
return list.toList()
}
fun size(): Int {
return list.size
}
@Synchronized
fun containsType(@MagicConstant(valuesFromClass = MessageId::class) type: Int): Boolean {
for (message in list) {
if (message.type == type) {
return true
}
}
return false
}
fun stream(): Stream<Message> {
return ArrayList(list).stream()
}
@Synchronized
override fun toStringImpl(): String {
return this.list.stream().map { it.toString() }.collect(Collectors.joining(""))
}
@Synchronized
override fun toObjectString(): String {
return String.format("MessageChain(%s)", this.list.stream().map { it.toObjectString() }.collect(Collectors.joining(", ")))
}
@Synchronized
override fun concat(tail: Message): Message {
this.list.add(tail)
clearToStringCache()
return this
}
override fun toChain(): MessageChain {
return this
}
override fun toByteArray(): ByteArray = lazyEncode {
stream().forEach { message ->
it.write(message.toByteArray())
}
}
override fun valueEquals(another: Message): Boolean {
if (another !is MessageChain) {
return false
}
return this.list == another.list
}
}
package net.mamoe.mirai.message.defaults;
import net.mamoe.mirai.message.Message;
/**
* @author Him188moe
*/
public final class PlainText extends Message {
private final String text;
public PlainText(String text) {
this.text = text;
}
@Override
public String toString() {
return text;
}
}
package net.mamoe.mirai.message.defaults
import net.mamoe.mirai.message.Message
import net.mamoe.mirai.message.MessageId
import net.mamoe.mirai.network.packet.writeVarByteArray
import net.mamoe.mirai.network.packet.writeVarString
import net.mamoe.mirai.utils.lazyEncode
/**
* @author Him188moe
*/
class PlainText(private val text: String) : Message() {
override val type: Int = MessageId.TEXT
override fun toStringImpl(): String {
return text
}
override fun toByteArray(): ByteArray = lazyEncode { section ->
section.writeByte(this.type)
section.writeVarByteArray(lazyEncode { child ->
child.writeByte(0x01)
child.writeVarString(this.text)
})
}
override fun valueEquals(another: Message): Boolean {
if (another !is PlainText) {
return false
}
return this.text == another.text
}
}
...@@ -10,10 +10,10 @@ import java.util.stream.Collectors ...@@ -10,10 +10,10 @@ import java.util.stream.Collectors
object Protocol { object Protocol {
val SERVER_IP: List<String> = object : ArrayList<String>() { val SERVER_IP: List<String> = object : ArrayList<String>() {
init { init {
add("183.60.56.29") //add("183.60.56.29")
arrayOf( arrayOf(
"sz3.tencent.com", //"sz3.tencent.com",
"sz4.tencent.com", "sz4.tencent.com",
"sz5.tencent.com", "sz5.tencent.com",
"sz6.tencent.com", "sz6.tencent.com",
...@@ -64,6 +64,11 @@ object Protocol { ...@@ -64,6 +64,11 @@ object Protocol {
*/ */
const val key0836 = "EF 4A 36 6A 16 A8 E6 3D 2E EA BD 1F 98 C1 3C DA" const val key0836 = "EF 4A 36 6A 16 A8 E6 3D 2E EA BD 1F 98 C1 3C DA"
/**
* 发送/接受消息中的一个const
*/
const val friendMessageConst1 = "00 00 0C E5 BE AE E8 BD AF E9 9B 85 E9 BB 91"
private val hexToByteArrayCacheMap: MutableMap<Int, ByteArray> = mutableMapOf() private val hexToByteArrayCacheMap: MutableMap<Int, ByteArray> = mutableMapOf()
@ExperimentalUnsignedTypes @ExperimentalUnsignedTypes
......
...@@ -2,6 +2,7 @@ package net.mamoe.mirai.network.packet ...@@ -2,6 +2,7 @@ package net.mamoe.mirai.network.packet
import lombok.Getter import lombok.Getter
import net.mamoe.mirai.network.Protocol import net.mamoe.mirai.network.Protocol
import net.mamoe.mirai.network.packet.PacketNameFormatter.adjustName
import net.mamoe.mirai.utils.* import net.mamoe.mirai.utils.*
import java.io.DataOutputStream import java.io.DataOutputStream
import java.io.IOException import java.io.IOException
...@@ -20,7 +21,7 @@ abstract class ClientPacket : ByteArrayDataOutputStream(), Packet { ...@@ -20,7 +21,7 @@ abstract class ClientPacket : ByteArrayDataOutputStream(), Packet {
init { init {
val annotation = this.javaClass.getAnnotation(PacketId::class.java) val annotation = this.javaClass.getAnnotation(PacketId::class.java)
idHex = annotation.value idHex = annotation.value.trim()
try { try {
this.writeHex(Protocol.head) this.writeHex(Protocol.head)
...@@ -29,7 +30,6 @@ abstract class ClientPacket : ByteArrayDataOutputStream(), Packet { ...@@ -29,7 +30,6 @@ abstract class ClientPacket : ByteArrayDataOutputStream(), Packet {
} catch (e: IOException) { } catch (e: IOException) {
throw RuntimeException(e) throw RuntimeException(e)
} }
} }
@Throws(IOException::class) @Throws(IOException::class)
...@@ -60,10 +60,20 @@ abstract class ClientPacket : ByteArrayDataOutputStream(), Packet { ...@@ -60,10 +60,20 @@ abstract class ClientPacket : ByteArrayDataOutputStream(), Packet {
return toByteArray() return toByteArray()
} }
open fun getFixedId(): String = when (this.idHex.length) {
0 -> "__ __ __ __"
2 -> this.idHex + " __ __ __"
5 -> this.idHex + " __ __"
7 -> this.idHex + " __"
else -> this.idHex
}
override fun toString(): String { override fun toString(): String {
return this.javaClass.simpleName + this.getAllDeclaredFields().joinToString(", ", "{", "}") { return adjustName(this.javaClass.simpleName + "(${this.getFixedId()})") + this.getAllDeclaredFields().filterNot { it.name == "idHex" || it.name == "idByteArray" || it.name == "encoded" }.joinToString(", ", "{", "}") {
it.trySetAccessible(); it.name + "=" + it.get(this).let { value -> it.trySetAccessible(); it.name + "=" + it.get(this).let { value ->
when (value) { when (value) {
null -> null
is ByteArray -> value.toUHexString() is ByteArray -> value.toUHexString()
is UByteArray -> value.toUHexString() is UByteArray -> value.toUHexString()
else -> value.toString() else -> value.toString()
...@@ -144,12 +154,6 @@ fun DataOutputStream.writeTLV0006(qq: Long, password: String, loginTime: Int, lo ...@@ -144,12 +154,6 @@ fun DataOutputStream.writeTLV0006(qq: Long, password: String, loginTime: Int, lo
} }
} }
/*
@ExperimentalUnsignedTypes
fun main() {
println(lazyEncode { it.writeTLV0006(1994701021, "D1 A5 C8 BB E1 Q3 CC DD", 131513, "123.123.123.123", "AA BB CC DD EE FF AA BB CC".hexToBytes()) }.toUByteArray().toUHexString())
}*/
@ExperimentalUnsignedTypes @ExperimentalUnsignedTypes
@TestedSuccessfully @TestedSuccessfully
fun DataOutputStream.writeCRC32() = writeCRC32(getRandomByteArray(16)) fun DataOutputStream.writeCRC32() = writeCRC32(getRandomByteArray(16))
...@@ -166,11 +170,10 @@ fun DataOutputStream.writeCRC32(key: ByteArray) { ...@@ -166,11 +170,10 @@ fun DataOutputStream.writeCRC32(key: ByteArray) {
@ExperimentalUnsignedTypes @ExperimentalUnsignedTypes
@TestedSuccessfully @TestedSuccessfully
fun DataOutputStream.writeDeviceName(random: Boolean = false) { fun DataOutputStream.writeDeviceName(random: Boolean = false) {
val deviceName: String val deviceName: String = if (random) {
if (random) { String(getRandomByteArray(10))
deviceName = String(getRandomByteArray(10))
} else { } else {
deviceName = InetAddress.getLocalHost().hostName InetAddress.getLocalHost().hostName
} }
this.writeShort(deviceName.length + 2) this.writeShort(deviceName.length + 2)
this.writeShort(deviceName.length) this.writeShort(deviceName.length)
...@@ -209,7 +212,7 @@ fun Int.toLByteArray(): ByteArray = byteArrayOf( ...@@ -209,7 +212,7 @@ fun Int.toLByteArray(): ByteArray = byteArrayOf(
) )
@ExperimentalUnsignedTypes @ExperimentalUnsignedTypes
fun Int.toHexString(separator: String = " "): String = this.toByteArray().toUByteArray().toUHexString(separator) fun Int.toUHexString(separator: String = " "): String = this.toByteArray().toUByteArray().toUHexString(separator)
internal fun md5(str: String): ByteArray = MessageDigest.getInstance("MD5").digest(str.toByteArray()) internal fun md5(str: String): ByteArray = MessageDigest.getInstance("MD5").digest(str.toByteArray())
...@@ -226,7 +229,7 @@ fun DataOutputStream.writeZero(count: Int) { ...@@ -226,7 +229,7 @@ fun DataOutputStream.writeZero(count: Int) {
@Throws(IOException::class) @Throws(IOException::class)
fun DataOutputStream.writeRandom(length: Int) { fun DataOutputStream.writeRandom(length: Int) {
repeat(length) { repeat(length) {
this.writeByte((Math.random() * 255).toInt().toByte().toInt()) this.writeByte((Math.random() * 255).toInt())
} }
} }
...@@ -241,3 +244,12 @@ fun DataOutputStream.writeQQ(qq: Long) { ...@@ -241,3 +244,12 @@ fun DataOutputStream.writeQQ(qq: Long) {
fun DataOutputStream.writeGroup(groupIdOrGroupNumber: Long) { fun DataOutputStream.writeGroup(groupIdOrGroupNumber: Long) {
this.write(groupIdOrGroupNumber.toUInt().toByteArray()) this.write(groupIdOrGroupNumber.toUInt().toByteArray())
} }
fun DataOutputStream.writeVarByteArray(byteArray: ByteArray) {
this.writeShort(byteArray.size)
this.write(byteArray)
}
fun DataOutputStream.writeVarString(str: String) {
this.writeVarByteArray(str.toByteArray())
}
\ No newline at end of file
...@@ -42,7 +42,7 @@ class ServerAccountInfoResponsePacket(input: DataInputStream) : ServerPacket(inp ...@@ -42,7 +42,7 @@ class ServerAccountInfoResponsePacket(input: DataInputStream) : ServerPacket(inp
fun decrypt(sessionKey: ByteArray): ServerAccountInfoResponsePacket { fun decrypt(sessionKey: ByteArray): ServerAccountInfoResponsePacket {
this.input goto 14 this.input goto 14
val data = this.input.readAllBytes().let { it.copyOfRange(0, it.size - 1) } val data = this.input.readAllBytes().let { it.copyOfRange(0, it.size - 1) }
return ServerAccountInfoResponsePacket(TEA.decrypt(data, sessionKey).dataInputStream()); return ServerAccountInfoResponsePacket(TEA.decrypt(data, sessionKey).dataInputStream()).setId(this.idHex)
} }
} }
} }
\ No newline at end of file
package net.mamoe.mirai.network.packet;
/**
* @author Him188moe
*/
public interface Packet {
}
package net.mamoe.mirai.network.packet
/**
* @author Him188moe
*/
interface Packet {
}
object PacketNameFormatter {
@JvmSynthetic
private var longestNameLength: Int = 43
@JvmSynthetic
fun adjustName(name: String): String {
if (name.length > longestNameLength) {
longestNameLength = name.length
return name
}
return StringBuilder().apply {
repeat(longestNameLength - name.length) {
this.append(" ")
}
}.toString() + name
}
}
\ No newline at end of file
...@@ -58,7 +58,7 @@ class ServerSKeyResponsePacket(input: DataInputStream) : ServerPacket(input) { ...@@ -58,7 +58,7 @@ class ServerSKeyResponsePacket(input: DataInputStream) : ServerPacket(input) {
fun decrypt(sessionKey: ByteArray): ServerSKeyResponsePacket { fun decrypt(sessionKey: ByteArray): ServerSKeyResponsePacket {
this.input goto 14 this.input goto 14
val data = this.input.readAllBytes().let { it.copyOfRange(0, it.size - 1) } val data = this.input.readAllBytes().let { it.copyOfRange(0, it.size - 1) }
return ServerSKeyResponsePacket(TEA.decrypt(data, sessionKey).dataInputStream()); return ServerSKeyResponsePacket(TEA.decrypt(data, sessionKey).dataInputStream()).setId(this.idHex)
} }
} }
} }
\ No newline at end of file
package net.mamoe.mirai.network.packet package net.mamoe.mirai.network.packet
import net.mamoe.mirai.network.packet.PacketNameFormatter.adjustName
import net.mamoe.mirai.network.packet.action.ServerCanAddFriendResponsePacket
import net.mamoe.mirai.network.packet.action.ServerSendFriendMessageResponsePacket import net.mamoe.mirai.network.packet.action.ServerSendFriendMessageResponsePacket
import net.mamoe.mirai.network.packet.action.ServerSendGroupMessageResponsePacket import net.mamoe.mirai.network.packet.action.ServerSendGroupMessageResponsePacket
import net.mamoe.mirai.network.packet.login.* import net.mamoe.mirai.network.packet.login.*
...@@ -10,6 +12,32 @@ import java.io.DataInputStream ...@@ -10,6 +12,32 @@ import java.io.DataInputStream
* @author Him188moe * @author Him188moe
*/ */
abstract class ServerPacket(val input: DataInputStream) : Packet { abstract class ServerPacket(val input: DataInputStream) : Packet {
var idHex: String
var idByteArray: ByteArray//fixed 4 size
var encoded: Boolean = false
init {
idHex = try {
val annotation = this.javaClass.getAnnotation(PacketId::class.java)
annotation.value.trim()
} catch (e: NullPointerException) {
""
}
idByteArray = if (idHex.isEmpty()) {
byteArrayOf(0, 0, 0, 0)
} else {
idHex.hexToBytes()
}
}
fun <P : ServerPacket> P.setId(idHex: String): P {
this.idHex = idHex
return this
}
open fun decode() { open fun decode() {
...@@ -19,34 +47,32 @@ abstract class ServerPacket(val input: DataInputStream) : Packet { ...@@ -19,34 +47,32 @@ abstract class ServerPacket(val input: DataInputStream) : Packet {
@ExperimentalUnsignedTypes @ExperimentalUnsignedTypes
fun ofByteArray(bytes: ByteArray): ServerPacket { fun ofByteArray(bytes: ByteArray): ServerPacket {
//println("Raw received: ${bytes.toUByteArray().toUHexString()}")
val stream = bytes.dataInputStream() val stream = bytes.dataInputStream()
stream.skip(3) stream.skip(3)
val idHex = stream.readInt().toUHexString(" ")
return when (val idHex = stream.readInt().toHexString(" ")) { return when (idHex) {
"08 25 31 01" -> ServerTouchResponsePacket.Encrypted(ServerTouchResponsePacket.Type.TYPE_08_25_31_01, stream) "08 25 31 01" -> ServerTouchResponsePacket.Encrypted(ServerTouchResponsePacket.Type.TYPE_08_25_31_01, stream)
"08 25 31 02" -> ServerTouchResponsePacket.Encrypted(ServerTouchResponsePacket.Type.TYPE_08_25_31_02, stream) "08 25 31 02" -> ServerTouchResponsePacket.Encrypted(ServerTouchResponsePacket.Type.TYPE_08_25_31_02, stream)
"08 36 31 03", "08 36 31 04", "08 36 31 05", "08 36 31 06" -> { "08 36 31 03", "08 36 31 04", "08 36 31 05", "08 36 31 06" -> {
when (bytes.size) { when (bytes.size) {
271, 207 -> return ServerLoginResponseResendPacket.Encrypted(stream, when (idHex) { 271, 207 -> return ServerLoginResponseKeyExchangePacket.Encrypted(stream, when (idHex) {
"08 36 31 03" -> ServerLoginResponseResendPacket.Flag.`08 36 31 03` "08 36 31 03" -> ServerLoginResponseKeyExchangePacket.Flag.`08 36 31 03`
else -> { else -> ServerLoginResponseKeyExchangePacket.Flag.OTHER
MiraiLogger debug ("ServerLoginResponseResendPacketEncrypted: flag=$idHex"); ServerLoginResponseResendPacket.Flag.OTHER
} }).apply { this.idHex = idHex }
}) 871 -> return ServerLoginResponseVerificationCodeInitPacket.Encrypted(stream).apply { this.idHex = idHex }
871 -> return ServerLoginResponseVerificationCodeInitPacket.Encrypted(stream)
} }
if (bytes.size > 700) { if (bytes.size > 700) {
return ServerLoginResponseSuccessPacket.Encrypted(stream) return ServerLoginResponseSuccessPacket.Encrypted(stream).apply { this.idHex = idHex }
} }
println(bytes.size)
return ServerLoginResponseFailedPacket(when (bytes.size) { return ServerLoginResponseFailedPacket(when (bytes.size) {
319, 135 -> LoginState.WRONG_PASSWORD 63, 319, 135, 351 -> LoginState.WRONG_PASSWORD//这四个其中一个也是被冻结
//135 -> LoginState.RETYPE_PASSWORD //135 -> LoginState.RETYPE_PASSWORD
279 -> LoginState.BLOCKED 279 -> LoginState.BLOCKED
263 -> LoginState.UNKNOWN_QQ_NUMBER 263 -> LoginState.UNKNOWN_QQ_NUMBER
...@@ -57,10 +83,10 @@ abstract class ServerPacket(val input: DataInputStream) : Packet { ...@@ -57,10 +83,10 @@ abstract class ServerPacket(val input: DataInputStream) : Packet {
/* /*
//unknown //unknown
63 -> throw IllegalArgumentException(bytes.size.toString() + " (Unknown error)") 63 -> throw IllegalArgumentException(bytes.size.toString() + " (Unknown error)")
351 -> throw IllegalArgumentException(bytes.size.toString() + " (Illegal package data or Unknown error)")//包数据有误 351 -> throw IllegalArgumentException(bytes.size.toString() + " (Unknown error)")
else -> throw IllegalArgumentException(bytes.size.toString())*/ else -> throw IllegalArgumentException(bytes.size.toString())*/
}, stream) }, stream).apply { this.idHex = idHex }
} }
"08 28 04 34" -> ServerSessionKeyResponsePacket.Encrypted(stream) "08 28 04 34" -> ServerSessionKeyResponsePacket.Encrypted(stream)
...@@ -83,15 +109,18 @@ abstract class ServerPacket(val input: DataInputStream) : Packet { ...@@ -83,15 +109,18 @@ abstract class ServerPacket(val input: DataInputStream) : Packet {
"00 CD" -> ServerSendFriendMessageResponsePacket(stream) "00 CD" -> ServerSendFriendMessageResponsePacket(stream)
"00 02" -> ServerSendGroupMessageResponsePacket(stream) "00 02" -> ServerSendGroupMessageResponsePacket(stream)
"00 A7" -> ServerCanAddFriendResponsePacket(stream)
else -> throw IllegalArgumentException(idHex) else -> throw IllegalArgumentException(idHex)
} }
}.apply { this.idHex = idHex }
} }
} }
}
@ExperimentalUnsignedTypes @ExperimentalUnsignedTypes
override fun toString(): String { override fun toString(): String {
return this.javaClass.simpleName + this.getAllDeclaredFields().joinToString(", \n", "{", "}") { return adjustName(this.javaClass.simpleName + "(${this.getFixedId()})") + this.getAllDeclaredFields().filterNot { it.name == "idHex" || it.name == "encoded" }.joinToString(", ", "{", "}") {
it.trySetAccessible(); it.name + "=" + it.get(this).let { value -> it.trySetAccessible(); it.name + "=" + it.get(this).let { value ->
when (value) { when (value) {
is ByteArray -> value.toUHexString() is ByteArray -> value.toUHexString()
...@@ -102,15 +131,48 @@ abstract class ServerPacket(val input: DataInputStream) : Packet { ...@@ -102,15 +131,48 @@ abstract class ServerPacket(val input: DataInputStream) : Packet {
} }
} }
open fun getFixedId(): String = getFixedId(this.idHex)
fun getFixedId(id: String): String = when (id.length) {
0 -> "__ __ __ __"
2 -> "$id __ __ __"
5 -> "$id __ __"
7 -> "$id __"
else -> id
}
fun decryptBy(key: ByteArray): DataInputStream { fun decryptBy(key: ByteArray): DataInputStream {
input.goto(14) return decryptAsByteArray(key).dataInputStream()
return DataInputStream(TEA.decrypt(input.readAllBytes().let { it.copyOfRange(0, it.size - 1) }, key).inputStream())
} }
@ExperimentalUnsignedTypes @ExperimentalUnsignedTypes
fun decryptBy(keyHex: String): DataInputStream { fun decryptBy(keyHex: String): DataInputStream {
return this.decryptBy(keyHex.hexToBytes()) return this.decryptBy(keyHex.hexToBytes())
} }
fun decryptBy(key1: ByteArray, key2: ByteArray): DataInputStream {
return TEA.decrypt(this.decryptAsByteArray(key1), key2).dataInputStream();
}
@ExperimentalUnsignedTypes
fun decryptBy(key1: String, key2: ByteArray): DataInputStream {
return this.decryptBy(key1.hexToBytes(), key2)
}
@ExperimentalUnsignedTypes
fun decryptBy(key1: ByteArray, key2: String): DataInputStream {
return this.decryptBy(key1, key2.hexToBytes())
}
@ExperimentalUnsignedTypes
fun decryptBy(keyHex1: String, keyHex2: String): DataInputStream {
return this.decryptBy(keyHex1.hexToBytes(), keyHex2.hexToBytes())
}
private fun decryptAsByteArray(key: ByteArray): ByteArray {
input.goto(14)
return TEA.decrypt(input.readAllBytes().cutTail(1), key)
}
} }
...@@ -125,11 +187,15 @@ fun DataInputStream.readIP(): String { ...@@ -125,11 +187,15 @@ fun DataInputStream.readIP(): String {
return buff return buff
} }
fun DataInputStream.readShortVarString(): String { fun DataInputStream.readVarString(): String {
return String(this.readNBytes(this.readShort().toInt())) return String(this.readVarByteArray())
}
fun DataInputStream.readVarByteArray(): ByteArray {
return this.readNBytes(this.readShort().toInt())
} }
fun DataInputStream.readVarString(length: Int): String { fun DataInputStream.readString(length: Int): String {
return String(this.readNBytes(length)) return String(this.readNBytes(length))
} }
...@@ -154,6 +220,17 @@ fun <N : Number> DataInputStream.readNBytes(length: N): ByteArray { ...@@ -154,6 +220,17 @@ fun <N : Number> DataInputStream.readNBytes(length: N): ByteArray {
return this.readNBytes(length.toInt()) return this.readNBytes(length.toInt())
} }
fun DataInputStream.readVarNumber(): Number {
return when (this.readShort().toInt()) {
1 -> this.readByte()
2 -> this.readShort()
4 -> this.readInt()
8 -> this.readLong()
else -> throw UnsupportedOperationException()
}
}
fun DataInputStream.readNBytesIn(range: IntRange): ByteArray { fun DataInputStream.readNBytesIn(range: IntRange): ByteArray {
this.goto(range.first) this.goto(range.first)
return this.readNBytes(range.last - range.first + 1) return this.readNBytes(range.last - range.first + 1)
...@@ -164,6 +241,12 @@ fun <N : Number> DataInputStream.readIntAt(position: N): Int { ...@@ -164,6 +241,12 @@ fun <N : Number> DataInputStream.readIntAt(position: N): Int {
return this.readInt(); return this.readInt();
} }
@ExperimentalUnsignedTypes
fun <N : Number> DataInputStream.readUIntAt(position: N): UInt {
this.goto(position)
return this.readNBytes(4).toUInt();
}
fun <N : Number> DataInputStream.readByteAt(position: N): Byte { fun <N : Number> DataInputStream.readByteAt(position: N): Byte {
this.goto(position) this.goto(position)
return this.readByte(); return this.readByte();
......
...@@ -107,7 +107,7 @@ class ServerSessionKeyResponsePacket(inputStream: DataInputStream, private val d ...@@ -107,7 +107,7 @@ class ServerSessionKeyResponsePacket(inputStream: DataInputStream, private val d
fun decrypt(sessionResponseDecryptionKey: ByteArray): ServerSessionKeyResponsePacket { fun decrypt(sessionResponseDecryptionKey: ByteArray): ServerSessionKeyResponsePacket {
this.input goto 14 this.input goto 14
val data = this.input.readAllBytes().let { it.copyOfRange(0, it.size - 1) } val data = this.input.readAllBytes().let { it.copyOfRange(0, it.size - 1) }
return ServerSessionKeyResponsePacket(TEA.decrypt(data, sessionResponseDecryptionKey).dataInputStream(), data.size) return ServerSessionKeyResponsePacket(TEA.decrypt(data, sessionResponseDecryptionKey).dataInputStream(), data.size).setId(this.idHex)
} }
} }
} }
\ No newline at end of file
package net.mamoe.mirai.network.packet package net.mamoe.mirai.network.packet
import net.mamoe.mirai.network.Protocol import net.mamoe.mirai.network.Protocol
import net.mamoe.mirai.utils.* import net.mamoe.mirai.utils.ByteArrayDataOutputStream
import net.mamoe.mirai.utils.TEA
import net.mamoe.mirai.utils.hexToBytes
import net.mamoe.mirai.utils.toUHexString
import java.io.DataInputStream import java.io.DataInputStream
import java.io.IOException import java.io.IOException
...@@ -13,13 +16,13 @@ import java.io.IOException ...@@ -13,13 +16,13 @@ import java.io.IOException
* *
* @author Him188moe * @author Him188moe
*/ */
@PacketId("08 25 31 01")
class ServerTouchResponsePacket(inputStream: DataInputStream) : ServerPacket(inputStream) { class ServerTouchResponsePacket(inputStream: DataInputStream) : ServerPacket(inputStream) {
var serverIP: String? = null; var serverIP: String? = null
var loginTime: Int = 0 var loginTime: Int = 0
lateinit var loginIP: String lateinit var loginIP: String
lateinit var token0825: ByteArray lateinit var token0825: ByteArray
lateinit var tgtgtKey: ByteArray
enum class Type { enum class Type {
TYPE_08_25_31_01, TYPE_08_25_31_01,
...@@ -40,7 +43,6 @@ class ServerTouchResponsePacket(inputStream: DataInputStream) : ServerPacket(inp ...@@ -40,7 +43,6 @@ class ServerTouchResponsePacket(inputStream: DataInputStream) : ServerPacket(inp
loginTime = input.readInt() loginTime = input.readInt()
loginIP = input.readIP() loginIP = input.readIP()
tgtgtKey = getRandomByteArray(16)
} }
else -> { else -> {
...@@ -54,7 +56,7 @@ class ServerTouchResponsePacket(inputStream: DataInputStream) : ServerPacket(inp ...@@ -54,7 +56,7 @@ class ServerTouchResponsePacket(inputStream: DataInputStream) : ServerPacket(inp
fun decrypt(): ServerTouchResponsePacket = ServerTouchResponsePacket(decryptBy(when (type) { fun decrypt(): ServerTouchResponsePacket = ServerTouchResponsePacket(decryptBy(when (type) {
Type.TYPE_08_25_31_02 -> Protocol.redirectionKey.hexToBytes() Type.TYPE_08_25_31_02 -> Protocol.redirectionKey.hexToBytes()
Type.TYPE_08_25_31_01 -> Protocol.key0825.hexToBytes() Type.TYPE_08_25_31_01 -> Protocol.key0825.hexToBytes()
})) })).setId(this.idHex)
} }
} }
...@@ -83,7 +85,6 @@ class ClientTouchPacket(val qq: Long, val serverIp: String) : ClientPacket() { ...@@ -83,7 +85,6 @@ class ClientTouchPacket(val qq: Long, val serverIp: String) : ClientPacket() {
this.writeIP(serverIp); this.writeIP(serverIp);
this.writeHex("00 02 00 36 00 12 00 02 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 14 00 1D 01 02 00 19") this.writeHex("00 02 00 36 00 12 00 02 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 14 00 1D 01 02 00 19")
this.writeHex(Protocol.publicKey) this.writeHex(Protocol.publicKey)
println(super.toUByteArray().toUHexString())
return super.toByteArray() return super.toByteArray()
} }
}.toByteArray())) }.toByteArray()))
......
package net.mamoe.mirai.network.packet.action
import net.mamoe.mirai.network.Protocol
import net.mamoe.mirai.network.packet.*
import net.mamoe.mirai.utils.getRandomByteArray
import net.mamoe.mirai.utils.toUHexString
import java.io.DataInputStream
import java.util.*
/**
* 向服务器检查是否可添加某人为好友
*
* @author Him188moe
*/
@PacketId("00 A7")
@ExperimentalUnsignedTypes
class ClientCanAddFriendPacket(
val bot: Long,
val qq: Long,
val sessionKey: ByteArray
) : ClientPacket() {
val packetIdLast = getRandomByteArray(2)
override fun getFixedId(): String {
return idHex + " " + packetIdLast.toUHexString()
}
override fun encode() {
this.write(packetIdLast)//id, 2bytes
this.writeQQ(bot)
this.writeHex(Protocol.fixVer2)
this.encryptAndWrite(sessionKey) {
it.writeQQ(qq)
}
}
}
@PacketId("00 A7")
class ServerCanAddFriendResponsePacket(input: DataInputStream) : ServerPacket(input) {
lateinit var state: State
enum class State {
ALREADY_ADDED,
REQUIRE_VERIFICATION,
NOT_REQUIRE_VERIFICATION,
FAILED,
}
@ExperimentalUnsignedTypes
override fun decode() {
val data = input.goto(0).readAllBytes()
if (data.size == 99) {
state = State.ALREADY_ADDED
return
}
state = when (data[data.size - 1].toUInt()) {
0u -> State.NOT_REQUIRE_VERIFICATION
1u -> State.REQUIRE_VERIFICATION
99u -> State.ALREADY_ADDED
3u, 4u -> State.FAILED
else -> throw IllegalArgumentException(Arrays.toString(data))
}
}
@PacketId("00 A7")
class Encrypted(inputStream: DataInputStream) : ServerPacket(inputStream) {
fun decrypt(sessionKey: ByteArray): ServerCanAddFriendResponsePacket {
return ServerCanAddFriendResponsePacket(this.decryptBy(sessionKey)).setId(this.idHex)
}
}
}
/**
* 请求添加好友
*/
@PacketId("00 AE")
@ExperimentalUnsignedTypes
class ClientAddFriendPacket(
val bot: Long,
val qq: Long,
val sessionKey: ByteArray
) : ClientPacket() {
val packetIdLast = getRandomByteArray(2)
override fun getFixedId(): String {
return idHex + " " + packetIdLast.toUHexString()
}
override fun encode() {
this.write(packetIdLast)//id, 2bytes
this.writeQQ(bot)
this.writeHex(Protocol.fixVer2)
this.encryptAndWrite(sessionKey) {
it.writeHex("01 00 01")
it.writeQQ(qq)
}
}
}
class ServerAddFriendResponsePacket(input: DataInputStream) : ServerAddContactResponsePacket(input)
class ServerAddGroupResponsePacket(input: DataInputStream) : ServerAddContactResponsePacket(input)
/**
* 添加好友/群的回复
*/
open class ServerAddContactResponsePacket(input: DataInputStream) : ServerPacket(input) {
class Raw(input: DataInputStream) : ServerPacket(input) {
override fun decode() {
}
fun distribute(): ServerAddContactResponsePacket {
TODO()
}
class Encrypted(input: DataInputStream) : ServerPacket(input) {
fun decrypt(sessionKey: ByteArray): Raw = Raw(this.decryptBy(sessionKey))
}
}
}
\ No newline at end of file
package net.mamoe.mirai.network.packet.action
/**
* 添加好友结果
*/
enum class AddFriendResult {
/**
* 等待对方处理
*/
WAITING_FOR_AGREEMENT,
/**
* 和对方已经是好友了
*/
ALREADY_ADDED,
/**
* 对方设置为不添加好友等
*/
FAILED,
}
\ No newline at end of file
package net.mamoe.mirai.network.packet.action package net.mamoe.mirai.network.packet.action
import net.mamoe.mirai.message.defaults.MessageChain
import net.mamoe.mirai.network.Protocol import net.mamoe.mirai.network.Protocol
import net.mamoe.mirai.network.packet.* import net.mamoe.mirai.network.packet.*
import net.mamoe.mirai.utils.lazyEncode import net.mamoe.mirai.utils.lazyEncode
...@@ -11,22 +12,22 @@ import java.io.DataInputStream ...@@ -11,22 +12,22 @@ import java.io.DataInputStream
@PacketId("00 CD") @PacketId("00 CD")
@ExperimentalUnsignedTypes @ExperimentalUnsignedTypes
class ClientSendFriendMessagePacket( class ClientSendFriendMessagePacket(
private val robotQQ: Long, private val botQQ: Long,
private val targetQQ: Long, private val targetQQ: Long,
private val sessionKey: ByteArray, private val sessionKey: ByteArray,
private val message: String private val message: MessageChain
) : ClientPacket() { ) : ClientPacket() {
override fun encode() { override fun encode() {
this.writeRandom(2)//part of packet id this.writeRandom(2)//part of packet id
this.writeQQ(robotQQ) this.writeQQ(botQQ)
this.writeHex(Protocol.fixVer2) this.writeHex(Protocol.fixVer2)
this.encryptAndWrite(sessionKey) { this.encryptAndWrite(sessionKey) {
it.writeQQ(robotQQ) it.writeQQ(botQQ)
it.writeQQ(targetQQ) it.writeQQ(targetQQ)
it.writeHex("00 00 00 08 00 01 00 04 00 00 00 00") it.writeHex("00 00 00 08 00 01 00 04 00 00 00 00")
it.writeHex("37 0F") it.writeHex("37 0F")
it.writeQQ(robotQQ) it.writeQQ(botQQ)
it.writeQQ(targetQQ) it.writeQQ(targetQQ)
it.write(md5(lazyEncode { md5Key -> md5Key.writeQQ(targetQQ); md5Key.write(sessionKey) })) it.write(md5(lazyEncode { md5Key -> md5Key.writeQQ(targetQQ); md5Key.write(sessionKey) }))
it.writeHex("00 0B") it.writeHex("00 0B")
...@@ -35,24 +36,21 @@ class ClientSendFriendMessagePacket( ...@@ -35,24 +36,21 @@ class ClientSendFriendMessagePacket(
it.writeHex("00 00 00 00 00 00 01 00 00 00 01 4D 53 47 00 00 00 00 00") it.writeHex("00 00 00 00 00 00 01 00 00 00 01 4D 53 47 00 00 00 00 00")
it.writeTime() it.writeTime()
it.writeRandom(4) it.writeRandom(4)
it.writeHex("00 00 00 00 09 00 86 00 00 0C E5 BE AE E8 BD AF E9 9B 85 E9 BB 91") it.writeHex("00 00 00 00 09 00 86")
it.writeHex(Protocol.friendMessageConst1)//... 85 E9 BB 91
it.writeZero(2) it.writeZero(2)
if ("[face" in message
|| ".gif]" in message it.write(message.toByteArray())
|| ".jpg]" in message
|| ".png]" in message /*
) {
TODO("复合消息构建")
} else {
//Plain text //Plain text
val bytes = message.toByteArray() val bytes = message.toByteArray()
it.writeByte(0x01) it.writeByte(0x01)
it.writeShort(bytes.size + 3) it.writeShort(bytes.size + 3)
it.writeByte(0x01) it.writeByte(0x01)
it.writeShort(bytes.size) it.writeShort(bytes.size)
it.write(bytes) it.write(bytes)*/
}
} }
} }
} }
......
...@@ -12,13 +12,13 @@ import java.io.DataInputStream ...@@ -12,13 +12,13 @@ import java.io.DataInputStream
@ExperimentalUnsignedTypes @ExperimentalUnsignedTypes
class ClientSendGroupMessagePacket( class ClientSendGroupMessagePacket(
private val groupId: Long,//不是 number private val groupId: Long,//不是 number
private val robotQQ: Long, private val botQQ: Long,
private val sessionKey: ByteArray, private val sessionKey: ByteArray,
private val message: String private val message: String
) : ClientPacket() { ) : ClientPacket() {
override fun encode() { override fun encode() {
this.writeRandom(2)//part of packet id this.writeRandom(2)//part of packet id
this.writeQQ(robotQQ) this.writeQQ(botQQ)
this.writeHex(Protocol.fixVer2) this.writeHex(Protocol.fixVer2)
this.encryptAndWrite(sessionKey) { this.encryptAndWrite(sessionKey) {
...@@ -30,7 +30,7 @@ class ClientSendGroupMessagePacket( ...@@ -30,7 +30,7 @@ class ClientSendGroupMessagePacket(
it.writeHex("00 01 01 00 00 00 00 00 00 00 4D 53 47 00 00 00 00 00") it.writeHex("00 01 01 00 00 00 00 00 00 00 4D 53 47 00 00 00 00 00")
it.writeTime() it.writeTime()
it.writeRandom(4) it.writeRandom(4)
it.writeHex("00 00 00 00 09 00 86 00 00 0C E5 BE AE E8 BD AF E9 9B 85 E9 BB 91") it.writeHex("Protocol.messageConst1")
it.writeZero(2) it.writeZero(2)
//messages //messages
......
...@@ -17,8 +17,10 @@ class ClientChangeOnlineStatusPacket( ...@@ -17,8 +17,10 @@ class ClientChangeOnlineStatusPacket(
private val loginStatus: ClientLoginStatus private val loginStatus: ClientLoginStatus
) : ClientPacket() { ) : ClientPacket() {
override fun encode() { override fun encode() {
this.writeRandom(2)//part of packet id this.writeRandom(2)//part of packet id
this.writeQQ(qq) this.writeQQ(qq)
this.writeHex(Protocol.fixVer2) this.writeHex(Protocol.fixVer2)
this.encryptAndWrite(sessionKey) { this.encryptAndWrite(sessionKey) {
......
...@@ -2,7 +2,10 @@ package net.mamoe.mirai.network.packet.login ...@@ -2,7 +2,10 @@ package net.mamoe.mirai.network.packet.login
import net.mamoe.mirai.network.Protocol import net.mamoe.mirai.network.Protocol
import net.mamoe.mirai.network.packet.* import net.mamoe.mirai.network.packet.*
import net.mamoe.mirai.utils.* import net.mamoe.mirai.utils.ByteArrayDataOutputStream
import net.mamoe.mirai.utils.TEA
import net.mamoe.mirai.utils.TestedSuccessfully
import net.mamoe.mirai.utils.hexToBytes
import java.io.DataOutputStream import java.io.DataOutputStream
/** /**
...@@ -32,15 +35,14 @@ class ClientPasswordSubmissionPacket( ...@@ -32,15 +35,14 @@ class ClientPasswordSubmissionPacket(
this.encryptAndWrite(Protocol.shareKey.hexToBytes()) { this.encryptAndWrite(Protocol.shareKey.hexToBytes()) {
it.writePart1(qq, password, loginTime, loginIP, tgtgtKey, token0825) it.writePart1(qq, password, loginTime, loginIP, tgtgtKey, token0825)
it.writePart2() it.writePart2()
println(it.toByteArray().toUHexString())
} }
} }
} }
@PacketId("08 36 31 04") @PacketId("08 36 31 04")
@ExperimentalUnsignedTypes @ExperimentalUnsignedTypes
class ClientLoginResendPacket3104(qq: Long, password: String, loginTime: Int, loginIP: String, tgtgtKey: ByteArray, token0825: ByteArray, token00BA: ByteArray, tlv_0006_encr: ByteArray? = null) class ClientLoginResendPacket3104(qq: Long, password: String, loginTime: Int, loginIP: String, tgtgtKey: ByteArray, token0825: ByteArray, token00BA: ByteArray, tlv0006: ByteArray? = null)
: ClientLoginResendPacket(qq, password, loginTime, loginIP, tgtgtKey, token0825, token00BA, tlv_0006_encr) : ClientLoginResendPacket(qq, password, loginTime, loginIP, tgtgtKey, token0825, token00BA, tlv0006)
@PacketId("08 36 31 05") @PacketId("08 36 31 05")
@ExperimentalUnsignedTypes @ExperimentalUnsignedTypes
...@@ -49,8 +51,8 @@ class ClientLoginResendPacket3105(qq: Long, password: String, loginTime: Int, lo ...@@ -49,8 +51,8 @@ class ClientLoginResendPacket3105(qq: Long, password: String, loginTime: Int, lo
@PacketId("08 36 31 06") @PacketId("08 36 31 06")
@ExperimentalUnsignedTypes @ExperimentalUnsignedTypes
class ClientLoginResendPacket3106(qq: Long, password: String, loginTime: Int, loginIP: String, tgtgtKey: ByteArray, token0825: ByteArray, token00BA: ByteArray, tlv_0006_encr: ByteArray? = null) class ClientLoginResendPacket3106(qq: Long, password: String, loginTime: Int, loginIP: String, tgtgtKey: ByteArray, token0825: ByteArray, token00BA: ByteArray, tlv0006: ByteArray? = null)
: ClientLoginResendPacket(qq, password, loginTime, loginIP, tgtgtKey, token0825, token00BA, tlv_0006_encr) : ClientLoginResendPacket(qq, password, loginTime, loginIP, tgtgtKey, token0825, token00BA, tlv0006)
@ExperimentalUnsignedTypes @ExperimentalUnsignedTypes
open class ClientLoginResendPacket internal constructor( open class ClientLoginResendPacket internal constructor(
...@@ -61,7 +63,7 @@ open class ClientLoginResendPacket internal constructor( ...@@ -61,7 +63,7 @@ open class ClientLoginResendPacket internal constructor(
val tgtgtKey: ByteArray, val tgtgtKey: ByteArray,
val token0825: ByteArray, val token0825: ByteArray,
val token00BA: ByteArray, val token00BA: ByteArray,
val tlv_0006_encr: ByteArray? = null val tlv0006: ByteArray? = null
) : ClientPacket() { ) : ClientPacket() {
override fun encode() { override fun encode() {
this.writeQQ(qq) this.writeQQ(qq)
...@@ -72,7 +74,7 @@ open class ClientLoginResendPacket internal constructor( ...@@ -72,7 +74,7 @@ open class ClientLoginResendPacket internal constructor(
this.write(TEA.encrypt(object : ByteArrayDataOutputStream() { this.write(TEA.encrypt(object : ByteArrayDataOutputStream() {
override fun toByteArray(): ByteArray { override fun toByteArray(): ByteArray {
this.writePart1(qq, password, loginTime, loginIP, tgtgtKey, token0825, tlv_0006_encr) this.writePart1(qq, password, loginTime, loginIP, tgtgtKey, token0825, tlv0006)
this.writeHex("01 10") //tag this.writeHex("01 10") //tag
this.writeHex("00 3C")//length this.writeHex("00 3C")//length
...@@ -92,21 +94,21 @@ open class ClientLoginResendPacket internal constructor( ...@@ -92,21 +94,21 @@ open class ClientLoginResendPacket internal constructor(
* @author Him188moe * @author Him188moe
*/ */
@ExperimentalUnsignedTypes @ExperimentalUnsignedTypes
private fun DataOutputStream.writePart1(qq: Long, password: String, loginTime: Int, loginIP: String, tgtgtKey: ByteArray, token0825: ByteArray, tlv_0006_encr: ByteArray? = null) { private fun DataOutputStream.writePart1(qq: Long, password: String, loginTime: Int, loginIP: String, tgtgtKey: ByteArray, token0825: ByteArray, tlv0006: ByteArray? = null) {
//this.writeInt(System.currentTimeMillis().toInt()) //this.writeInt(System.currentTimeMillis().toInt())
this.writeHex("01 12")//tag this.writeHex("01 12")//tag
this.writeHex("00 38")//length this.writeHex("00 38")//length
this.write(token0825)//length this.write(token0825)//length
this.writeHex("03 0F")//tag this.writeHex("03 0F")//tag
this.writeDeviceName(true)//todo 随机 this.writeDeviceName(true)
this.writeHex("00 05 00 06 00 02") this.writeHex("00 05 00 06 00 02")
this.writeQQ(qq) this.writeQQ(qq)
this.writeHex("00 06")//tag this.writeHex("00 06")//tag
this.writeHex("00 78")//length this.writeHex("00 78")//length
if (tlv_0006_encr != null) { if (tlv0006 != null) {
this.write(tlv_0006_encr) this.write(tlv0006)
} else { } else {
this.writeTLV0006(qq, password, loginTime, loginIP, tgtgtKey) this.writeTLV0006(qq, password, loginTime, loginIP, tgtgtKey)
} }
......
...@@ -7,7 +7,7 @@ enum class LoginState { ...@@ -7,7 +7,7 @@ enum class LoginState {
/** /**
* 登录成功 * 登录成功
*/ */
SUCCEED, SUCCESS,
/** /**
* 密码错误 * 密码错误
......
package net.mamoe.mirai.network.packet.login package net.mamoe.mirai.network.packet.login
import net.mamoe.mirai.network.Protocol
import net.mamoe.mirai.network.packet.PacketId import net.mamoe.mirai.network.packet.PacketId
import net.mamoe.mirai.network.packet.ServerPacket import net.mamoe.mirai.network.packet.ServerPacket
import net.mamoe.mirai.network.packet.goto import net.mamoe.mirai.network.packet.goto
...@@ -7,16 +8,18 @@ import net.mamoe.mirai.utils.TestedSuccessfully ...@@ -7,16 +8,18 @@ import net.mamoe.mirai.utils.TestedSuccessfully
import java.io.DataInputStream import java.io.DataInputStream
/** /**
* 服务器进行加密后返回 tgtgtKey
*
* @author NaturalHG * @author NaturalHG
*/ */
@PacketId("08 36 31 03") @PacketId("08 36 31 03")
class ServerLoginResponseResendPacket(input: DataInputStream, val flag: Flag) : ServerPacket(input) { class ServerLoginResponseKeyExchangePacket(input: DataInputStream, val flag: Flag) : ServerPacket(input) {
enum class Flag { enum class Flag {
`08 36 31 03`, `08 36 31 03`,
OTHER, OTHER,
} }
lateinit var _0836_tlv0006_encr: ByteArray;//120bytes lateinit var tlv0006: ByteArray;//120bytes
var tokenUnknown: ByteArray? = null var tokenUnknown: ByteArray? = null
lateinit var tgtgtKey: ByteArray//16bytes lateinit var tgtgtKey: ByteArray//16bytes
...@@ -26,7 +29,7 @@ class ServerLoginResponseResendPacket(input: DataInputStream, val flag: Flag) : ...@@ -26,7 +29,7 @@ class ServerLoginResponseResendPacket(input: DataInputStream, val flag: Flag) :
tgtgtKey = this.input.readNBytes(16)//22 tgtgtKey = this.input.readNBytes(16)//22
//this.input.skip(2)//25 //this.input.skip(2)//25
this.input.goto(25) this.input.goto(25)
_0836_tlv0006_encr = this.input.readNBytes(120) tlv0006 = this.input.readNBytes(120)
when (flag) { when (flag) {
Flag.`08 36 31 03` -> { Flag.`08 36 31 03` -> {
...@@ -36,14 +39,17 @@ class ServerLoginResponseResendPacket(input: DataInputStream, val flag: Flag) : ...@@ -36,14 +39,17 @@ class ServerLoginResponseResendPacket(input: DataInputStream, val flag: Flag) :
Flag.OTHER -> { Flag.OTHER -> {
//do nothing in this packet. //do nothing in this packet.
//[this.token] will be set in [RobotNetworkHandler] //[this.token] will be set in [BotNetworkHandler]
//token //token
} }
} }
} }
class Encrypted(input: DataInputStream, private val flag: Flag) : ServerPacket(input) { class Encrypted(input: DataInputStream, private val flag: Flag) : ServerPacket(input) {
@ExperimentalUnsignedTypes
@TestedSuccessfully @TestedSuccessfully
fun decrypt(tgtgtKey: ByteArray): ServerLoginResponseResendPacket = ServerLoginResponseResendPacket(decryptBy(tgtgtKey), flag) fun decrypt(tgtgtKey: ByteArray): ServerLoginResponseKeyExchangePacket {
return ServerLoginResponseKeyExchangePacket(this.decryptBy(Protocol.shareKey, tgtgtKey), flag).setId(this.idHex)
}
} }
} }
package net.mamoe.mirai.network.packet.login package net.mamoe.mirai.network.packet.login
import net.mamoe.mirai.network.Protocol import net.mamoe.mirai.network.Protocol
import net.mamoe.mirai.network.packet.* import net.mamoe.mirai.network.packet.ServerPacket
import net.mamoe.mirai.utils.TEA import net.mamoe.mirai.network.packet.goto
import net.mamoe.mirai.network.packet.readNBytesAt
import net.mamoe.mirai.network.packet.readString
import net.mamoe.mirai.utils.TestedSuccessfully import net.mamoe.mirai.utils.TestedSuccessfully
import net.mamoe.mirai.utils.toUHexString import net.mamoe.mirai.utils.toUHexString
import java.io.DataInputStream import java.io.DataInputStream
...@@ -41,7 +43,7 @@ class ServerLoginResponseSuccessPacket(input: DataInputStream) : ServerPacket(in ...@@ -41,7 +43,7 @@ class ServerLoginResponseSuccessPacket(input: DataInputStream) : ServerPacket(in
this.token88 = this.input.readNBytesAt(189 + msgLength, 136) this.token88 = this.input.readNBytesAt(189 + msgLength, 136)
val nickLength = this.input.goto(624 + msgLength).readByte().toInt() val nickLength = this.input.goto(624 + msgLength).readByte().toInt()
this.nickname = this.input.readVarString(nickLength) this.nickname = this.input.readString(nickLength)
//this.age = this.input.goto(packetDataLength - 28).readShortAt() //this.age = this.input.goto(packetDataLength - 28).readShortAt()
...@@ -53,7 +55,7 @@ class ServerLoginResponseSuccessPacket(input: DataInputStream) : ServerPacket(in ...@@ -53,7 +55,7 @@ class ServerLoginResponseSuccessPacket(input: DataInputStream) : ServerPacket(in
@ExperimentalUnsignedTypes @ExperimentalUnsignedTypes
fun decrypt(tgtgtKey: ByteArray): ServerLoginResponseSuccessPacket { fun decrypt(tgtgtKey: ByteArray): ServerLoginResponseSuccessPacket {
input goto 14 input goto 14
return ServerLoginResponseSuccessPacket(TEA.decrypt(TEA.decrypt(input.readAllBytes().cutTail(1), Protocol.shareKey), tgtgtKey).dataInputStream()); return ServerLoginResponseSuccessPacket(this.decryptBy(Protocol.shareKey, tgtgtKey)).setId(this.idHex)
} }
} }
......
...@@ -43,7 +43,7 @@ class ServerLoginResponseVerificationCodeInitPacket(input: DataInputStream, priv ...@@ -43,7 +43,7 @@ class ServerLoginResponseVerificationCodeInitPacket(input: DataInputStream, priv
fun decrypt(): ServerLoginResponseVerificationCodeInitPacket { fun decrypt(): ServerLoginResponseVerificationCodeInitPacket {
this.input goto 14 this.input goto 14
val data = TEA.CRYPTOR_SHARE_KEY.decrypt(this.input.readAllBytes().cutTail(1)); val data = TEA.CRYPTOR_SHARE_KEY.decrypt(this.input.readAllBytes().cutTail(1));
return ServerLoginResponseVerificationCodeInitPacket(data.dataInputStream(), data.size) return ServerLoginResponseVerificationCodeInitPacket(data.dataInputStream(), data.size).setId(this.idHex)
} }
} }
} }
......
package net.mamoe.mirai.plugin; package net.mamoe.mirai.plugin;
import net.mamoe.mirai.Robot; import net.mamoe.mirai.Bot;
/** /**
* 插件基类. * 插件基类.
* <p> * <p>
* 插件属于整个 Mirai, 而不是属于单个 {@link Robot}. * 插件属于整个 Mirai, 而不是属于单个 {@link Bot}.
* *
* @see net.mamoe.mirai.event.MiraiEventManager * @see net.mamoe.mirai.event.MiraiEventManager
* @see net.mamoe.mirai.event.MiraiEventManagerKt * @see net.mamoe.mirai.event.MiraiEventManagerKt
......
package net.mamoe.mirai.utils package net.mamoe.mirai.utils
import net.mamoe.mirai.Bot
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.* import java.util.*
...@@ -28,12 +29,42 @@ object MiraiLogger { ...@@ -28,12 +29,42 @@ object MiraiLogger {
this.print(e.cause.toString())*/ this.print(e.cause.toString())*/
} }
@Synchronized
private fun print(value: String?, color: LoggerTextFormat = LoggerTextFormat.WHITE) { private fun print(value: String?, color: LoggerTextFormat = LoggerTextFormat.WHITE) {
val s = SimpleDateFormat("MM-dd HH:mm:ss").format(Date()) val s = SimpleDateFormat("MM-dd HH:mm:ss").format(Date())
kotlin.io.println("$color[Mirai] $s : $value") kotlin.io.println("$color[Mirai] $s : $value")
} }
} }
infix fun Bot.log(o: Any?) = info(o)
infix fun Bot.println(o: Any?) = info(o)
infix fun Bot.info(o: Any?) = print(this, o.toString(), LoggerTextFormat.RESET)
infix fun Bot.error(o: Any?) = print(this, o.toString(), LoggerTextFormat.RED)
infix fun Bot.notice(o: Any?) = print(this, o.toString(), LoggerTextFormat.LIGHT_BLUE)
infix fun Bot.purple(o: Any?) = print(this, o.toString(), LoggerTextFormat.PURPLE)
infix fun Bot.cyanL(o: Any?) = print(this, o.toString(), LoggerTextFormat.LIGHT_CYAN)
infix fun Bot.success(o: Any?) = print(this, o.toString(), LoggerTextFormat.GREEN)
infix fun Bot.debug(o: Any?) = print(this, o.toString(), LoggerTextFormat.YELLOW)
@Synchronized
private fun print(bot: Bot, value: String?, color: LoggerTextFormat = LoggerTextFormat.WHITE) {
val s = SimpleDateFormat("MM-dd HH:mm:ss").format(Date())
kotlin.io.println("$color[Mirai] $s #R${bot.id}: $value")
}
@Synchronized
private fun print(value: String?, color: LoggerTextFormat = LoggerTextFormat.WHITE) {
val s = SimpleDateFormat("MM-dd HH:mm:ss").format(Date())
kotlin.io.println("$color[Mirai] $s : $value")
}
fun Any.logInfo() = MiraiLogger.info(this) fun Any.logInfo() = MiraiLogger.info(this)
......
...@@ -6,11 +6,11 @@ import lombok.Data; ...@@ -6,11 +6,11 @@ import lombok.Data;
* @author Him188moe * @author Him188moe
*/ */
@Data @Data
public final class RobotAccount { public final class BotAccount {
public final long qqNumber; public final long qqNumber;
public final String password; public final String password;
public RobotAccount(long qqNumber, String password) { public BotAccount(long qqNumber, String password) {
this.qqNumber = qqNumber; this.qqNumber = qqNumber;
this.password = password; this.password = password;
} }
......
...@@ -17,6 +17,7 @@ public final class TEA { ...@@ -17,6 +17,7 @@ public final class TEA {
private static final long UINT32_MASK = 0xffffffffL; private static final long UINT32_MASK = 0xffffffffL;
private final long[] mKey; private final long[] mKey;
private final Random mRandom; private final Random mRandom;
private final byte[] key;
private byte[] mOutput; private byte[] mOutput;
private byte[] mInBlock; private byte[] mInBlock;
private int mIndexPos; private int mIndexPos;
...@@ -26,6 +27,7 @@ public final class TEA { ...@@ -26,6 +27,7 @@ public final class TEA {
private boolean isFirstBlock; private boolean isFirstBlock;
public TEA(byte[] key) { public TEA(byte[] key) {
this.key = key;
mKey = new long[4]; mKey = new long[4];
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
mKey[i] = pack(key, i * 4, 4); mKey[i] = pack(key, i * 4, 4);
...@@ -250,6 +252,12 @@ public final class TEA { ...@@ -250,6 +252,12 @@ public final class TEA {
} }
public byte[] decrypt(byte[] ciphertext) { public byte[] decrypt(byte[] ciphertext) {
try {
return decrypt(ciphertext, 0, ciphertext.length); return decrypt(ciphertext, 0, ciphertext.length);
} catch (Exception e) {
System.out.println("Source: " + UtilsKt.toUHexString(ciphertext, " "));
System.out.println("Key: " + UtilsKt.toUHexString(this.key, " "));
throw e;
}
} }
} }
\ No newline at end of file
...@@ -45,9 +45,6 @@ fun UByteArray.toUHexString(): String = this.toUHexString(" ") ...@@ -45,9 +45,6 @@ fun UByteArray.toUHexString(): String = this.toUHexString(" ")
@ExperimentalUnsignedTypes @ExperimentalUnsignedTypes
fun Byte.toUHexString(): String = this.toUByte().toString(16) fun Byte.toUHexString(): String = this.toUByte().toString(16)
/**
* firstly [Protocol.hexToUBytes], secondly [UByteArray.toByteArray]
*/
@ExperimentalUnsignedTypes @ExperimentalUnsignedTypes
fun String.hexToBytes(): ByteArray = Protocol.hexToBytes(this) fun String.hexToBytes(): ByteArray = Protocol.hexToBytes(this)
...@@ -55,13 +52,11 @@ fun String.hexToBytes(): ByteArray = Protocol.hexToBytes(this) ...@@ -55,13 +52,11 @@ fun String.hexToBytes(): ByteArray = Protocol.hexToBytes(this)
fun String.hexToUBytes(): UByteArray = Protocol.hexToUBytes(this) fun String.hexToUBytes(): UByteArray = Protocol.hexToUBytes(this)
@ExperimentalUnsignedTypes @ExperimentalUnsignedTypes
fun String.hexToShort(): Short = hexToBytes().let { ((it[1].toInt() shl 8) + it[0]).toShort() } fun String.hexToInt(): Int = hexToBytes().toUInt().toInt()
@ExperimentalUnsignedTypes @ExperimentalUnsignedTypes
fun String.hexToInt(): Int = hexToBytes().let { ((it[0].toInt() shl 24) + (it[1].toInt() shl 16) + (it[2].toInt() shl 8) + it[3]) } fun ByteArray.toUInt(): UInt =
this[0].toUInt().and(255u).shl(24) + this[1].toUInt().and(255u).shl(16) + this[2].toUInt().and(255u).shl(8) + this[3].toUInt().and(255u).shl(0)
@ExperimentalUnsignedTypes
fun String.hexToByte(): Byte = hexToBytes()[0]
open class ByteArrayDataOutputStream : DataOutputStream(ByteArrayOutputStream()) { open class ByteArrayDataOutputStream : DataOutputStream(ByteArrayOutputStream()) {
open fun toByteArray(): ByteArray = (out as ByteArrayOutputStream).toByteArray() open fun toByteArray(): ByteArray = (out as ByteArrayOutputStream).toByteArray()
...@@ -101,8 +96,6 @@ internal fun getCrc32(key: ByteArray): Int = CRC32().let { it.update(key); it.va ...@@ -101,8 +96,6 @@ internal fun getCrc32(key: ByteArray): Int = CRC32().let { it.update(key); it.va
* 相当于将这个类和它所有父类的 [Class.getDeclaredFields] 都合并成一个 [List] <br></br> * 相当于将这个类和它所有父类的 [Class.getDeclaredFields] 都合并成一个 [List] <br></br>
* 不会排除重名的字段. <br></br> * 不会排除重名的字段. <br></br>
* *
* @param clazz class
*
* @return field list * @return field list
*/ */
@Throws(SecurityException::class) @Throws(SecurityException::class)
......
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import net.mamoe.mirai.Robot import net.mamoe.mirai.Bot
import net.mamoe.mirai.network.packet.login.LoginState import net.mamoe.mirai.network.packet.login.LoginState
import net.mamoe.mirai.utils.RobotAccount import net.mamoe.mirai.utils.BotAccount
import java.util.* import java.util.*
/** /**
...@@ -64,21 +64,21 @@ val qqList = "2535777366----abc123456\n" + ...@@ -64,21 +64,21 @@ val qqList = "2535777366----abc123456\n" +
fun main() { fun main() {
val goodRobotList = Collections.synchronizedList(mutableListOf<Robot>()) val goodBotList = Collections.synchronizedList(mutableListOf<Bot>())
qqList.split("\n").forEach { qqList.split("\n").forEach {
GlobalScope.launch { GlobalScope.launch {
val strings = it.split("----") val strings = it.split("----")
val robot = Robot(RobotAccount(strings[0].toLong(), strings[1].let { password -> val bot = Bot(BotAccount(strings[0].toLong(), strings[1].let { password ->
if (password.endsWith(".")) { if (password.endsWith(".")) {
return@let password.substring(0, password.length - 1) return@let password.substring(0, password.length - 1)
} }
return@let password return@let password
}), listOf()) }), listOf())
robot.network.tryLogin().whenComplete { state, _ -> bot.network.tryLogin().whenComplete { state, _ ->
if (!(state == LoginState.BLOCKED || state == LoginState.DEVICE_LOCK || state == LoginState.WRONG_PASSWORD)) { if (!(state == LoginState.BLOCKED || state == LoginState.DEVICE_LOCK || state == LoginState.WRONG_PASSWORD)) {
goodRobotList.add(robot) goodBotList.add(bot)
} }
} }
} }
...@@ -86,6 +86,5 @@ fun main() { ...@@ -86,6 +86,5 @@ fun main() {
Thread.sleep(9 * 3000) Thread.sleep(9 * 3000)
println(goodRobotList.joinToString("\n") { it.account.qqNumber.toString() + " " + it.account.password }) println(goodBotList.joinToString("\n") { it.account.qqNumber.toString() + " " + it.account.password })
} }
...@@ -50,10 +50,20 @@ public class HexComparator { ...@@ -50,10 +50,20 @@ public class HexComparator {
@SuppressWarnings({"unused", "NonAsciiCharacters"}) @SuppressWarnings({"unused", "NonAsciiCharacters"})
private static class TestConsts { private static class TestConsts {
private static final String 牛逼 = UtilsKt.toUHexString("牛逼".getBytes(), " "); private static final String NIU_BI = UtilsKt.toUHexString("牛逼".getBytes(), " ");
private static final String _1994701021 = ClientPacketKt.toHexString(1994701021, " "); private static final String _1994701021 = ClientPacketKt.toUHexString(1994701021, " ");
private static final String _1040400290 = ClientPacketKt.toHexString(1040400290, " "); private static final String _1040400290 = ClientPacketKt.toUHexString(1040400290, " ");
private static final String _580266363 = ClientPacketKt.toHexString(580266363, " "); private static final String _580266363 = ClientPacketKt.toUHexString(580266363, " ");
private static final String _1040400290_ = "3E 03 3F A2";
private static final String _1994701021_ = "76 E4 B8 DD";
private static final String _jiahua_ = "B1 89 BE 09";
private static final String SINGLE_PLAIN_MESSAGE_HEAD = "00 00 01 00 09 01";
private static final String MESSAGE_TAIL_10404 = "0E 00 07 01 00 04 00 00 00 09 19 00 18 01 00 15 AA 02 12 9A 01 0F 80 01 01 C8 01 00 F0 01 00 F8 01 00 90 02 00".replace(" ", " ");
//private static final String MESSAGE_TAIL2_10404 ="".replace(" ", " ");
} }
private final List<Match> matches = new LinkedList<>(); private final List<Match> matches = new LinkedList<>();
...@@ -88,7 +98,7 @@ public class HexComparator { ...@@ -88,7 +98,7 @@ public class HexComparator {
return new LinkedList<>(); return new LinkedList<>();
} }
return new LinkedList<>() {{ return new LinkedList<>() {{
int index = 0; int index = -1;
while ((index = hex.indexOf(constValue, index + 1)) != -1) { while ((index = hex.indexOf(constValue, index + 1)) != -1) {
add(new IntRange(index / 3, (index + constValue.length()) / 3)); add(new IntRange(index / 3, (index + constValue.length()) / 3));
} }
...@@ -105,18 +115,20 @@ public class HexComparator { ...@@ -105,18 +115,20 @@ public class HexComparator {
} }
private static void buildConstNameChain(int length, ConstMatcher constMatcher, StringBuilder constNameBuilder) { private static void buildConstNameChain(int length, ConstMatcher constMatcher, StringBuilder constNameBuilder) {
//System.out.println(constMatcher.matches);
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
constNameBuilder.append(" "); constNameBuilder.append(" ");
String match = constMatcher.getMatchedConstName(i / 4); String match = constMatcher.getMatchedConstName(i / 4);
if (match != null) { if (match != null) {
int appendedNameLength = match.length(); int appendedNameLength = match.length();
constNameBuilder.append(match); constNameBuilder.append(match);
while (constMatcher.getMatchedConstName(i++ / 4) != null) { while (match.equals(constMatcher.getMatchedConstName(i++ / 4))) {
if (appendedNameLength-- <= 0) { if (appendedNameLength-- < 0) {
constNameBuilder.append(" "); constNameBuilder.append(" ");
} }
} }
constNameBuilder.append(" ".repeat(match.length() % 4));
} }
} }
} }
...@@ -281,10 +293,10 @@ public class HexComparator { ...@@ -281,10 +293,10 @@ public class HexComparator {
System.out.println(HexComparator.compare( System.out.println(HexComparator.compare(
//mirai //mirai
"2A 22 96 29 7B 00 40 00 01 01 00 00 00 00 00 00 00 4D 53 47 00 00 00 00 00 EC 21 40 06 18 89 54 BC 00 00 00 00 09 00 86 00 00 0C E5 BE AE E8 BD AF E9 9B 85 E9 BB 91 00 00 01 00 0A 01 00 07 E7 89 9B E9 80 BC 21\n" "2A 22 96 29 7B 00 40 00 01 01 00 00 00 00 00 00 00 4D 53 47 00 00 00 00 00 EC 21 40 06 18 89 54 BC Protocol.messageConst1 00 00 01 00 0A 01 00 07 E7 89 9B E9 80 BC 21\n"
, ,
//e //e
"2A 22 96 29 7B 00 3F 00 01 01 00 00 00 00 00 00 00 4D 53 47 00 00 00 00 00 5D 6B 8E 1A FE 39 0B FC 00 00 00 00 09 00 86 00 00 0C E5 BE AE E8 BD AF E9 9B 85 E9 BB 91 00 00 01 00 0A 01 00 07 6D 65 73 73 61 67 65" "2A 22 96 29 7B 00 3F 00 01 01 00 00 00 00 00 00 00 4D 53 47 00 00 00 00 00 5D 6B 8E 1A FE 39 0B FC Protocol.messageConst1 00 00 01 00 0A 01 00 07 6D 65 73 73 61 67 65"
)); ));
......
package net.mamoe.mirai.util;
/**
* @author Him188moe
*/
public final class TeaEncryption {
public static native int Decrypt();
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>mirai-ui</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<parent>
<groupId>net.mamoe</groupId>
<artifactId>mirai</artifactId>
<version>1.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<dependencies>
</dependencies>
<build>
<resources>
<resource>
<directory>/src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
<plugins>
</plugins>
</build>
</project>
\ No newline at end of file
...@@ -11,7 +11,9 @@ ...@@ -11,7 +11,9 @@
<modules> <modules>
<module>mirai-core</module> <module>mirai-core</module>
<module>mirai-native</module> <module>mirai-ui</module>
<module>mirai-console</module>
<module>mirai-api</module>
</modules> </modules>
<repositories> <repositories>
...@@ -27,41 +29,14 @@ ...@@ -27,41 +29,14 @@
<packaging>pom</packaging> <packaging>pom</packaging>
<properties> <properties>
<kotlin.version>1.3.41</kotlin.version>
<maven.compiler.source>11</maven.compiler.source> <maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target> <maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> </properties>
<build>
<defaultGoal>package</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<dependencies>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
<version>7.1</version>
</dependency>
</dependencies>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-maven-plugin</artifactId>
<version>1.18.6.0</version>
</plugin>
</plugins>
</build>
<dependencies> <dependencies>
<dependency> <dependency>
...@@ -121,7 +96,114 @@ ...@@ -121,7 +96,114 @@
<version>0.5.2</version> <version>0.5.2</version>
</dependency> </dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${kotlin.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.12.1</version>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.18</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
<version>1.3.41</version>
<scope>compile</scope>
</dependency>
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>
<build>
<defaultGoal>package</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<dependencies>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
<version>7.1</version>
</dependency>
</dependencies>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-maven-plugin</artifactId>
<version>1.18.6.0</version>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>shaded</shadedClassifierName>
</configuration>
</plugin>
<plugin>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-maven-plugin</artifactId>
<version>1.18.6.0</version>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<jvmTarget>1.8</jvmTarget>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project> </project>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment