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.

commands.go 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. package irc
  2. import (
  3. "errors"
  4. "fmt"
  5. "regexp"
  6. "strconv"
  7. "strings"
  8. )
  9. type Message interface {
  10. Handle(s *Server, c *Client)
  11. }
  12. var (
  13. ErrNotEnoughArgs = errors.New("not enough arguments")
  14. ErrUModeUnknownFlag = errors.New("unknown umode flag")
  15. )
  16. // unknown
  17. type UnknownMessage struct {
  18. command string
  19. }
  20. // NB: no constructor, created on demand in parser for invalid messages.
  21. func (m *UnknownMessage) Handle(s *Server, c *Client) {
  22. c.send <- ErrUnknownCommand(s, m.command)
  23. }
  24. // PING
  25. type PingMessage struct {
  26. server string
  27. server2 string
  28. }
  29. func NewPingMessage(args []string) (Message, error) {
  30. if len(args) < 1 {
  31. return nil, ErrNotEnoughArgs
  32. }
  33. msg := &PingMessage{server: args[0]}
  34. if len(args) > 1 {
  35. msg.server2 = args[1]
  36. }
  37. return msg, nil
  38. }
  39. func (m *PingMessage) Handle(s *Server, c *Client) {
  40. c.send <- RplPong(s)
  41. }
  42. // PONG
  43. type PongMessage struct {
  44. server1 string
  45. server2 string
  46. }
  47. func NewPongMessage(args []string) (Message, error) {
  48. if len(args) < 1 {
  49. return nil, ErrNotEnoughArgs
  50. }
  51. message := &PongMessage{server1: args[0]}
  52. if len(args) > 1 {
  53. message.server2 = args[1]
  54. }
  55. return message, nil
  56. }
  57. func (m *PongMessage) Handle(s *Server, c *Client) {
  58. // no-op
  59. }
  60. // NICK
  61. type NickMessage struct {
  62. nickname string
  63. }
  64. func NewNickMessage(args []string) (Message, error) {
  65. if len(args) != 1 {
  66. return nil, ErrNotEnoughArgs
  67. }
  68. return &NickMessage{args[0]}, nil
  69. }
  70. func (m *NickMessage) Handle(s *Server, c *Client) {
  71. s.ChangeNick(c, m.nickname)
  72. }
  73. // USER
  74. type UserMessage struct {
  75. user string
  76. mode uint8
  77. unused string
  78. realname string
  79. }
  80. func NewUserMessage(args []string) (Message, error) {
  81. if len(args) != 4 {
  82. return nil, ErrNotEnoughArgs
  83. }
  84. msg := &UserMessage{
  85. user: args[0],
  86. unused: args[2],
  87. realname: args[3],
  88. }
  89. mode, err := strconv.ParseUint(args[1], 10, 8)
  90. if err == nil {
  91. msg.mode = uint8(mode)
  92. }
  93. return msg, nil
  94. }
  95. func (m *UserMessage) Handle(s *Server, c *Client) {
  96. s.UserLogin(c, m.user, m.realname)
  97. }
  98. // QUIT
  99. type QuitMessage struct {
  100. message string
  101. }
  102. func NewQuitMessage(args []string) (Message, error) {
  103. msg := &QuitMessage{}
  104. if len(args) > 0 {
  105. msg.message = args[0]
  106. }
  107. return msg, nil
  108. }
  109. func (m *QuitMessage) Handle(s *Server, c *Client) {
  110. s.Quit(c, m.message)
  111. }
  112. // MODE
  113. type ModeMessage struct {
  114. nickname string
  115. modes []string
  116. }
  117. var MODE_RE = regexp.MustCompile("^[-+][a-zA-Z]+$")
  118. func NewModeMessage(args []string) (Message, error) {
  119. if len(args) < 1 {
  120. return nil, ErrNotEnoughArgs
  121. }
  122. msg := &ModeMessage{
  123. nickname: args[0],
  124. }
  125. for _, arg := range args[1:] {
  126. if !MODE_RE.MatchString(arg) {
  127. return nil, ErrUModeUnknownFlag
  128. }
  129. prefix := arg[0]
  130. for _, c := range arg[1:] {
  131. mode := fmt.Sprintf("%c%c", prefix, c)
  132. msg.modes = append(msg.modes, mode)
  133. }
  134. }
  135. return msg, nil
  136. }
  137. func (m *ModeMessage) Handle(s *Server, c *Client) {
  138. if m.nickname != c.nick {
  139. c.send <- ErrUsersDontMatch(s)
  140. return
  141. }
  142. s.ChangeUserMode(c, m.modes)
  143. }
  144. // JOIN
  145. type JoinMessage struct {
  146. channels []string
  147. keys []string
  148. zero bool
  149. }
  150. func NewJoinMessage(args []string) (Message, error) {
  151. msg := &JoinMessage{}
  152. if len(args) > 0 {
  153. if args[0] == "0" {
  154. msg.zero = true
  155. } else {
  156. msg.channels = strings.Split(args[0], ",")
  157. }
  158. if len(args) > 1 {
  159. msg.keys = strings.Split(args[1], ",")
  160. }
  161. }
  162. return msg, nil
  163. }
  164. func (m *JoinMessage) Handle(s *Server, c *Client) {
  165. if m.zero {
  166. for channel := range c.channels {
  167. channel.Part(c, "")
  168. }
  169. } else {
  170. for i, name := range m.channels {
  171. key := ""
  172. if len(m.keys) > i {
  173. key = m.keys[i]
  174. }
  175. s.GetOrMakeChannel(name).Join(c, key)
  176. }
  177. }
  178. }
  179. // PART
  180. type PartMessage struct {
  181. channels []string
  182. message string
  183. }
  184. func NewPartMessage(args []string) (Message, error) {
  185. if len(args) < 1 {
  186. return nil, ErrNotEnoughArgs
  187. }
  188. msg := &PartMessage{channels: strings.Split(args[0], ",")}
  189. if len(args) > 1 {
  190. msg.message = args[1]
  191. }
  192. return msg, nil
  193. }
  194. func (m *PartMessage) Handle(s *Server, c *Client) {
  195. for _, chname := range m.channels {
  196. channel := s.channels[chname]
  197. if channel == nil {
  198. c.send <- ErrNoSuchChannel(s, chname)
  199. continue
  200. }
  201. channel.Part(c, m.message)
  202. }
  203. }
  204. // PRIVMSG
  205. type PrivMsgMessage struct {
  206. target string
  207. message string
  208. }
  209. func NewPrivMsgMessage(args []string) (Message, error) {
  210. if len(args) < 2 {
  211. return nil, ErrNotEnoughArgs
  212. }
  213. return &PrivMsgMessage{
  214. target: args[0],
  215. message: args[1],
  216. }, nil
  217. }
  218. func (m *PrivMsgMessage) TargetIsChannel() bool {
  219. switch m.target[0] {
  220. case '&', '#', '+', '!':
  221. return true
  222. }
  223. return false
  224. }
  225. func (m *PrivMsgMessage) Handle(s *Server, c *Client) {
  226. if m.TargetIsChannel() {
  227. channel := s.channels[m.target]
  228. if channel != nil {
  229. channel.PrivMsg(c, m.message)
  230. } else {
  231. c.send <- ErrNoSuchNick(s, m.target)
  232. }
  233. } else {
  234. client := s.nicks[m.target]
  235. if client != nil {
  236. client.send <- RplPrivMsg(client, m.message)
  237. } else {
  238. c.send <- ErrNoSuchNick(s, m.target)
  239. }
  240. }
  241. }
  242. // TOPIC [newtopic]
  243. type TopicMessage struct {
  244. channel string
  245. topic string
  246. }
  247. func NewTopicMessage(args []string) (Message, error) {
  248. if len(args) < 1 {
  249. return nil, ErrNotEnoughArgs
  250. }
  251. msg := &TopicMessage{channel: args[0]}
  252. if len(args) > 1 {
  253. msg.topic = args[1]
  254. }
  255. return msg, nil
  256. }
  257. func (m *TopicMessage) Handle(s *Server, c *Client) {
  258. channel := s.channels[m.channel]
  259. if channel == nil {
  260. c.send <- ErrNoSuchChannel(s, m.channel)
  261. return
  262. }
  263. if m.topic == "" {
  264. channel.GetTopic(c)
  265. } else {
  266. channel.ChangeTopic(c, m.topic)
  267. }
  268. }
  269. // INVITE <nickname> <channel>
  270. type InviteMessage struct {
  271. nickname string
  272. channel string
  273. }
  274. func NewInviteMessage(args []string) (Message, error) {
  275. if len(args) < 2 {
  276. return nil, ErrNotEnoughArgs
  277. }
  278. return &InviteMessage{
  279. nickname: args[0],
  280. channel: args[1],
  281. }, nil
  282. }
  283. func (m *InviteMessage) Handle(s *Server, c *Client) {
  284. channel := s.channels[m.channel]
  285. if channel == nil {
  286. c.send <- ErrNoSuchNick(s, m.channel)
  287. return
  288. }
  289. invitee := s.nicks[m.nickname]
  290. if invitee == nil {
  291. c.send <- ErrNoSuchNick(s, m.nickname)
  292. return
  293. }
  294. channel.Invite(c, invitee)
  295. }