PageRenderTime 39ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/irc/reply.go

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