libnmdc/libnmdc.go

69 lines
2.0 KiB
Go
Raw Normal View History

2016-02-12 04:03:42 +00:00
package libnmdc
import (
"errors"
2016-02-12 04:03:42 +00:00
"fmt"
"regexp"
"strings"
"time"
)
2016-04-03 06:53:14 +00:00
const (
DEFAULT_CLIENT_TAG string = "libnmdc.go"
2017-11-14 05:45:36 +00:00
DEFAULT_CLIENT_VERSION string = "0.15"
DEFAULT_HUB_NAME string = "(unknown)"
SEND_KEEPALIVE_EVERY time.Duration = 29 * time.Second
AUTO_RECONNECT_AFTER time.Duration = 30 * time.Second
RECONNECT_IF_NO_DATA_RECIEVED_IN time.Duration = 24 * time.Hour // we expect keepalives wayyyy more frequently than this
2016-04-03 06:53:14 +00:00
)
2016-02-12 04:03:42 +00:00
var rx_protocolMessage *regexp.Regexp
var rx_publicChat *regexp.Regexp
var rx_incomingTo *regexp.Regexp
2016-10-08 02:19:21 +00:00
var rx_userCommand *regexp.Regexp
var ErrNotConnected error = errors.New("Not connected")
2016-02-12 04:03:42 +00:00
func init() {
// With the `m` flag, use \A instead of ^ to anchor to start
// This fixes accidentally finding a better match in the middle of a multi-line message
rx_protocolMessage = regexp.MustCompile(`(?ms)\A[^|]*\|`)
rx_publicChat = regexp.MustCompile(`(?ms)\A<([^>]*)> (.*)$`)
rx_incomingTo = regexp.MustCompile(`(?ms)\A([^ ]+) From: ([^ ]+) \$<([^>]*)> (.*)`)
rx_userCommand = regexp.MustCompile(`(?ms)\A(\d+) (\d+)\s?([^\$]*)\$?(.*)`)
2016-02-12 04:03:42 +00:00
}
func Unescape(encoded string) string {
2016-02-12 04:03:42 +00:00
v1 := strings.Replace(encoded, "&#36;", "$", -1)
v2 := strings.Replace(v1, "&#124;", "|", -1)
return strings.Replace(v2, "&amp;", "&", -1)
}
func Escape(plaintext string) string {
2016-02-12 04:03:42 +00:00
v1 := strings.Replace(plaintext, "&", "&amp;", -1)
v2 := strings.Replace(v1, "|", "&#124;", -1)
return strings.Replace(v2, "$", "&#36;", -1)
}
func unlock(lock []byte) string {
2016-02-12 04:03:42 +00:00
nibble_swap := func(b byte) byte {
return ((b << 4) & 0xF0) | ((b >> 4) & 0x0F)
}
chr := func(b byte) string {
if b == 0 || b == 5 || b == 36 || b == 96 || b == 124 || b == 126 {
return fmt.Sprintf("/%%DCN%04d%%/", b)
} else {
return string(b)
}
}
key := chr(nibble_swap(lock[0] ^ lock[len(lock)-2] ^ lock[len(lock)-3] ^ 5))
for i := 1; i < len(lock); i += 1 {
key += chr(nibble_swap(lock[i] ^ lock[i-1]))
}
return key
}