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

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