23 TranSendInstantMsg = 108
24 TranShowAgreement = 109
25 TranDisconnectUser = 110
26 TranDisconnectMsg = 111 // TODO: implement server initiated friendly disconnect
27 TranInviteNewChat = 112
28 TranInviteToChat = 113
29 TranRejectChatInvite = 114
32 TranNotifyChatChangeUser = 117
33 TranNotifyChatDeleteUser = 118
34 TranNotifyChatSubject = 119
35 TranSetChatSubject = 120
37 TranServerBanner = 122
38 TranGetFileNameList = 200
39 TranDownloadFile = 202
46 TranMakeFileAlias = 209
47 TranDownloadFldr = 210
48 TranDownloadInfo = 211 // TODO: implement file transfer queue
49 TranDownloadBanner = 212
51 TranGetUserNameList = 300
52 TranNotifyChangeUser = 301
53 TranNotifyDeleteUser = 302
54 TranGetClientInfoText = 303
55 TranSetClientUserInfo = 304
63 TranUserBroadcast = 355
64 TranGetNewsCatNameList = 370
65 TranGetNewsArtNameList = 371
69 TranGetNewsArtData = 400
75 type Transaction struct {
76 Flags byte // Reserved (should be 0)
77 IsReply byte // Request (0) or reply (1)
78 Type []byte // Requested operation (user defined)
79 ID []byte // Unique transaction ID (must be != 0)
80 ErrorCode []byte // Used in the reply (user defined, 0 = no error)
81 TotalSize []byte // Total data size for the transaction (all parts)
82 DataSize []byte // Size of data in this transaction part. This allows splitting large transactions into smaller parts.
83 ParamCount []byte // Number of the parameters for this transaction
86 clientID *[]byte // Internal identifier for target client
87 readOffset int // Internal offset to track read progress
90 func NewTransaction(t int, clientID *[]byte, fields ...Field) *Transaction {
91 typeSlice := make([]byte, 2)
92 binary.BigEndian.PutUint16(typeSlice, uint16(t))
94 idSlice := make([]byte, 4)
95 binary.BigEndian.PutUint32(idSlice, rand.Uint32())
103 ErrorCode: []byte{0, 0, 0, 0},
108 // Write implements io.Writer interface for Transaction
109 func (t *Transaction) Write(p []byte) (n int, err error) {
110 totalSize := binary.BigEndian.Uint32(p[12:16])
112 // the buf may include extra bytes that are not part of the transaction
113 // tranLen represents the length of bytes that are part of the transaction
114 tranLen := int(20 + totalSize)
116 if tranLen > len(p) {
117 return n, errors.New("buflen too small for tranLen")
120 // Create a new scanner for parsing incoming bytes into transaction tokens
121 scanner := bufio.NewScanner(bytes.NewReader(p[22:tranLen]))
122 scanner.Split(fieldScanner)
124 for i := 0; i < int(binary.BigEndian.Uint16(p[20:22])); i++ {
128 if _, err := field.Write(scanner.Bytes()); err != nil {
129 return 0, fmt.Errorf("error reading field: %w", err)
131 t.Fields = append(t.Fields, field)
138 t.ErrorCode = p[8:12]
139 t.TotalSize = p[12:16]
140 t.DataSize = p[16:20]
141 t.ParamCount = p[20:22]
146 const tranHeaderLen = 20 // fixed length of transaction fields before the variable length fields
148 // transactionScanner implements bufio.SplitFunc for parsing incoming byte slices into complete tokens
149 func transactionScanner(data []byte, _ bool) (advance int, token []byte, err error) {
150 // The bytes that contain the size of a transaction are from 12:16, so we need at least 16 bytes
155 totalSize := binary.BigEndian.Uint32(data[12:16])
157 // tranLen represents the length of bytes that are part of the transaction
158 tranLen := int(tranHeaderLen + totalSize)
159 if tranLen > len(data) {
163 return tranLen, data[0:tranLen], nil
166 const minFieldLen = 4
168 func ReadFields(paramCount []byte, buf []byte) ([]Field, error) {
169 paramCountInt := int(binary.BigEndian.Uint16(paramCount))
170 if paramCountInt > 0 && len(buf) < minFieldLen {
171 return []Field{}, fmt.Errorf("invalid field length %v", len(buf))
174 // A Field consists of:
177 // Data: FieldSize number of bytes
179 for i := 0; i < paramCountInt; i++ {
180 if len(buf) < minFieldLen {
181 return []Field{}, fmt.Errorf("invalid field length %v", len(buf))
184 fieldSize := buf[2:4]
185 fieldSizeInt := int(binary.BigEndian.Uint16(buf[2:4]))
186 expectedLen := minFieldLen + fieldSizeInt
187 if len(buf) < expectedLen {
188 return []Field{}, fmt.Errorf("field length too short")
191 fields = append(fields, Field{
192 ID: [2]byte(fieldID),
193 FieldSize: [2]byte(fieldSize),
194 Data: buf[4 : 4+fieldSizeInt],
197 buf = buf[fieldSizeInt+4:]
201 return []Field{}, fmt.Errorf("extra field bytes")
207 // Read implements the io.Reader interface for Transaction
208 func (t *Transaction) Read(p []byte) (int, error) {
209 payloadSize := t.Size()
211 fieldCount := make([]byte, 2)
212 binary.BigEndian.PutUint16(fieldCount, uint16(len(t.Fields)))
214 bbuf := new(bytes.Buffer)
216 for _, field := range t.Fields {
218 _, err := bbuf.ReadFrom(&f)
220 return 0, fmt.Errorf("error reading field: %w", err)
224 buf := slices.Concat(
225 []byte{t.Flags, t.IsReply},
230 payloadSize, // this is the dataSize field, but seeming the same as totalSize
235 if t.readOffset >= len(buf) {
236 return 0, io.EOF // All bytes have been read
239 n := copy(p, buf[t.readOffset:])
245 // Size returns the total size of the transaction payload
246 func (t *Transaction) Size() []byte {
247 bs := make([]byte, 4)
250 for _, field := range t.Fields {
251 fieldSize += len(field.Data) + 4
254 binary.BigEndian.PutUint32(bs, uint32(fieldSize+2))
259 func (t *Transaction) GetField(id int) Field {
260 for _, field := range t.Fields {
261 if id == int(binary.BigEndian.Uint16(field.ID[:])) {
269 func (t *Transaction) IsError() bool {
270 return bytes.Equal(t.ErrorCode, []byte{0, 0, 0, 1})