]> git.lizzy.rs Git - go-anidb.git/commitdiff
anidb: Don't send UDP queries if we have no credentials
authorDiogo Franco (Kovensky) <diogomfranco@gmail.com>
Sun, 14 Jul 2013 23:19:51 +0000 (20:19 -0300)
committerDiogo Franco (Kovensky) <diogomfranco@gmail.com>
Sun, 14 Jul 2013 23:22:56 +0000 (20:22 -0300)
We'd just get a 501 error and wouldn't be able to do anything about it.
Skip the middleman and return the 501 right away.

udp.go

diff --git a/udp.go b/udp.go
index abd8a3aa1856e7c4168f3f88ec7f7df044555805..df320041a6dd208bfd1a6df110ca7531adf61579 100644 (file)
--- a/udp.go
+++ b/udp.go
@@ -63,6 +63,22 @@ func newUDPWrap() *udpWrap {
 
 type paramMap udpapi.ParamMap // shortcut
 
+type noauthAPIReply struct {
+       udpapi.APIReply
+}
+
+func (r *noauthAPIReply) Code() int {
+       return 501
+}
+
+func (r *noauthAPIReply) Text() string {
+       return "LOGIN FIRST"
+}
+
+func (r *noauthAPIReply) Error() error {
+       return &udpapi.APIError{Code: r.Code(), Desc: r.Text()}
+}
+
 type bannedAPIReply struct {
        udpapi.APIReply
 }
@@ -74,7 +90,7 @@ func (r *bannedAPIReply) Text() string {
        return "BANNED"
 }
 func (r *bannedAPIReply) Error() error {
-       return &udpapi.APIError{Code: 555, Desc: "BANNED"}
+       return &udpapi.APIError{Code: r.Code(), Desc: r.Text()}
 }
 
 var bannedReply udpapi.APIReply = &bannedAPIReply{}
@@ -108,6 +124,11 @@ func (udp *udpWrap) sendQueue() {
 
 func (udp *udpWrap) SendRecv(cmd string, params paramMap) <-chan udpapi.APIReply {
        ch := make(chan udpapi.APIReply, 1)
+       if udp.credentials == nil {
+               ch <- &noauthAPIReply{}
+               close(ch)
+               return ch
+       }
 
        if Banned() {
                ch <- bannedReply