diff options
Diffstat (limited to 'client')
| -rw-r--r-- | client/application.go | 9 | ||||
| -rw-r--r-- | client/command.go | 12 | ||||
| -rw-r--r-- | client/config.go | 91 |
3 files changed, 112 insertions, 0 deletions
diff --git a/client/application.go b/client/application.go index e1d0956..7e46199 100644 --- a/client/application.go +++ b/client/application.go @@ -34,6 +34,15 @@ func newApplication(serverAddress string, insecure bool) *application { app.cache = object.NewCache(&app) app.windowCache = window.NewCache() app.goTo(app.cmdWindow.Location()) + + app.cmdWindow.info( + "configuration file: %s/talk.conf", getConfigDir(), + ) + err := app.executeConfigOptional("talk.conf") + if err != nil { + app.cmdWindow.err(err.Error()) + } + app.cmdWindow.info("connecting to %s", app.Client.Address) app.redraw = true diff --git a/client/command.go b/client/command.go index ea3acb6..a96839c 100644 --- a/client/command.go +++ b/client/command.go @@ -94,6 +94,18 @@ func (a *application) doCommand(command string, args []string, text string) { a.cmdWindow.Buffer().Add(hm) } return + case "echo": + a.cmdWindow.info("[echo] %s", text) + return + case "include": + err := a.executeConfig(text) + if err != nil { + err = a.executeConfig(text + ".conf") + } + if err != nil { + a.cmdWindow.err(err.Error()) + } + return } if !a.connected { diff --git a/client/config.go b/client/config.go new file mode 100644 index 0000000..ad0eedb --- /dev/null +++ b/client/config.go @@ -0,0 +1,91 @@ +package main + +import ( + "path/filepath" + "strings" + "bufio" + "fmt" + "io" + "os" +) + +func getBaseConfigDir() (path string) { + path = os.Getenv("XDG_CONFIG_HOME") + if path != "" { + return + } + path = os.Getenv("APPDATA") + if path != "" { + return + } + if os.Getenv("HOME") != "" { + return filepath.Join(os.Getenv("HOME"), ".config") + } else { + return filepath.Join(filepath.Base(os.Args[0]), "config") + } +} + +func getConfigDir() string { + return filepath.Join(getBaseConfigDir(), "talk") +} + +func (a *application) executeScript(rd io.Reader) error { + var ( + command strings.Builder + br = bufio.NewReader(rd) + ) + for { + command.Reset() + command.WriteRune('/') + for { + c, _, err := br.ReadRune() + if err == io.EOF { + break + } + if err != nil { + return fmt.Errorf("error reading configuration: %s", err) + } + if c == '/' && command.Len() == 1 { + continue + } + if c == '\\' { + command.WriteRune(c) + c, _, _ = br.ReadRune() + } else if c == '#' { + for c != '\n' { + c, _, err = br.ReadRune() + if err != nil { + break + } + } + } else if c == '\n' { + break + } + command.WriteRune(c) + } + if strings.TrimSpace(command.String()) != "" { + a.processCommand(command.String()) + } + _, err := br.Peek(1) + if err == io.EOF { + break + } + } + return nil +} + +func (a *application) executeConfig(name string) error { + file, err := os.Open(filepath.Join(getConfigDir(), name)) + if err != nil { + return fmt.Errorf("error reading configuration: %s", err) + } + return a.executeScript(file) +} + +func (a *application) executeConfigOptional(name string) (err error) { + err = a.executeConfig(name) + if os.IsNotExist(err) { + return nil + } + return +} |
