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

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