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.

100 lines
2.2 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, "Whether to connect securely")
  20. func main() {
  21. ctx, cancel := context.WithCancel(context.Background())
  22. defer cancel()
  23. flag.Parse()
  24. client := irc.New(ctx, irc.Config{
  25. Nick: *flagNick,
  26. User: *flagUser,
  27. Alternatives: strings.Split(*flagAlts, ","),
  28. Password: *flagPass,
  29. Languages: []string{"no_NB", "no", "en_US", "en"},
  30. })
  31. client.AddHandler(handlers.Input)
  32. client.AddHandler(handlers.MRoleplay)
  33. err := client.Connect(*flagServer, *flagSsl)
  34. if err != nil {
  35. fmt.Fprintf(os.Stderr, "Failed to connect: %s", err)
  36. }
  37. var target irc.Target
  38. client.AddHandler(func(event *irc.Event, client *irc.Client) {
  39. if event.Name() == "input.target" {
  40. name := event.Arg(0)
  41. if client.ISupport().IsChannel(name) {
  42. log.Println("Set target channel", name)
  43. target = client.Channel(name)
  44. } else if len(name) > 0 {
  45. log.Println("Set target query", name)
  46. target = client.Query(name)
  47. } else {
  48. log.Println("Set target status")
  49. target = client.Status()
  50. }
  51. if target == nil {
  52. log.Println("Target does not exist, set to status")
  53. target = client.Status()
  54. }
  55. event.PreventDefault()
  56. return
  57. }
  58. if event.Name() == "input.clientstatus" {
  59. j, err := json.MarshalIndent(client.State(), "", " ")
  60. if err != nil {
  61. return
  62. }
  63. fmt.Println(string(j))
  64. event.PreventDefault()
  65. return
  66. }
  67. j, err := json.MarshalIndent(event, "", " ")
  68. if err != nil {
  69. return
  70. }
  71. fmt.Println(string(j))
  72. })
  73. reader := bufio.NewReader(os.Stdin)
  74. for {
  75. line, err := reader.ReadString('\n')
  76. if err != nil {
  77. break
  78. }
  79. client.EmitInput(line[:len(line)-1], target)
  80. }
  81. }