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 | fd1538251a7d3637a4613077c05991330a85b598 (patch) | |
tree | 1465e8a7d580a15dfdb9392e1d5097546a8ad8f9 /xS | |
parent | ffad1f15a58a6795fa978b97e928f099f44e9778 (diff) | |
download | xK-fd1538251a7d3637a4613077c05991330a85b598.tar.gz xK-fd1538251a7d3637a4613077c05991330a85b598.tar.xz xK-fd1538251a7d3637a4613077c05991330a85b598.zip |
hid: add support for customized replies
Diffstat (limited to 'xS')
-rw-r--r-- | xS/main.go | 46 |
1 files changed, 41 insertions, 5 deletions
@@ -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 { |