diff --git a/src/client.zig b/src/client.zig index ae41f14..7a7acba 100644 --- a/src/client.zig +++ b/src/client.zig @@ -7,7 +7,9 @@ const read = @import("./qtshit/read.zig"); const write = @import("./qtshit/write.zig"); const QVariant = @import("./qtshit/types/QVariant.zig").QVariant; +const QDateTime = @import("./qtshit/types/QVariant.zig").QDateTime; const UserType = @import("./qtshit/types/UserType.zig"); +const RequestType = @import("./types/RequestType.zig").RequestType; const prettyPrintQVariant = @import("./qtshit/utils/prettyPrintQVariant.zig").prettyPrintQVariant; 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 { for (sessionState.get("BufferInfos").?.QVariantList) |qvar| { try s.bufferManager.addBufferInfo(qvar.UserType.BufferInfo); @@ -124,33 +162,23 @@ pub const Client = struct { if (std.mem.eql(u8, msgType.String, "SessionInit")) { var sessionState = map.get("SessionState").?.QVariantMap; try s.handle_session_init_packet(sessionState); + return; } } }, - else => { - std.debug.print("\n\n Unknown: Quassel Packet: \n", .{}); - prettyPrintQVariant(variant, 0); + .QVariantList => |list| { + switch (@intToEnum(RequestType, list[0].Int)) { + .HeartBeat => { + try s.reply_to_heartbeat(list[1].QDateTime); + return; + }, + else => {}, + } }, + else => {}, } - } - - 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); + std.debug.print("\n\n Unknown: Quassel Packet: \n", .{}); + prettyPrintQVariant(variant, 0); } }; diff --git a/src/main.zig b/src/main.zig index 67a517a..b3efc47 100644 --- a/src/main.zig +++ b/src/main.zig @@ -30,24 +30,23 @@ pub fn realMain(allocator: *std.mem.Allocator) !void { try client.quassel_init_packet(); 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) { + std.time.sleep(100 * std.time.ns_per_ms); + client.read_quassel_packet() catch |err| { if (err == error.DecodeError) { std.debug.print("Decode Error.\n", .{}); } else if (err == error.EndOfStream) { std.debug.print("EOS.\n", .{}); - std.time.sleep(1000 * std.time.ns_per_ms); + //std.time.sleep(1000 * std.time.ns_per_ms); return; } else { return err; } }; - break; } } diff --git a/src/qtshit/utils/prettyPrintQVariant.zig b/src/qtshit/utils/prettyPrintQVariant.zig index 077f845..3d1d17b 100644 --- a/src/qtshit/utils/prettyPrintQVariant.zig +++ b/src/qtshit/utils/prettyPrintQVariant.zig @@ -157,7 +157,9 @@ pub fn prettyPrintQVariant(variant: QVariant, indentLevel: u64) void { std.debug.print("\"{s}\" \n", .{str}); } }, - .QByteArray => {}, + .QByteArray => |value| { + std.debug.print("QByteArray: \"{s}\"\n", .{value}); + }, .QDateTime => |value| { std.debug.print("QDateTime:\n", .{}); print_indent_level(indentLevel + 1); diff --git a/src/types/RequestType.zig b/src/types/RequestType.zig new file mode 100644 index 0000000..8147f25 --- /dev/null +++ b/src/types/RequestType.zig @@ -0,0 +1,8 @@ +pub const RequestType = enum(i32) { + SyncMessage = 0x00000001, + RpcCall = 0x00000002, + InitRequest = 0x00000003, + InitData = 0x00000004, + HeartBeat = 0x00000005, + HeartBeatReply = 0x00000006, +};