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

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