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.

reply.go 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. package irc
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. )
  7. type ReplyCode interface {
  8. String() string
  9. }
  10. type StringCode string
  11. func (code StringCode) String() string {
  12. return string(code)
  13. }
  14. type NumericCode uint
  15. func (code NumericCode) String() string {
  16. return fmt.Sprintf("%03d", code)
  17. }
  18. func NewStringReply(source Identifiable, code StringCode,
  19. format string, args ...interface{}) string {
  20. var header string
  21. if source == nil {
  22. header = code.String() + " "
  23. } else {
  24. header = fmt.Sprintf(":%s %s ", source, code)
  25. }
  26. var message string
  27. if len(args) > 0 {
  28. message = fmt.Sprintf(format, args...)
  29. } else {
  30. message = format
  31. }
  32. return header + message
  33. }
  34. func NewNumericReply(target *Client, code NumericCode,
  35. format string, args ...interface{}) string {
  36. header := fmt.Sprintf(":%s %s %s ", target.server.Id(), code, target.Nick())
  37. var message string
  38. if len(args) > 0 {
  39. message = fmt.Sprintf(format, args...)
  40. } else {
  41. message = format
  42. }
  43. return header + message
  44. }
  45. func (target *Client) NumericReply(code NumericCode,
  46. format string, args ...interface{}) {
  47. target.Reply(NewNumericReply(target, code, format, args...))
  48. }
  49. //
  50. // multiline replies
  51. //
  52. func joinedLen(names []string) int {
  53. var l = len(names) - 1 // " " between names
  54. for _, name := range names {
  55. l += len(name)
  56. }
  57. return l
  58. }
  59. func (target *Client) MultilineReply(names []string, code NumericCode, format string,
  60. args ...interface{}) {
  61. baseLen := len(NewNumericReply(target, code, format))
  62. tooLong := func(names []string) bool {
  63. return (baseLen + joinedLen(names)) > MAX_REPLY_LEN
  64. }
  65. argsAndNames := func(names []string) []interface{} {
  66. return append(args, strings.Join(names, " "))
  67. }
  68. from, to := 0, 1
  69. for to < len(names) {
  70. if (from < (to - 1)) && tooLong(names[from:to]) {
  71. target.NumericReply(code, format, argsAndNames(names[from:to-1])...)
  72. from, to = to-1, to
  73. } else {
  74. to += 1
  75. }
  76. }
  77. if from < len(names) {
  78. target.NumericReply(code, format, argsAndNames(names[from:])...)
  79. }
  80. }
  81. //
  82. // messaging replies
  83. //
  84. func RplPrivMsg(source Identifiable, target Identifiable, message Text) string {
  85. return NewStringReply(source, PRIVMSG, "%s :%s", target.Nick(), message)
  86. }
  87. func RplNotice(source Identifiable, target Identifiable, message Text) string {
  88. return NewStringReply(source, NOTICE, "%s :%s", target.Nick(), message)
  89. }
  90. func RplNick(source Identifiable, newNick Name) string {
  91. return NewStringReply(source, NICK, newNick.String())
  92. }
  93. func RplJoin(client *Client, channel *Channel) string {
  94. return NewStringReply(client, JOIN, channel.name.String())
  95. }
  96. func RplPart(client *Client, channel *Channel, message Text) string {
  97. return NewStringReply(client, PART, "%s :%s", channel, message)
  98. }
  99. func RplMode(client *Client, target *Client, changes ModeChanges) string {
  100. return NewStringReply(client, MODE, "%s :%s", target.Nick(), changes)
  101. }
  102. func RplChannelMode(client *Client, channel *Channel,
  103. changes ChannelModeChanges) string {
  104. return NewStringReply(client, MODE, "%s %s", channel, changes)
  105. }
  106. func RplTopicMsg(source Identifiable, channel *Channel) string {
  107. return NewStringReply(source, TOPIC, "%s :%s", channel, channel.topic)
  108. }
  109. func RplPing(target Identifiable) string {
  110. return NewStringReply(nil, PING, ":%s", target.Nick())
  111. }
  112. func RplPong(client *Client) string {
  113. return NewStringReply(nil, PONG, client.Nick().String())
  114. }
  115. func RplQuit(client *Client, message Text) string {
  116. return NewStringReply(client, QUIT, ":%s", message)
  117. }
  118. func RplError(message string) string {
  119. return NewStringReply(nil, ERROR, ":%s", message)
  120. }
  121. func RplInviteMsg(inviter *Client, invitee *Client, channel Name) string {
  122. return NewStringReply(inviter, INVITE, "%s :%s", invitee.Nick(), channel)
  123. }
  124. func RplKick(channel *Channel, client *Client, target *Client, comment Text) string {
  125. return NewStringReply(client, KICK, "%s %s :%s",
  126. channel, target.Nick(), comment)
  127. }
  128. func RplKill(client *Client, target *Client, comment Text) string {
  129. return NewStringReply(client, KICK,
  130. "%s :%s", target.Nick(), comment)
  131. }
  132. func RplCap(client *Client, subCommand CapSubCommand, arg interface{}) string {
  133. return NewStringReply(nil, CAP, "%s %s :%s", client.Nick(), subCommand, arg)
  134. }
  135. // numeric replies
  136. func (target *Client) RplWelcome() {
  137. target.NumericReply(RPL_WELCOME,
  138. ":Welcome to the Internet Relay Network %s", target.Id())
  139. }
  140. func (target *Client) RplYourHost() {
  141. target.NumericReply(RPL_YOURHOST,
  142. ":Your host is %s, running version %s", target.server.name, SEM_VER)
  143. }
  144. func (target *Client) RplCreated() {
  145. target.NumericReply(RPL_CREATED,
  146. ":This server was created %s", target.server.ctime.Format(time.RFC1123))
  147. }
  148. func (target *Client) RplMyInfo() {
  149. target.NumericReply(RPL_MYINFO,
  150. "%s %s %s %s",
  151. target.server.name, SEM_VER, SupportedUserModes, SupportedChannelModes)
  152. }
  153. func (target *Client) RplUModeIs(client *Client) {
  154. target.NumericReply(RPL_UMODEIS, client.ModeString())
  155. }
  156. func (target *Client) RplNoTopic(channel *Channel) {
  157. target.NumericReply(RPL_NOTOPIC,
  158. "%s :No topic is set", channel.name)
  159. }
  160. func (target *Client) RplTopic(channel *Channel) {
  161. target.NumericReply(RPL_TOPIC,
  162. "%s :%s", channel.name, channel.topic)
  163. }
  164. // <nick> <channel>
  165. // NB: correction in errata
  166. func (target *Client) RplInvitingMsg(invitee *Client, channel Name) {
  167. target.NumericReply(RPL_INVITING,
  168. "%s %s", invitee.Nick(), channel)
  169. }
  170. func (target *Client) RplEndOfNames(channel *Channel) {
  171. target.NumericReply(RPL_ENDOFNAMES,
  172. "%s :End of NAMES list", channel.name)
  173. }
  174. // :You are now an IRC operator
  175. func (target *Client) RplYoureOper() {
  176. target.NumericReply(RPL_YOUREOPER,
  177. ":You are now an IRC operator")
  178. }
  179. func (target *Client) RplWhois(client *Client) {
  180. target.RplWhoisUser(client)
  181. if client.flags[Operator] {
  182. target.RplWhoisOperator(client)
  183. }
  184. target.RplWhoisIdle(client)
  185. target.RplWhoisChannels(client)
  186. target.RplEndOfWhois()
  187. }
  188. func (target *Client) RplWhoisUser(client *Client) {
  189. target.NumericReply(RPL_WHOISUSER,
  190. "%s %s %s * :%s", client.Nick(), client.username, client.hostname,
  191. client.realname)
  192. }
  193. func (target *Client) RplWhoisOperator(client *Client) {
  194. target.NumericReply(RPL_WHOISOPERATOR,
  195. "%s :is an IRC operator", client.Nick())
  196. }
  197. func (target *Client) RplWhoisIdle(client *Client) {
  198. target.NumericReply(RPL_WHOISIDLE,
  199. "%s %d %d :seconds idle, signon time",
  200. client.Nick(), client.IdleSeconds(), client.SignonTime())
  201. }
  202. func (target *Client) RplEndOfWhois() {
  203. target.NumericReply(RPL_ENDOFWHOIS,
  204. ":End of WHOIS list")
  205. }
  206. func (target *Client) RplChannelModeIs(channel *Channel) {
  207. target.NumericReply(RPL_CHANNELMODEIS,
  208. "%s %s", channel, channel.ModeString(target))
  209. }
  210. // <channel> <user> <host> <server> <nick> ( "H" / "G" ) ["*"] [ ( "@" / "+" ) ]
  211. // :<hopcount> <real name>
  212. func (target *Client) RplWhoReply(channel *Channel, client *Client) {
  213. channelName := "*"
  214. flags := ""
  215. if client.flags[Away] {
  216. flags = "G"
  217. } else {
  218. flags = "H"
  219. }
  220. if client.flags[Operator] {
  221. flags += "*"
  222. }
  223. if channel != nil {
  224. channelName = channel.name.String()
  225. if target.capabilities[MultiPrefix] {
  226. if channel.members[client][ChannelOperator] {
  227. flags += "@"
  228. }
  229. if channel.members[client][Voice] {
  230. flags += "+"
  231. }
  232. } else {
  233. if channel.members[client][ChannelOperator] {
  234. flags += "@"
  235. } else if channel.members[client][Voice] {
  236. flags += "+"
  237. }
  238. }
  239. }
  240. target.NumericReply(RPL_WHOREPLY,
  241. "%s %s %s %s %s %s :%d %s", channelName, client.username, client.hostname,
  242. client.server.name, client.Nick(), flags, client.hops, client.realname)
  243. }
  244. // <name> :End of WHO list
  245. func (target *Client) RplEndOfWho(name Name) {
  246. target.NumericReply(RPL_ENDOFWHO,
  247. "%s :End of WHO list", name)
  248. }
  249. func (target *Client) RplMaskList(mode ChannelMode, channel *Channel, mask Name) {
  250. switch mode {
  251. case BanMask:
  252. target.RplBanList(channel, mask)
  253. case ExceptMask:
  254. target.RplExceptList(channel, mask)
  255. case InviteMask:
  256. target.RplInviteList(channel, mask)
  257. }
  258. }
  259. func (target *Client) RplEndOfMaskList(mode ChannelMode, channel *Channel) {
  260. switch mode {
  261. case BanMask:
  262. target.RplEndOfBanList(channel)
  263. case ExceptMask:
  264. target.RplEndOfExceptList(channel)
  265. case InviteMask:
  266. target.RplEndOfInviteList(channel)
  267. }
  268. }
  269. func (target *Client) RplBanList(channel *Channel, mask Name) {
  270. target.NumericReply(RPL_BANLIST,
  271. "%s %s", channel, mask)
  272. }
  273. func (target *Client) RplEndOfBanList(channel *Channel) {
  274. target.NumericReply(RPL_ENDOFBANLIST,
  275. "%s :End of channel ban list", channel)
  276. }
  277. func (target *Client) RplExceptList(channel *Channel, mask Name) {
  278. target.NumericReply(RPL_EXCEPTLIST,
  279. "%s %s", channel, mask)
  280. }
  281. func (target *Client) RplEndOfExceptList(channel *Channel) {
  282. target.NumericReply(RPL_ENDOFEXCEPTLIST,
  283. "%s :End of channel exception list", channel)
  284. }
  285. func (target *Client) RplInviteList(channel *Channel, mask Name) {
  286. target.NumericReply(RPL_INVITELIST,
  287. "%s %s", channel, mask)
  288. }
  289. func (target *Client) RplEndOfInviteList(channel *Channel) {
  290. target.NumericReply(RPL_ENDOFINVITELIST,
  291. "%s :End of channel invite list", channel)
  292. }
  293. func (target *Client) RplNowAway() {
  294. target.NumericReply(RPL_NOWAWAY,
  295. ":You have been marked as being away")
  296. }
  297. func (target *Client) RplUnAway() {
  298. target.NumericReply(RPL_UNAWAY,
  299. ":You are no longer marked as being away")
  300. }
  301. func (target *Client) RplAway(client *Client) {
  302. target.NumericReply(RPL_AWAY,
  303. "%s :%s", client.Nick(), client.awayMessage)
  304. }
  305. func (target *Client) RplIsOn(nicks []string) {
  306. target.NumericReply(RPL_ISON,
  307. ":%s", strings.Join(nicks, " "))
  308. }
  309. func (target *Client) RplMOTDStart() {
  310. target.NumericReply(RPL_MOTDSTART,
  311. ":- %s Message of the day - ", target.server.name)
  312. }
  313. func (target *Client) RplMOTD(line string) {
  314. target.NumericReply(RPL_MOTD,
  315. ":- %s", line)
  316. }
  317. func (target *Client) RplMOTDEnd() {
  318. target.NumericReply(RPL_ENDOFMOTD,
  319. ":End of MOTD command")
  320. }
  321. func (target *Client) RplList(channel *Channel) {
  322. target.NumericReply(RPL_LIST,
  323. "%s %d :%s", channel, len(channel.members), channel.topic)
  324. }
  325. func (target *Client) RplListEnd(server *Server) {
  326. target.NumericReply(RPL_LISTEND,
  327. ":End of LIST")
  328. }
  329. func (target *Client) RplNamReply(channel *Channel) {
  330. target.MultilineReply(channel.Nicks(target), RPL_NAMREPLY,
  331. "= %s :%s", channel)
  332. }
  333. func (target *Client) RplWhoisChannels(client *Client) {
  334. target.MultilineReply(client.WhoisChannelsNames(), RPL_WHOISCHANNELS,
  335. "%s :%s", client.Nick())
  336. }
  337. func (target *Client) RplVersion() {
  338. target.NumericReply(RPL_VERSION,
  339. "%s %s", SEM_VER, target.server.name)
  340. }
  341. func (target *Client) RplInviting(invitee *Client, channel Name) {
  342. target.NumericReply(RPL_INVITING,
  343. "%s %s", invitee.Nick(), channel)
  344. }
  345. func (target *Client) RplTime() {
  346. target.NumericReply(RPL_TIME,
  347. "%s :%s", target.server.name, time.Now().Format(time.RFC1123))
  348. }
  349. func (target *Client) RplWhoWasUser(whoWas *WhoWas) {
  350. target.NumericReply(RPL_WHOWASUSER,
  351. "%s %s %s * :%s",
  352. whoWas.nickname, whoWas.username, whoWas.hostname, whoWas.realname)
  353. }
  354. func (target *Client) RplEndOfWhoWas(nickname Name) {
  355. target.NumericReply(RPL_ENDOFWHOWAS,
  356. "%s :End of WHOWAS", nickname)
  357. }
  358. //
  359. // errors (also numeric)
  360. //
  361. func (target *Client) ErrAlreadyRegistered() {
  362. target.NumericReply(ERR_ALREADYREGISTRED,
  363. ":You may not reregister")
  364. }
  365. func (target *Client) ErrNickNameInUse(nick Name) {
  366. target.NumericReply(ERR_NICKNAMEINUSE,
  367. "%s :Nickname is already in use", nick)
  368. }
  369. func (target *Client) ErrUnknownCommand(code StringCode) {
  370. target.NumericReply(ERR_UNKNOWNCOMMAND,
  371. "%s :Unknown command", code)
  372. }
  373. func (target *Client) ErrUsersDontMatch() {
  374. target.NumericReply(ERR_USERSDONTMATCH,
  375. ":Cannot change mode for other users")
  376. }
  377. func (target *Client) ErrNeedMoreParams(command StringCode) {
  378. target.NumericReply(ERR_NEEDMOREPARAMS,
  379. "%s :Not enough parameters", command)
  380. }
  381. func (target *Client) ErrNoSuchChannel(channel Name) {
  382. target.NumericReply(ERR_NOSUCHCHANNEL,
  383. "%s :No such channel", channel)
  384. }
  385. func (target *Client) ErrUserOnChannel(channel *Channel, member *Client) {
  386. target.NumericReply(ERR_USERONCHANNEL,
  387. "%s %s :is already on channel", member.Nick(), channel.name)
  388. }
  389. func (target *Client) ErrNotOnChannel(channel *Channel) {
  390. target.NumericReply(ERR_NOTONCHANNEL,
  391. "%s :You're not on that channel", channel.name)
  392. }
  393. func (target *Client) ErrInviteOnlyChannel(channel *Channel) {
  394. target.NumericReply(ERR_INVITEONLYCHAN,
  395. "%s :Cannot join channel (+i)", channel.name)
  396. }
  397. func (target *Client) ErrBadChannelKey(channel *Channel) {
  398. target.NumericReply(ERR_BADCHANNELKEY,
  399. "%s :Cannot join channel (+k)", channel.name)
  400. }
  401. func (target *Client) ErrNoSuchNick(nick Name) {
  402. target.NumericReply(ERR_NOSUCHNICK,
  403. "%s :No such nick/channel", nick)
  404. }
  405. func (target *Client) ErrPasswdMismatch() {
  406. target.NumericReply(ERR_PASSWDMISMATCH, ":Password incorrect")
  407. }
  408. func (target *Client) ErrNoChanModes(channel *Channel) {
  409. target.NumericReply(ERR_NOCHANMODES,
  410. "%s :Channel doesn't support modes", channel)
  411. }
  412. func (target *Client) ErrNoPrivileges() {
  413. target.NumericReply(ERR_NOPRIVILEGES, ":Permission Denied")
  414. }
  415. func (target *Client) ErrRestricted() {
  416. target.NumericReply(ERR_RESTRICTED, ":Your connection is restricted!")
  417. }
  418. func (target *Client) ErrNoSuchServer(server Name) {
  419. target.NumericReply(ERR_NOSUCHSERVER, "%s :No such server", server)
  420. }
  421. func (target *Client) ErrUserNotInChannel(channel *Channel, client *Client) {
  422. target.NumericReply(ERR_USERNOTINCHANNEL,
  423. "%s %s :They aren't on that channel", client.Nick(), channel)
  424. }
  425. func (target *Client) ErrCannotSendToChan(channel *Channel) {
  426. target.NumericReply(ERR_CANNOTSENDTOCHAN,
  427. "%s :Cannot send to channel", channel)
  428. }
  429. // <channel> :You're not channel operator
  430. func (target *Client) ErrChanOPrivIsNeeded(channel *Channel) {
  431. target.NumericReply(ERR_CHANOPRIVSNEEDED,
  432. "%s :You're not channel operator", channel)
  433. }
  434. func (target *Client) ErrNoMOTD() {
  435. target.NumericReply(ERR_NOMOTD, ":MOTD File is missing")
  436. }
  437. func (target *Client) ErrNoNicknameGiven() {
  438. target.NumericReply(ERR_NONICKNAMEGIVEN, ":No nickname given")
  439. }
  440. func (target *Client) ErrErroneusNickname(nick Name) {
  441. target.NumericReply(ERR_ERRONEUSNICKNAME,
  442. "%s :Erroneous nickname", nick)
  443. }
  444. func (target *Client) ErrUnknownMode(mode ChannelMode, channel *Channel) {
  445. target.NumericReply(ERR_UNKNOWNMODE,
  446. "%s :is unknown mode char to me for %s", mode, channel)
  447. }
  448. func (target *Client) ErrChannelIsFull(channel *Channel) {
  449. target.NumericReply(ERR_CHANNELISFULL,
  450. "%s :Cannot join channel (+l)", channel)
  451. }
  452. func (target *Client) ErrWasNoSuchNick(nickname Name) {
  453. target.NumericReply(ERR_WASNOSUCHNICK,
  454. "%s :There was no such nickname", nickname)
  455. }
  456. func (target *Client) ErrInvalidCapCmd(subCommand CapSubCommand) {
  457. target.NumericReply(ERR_INVALIDCAPCMD,
  458. "%s :Invalid CAP subcommand", subCommand)
  459. }
  460. func (target *Client) ErrBannedFromChan(channel *Channel) {
  461. target.NumericReply(ERR_BANNEDFROMCHAN,
  462. "%s :Cannot join channel (+b)", channel)
  463. }
  464. func (target *Client) ErrInviteOnlyChan(channel *Channel) {
  465. target.NumericReply(ERR_INVITEONLYCHAN,
  466. "%s :Cannot join channel (+i)", channel)
  467. }