aboutsummaryrefslogtreecommitdiff
path: root/hotline/field.go
blob: aef790d171351e3c78ce8e362a01166e4a3beb74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package hotline

import (
	"encoding/binary"
	"github.com/jhalter/mobius/concat"
)

const fieldError = 100
const fieldData = 101
const fieldUserName = 102
const fieldUserID = 103
const fieldUserIconID = 104
const fieldUserLogin = 105
const fieldUserPassword = 106
const fieldRefNum = 107
const fieldTransferSize = 108
const fieldChatOptions = 109
const fieldUserAccess = 110

// const fieldUserAlias = 111 TODO: implement
const fieldUserFlags = 112
const fieldOptions = 113
const fieldChatID = 114
const fieldChatSubject = 115
const fieldWaitingCount = 116
const fieldBannerType = 152
const fieldVersion = 160
const fieldCommunityBannerID = 161
const fieldServerName = 162
const fieldFileNameWithInfo = 200
const fieldFileName = 201
const fieldFilePath = 202
const fieldFileResumeData = 203
const fieldFileTransferOptions = 204
const fieldFileTypeString = 205
const fieldFileCreatorString = 206
const fieldFileSize = 207
const fieldFileCreateDate = 208
const fieldFileModifyDate = 209
const fieldFileComment = 210
const fieldFileNewName = 211
const fieldFileNewPath = 212
const fieldFileType = 213
const fieldQuotingMsg = 214
const fieldAutomaticResponse = 215
const fieldFolderItemCount = 220
const fieldUsernameWithInfo = 300
const fieldNewsArtListData = 321
const fieldNewsCatName = 322
const fieldNewsCatListData15 = 323
const fieldNewsPath = 325
const fieldNewsArtID = 326
const fieldNewsArtDataFlav = 327
const fieldNewsArtTitle = 328
const fieldNewsArtPoster = 329
const fieldNewsArtDate = 330
const fieldNewsArtPrevArt = 331
const fieldNewsArtNextArt = 332
const fieldNewsArtData = 333
const fieldNewsArtFlags = 334
const fieldNewsArtParentArt = 335
const fieldNewsArt1stChildArt = 336
const fieldNewsArtRecurseDel = 337

type Field struct {
	ID        []byte // Type of field
	FieldSize []byte // Size of the data part
	Data      []byte // Actual field content
}

type requiredField struct {
	ID     int
	minLen int
	maxLen int
}

func NewField(id uint16, data []byte) Field {
	idBytes := make([]byte, 2)
	binary.BigEndian.PutUint16(idBytes, id)

	bs := make([]byte, 2)
	binary.BigEndian.PutUint16(bs, uint16(len(data)))

	return Field{
		ID:        idBytes,
		FieldSize: bs,
		Data:      data,
	}
}

func (f Field) Payload() []byte {
	return concat.Slices(f.ID, f.FieldSize, f.Data)
}

func getField(id int, fields *[]Field) *Field {
	for _, field := range *fields {
		if id == int(binary.BigEndian.Uint16(field.ID)) {
			return &field
		}
	}
	return nil
}