Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

reply.go 16KB

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