Commit f2986d91 authored by Him188moe's avatar Him188moe

Updated message

parent fa6a9b66
...@@ -24,8 +24,12 @@ Network部分使用 Kotlin 完成(因为kt有对 unsigned byte 的支持). ...@@ -24,8 +24,12 @@ Network部分使用 Kotlin 完成(因为kt有对 unsigned byte 的支持).
1. Clone 1. Clone
2. Import as Maven project 2. Import as Maven project
3. Run [MiraiMain](mirai-core/src/main/java/net/mamoe/mirai/MiraiMain.java#L7) 3. Run [MiraiMain](mirai-core/src/main/java/net/mamoe/mirai/MiraiMain.java#L7)
简略阅读源码便可测试接收和发送消息 简略阅读源码便可测试接收和发送消息
Hook 一个事件(Kotlin):
![event hook.png](.github/event%20hook.png)
### TODO ### TODO
- [x] 事件(Event)模块 - [x] 事件(Event)模块
- [ ] 插件(Plugin)模块 **(Working on)** - [ ] 插件(Plugin)模块 **(Working on)**
......
...@@ -178,7 +178,13 @@ public class MiraiServer { ...@@ -178,7 +178,13 @@ public class MiraiServer {
getLogger().info("Initializing [Robot]s"); getLogger().info("Initializing [Robot]s");
try {
getAvailableRobot();
} 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 [Robot] " + section.getString("account"));
try { try {
...@@ -199,7 +205,7 @@ public class MiraiServer { ...@@ -199,7 +205,7 @@ public class MiraiServer {
getLogger().error("Could not load QQ robots config!"); getLogger().error("Could not load QQ robots config!");
System.exit(1); System.exit(1);
} }
}); });*/
} }
...@@ -209,20 +215,9 @@ public class MiraiServer { ...@@ -209,20 +215,9 @@ public class MiraiServer {
"3145561616----987654321\n" + "3145561616----987654321\n" +
"2374150554----12345678910\n" + "2374150554----12345678910\n" +
"2375307394----12345678910\n" + "2375307394----12345678910\n" +
"1531848970----1234567890\n" +
"1947293188----a123456789\n" +
"1771231721----123456789a\n" +
"2401645747----12345678910\n" + "2401645747----12345678910\n" +
"3338427598----987654321\n" +
"3055657369----1234567890\n" +
"3502771486----987654321\n" +
"1515419818----1234567890\n" + "1515419818----1234567890\n" +
"2402273360----12345678910\n" + "3107367848----987654321\n";
"3107367848----987654321\n" +
"3487109947----123456789a\n" +
"3489288352----123456789a\n" +
"2385617018----12345678910\n" +
"1251003390----123456789a\n";
private Robot getAvailableRobot() throws ExecutionException, InterruptedException { private Robot getAvailableRobot() throws ExecutionException, InterruptedException {
for (String it : qqList.split("\n")) { for (String it : qqList.split("\n")) {
......
...@@ -15,7 +15,17 @@ import java.util.* ...@@ -15,7 +15,17 @@ import java.util.*
* 可发送的或从服务器接收的消息. * 可发送的或从服务器接收的消息.
* 采用这样的消息模式是因为 QQ 的消息多元化, 一条消息中可包含 [纯文本][PlainText], [图片][Image] 等. * 采用这样的消息模式是因为 QQ 的消息多元化, 一条消息中可包含 [纯文本][PlainText], [图片][Image] 等.
* *
* 在 Kotlin, 使用 [Message] 与使用 [String] 几乎没有什么用法上的区别, 除了 * #### 在 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 * @author Him188moe
* @see Contact.sendMessage * @see Contact.sendMessage
......
...@@ -3,6 +3,7 @@ package net.mamoe.mirai.message.defaults ...@@ -3,6 +3,7 @@ package net.mamoe.mirai.message.defaults
import net.mamoe.mirai.message.Message import net.mamoe.mirai.message.Message
import net.mamoe.mirai.message.MessageId import net.mamoe.mirai.message.MessageId
import net.mamoe.mirai.utils.lazyEncode import net.mamoe.mirai.utils.lazyEncode
import org.intellij.lang.annotations.MagicConstant
import java.util.* import java.util.*
import java.util.stream.Collectors import java.util.stream.Collectors
import java.util.stream.Stream import java.util.stream.Stream
...@@ -31,6 +32,20 @@ class MessageChain : Message { ...@@ -31,6 +32,20 @@ class MessageChain : Message {
return list.toList() 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> { fun stream(): Stream<Message> {
return ArrayList(list).stream() return ArrayList(list).stream()
} }
......
...@@ -12,6 +12,7 @@ import net.mamoe.mirai.event.events.network.PacketSentEvent ...@@ -12,6 +12,7 @@ import net.mamoe.mirai.event.events.network.PacketSentEvent
import net.mamoe.mirai.event.events.network.ServerPacketReceivedEvent import net.mamoe.mirai.event.events.network.ServerPacketReceivedEvent
import net.mamoe.mirai.event.events.qq.FriendMessageEvent import net.mamoe.mirai.event.events.qq.FriendMessageEvent
import net.mamoe.mirai.event.events.robot.RobotLoginSucceedEvent import net.mamoe.mirai.event.events.robot.RobotLoginSucceedEvent
import net.mamoe.mirai.event.hookAlways
import net.mamoe.mirai.event.hookWhile import net.mamoe.mirai.event.hookWhile
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.MessageChain
...@@ -483,6 +484,16 @@ class RobotNetworkHandler(private val robot: Robot) : Closeable { ...@@ -483,6 +484,16 @@ class RobotNetworkHandler(private val robot: Robot) : Closeable {
inner class MessageHandler : PacketHandler() { inner class MessageHandler : PacketHandler() {
internal var ignoreMessage: Boolean = false internal var ignoreMessage: Boolean = false
init {
FriendMessageEvent::class.hookAlways {
if (it.message() valueEquals "你好") {
it.qq.sendMessage("你好!")
} else if (it.message().toString().startsWith("复读")) {
it.qq.sendMessage(it.message())
}
}
}
override fun onPacketReceived(packet: ServerPacket) { override fun onPacketReceived(packet: ServerPacket) {
when (packet) { when (packet) {
is ServerGroupUploadFileEventPacket -> { is ServerGroupUploadFileEventPacket -> {
...@@ -494,12 +505,7 @@ class RobotNetworkHandler(private val robot: Robot) : Closeable { ...@@ -494,12 +505,7 @@ class RobotNetworkHandler(private val robot: Robot) : Closeable {
return return
} }
val friendMessageEvent = FriendMessageEvent(robot, robot.contacts.getQQ(packet.qq), packet.message) FriendMessageEvent(robot, robot.contacts.getQQ(packet.qq), packet.message).broadcast()
friendMessageEvent.broadcast()
if (friendMessageEvent.message() valueEquals "你好") {
friendMessageEvent.qq.sendMessage("你好")
}
} }
is ServerGroupMessageEventPacket -> { is ServerGroupMessageEventPacket -> {
......
...@@ -70,7 +70,7 @@ abstract class ClientPacket : ByteArrayDataOutputStream(), Packet { ...@@ -70,7 +70,7 @@ abstract class ClientPacket : ByteArrayDataOutputStream(), Packet {
override fun toString(): String { override fun toString(): String {
return adjustName(this.javaClass.simpleName + "(${this.getFixedId()})") + this.getAllDeclaredFields().filterNot { it.name == "idHex" || it.name == "encoded" }.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 null -> null
......
...@@ -16,7 +16,7 @@ import java.io.IOException ...@@ -16,7 +16,7 @@ import java.io.IOException
* *
* @author Him188moe * @author Him188moe
*/ */
@PacketId("08 25 31 0?") @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
......
...@@ -101,7 +101,7 @@ private fun DataOutputStream.writePart1(qq: Long, password: String, loginTime: I ...@@ -101,7 +101,7 @@ private fun DataOutputStream.writePart1(qq: Long, password: String, loginTime: I
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(false) 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)
......
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>mirai</artifactId>
<groupId>net.mamoe</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mirai-native</artifactId>
<version>1.0</version>
</project>
package net.mamoe.mirai.util;
/**
* @author Him188moe
*/
public final class TeaEncryption {
public static native int Decrypt();
}
...@@ -11,7 +11,6 @@ ...@@ -11,7 +11,6 @@
<modules> <modules>
<module>mirai-core</module> <module>mirai-core</module>
<module>mirai-native</module>
<module>mirai-ui</module> <module>mirai-ui</module>
<module>mirai-console</module> <module>mirai-console</module>
<module>mirai-api</module> <module>mirai-api</module>
......
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