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.

115 lines
2.3 KiB

  1. package irc
  2. import (
  3. "strings"
  4. "git.aiterp.net/gisle/irc/list"
  5. )
  6. // A Channel is a target that manages the userlist
  7. type Channel struct {
  8. name string
  9. userlist *list.List
  10. }
  11. // Kind returns "channel"
  12. func (channel *Channel) Kind() string {
  13. return "channel"
  14. }
  15. // Name gets the channel name
  16. func (channel *Channel) Name() string {
  17. return channel.name
  18. }
  19. // UserList gets the channel userlist
  20. func (channel *Channel) UserList() list.Immutable {
  21. return channel.userlist.Immutable()
  22. }
  23. // Handle handles messages routed to this channel by the client's event loop
  24. func (channel *Channel) Handle(event *Event, client *Client) {
  25. switch event.Name() {
  26. case "packet.join":
  27. {
  28. // Support extended-join
  29. account := ""
  30. if accountArg := event.Arg(1); accountArg != "" && accountArg != "*" {
  31. account = accountArg
  32. }
  33. channel.userlist.Insert(list.User{
  34. Nick: event.Nick,
  35. User: event.User,
  36. Host: event.Host,
  37. Account: account,
  38. })
  39. }
  40. case "packet.part", "packet.quit":
  41. {
  42. channel.userlist.Remove(event.Nick)
  43. }
  44. case "packet.account":
  45. {
  46. newAccount := event.Arg(0)
  47. if newAccount != "*" && newAccount != "" {
  48. channel.userlist.Patch(event.Nick, list.UserPatch{Account: newAccount})
  49. } else {
  50. channel.userlist.Patch(event.Nick, list.UserPatch{ClearAccount: true})
  51. }
  52. }
  53. case "packet.chghost":
  54. {
  55. newUser := event.Arg(0)
  56. newHost := event.Arg(1)
  57. channel.userlist.Patch(event.Nick, list.UserPatch{User: newUser, Host: newHost})
  58. }
  59. case "packet.353": // NAMES
  60. {
  61. channel.userlist.SetAutoSort(false)
  62. tokens := strings.Split(event.Text, " ")
  63. for _, token := range tokens {
  64. channel.userlist.InsertFromNamesToken(token)
  65. }
  66. }
  67. case "packet.366": // End of NAMES
  68. {
  69. channel.userlist.SetAutoSort(true)
  70. }
  71. case "packet.mode":
  72. {
  73. isupport := client.ISupport()
  74. plus := false
  75. argIndex := 2
  76. for _, ch := range event.Arg(1) {
  77. if ch == '+' {
  78. plus = true
  79. continue
  80. }
  81. if ch == '-' {
  82. plus = false
  83. continue
  84. }
  85. arg := ""
  86. if isupport.ModeTakesArgument(ch, plus) {
  87. arg = event.Arg(argIndex)
  88. argIndex++
  89. }
  90. if isupport.IsPermissionMode(ch) {
  91. if plus {
  92. channel.userlist.AddMode(arg, ch)
  93. } else {
  94. channel.userlist.RemoveMode(arg, ch)
  95. }
  96. } else {
  97. // TODO: track non-permission modes
  98. }
  99. }
  100. }
  101. }
  102. }