From 4cd460886e0b3d72a754f7348d9489e52d0c4ebe Mon Sep 17 00:00:00 2001 From: Přemysl Janouch
Date: Fri, 3 Aug 2018 21:44:58 +0200 Subject: hid: add support for customized replies --- hid/main.go | 46 +++++++++++++++++++++++++++++++++++++++++----- 1 file 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 { -- cgit v1.2.3-70-g09d2