diff options
author | Přemysl Janouch <p@janouch.name> | 2018-08-03 21:44:58 +0200 |
---|---|---|
committer | Přemysl Janouch <p@janouch.name> | 2018-08-03 21:45:53 +0200 |
commit | 4cd460886e0b3d72a754f7348d9489e52d0c4ebe (patch) | |
tree | 7cbe5795283f7b60a1c2ac3d5712523d567e37db /hid | |
parent | 4d8376fd3c6384bdb9fcfadcf513685eef057747 (diff) | |
download | haven-4cd460886e0b3d72a754f7348d9489e52d0c4ebe.tar.gz haven-4cd460886e0b3d72a754f7348d9489e52d0c4ebe.tar.xz haven-4cd460886e0b3d72a754f7348d9489e52d0c4ebe.zip |
hid: add support for customized replies
Diffstat (limited to 'hid')
-rw-r--r-- | hid/main.go | 46 |
1 files changed, 41 insertions, 5 deletions
diff --git a/hid/main.go b/hid/main.go index d2b44b7..0026d55 100644 --- a/hid/main.go +++ b/hid/main.go @@ -1081,8 +1081,10 @@ func (c *client) setPingTimer() { func (c *client) makeReply(id int, ap ...interface{}) string { s := fmt.Sprintf(":%s %03d %s ", serverName, id, c.nicknameOrStar()) - a := fmt.Sprintf(defaultReplies[id], ap...) - return s + a + if reply, ok := catalog[id]; ok { + return s + fmt.Sprintf(reply, ap...) + } + return s + fmt.Sprintf(defaultReplies[id], ap...) } // XXX: This way simple static analysis cannot typecheck the arguments, so we @@ -3150,9 +3152,43 @@ func ircInitializeTLS() error { } func ircInitializeCatalog() error { - // TODO: Not going to use catgets but a simple text file with basic - // checking whether the index is used by this daemon at all should do. - return nil + configCatalog := config["catalog"] + if configCatalog == "" { + return nil + } + + path := resolveFilename(configCatalog, resolveRelativeConfigFilename) + if path == "" { + return fmt.Errorf("cannot find file: %s", configCatalog) + } + + f, err := os.Open(path) + if err != nil { + return fmt.Errorf("failed reading the MOTD file: %s", err) + } + defer f.Close() + + scanner := bufio.NewScanner(f) + catalog = make(map[int]string) + for lineNo := 1; scanner.Scan(); lineNo++ { + line := strings.TrimLeft(scanner.Text(), " \t") + if line == "" || strings.HasPrefix(line, "#") { + continue + } + + delim := strings.IndexAny(line, " \t") + if delim < 0 { + return fmt.Errorf("%s:%d: malformed line", path, lineNo) + } + + id, err := strconv.ParseUint(line[:delim], 10, 16) + if err != nil { + return fmt.Errorf("%s:%d: %s", path, lineNo, err) + } + + catalog[int(id)] = line[delim+1:] + } + return scanner.Err() } func ircInitializeMOTD() error { |