Mirror of github.com/gissleh/irc
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.1 KiB

  1. package main
  2. import (
  3. "bufio"
  4. "context"
  5. "encoding/json"
  6. "flag"
  7. "fmt"
  8. "github.com/gissleh/irc/handlers"
  9. "log"
  10. "os"
  11. "strings"
  12. "github.com/gissleh/irc"
  13. )
  14. var flagNick = flag.String("nick", "Test", "The client nick")
  15. var flagAlts = flag.String("alts", "Test2,Test3,Test4,Test5", "Alternative nicks to use")
  16. var flagUser = flag.String("user", "test", "The client user/ident")
  17. var flagPass = flag.String("pass", "", "The server password")
  18. var flagServer = flag.String("server", "localhost:6667", "The server to connect to")
  19. var flagSsl = flag.Bool("ssl", false, "Wether to connect securely")
  20. func main() {
  21. ctx, cancel := context.WithCancel(context.Background())
  22. defer cancel()
  23. flag.Parse()
  24. irc.AddHandler(handlers.Input)
  25. irc.AddHandler(handlers.MRoleplay)
  26. client := irc.New(ctx, irc.Config{
  27. Nick: *flagNick,
  28. User: *flagUser,
  29. Alternatives: strings.Split(*flagAlts, ","),
  30. Password: *flagPass,
  31. })
  32. err := client.Connect(*flagServer, *flagSsl)
  33. if err != nil {
  34. fmt.Fprintf(os.Stderr, "Failed to connect: %s", err)
  35. }
  36. var target irc.Target
  37. irc.AddHandler(func(event *irc.Event, client *irc.Client) {
  38. if event.Name() == "input.target" {
  39. name := event.Arg(0)
  40. if client.ISupport().IsChannel(name) {
  41. log.Println("Set target channel", name)
  42. target = client.Channel(name)
  43. } else if len(name) > 0 {
  44. log.Println("Set target query", name)
  45. target = client.Query(name)
  46. } else {
  47. log.Println("Set target status")
  48. target = client.Status()
  49. }
  50. if target == nil {
  51. log.Println("Target does not exist, set to status")
  52. target = client.Status()
  53. }
  54. event.PreventDefault()
  55. return
  56. }
  57. if event.Name() == "input.clientstatus" {
  58. j, err := json.MarshalIndent(client.State(), "", " ")
  59. if err != nil {
  60. return
  61. }
  62. fmt.Println(string(j))
  63. event.PreventDefault()
  64. return
  65. }
  66. j, err := json.MarshalIndent(event, "", " ")
  67. if err != nil {
  68. return
  69. }
  70. fmt.Println(string(j))
  71. })
  72. reader := bufio.NewReader(os.Stdin)
  73. for {
  74. line, err := reader.ReadString('\n')
  75. if err != nil {
  76. break
  77. }
  78. client.EmitInput(string(line[:len(line)-1]), target)
  79. }
  80. }