2016-02-12 04:03:42 +00:00
|
|
|
// libnmdc project libnmdc.go
|
|
|
|
package libnmdc
|
|
|
|
|
|
|
|
import (
|
2016-04-03 01:15:57 +00:00
|
|
|
"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
|
2016-05-04 07:15:02 +00:00
|
|
|
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-04-03 01:15:57 +00:00
|
|
|
var ErrNotConnected error = errors.New("Not connected")
|
2016-02-12 04:03:42 +00:00
|
|
|
|
|
|
|
func init() {
|
2016-04-03 07:20:58 +00:00
|
|
|
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-02-12 04:03:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NMDCUnescape(encoded string) string {
|
|
|
|
v1 := strings.Replace(encoded, "$", "$", -1)
|
|
|
|
v2 := strings.Replace(v1, "|", "|", -1)
|
|
|
|
return strings.Replace(v2, "&", "&", -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NMDCEscape(plaintext string) string {
|
|
|
|
v1 := strings.Replace(plaintext, "&", "&", -1)
|
|
|
|
v2 := strings.Replace(v1, "|", "|", -1)
|
|
|
|
return strings.Replace(v2, "$", "$", -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
|
|
|
|
}
|