diff --git a/cli/util.go b/cli/util.go index a20d66e..9c247d7 100644 --- a/cli/util.go +++ b/cli/util.go @@ -38,7 +38,10 @@ func instantiate(config *Config) { fmt.Printf("Connecting to %s.", config.Server) conn := irc.New(config.UserName, config.Version) config.irc = conn - core.Join(conn, config.Server) + err := core.Join(conn, config.Server) + if err != nil { + log.Fatal(err) + } fmt.Printf("%sConnected to %s.\n", clearLine, config.Server) } diff --git a/core/irchighway.go b/core/irchighway.go index 2691f21..907e197 100644 --- a/core/irchighway.go +++ b/core/irchighway.go @@ -11,12 +11,16 @@ import ( // Specific irc.irchighway.net commands // Join connects to the irc.irchighway.net server and joins the #ebooks channel -func Join(irc *irc.Conn, url string) { - irc.Connect(url) +func Join(irc *irc.Conn, url string) error { + err := irc.Connect(url) + if err != nil { + return err + } // Wait before joining the ebooks room // Often you recieve a private message from the server time.Sleep(time.Second * 2) irc.JoinChannel("ebooks") + return nil } // SearchBook sends a search query to the search bot diff --git a/core/search_parser.go b/core/search_parser.go index d48623a..017a55b 100644 --- a/core/search_parser.go +++ b/core/search_parser.go @@ -6,7 +6,6 @@ import ( "errors" "fmt" "io" - "log" "os" "sort" "strings" @@ -64,14 +63,15 @@ func (p ParseError) String() string { } // ParseSearchFile converts a single search file into an array of BookDetail -func ParseSearchFile(filePath string) ([]BookDetail, []ParseError) { +func ParseSearchFile(filePath string) ([]BookDetail, []ParseError, error) { file, err := os.Open(filePath) if err != nil { - log.Fatal(err) + return nil, nil, err } defer file.Close() - return ParseSearchV2(file) + books, errs := ParseSearchV2(file) + return books, errs, nil } func ParseSearch(reader io.Reader) ([]BookDetail, []ParseError) { diff --git a/dcc/dcc.go b/dcc/dcc.go index 209954c..1576b3d 100644 --- a/dcc/dcc.go +++ b/dcc/dcc.go @@ -4,7 +4,6 @@ import ( "encoding/binary" "errors" "io" - "log" "net" "regexp" "strconv" @@ -76,13 +75,13 @@ func (download Download) Download(writer io.Writer) error { bytes := make([]byte, 4096) for int64(received) < download.Size { n, err := conn.Read(bytes) - if err != nil { - log.Fatal("Error Downloading Data", err) + return err } + _, err = writer.Write(bytes[:n]) if err != nil { - log.Println(err) + return err } received += n } diff --git a/irc/irc.go b/irc/irc.go index 713c7ae..e177cef 100644 --- a/irc/irc.go +++ b/irc/irc.go @@ -1,7 +1,6 @@ package irc import ( - "log" "net" ) @@ -25,11 +24,10 @@ func New(username, realname string) *Conn { } // Connect connects to the given server at port 6667 -func (i *Conn) Connect(address string) { +func (i *Conn) Connect(address string) error { conn, err := net.Dial("tcp", address+":6667") - if err != nil { - log.Fatal("IRC Connection Error ", err) + return err } i.Conn = conn @@ -39,40 +37,56 @@ func (i *Conn) Connect(address string) { i.Write([]byte(user)) i.Write([]byte(nick)) + return nil } // Disconnect closes connection to the IRC server func (i *Conn) Disconnect() { + if !i.IsConnected() { + return + } i.Write([]byte("QUIT :Goodbye\r\n")) i.Conn.Close() } // SendMessage sends the given message string to the connected IRC server func (i *Conn) SendMessage(message string) { + if !i.IsConnected() { + return + } i.Write([]byte("PRIVMSG #" + i.channel + " :" + message + "\r\n")) } // SendNotice sends a notice message to the specified user func (i *Conn) SendNotice(user string, message string) { + if !i.IsConnected() { + return + } i.Write([]byte("NOTICE " + user + " :" + message + "\r\n")) } // JoinChannel joins the channel given by channel string func (i *Conn) JoinChannel(channel string) { - i.channel = channel - _, err := i.Write([]byte("JOIN #" + channel + "\r\n")) - if err != nil { - log.Fatal(err) + if !i.IsConnected() { + return } + i.channel = channel + i.Write([]byte("JOIN #" + channel + "\r\n")) } // GetUsers sends a NAMES request to the IRC server func (i *Conn) GetUsers(channel string) { + if !i.IsConnected() { + return + } i.Write([]byte("NAMES #" + channel + "\r\n")) } // Pong sends a Pong message to the server, often used after a PING request func (i *Conn) Pong(server string) { + if !i.IsConnected() { + return + } i.Write([]byte("PONG " + server + "\r\n")) } diff --git a/server/irc_events.go b/server/irc_events.go index a693468..88f94cc 100644 --- a/server/irc_events.go +++ b/server/irc_events.go @@ -28,24 +28,32 @@ func (c *Client) searchResultHandler(downloadDir string) core.HandlerFunc { extractedPath, err := core.DownloadExtractDCCString(filepath.Join(downloadDir, "books"), text, nil) if err != nil { c.log.Println(err) + c.send <- newErrorResponse("Error when downloading search results.") + return } - results, errors := core.ParseSearchFile(extractedPath) - // Output all errors so parser can be improved over time - if len(errors) > 0 { - c.log.Printf("%d Search Result Parsing Errors\n", len(errors)) - for _, err := range errors { - c.log.Println(err) - } + bookResults, parseErrors, err := core.ParseSearchFile(extractedPath) + if err != nil { + c.log.Println(err) + c.send <- newErrorResponse("Error when parsing search results.") + return } - if len(results) == 0 && len(errors) == 0 { + if len(bookResults) == 0 && len(parseErrors) == 0 { c.noResultsHandler(text) return } - c.log.Printf("Sending %d search results.\n", len(results)) - c.send <- newSearchResponse(results, errors) + // Output all errors so parser can be improved over time + if len(parseErrors) > 0 { + c.log.Printf("%d Search Result Parsing Errors\n", len(parseErrors)) + for _, err := range parseErrors { + c.log.Println(err) + } + } + + c.log.Printf("Sending %d search results.\n", len(bookResults)) + c.send <- newSearchResponse(bookResults, parseErrors) err = os.Remove(extractedPath) if err != nil { @@ -60,8 +68,7 @@ func (c *Client) bookResultHandler(downloadDir string) core.HandlerFunc { extractedPath, err := core.DownloadExtractDCCString(filepath.Join(downloadDir, "books"), text, nil) if err != nil { c.log.Println(err) - - c.send <- newErrorResponse(err.Error()) + c.send <- newErrorResponse("Error when downloading book.") return } diff --git a/server/routes.go b/server/routes.go index dcd1f00..085469d 100644 --- a/server/routes.go +++ b/server/routes.go @@ -95,7 +95,7 @@ func (server *server) staticFilesHandler(assetPath string) http.Handler { // update the embedded file system's tree so that index.html is at the root app, err := fs.Sub(reactClient, assetPath) if err != nil { - server.log.Fatal(err) + server.log.Println(err) } // strip the predefined base path and serve the static file diff --git a/server/websocket_requests.go b/server/websocket_requests.go index d6d50e3..badd28f 100644 --- a/server/websocket_requests.go +++ b/server/websocket_requests.go @@ -49,7 +49,13 @@ func (server *server) routeMessage(message Request, c *Client) { // handle ConnectionRequests and either connect to the server or do nothing func (c *Client) startIrcConnection(server *server) { - core.Join(c.irc, server.config.Server) + err := core.Join(c.irc, server.config.Server) + if err != nil { + c.log.Println(err) + c.send <- newErrorResponse("Unable to connect to IRC server.") + return + } + handler := server.NewIrcEventHandler(c) if server.config.Log {