aboutsummaryrefslogtreecommitdiff
path: root/field.go
blob: 161a9b29007b14199e3905f763782071abd27779 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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
const fieldUserFlags = 112
const fieldOptions = 113
const fieldChatID = 114
const fieldChatSubject = 115
const fieldWaitingCount = 116
const fieldVersion = 160
const fieldCommunityBannerID = 161
const fieldServerName = 162
const fieldFileNameWithInfo = 200
const fieldFileName = 201
const fieldFilePath = 202
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 // Defined but unused in the Hotline Protocol spec
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)
}

type FileNameWithInfo struct {
	Type       string // file type code
	Creator    []byte // File creator code
	FileSize   uint32 // File Size in bytes
	NameScript []byte // TODO: What is this?
	NameSize   []byte // Length of name field
	Name       string // File name
}

func (f FileNameWithInfo) Payload() []byte {
	name := []byte(f.Name)
	nameSize := make([]byte, 2)
	binary.BigEndian.PutUint16(nameSize, uint16(len(name)))

	kb := f.FileSize

	fSize := make([]byte, 4)
	binary.BigEndian.PutUint32(fSize, kb)

	return concat.Slices(
		[]byte(f.Type),
		f.Creator,
		fSize,
		[]byte{0, 0, 0, 0},
		f.NameScript,
		nameSize,
		[]byte(f.Name),
	)

}