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 16KB

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