commit c146d4cc5ccc2e7ba646fd75f9c80d89c00ba3aa
parent a9ab4724a5b2c5ecafdb0a805f51edae5ef4f822
Author: sin <sin@2f30.org>
Date: Wed, 8 May 2013 13:29:45 +0100
fix irc parsing
Diffstat:
2 files changed, 43 insertions(+), 9 deletions(-)
diff --git a/src/irc/irc.go b/src/irc/irc.go
@@ -3,6 +3,7 @@ package irc
import (
"crypto/tls"
"fmt"
+ "log"
"net"
"strings"
)
@@ -166,7 +167,7 @@ func (i *IrcContext) Connect() error {
// Fire off the goroutines now!
go i.incomingMsgLoop()
go i.outgoingMsgLoop()
- go i.RxRawMessages()
+ go i.rxRawMessages()
return nil
}
@@ -189,7 +190,7 @@ func (i *IrcContext) read(buf []byte) (int, error) {
// This is the actual raw read loop. Here we parse the incoming bytes
// and form messages.
-func (i *IrcContext) RxRawMessages() error {
+func (i *IrcContext) rxRawMessages() error {
for {
var buf [512]byte
n, err := i.read(buf[0:])
@@ -200,10 +201,14 @@ func (i *IrcContext) RxRawMessages() error {
fmt.Println(string(buf[0 : n-1]))
s := string(buf[0:n])
- msg := i.ParseRawMessage(s)
- i.incomingMsg <- msg
- for _, v := range i.intercepts {
- *v <- msg
+ msg, err := i.ParseRawMessage(s)
+ if err != nil {
+ log.Printf("%s", err)
+ } else {
+ i.incomingMsg <- *msg
+ for _, v := range i.intercepts {
+ *v <- *msg
+ }
}
}
}
diff --git a/src/irc/message.go b/src/irc/message.go
@@ -1,6 +1,10 @@
package irc
-import "strings"
+import (
+ "fmt"
+ "strings"
+ "unicode"
+)
type IrcMessage struct {
Command string
@@ -113,7 +117,22 @@ func (i *IrcContext) Topic(channel string, text string) {
i.outgoingMsg <- msg
}
-func (i *IrcContext) ParseRawMessage(raw string) IrcMessage {
+func validNick(n string) bool {
+ for i, _ := range n {
+ c := n[i]
+ if !unicode.IsLetter(rune(c)) &&
+ !unicode.IsDigit(rune(c)) {
+ switch c {
+ case '[', ']', '\\', '`', '_', '^', '{', '|', '}', '-':
+ default:
+ return false
+ }
+ }
+ }
+ return true
+}
+
+func (i *IrcContext) ParseRawMessage(raw string) (*IrcMessage, error) {
var msg IrcMessage
msg.Raw = raw
raw = strings.TrimSpace(raw)
@@ -121,30 +140,40 @@ func (i *IrcContext) ParseRawMessage(raw string) IrcMessage {
if i := strings.Index(raw, " "); i != -1 {
msg.Source = raw[1:i]
raw = raw[1:]
+ } else {
+ return nil, fmt.Errorf("Could not parse source, ignoring...")
}
if i := strings.Index(raw, "!"); i != -1 {
msg.Nick = raw[:i]
raw = raw[i+1:]
+ } else {
+ return nil, fmt.Errorf("Could not parse nick, ignoring...")
}
if i := strings.Index(raw, "@"); i != -1 {
msg.User = raw[:i]
raw = raw[i+1:]
+ } else {
+ return nil, fmt.Errorf("Could not parse user, ignoring...")
}
if i := strings.Index(raw, " "); i != -1 {
msg.Host = raw[:i]
raw = raw[i+1:]
+ } else {
+ return nil, fmt.Errorf("Could not parse host, ignoring...")
}
}
if i := strings.Index(raw, " "); i != -1 {
msg.Command = raw[:i]
raw = raw[i+1:]
+ } else {
+ return nil, fmt.Errorf("Could not parse command, ignoring...")
}
if raw[0] == ':' {
msg.Args = []string{raw[1:]}
} else {
msg.Args = strings.SplitN(raw, " :", 2)
}
- return msg
+ return &msg, nil
}
// Unpack a message into a byte array