15 type ClientPrefs struct {
16 Username string `yaml:"Username"`
17 IconID int `yaml:"IconID"`
18 Tracker string `yaml:"Tracker"`
19 EnableBell bool `yaml:"EnableBell"`
22 func (cp *ClientPrefs) IconBytes() []byte {
23 iconBytes := make([]byte, 2)
24 binary.BigEndian.PutUint16(iconBytes, uint16(cp.IconID))
32 Handlers map[[2]byte]ClientHandler
33 activeTasks map[[4]byte]*Transaction
37 type ClientHandler func(context.Context, *Client, *Transaction) ([]Transaction, error)
39 func (c *Client) HandleFunc(tranType [2]byte, handler ClientHandler) {
40 c.Handlers[tranType] = handler
43 func NewClient(username string, logger *slog.Logger) *Client {
46 activeTasks: make(map[[4]byte]*Transaction),
47 Handlers: make(map[[2]byte]ClientHandler),
48 Pref: &ClientPrefs{Username: username},
54 type ClientTransaction struct {
56 Handler func(*Client, *Transaction) ([]Transaction, error)
59 func (ch ClientTransaction) Handle(cc *Client, t *Transaction) ([]Transaction, error) {
60 return ch.Handler(cc, t)
63 type ClientTHandler interface {
64 Handle(*Client, *Transaction) ([]Transaction, error)
67 // JoinServer connects to a Hotline server and completes the login flow
68 func (c *Client) Connect(address, login, passwd string) (err error) {
69 // Establish TCP connection to server
70 c.Connection, err = net.DialTimeout("tcp", address, 5*time.Second)
75 // Send handshake sequence
76 if err := c.Handshake(); err != nil {
80 // Authenticate (send TranLogin 107)
84 TranLogin, [2]byte{0, 0},
85 NewField(FieldUserName, []byte(c.Pref.Username)),
86 NewField(FieldUserIconID, c.Pref.IconBytes()),
87 NewField(FieldUserLogin, EncodeString([]byte(login))),
88 NewField(FieldUserPassword, EncodeString([]byte(passwd))),
92 return fmt.Errorf("error sending login transaction: %w", err)
95 // start keepalive go routine
96 go func() { _ = c.keepalive() }()
101 const keepaliveInterval = 300 * time.Second
103 func (c *Client) keepalive() error {
105 time.Sleep(keepaliveInterval)
106 _ = c.Send(NewTransaction(TranKeepAlive, [2]byte{}))
110 var ClientHandshake = []byte{
111 0x54, 0x52, 0x54, 0x50, // TRTP
112 0x48, 0x4f, 0x54, 0x4c, // HOTL
117 var ServerHandshake = []byte{
118 0x54, 0x52, 0x54, 0x50, // TRTP
119 0x00, 0x00, 0x00, 0x00, // ErrorCode
122 func (c *Client) Handshake() error {
123 // Protocol Type 4 ‘TRTP’ 0x54 52 54 50
124 // Sub-protocol Type 4 User defined
125 // Version 2 1 Currently 1
126 // Sub-version 2 User defined
127 if _, err := c.Connection.Write(ClientHandshake); err != nil {
128 return fmt.Errorf("handshake write err: %s", err)
131 replyBuf := make([]byte, 8)
132 _, err := c.Connection.Read(replyBuf)
137 if bytes.Equal(replyBuf, ServerHandshake) {
141 // In the case of an error, client and server close the connection.
142 return fmt.Errorf("handshake response err: %s", err)
145 func (c *Client) Send(t Transaction) error {
146 requestNum := binary.BigEndian.Uint16(t.Type[:])
148 // if transaction is NOT reply, add it to the list to transactions we're expecting a response for
150 c.activeTasks[t.ID] = &t
153 n, err := io.Copy(c.Connection, &t)
155 return fmt.Errorf("error sending transaction: %w", err)
158 c.Logger.Debug("Sent Transaction",
159 "IsReply", t.IsReply,
166 func (c *Client) HandleTransaction(ctx context.Context, t *Transaction) error {
167 var origT Transaction
169 origT = *c.activeTasks[t.ID]
173 if handler, ok := c.Handlers[t.Type]; ok {
175 "Received transaction",
176 "IsReply", t.IsReply,
179 outT, err := handler(ctx, c, t)
181 c.Logger.Error("error handling transaction", "err", err)
183 for _, t := range outT {
184 if err := c.Send(t); err != nil {
193 func (c *Client) Disconnect() error {
194 return c.Connection.Close()
197 func (c *Client) HandleTransactions(ctx context.Context) error {
198 // Create a new scanner for parsing incoming bytes into transaction tokens
199 scanner := bufio.NewScanner(c.Connection)
200 scanner.Split(transactionScanner)
202 // Scan for new transactions and handle them as they come in.
204 // Make a new []byte slice and copy the scanner bytes to it. This is critical to avoid a data race as the
205 // scanner re-uses the buffer for subsequent scans.
206 buf := make([]byte, len(scanner.Bytes()))
207 copy(buf, scanner.Bytes())
210 _, err := t.Write(buf)
215 if err := c.HandleTransaction(ctx, &t); err != nil {
216 c.Logger.Error("Error handling transaction", "err", err)
220 if scanner.Err() == nil {