forked from evan-buss/openbooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathirc_events.go
115 lines (97 loc) · 3.78 KB
/
irc_events.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package server
import (
"fmt"
"os"
"path/filepath"
"github.com/evan-buss/openbooks/core"
)
func (server *server) NewIrcEventHandler(client *Client) core.EventHandler {
handler := core.EventHandler{}
handler[core.SearchResult] = client.searchResultHandler(server.config.DownloadDir)
handler[core.BookResult] = client.bookResultHandler(server.config.DownloadDir, server.config.DisableBrowserDownloads)
handler[core.NoResults] = client.noResultsHandler
handler[core.BadServer] = client.badServerHandler
handler[core.SearchAccepted] = client.searchAcceptedHandler
handler[core.MatchesFound] = client.matchesFoundHandler
handler[core.Ping] = client.pingHandler
handler[core.ServerList] = client.userListHandler(server.repository)
handler[core.Version] = client.versionHandler(server.config.UserAgent)
return handler
}
// searchResultHandler downloads from DCC server, parses data, and sends data to client
func (c *Client) searchResultHandler(downloadDir string) core.HandlerFunc {
return func(text string) {
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
}
bookResults, parseErrors, err := core.ParseSearchFile(extractedPath)
if err != nil {
c.log.Println(err)
c.send <- newErrorResponse("Error when parsing search results.")
return
}
if len(bookResults) == 0 && len(parseErrors) == 0 {
c.noResultsHandler(text)
return
}
// 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 {
c.log.Printf("Error deleting search results file: %v", err)
}
}
}
// bookResultHandler downloads the book file and sends it over the websocket
func (c *Client) bookResultHandler(downloadDir string, disableBrowserDownloads bool) core.HandlerFunc {
return func(text string) {
extractedPath, err := core.DownloadExtractDCCString(filepath.Join(downloadDir, "books"), text, nil)
if err != nil {
c.log.Println(err)
c.send <- newErrorResponse("Error when downloading book.")
return
}
c.log.Printf("Sending book entitled '%s'.\n", filepath.Base(extractedPath))
c.send <- newDownloadResponse(extractedPath, disableBrowserDownloads)
}
}
// NoResults is called when the server returns that nothing was found for the query
func (c *Client) noResultsHandler(_ string) {
c.send <- newErrorResponse("No results found for the query.")
}
// BadServer is called when the requested download fails because the server is not available
func (c *Client) badServerHandler(_ string) {
c.send <- newErrorResponse("Server is not available. Try another one.")
}
// SearchAccepted is called when the user's query is accepted into the search queue
func (c *Client) searchAcceptedHandler(_ string) {
c.send <- newStatusResponse(NOTIFY, "Search accepted into the queue.")
}
// MatchesFound is called when the server finds matches for the user's query
func (c *Client) matchesFoundHandler(num string) {
c.send <- newStatusResponse(NOTIFY, fmt.Sprintf("Found %s results for your query.", num))
}
func (c *Client) pingHandler(serverUrl string) {
c.irc.Pong(serverUrl)
}
func (c *Client) versionHandler(version string) core.HandlerFunc {
return func(line string) {
c.log.Printf("Sending CTCP version response: %s", line)
core.SendVersionInfo(c.irc, line, version)
}
}
func (c *Client) userListHandler(repo *Repository) core.HandlerFunc {
return func(text string) {
repo.servers = core.ParseServers(text)
}
}