Add some more decoding logic.

This commit is contained in:
Kitteh 2021-06-06 13:41:27 +01:00
parent 1b685b5dba
commit d7b5fcd568
4 changed files with 66 additions and 29 deletions

View file

@ -7,7 +7,9 @@ const read = @import("./qtshit/read.zig");
const write = @import("./qtshit/write.zig"); const write = @import("./qtshit/write.zig");
const QVariant = @import("./qtshit/types/QVariant.zig").QVariant; const QVariant = @import("./qtshit/types/QVariant.zig").QVariant;
const QDateTime = @import("./qtshit/types/QVariant.zig").QDateTime;
const UserType = @import("./qtshit/types/UserType.zig"); const UserType = @import("./qtshit/types/UserType.zig");
const RequestType = @import("./types/RequestType.zig").RequestType;
const prettyPrintQVariant = @import("./qtshit/utils/prettyPrintQVariant.zig").prettyPrintQVariant; const prettyPrintQVariant = @import("./qtshit/utils/prettyPrintQVariant.zig").prettyPrintQVariant;
const freeQVariant = @import("./qtshit/utils/free/freeQVariant.zig").freeQVariant; const freeQVariant = @import("./qtshit/utils/free/freeQVariant.zig").freeQVariant;
@ -108,6 +110,42 @@ pub const Client = struct {
} }
} }
pub fn send_message(s: *Client, bufferInfo: UserType.BufferInfo, message: []const u8) !void {
var data = std.ArrayList(u8).init(s.allocator);
defer data.deinit();
var listItems = std.ArrayList(QVariant).init(s.allocator);
defer listItems.deinit();
try listItems.append(.{ .Int = @enumToInt(RequestType.RpcCall) });
try listItems.append(.{ .QByteArray = "2sendInput(BufferInfo,QString)" });
try listItems.append(.{ .UserType = .{ .BufferInfo = bufferInfo } });
try listItems.append(.{ .String = message });
try write.writeQVariant(data.writer(), s.allocator, .{
.QVariantList = listItems.items,
});
try s.socketManager.writeFrame(data);
}
fn reply_to_heartbeat(s: *Client, timestamp: QDateTime) !void {
var data = std.ArrayList(u8).init(s.allocator);
defer data.deinit();
var listItems = std.ArrayList(QVariant).init(s.allocator);
defer listItems.deinit();
try listItems.append(.{ .Int = @enumToInt(RequestType.HeartBeatReply) });
try listItems.append(.{ .QDateTime = timestamp });
try write.writeQVariant(data.writer(), s.allocator, .{
.QVariantList = listItems.items,
});
try s.socketManager.writeFrame(data);
}
fn handle_session_init_packet(s: *Client, sessionState: std.StringHashMap(QVariant)) !void { fn handle_session_init_packet(s: *Client, sessionState: std.StringHashMap(QVariant)) !void {
for (sessionState.get("BufferInfos").?.QVariantList) |qvar| { for (sessionState.get("BufferInfos").?.QVariantList) |qvar| {
try s.bufferManager.addBufferInfo(qvar.UserType.BufferInfo); try s.bufferManager.addBufferInfo(qvar.UserType.BufferInfo);
@ -124,33 +162,23 @@ pub const Client = struct {
if (std.mem.eql(u8, msgType.String, "SessionInit")) { if (std.mem.eql(u8, msgType.String, "SessionInit")) {
var sessionState = map.get("SessionState").?.QVariantMap; var sessionState = map.get("SessionState").?.QVariantMap;
try s.handle_session_init_packet(sessionState); try s.handle_session_init_packet(sessionState);
return;
} }
} }
}, },
else => { .QVariantList => |list| {
std.debug.print("\n\n Unknown: Quassel Packet: \n", .{}); switch (@intToEnum(RequestType, list[0].Int)) {
prettyPrintQVariant(variant, 0); .HeartBeat => {
try s.reply_to_heartbeat(list[1].QDateTime);
return;
},
else => {},
}
}, },
else => {},
} }
} std.debug.print("\n\n Unknown: Quassel Packet: \n", .{});
prettyPrintQVariant(variant, 0);
pub fn send_message(s: *Client, bufferInfo: UserType.BufferInfo, message: []const u8) !void {
var data = std.ArrayList(u8).init(s.allocator);
defer data.deinit();
var listItems = std.ArrayList(QVariant).init(s.allocator);
defer listItems.deinit();
try listItems.append(.{ .Int = 2 });
try listItems.append(.{ .QByteArray = "2sendInput(BufferInfo,QString)" });
try listItems.append(.{ .UserType = .{ .BufferInfo = bufferInfo } });
try listItems.append(.{ .String = message });
try write.writeQVariant(data.writer(), s.allocator, .{
.QVariantList = listItems.items,
});
try s.socketManager.writeFrame(data);
} }
}; };

View file

@ -30,24 +30,23 @@ pub fn realMain(allocator: *std.mem.Allocator) !void {
try client.quassel_init_packet(); try client.quassel_init_packet();
try client.quassel_login(username, password); try client.quassel_login(username, password);
_ = try client.read_quassel_packet(); //var bufferInfo = try client.bufferManager.getFirstByName("z_is_stimky");
//try client.send_message(bufferInfo.?, "uwu, owo, uwu");
var bufferInfo = try client.bufferManager.getFirstByName("z_is_stimky");
try client.send_message(bufferInfo.?, "uwu, owo, uwu");
while (true) { while (true) {
std.time.sleep(100 * std.time.ns_per_ms);
client.read_quassel_packet() catch |err| { client.read_quassel_packet() catch |err| {
if (err == error.DecodeError) { if (err == error.DecodeError) {
std.debug.print("Decode Error.\n", .{}); std.debug.print("Decode Error.\n", .{});
} else if (err == error.EndOfStream) { } else if (err == error.EndOfStream) {
std.debug.print("EOS.\n", .{}); std.debug.print("EOS.\n", .{});
std.time.sleep(1000 * std.time.ns_per_ms); //std.time.sleep(1000 * std.time.ns_per_ms);
return; return;
} else { } else {
return err; return err;
} }
}; };
break;
} }
} }

View file

@ -157,7 +157,9 @@ pub fn prettyPrintQVariant(variant: QVariant, indentLevel: u64) void {
std.debug.print("\"{s}\" \n", .{str}); std.debug.print("\"{s}\" \n", .{str});
} }
}, },
.QByteArray => {}, .QByteArray => |value| {
std.debug.print("QByteArray: \"{s}\"\n", .{value});
},
.QDateTime => |value| { .QDateTime => |value| {
std.debug.print("QDateTime:\n", .{}); std.debug.print("QDateTime:\n", .{});
print_indent_level(indentLevel + 1); print_indent_level(indentLevel + 1);

View file

@ -0,0 +1,8 @@
pub const RequestType = enum(i32) {
SyncMessage = 0x00000001,
RpcCall = 0x00000002,
InitRequest = 0x00000003,
InitData = 0x00000004,
HeartBeat = 0x00000005,
HeartBeatReply = 0x00000006,
};