diff options
author | Hubert Hirtz <hubert.hirtz@laposte.net> | 2020-05-31 23:20:18 +0200 |
---|---|---|
committer | Hubert Hirtz <hubert.hirtz@laposte.net> | 2020-06-03 15:41:51 +0200 |
commit | 881d63465cdb17357438763d4d2996d5f1d92fcd (patch) | |
tree | 36b1b7c852105d34ee2a8d71f6967b3e448ba3b7 /cmd |
Initial commit
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/irc/main.go | 173 | ||||
-rw-r--r-- | cmd/test/main.go | 40 |
2 files changed, 213 insertions, 0 deletions
diff --git a/cmd/irc/main.go b/cmd/irc/main.go new file mode 100644 index 0000000..2e76c3a --- /dev/null +++ b/cmd/irc/main.go @@ -0,0 +1,173 @@ +package main + +import ( + "crypto/tls" + "fmt" + "git.sr.ht/~taiite/senpai" + "git.sr.ht/~taiite/senpai/irc" + "git.sr.ht/~taiite/senpai/ui" + "github.com/gdamore/tcell" + "log" + "os/user" + "strings" + "time" +) + +func main() { + tcell.SetEncodingFallback(tcell.EncodingFallbackASCII) + + usr, err := user.Current() + if err != nil { + log.Panicln(err) + } + + cfg, err := senpai.LoadConfigFile(usr.HomeDir + "/.config/senpai/senpai.yaml") + if err != nil { + log.Panicln(err) + } + + app, err := ui.New() + if err != nil { + log.Panicln(err) + } + defer app.Close() + + addr := cfg.Addr + app.AddLine("home", fmt.Sprintf("Connecting to %s...", addr), time.Now()) + + conn, err := tls.Dial("tcp", addr, nil) + if err != nil { + log.Panicln(err) + } + + s, err := irc.NewSession(conn, irc.SessionParams{ + Nickname: "taiite", + Username: "taiitent", + RealName: "Taiite Ier", + Auth: &irc.SASLPlain{Username: cfg.User, Password: cfg.Password}, + }) + if err != nil { + log.Panicln(err) + } + defer s.Stop() + + for !app.ShouldExit() { + select { + case ev := <-s.Poll(): + switch ev := ev.(type) { + case irc.RegisteredEvent: + app.AddLine("home", "Connected to the server", time.Now()) + case irc.SelfJoinEvent: + app.AddBuffer(ev.Channel) + case irc.ChannelMessageEvent: + line := formatIRCMessage(ev.Nick, ev.Content) + app.AddLine(ev.Channel, line, ev.Time) + case error: + log.Panicln(ev) + } + case ev := <-app.Events: + switch ev := ev.(type) { + case *tcell.EventResize: + app.Draw() + case *tcell.EventKey: + switch ev.Key() { + case tcell.KeyCtrlC: + app.Exit() + case tcell.KeyCtrlL: + app.Draw() + case tcell.KeyCtrlN: + app.NextBuffer() + case tcell.KeyCtrlP: + app.PreviousBuffer() + case tcell.KeyRight: + if ev.Modifiers() == tcell.ModAlt { + app.NextBuffer() + } else { + app.InputRight() + } + case tcell.KeyLeft: + if ev.Modifiers() == tcell.ModAlt { + app.PreviousBuffer() + } else { + app.InputLeft() + } + case tcell.KeyBackspace2: + app.InputBackspace() + case tcell.KeyEnter: + content := app.InputEnter() + handleInput(app, &s, content) + case tcell.KeyRune: + app.InputRune(ev.Rune()) + s.Typing(app.CurrentBuffer()) + } + } + } + } +} + +func parseCommand(s string) (command, args string) { + if s == "" { + return + } + + if s[0] != '/' { + args = s + return + } + + i := strings.IndexByte(s, ' ') + if i < 0 { + i = len(s) - 1 + } + + command = strings.ToUpper(s[1:i]) + args = s[i+1:] + + return +} + +func handleInput(app *ui.UI, s *irc.Session, content string) { + cmd, args := parseCommand(content) + + switch cmd { + case "": + ch := app.CurrentBuffer() + if ch == "home" { + return + } + + s.PrivMsg(ch, args) + case "ME": + ch := app.CurrentBuffer() + if ch == "home" { + return + } + + line := fmt.Sprintf("\x01ACTION %s\x01", args) + s.PrivMsg(ch, line) + default: + } +} + +func formatIRCMessage(nick, content string) (line string) { + if content == "" { + line = fmt.Sprintf("\x02%s\x00:", nick) + return + } + + if content[0] == 1 { + content = strings.TrimSuffix(content[1:], "\x01") + + if strings.HasPrefix(content, "ACTION") { + line = fmt.Sprintf("*\x02%s\x00%s", nick, content[6:]) + } else { + line = fmt.Sprintf("\x1dCTCP request from\x1d \x02%s\x00: %s", nick, content) + } + + return + } + + line = fmt.Sprintf("\x02%s\x00 %s", nick, content) + + return +} diff --git a/cmd/test/main.go b/cmd/test/main.go new file mode 100644 index 0000000..2010446 --- /dev/null +++ b/cmd/test/main.go @@ -0,0 +1,40 @@ +package main + +import ( + "crypto/tls" + "git.sr.ht/~taiite/senpai/irc" + "log" + "os" +) + +func main() { + addr := os.Args[1] + + log.Printf("Connecting to %s...\n", addr) + + conn, err := tls.Dial("tcp", addr, nil) + if err != nil { + log.Panicf("Failed to connect to %s: %v", addr, err) + } + defer conn.Close() + + log.Printf("Connected. Registration in progress...\n") + + cli, err := irc.NewSession(conn, irc.SessionParams{ + Nickname: "senpai", + Username: "senpai", + RealName: "senpai Ier", + Auth: &irc.SASLPlain{Username: os.Args[2], Password: os.Args[3]}, + }) + if err != nil { + log.Panicf("Failed to register to %s: %v", addr, err) + } + + for { + ev := <-cli.Poll() + switch ev := ev.(type) { + case error: + log.Panicln(ev) + } + } +} |