libnmdc/libnmdc.go

64 lines
1.6 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 (
2016-04-04 07:01:47 +00:00
DEFAULT_CLIENT_TAG string = "libnmdc.go"
DEFAULT_HUB_NAME string = "(unknown)"
SEND_KEEPALIVE_EVERY time.Duration = 29 * time.Second
AUTO_RECONNECT_AFTER time.Duration = 30 * time.Second
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() {
rx_protocolMessage = regexp.MustCompile("(?ms)^[^|]*\\|")
2016-02-12 04:03:42 +00:00
rx_publicChat = regexp.MustCompile("(?ms)^<([^>]*)> (.*)$")
2016-04-02 01:01:58 +00:00
rx_incomingTo = regexp.MustCompile("(?ms)^([^ ]+) From: ([^ ]+) \\$<([^>]*)> (.*)")
2016-10-08 02:19:21 +00:00
rx_userCommand = regexp.MustCompile(`(?ms)^(\d+) (\d+)\s?([^\$]*)\$?(.*)`)
2016-02-12 04:03:42 +00:00
}
func NMDCUnescape(encoded string) string {
v1 := strings.Replace(encoded, "&#36;", "$", -1)
v2 := strings.Replace(v1, "&#124;", "|", -1)
return strings.Replace(v2, "&amp;", "&", -1)
}
func NMDCEscape(plaintext string) string {
v1 := strings.Replace(plaintext, "&", "&amp;", -1)
v2 := strings.Replace(v1, "|", "&#124;", -1)
return strings.Replace(v2, "$", "&#36;", -1)
}
2016-05-04 07:03:36 +00:00
func NMDCUnlock(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
}