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.

client.go 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. // Copyright (c) 2012-2014 Jeremy Latt
  2. // Copyright (c) 2014-2015 Edmund Huber
  3. // Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
  4. // released under the MIT license
  5. package irc
  6. import (
  7. "errors"
  8. "fmt"
  9. "log"
  10. "net"
  11. "runtime/debug"
  12. "strconv"
  13. "strings"
  14. "sync"
  15. "time"
  16. "github.com/goshuirc/irc-go/ircfmt"
  17. "github.com/goshuirc/irc-go/ircmsg"
  18. ident "github.com/oragono/go-ident"
  19. "github.com/oragono/oragono/irc/caps"
  20. "github.com/oragono/oragono/irc/sno"
  21. "github.com/oragono/oragono/irc/utils"
  22. )
  23. const (
  24. // IdleTimeout is how long without traffic before a client's considered idle.
  25. IdleTimeout = time.Minute + time.Second*30
  26. // QuitTimeout is how long without traffic (after they're considered idle) that clients are killed.
  27. QuitTimeout = time.Minute
  28. // IdentTimeoutSeconds is how many seconds before our ident (username) check times out.
  29. IdentTimeoutSeconds = 5
  30. )
  31. var (
  32. // TimeoutStatedSeconds is how many seconds before clients are timed out (IdleTimeout plus QuitTimeout).
  33. TimeoutStatedSeconds = strconv.Itoa(int((IdleTimeout + QuitTimeout).Seconds()))
  34. // ErrNickAlreadySet is a weird error that's sent when the server's consistency has been compromised.
  35. ErrNickAlreadySet = errors.New("Nickname is already set")
  36. )
  37. // Client is an IRC client.
  38. type Client struct {
  39. account *ClientAccount
  40. atime time.Time
  41. authorized bool
  42. awayMessage string
  43. capabilities *caps.Set
  44. capState CapState
  45. capVersion caps.Version
  46. certfp string
  47. channels ChannelSet
  48. class *OperClass
  49. ctime time.Time
  50. destroyMutex sync.Mutex
  51. exitedSnomaskSent bool
  52. flags map[Mode]bool
  53. hasQuit bool
  54. hops int
  55. hostname string
  56. idleTimer *time.Timer
  57. isDestroyed bool
  58. isQuitting bool
  59. nick string
  60. nickCasefolded string
  61. nickMaskCasefolded string
  62. nickMaskString string // cache for nickmask string since it's used with lots of replies
  63. operName string
  64. proxiedIP string // actual remote IP if using the PROXY protocol
  65. quitMessage string
  66. quitMessageSent bool
  67. quitMutex sync.Mutex
  68. quitTimer *time.Timer
  69. rawHostname string
  70. realname string
  71. registered bool
  72. saslInProgress bool
  73. saslMechanism string
  74. saslValue string
  75. server *Server
  76. socket *Socket
  77. stateMutex sync.RWMutex // generic protection for mutable state
  78. timerMutex sync.Mutex
  79. username string
  80. vhost string
  81. whoisLine string
  82. }
  83. // NewClient returns a client with all the appropriate info setup.
  84. func NewClient(server *Server, conn net.Conn, isTLS bool) *Client {
  85. now := time.Now()
  86. socket := NewSocket(conn, server.MaxSendQBytes)
  87. go socket.RunSocketWriter()
  88. client := &Client{
  89. atime: now,
  90. authorized: server.getPassword() == nil,
  91. capabilities: caps.NewSet(),
  92. capState: CapNone,
  93. capVersion: caps.Cap301,
  94. channels: make(ChannelSet),
  95. ctime: now,
  96. flags: make(map[Mode]bool),
  97. server: server,
  98. socket: &socket,
  99. account: &NoAccount,
  100. nick: "*", // * is used until actual nick is given
  101. nickCasefolded: "*",
  102. nickMaskString: "*", // * is used until actual nick is given
  103. }
  104. if isTLS {
  105. client.flags[TLS] = true
  106. // error is not useful to us here anyways so we can ignore it
  107. client.certfp, _ = client.socket.CertFP()
  108. }
  109. if server.checkIdent {
  110. _, serverPortString, err := net.SplitHostPort(conn.LocalAddr().String())
  111. serverPort, _ := strconv.Atoi(serverPortString)
  112. if err != nil {
  113. log.Fatal(err)
  114. }
  115. clientHost, clientPortString, err := net.SplitHostPort(conn.RemoteAddr().String())
  116. clientPort, _ := strconv.Atoi(clientPortString)
  117. if err != nil {
  118. log.Fatal(err)
  119. }
  120. client.Notice("*** Looking up your username")
  121. resp, err := ident.Query(clientHost, serverPort, clientPort, IdentTimeoutSeconds)
  122. if err == nil {
  123. username := resp.Identifier
  124. _, err := CasefoldName(username) // ensure it's a valid username
  125. if err == nil {
  126. client.Notice("*** Found your username")
  127. client.username = username
  128. // we don't need to updateNickMask here since nickMask is not used for anything yet
  129. } else {
  130. client.Notice("*** Got a malformed username, ignoring")
  131. }
  132. } else {
  133. client.Notice("*** Could not find your username")
  134. }
  135. }
  136. client.Touch()
  137. go client.run()
  138. return client
  139. }
  140. // IP returns the IP address of this client.
  141. func (client *Client) IP() net.IP {
  142. if client.proxiedIP != "" {
  143. return net.ParseIP(client.proxiedIP)
  144. }
  145. return net.ParseIP(utils.IPString(client.socket.conn.RemoteAddr()))
  146. }
  147. // IPString returns the IP address of this client as a string.
  148. func (client *Client) IPString() string {
  149. if client.proxiedIP != "" {
  150. return client.proxiedIP
  151. }
  152. ip := client.IP().String()
  153. if 0 < len(ip) && ip[0] == ':' {
  154. ip = "0" + ip
  155. }
  156. return ip
  157. }
  158. //
  159. // command goroutine
  160. //
  161. func (client *Client) maxlens() (int, int) {
  162. maxlenTags := 512
  163. maxlenRest := 512
  164. if client.capabilities.Has(caps.MessageTags) {
  165. maxlenTags = 4096
  166. }
  167. if client.capabilities.Has(caps.MaxLine) {
  168. limits := client.server.getLimits()
  169. if limits.LineLen.Tags > maxlenTags {
  170. maxlenTags = limits.LineLen.Tags
  171. }
  172. maxlenRest = limits.LineLen.Rest
  173. }
  174. return maxlenTags, maxlenRest
  175. }
  176. func (client *Client) run() {
  177. var err error
  178. var isExiting bool
  179. var line string
  180. var msg ircmsg.IrcMessage
  181. // Set the hostname for this client
  182. // (may be overridden by a later PROXY command from stunnel)
  183. client.rawHostname = utils.AddrLookupHostname(client.socket.conn.RemoteAddr())
  184. for {
  185. line, err = client.socket.Read()
  186. if err != nil {
  187. client.Quit("connection closed")
  188. break
  189. }
  190. maxlenTags, maxlenRest := client.maxlens()
  191. client.server.logger.Debug("userinput ", client.nick, "<- ", line)
  192. msg, err = ircmsg.ParseLineMaxLen(line, maxlenTags, maxlenRest)
  193. if err == ircmsg.ErrorLineIsEmpty {
  194. continue
  195. } else if err != nil {
  196. client.Quit("received malformed line")
  197. break
  198. }
  199. cmd, exists := Commands[msg.Command]
  200. if !exists {
  201. if len(msg.Command) > 0 {
  202. client.Send(nil, client.server.name, ERR_UNKNOWNCOMMAND, client.nick, msg.Command, "Unknown command")
  203. } else {
  204. client.Send(nil, client.server.name, ERR_UNKNOWNCOMMAND, client.nick, "lastcmd", "No command given")
  205. }
  206. continue
  207. }
  208. isExiting = cmd.Run(client.server, client, msg)
  209. if isExiting || client.isQuitting {
  210. break
  211. }
  212. }
  213. // ensure client connection gets closed
  214. client.destroy()
  215. }
  216. //
  217. // idle, quit, timers and timeouts
  218. //
  219. // Active updates when the client was last 'active' (i.e. the user should be sitting in front of their client).
  220. func (client *Client) Active() {
  221. client.atime = time.Now()
  222. }
  223. // Touch marks the client as alive (as it it has a connection to us and we
  224. // can receive messages from it), and resets when we'll send the client a
  225. // keepalive PING.
  226. func (client *Client) Touch() {
  227. client.timerMutex.Lock()
  228. defer client.timerMutex.Unlock()
  229. if client.quitTimer != nil {
  230. client.quitTimer.Stop()
  231. }
  232. if client.idleTimer == nil {
  233. client.idleTimer = time.AfterFunc(IdleTimeout, client.connectionIdle)
  234. } else {
  235. client.idleTimer.Reset(IdleTimeout)
  236. }
  237. }
  238. // connectionIdle is run when the client has not sent us any data for a while,
  239. // sends the client a PING and starts the quit timeout.
  240. func (client *Client) connectionIdle() {
  241. client.timerMutex.Lock()
  242. defer client.timerMutex.Unlock()
  243. client.Send(nil, "", "PING", client.nick)
  244. if client.quitTimer == nil {
  245. client.quitTimer = time.AfterFunc(QuitTimeout, client.connectionTimeout)
  246. } else {
  247. client.quitTimer.Reset(QuitTimeout)
  248. }
  249. }
  250. // connectionTimeout runs after connectionIdle has been run, if we do not receive a
  251. // ping or any other activity back from the client. When this happens we assume the
  252. // connection has died and remove the client from the network.
  253. func (client *Client) connectionTimeout() {
  254. client.Quit(fmt.Sprintf("Ping timeout: %s seconds", TimeoutStatedSeconds))
  255. client.isQuitting = true
  256. }
  257. //
  258. // server goroutine
  259. //
  260. // Register sets the client details as appropriate when entering the network.
  261. func (client *Client) Register() {
  262. if client.registered {
  263. return
  264. }
  265. client.registered = true
  266. client.Touch()
  267. client.updateNickMask("")
  268. client.server.monitorManager.AlertAbout(client, true)
  269. }
  270. // IdleTime returns how long this client's been idle.
  271. func (client *Client) IdleTime() time.Duration {
  272. return time.Since(client.atime)
  273. }
  274. // SignonTime returns this client's signon time as a unix timestamp.
  275. func (client *Client) SignonTime() int64 {
  276. return client.ctime.Unix()
  277. }
  278. // IdleSeconds returns the number of seconds this client's been idle.
  279. func (client *Client) IdleSeconds() uint64 {
  280. return uint64(client.IdleTime().Seconds())
  281. }
  282. // HasNick returns true if the client's nickname is set (used in registration).
  283. func (client *Client) HasNick() bool {
  284. return client.nick != "" && client.nick != "*"
  285. }
  286. // HasUsername returns true if the client's username is set (used in registration).
  287. func (client *Client) HasUsername() bool {
  288. return client.username != "" && client.username != "*"
  289. }
  290. // HasRoleCapabs returns true if client has the given (role) capabilities.
  291. func (client *Client) HasRoleCapabs(capabs ...string) bool {
  292. if client.class == nil {
  293. return false
  294. }
  295. for _, capab := range capabs {
  296. if !client.class.Capabilities[capab] {
  297. return false
  298. }
  299. }
  300. return true
  301. }
  302. // ModeString returns the mode string for this client.
  303. func (client *Client) ModeString() (str string) {
  304. str = "+"
  305. for flag := range client.flags {
  306. str += flag.String()
  307. }
  308. return
  309. }
  310. // Friends refers to clients that share a channel with this client.
  311. func (client *Client) Friends(capabs ...caps.Capability) ClientSet {
  312. friends := make(ClientSet)
  313. // make sure that I have the right caps
  314. hasCaps := true
  315. for _, capab := range capabs {
  316. if !client.capabilities.Has(capab) {
  317. hasCaps = false
  318. break
  319. }
  320. }
  321. if hasCaps {
  322. friends.Add(client)
  323. }
  324. for channel := range client.channels {
  325. channel.membersMutex.RLock()
  326. for member := range channel.members {
  327. // make sure they have all the required caps
  328. hasCaps = true
  329. for _, capab := range capabs {
  330. if !member.capabilities.Has(capab) {
  331. hasCaps = false
  332. break
  333. }
  334. }
  335. if hasCaps {
  336. friends.Add(member)
  337. }
  338. }
  339. channel.membersMutex.RUnlock()
  340. }
  341. return friends
  342. }
  343. // updateNick updates `nick` and `nickCasefolded`.
  344. func (client *Client) updateNick(nick string) {
  345. casefoldedName, err := CasefoldName(nick)
  346. if err != nil {
  347. log.Println(fmt.Sprintf("ERROR: Nick [%s] couldn't be casefolded... this should never happen. Printing stacktrace.", client.nick))
  348. debug.PrintStack()
  349. }
  350. client.stateMutex.Lock()
  351. client.nick = nick
  352. client.nickCasefolded = casefoldedName
  353. client.stateMutex.Unlock()
  354. }
  355. // updateNickMask updates the casefolded nickname and nickmask.
  356. func (client *Client) updateNickMask(nick string) {
  357. // on "", just regenerate the nickmask etc.
  358. // otherwise, update the actual nick
  359. if nick != "" {
  360. client.updateNick(nick)
  361. }
  362. client.stateMutex.Lock()
  363. if len(client.vhost) > 0 {
  364. client.hostname = client.vhost
  365. } else {
  366. client.hostname = client.rawHostname
  367. }
  368. nickMaskString := fmt.Sprintf("%s!%s@%s", client.nick, client.username, client.hostname)
  369. nickMaskCasefolded, err := Casefold(nickMaskString)
  370. if err != nil {
  371. log.Println(fmt.Sprintf("ERROR: Nickmask [%s] couldn't be casefolded... this should never happen. Printing stacktrace.", client.nickMaskString))
  372. debug.PrintStack()
  373. }
  374. client.nickMaskString = nickMaskString
  375. client.nickMaskCasefolded = nickMaskCasefolded
  376. client.stateMutex.Unlock()
  377. }
  378. // AllNickmasks returns all the possible nickmasks for the client.
  379. func (client *Client) AllNickmasks() []string {
  380. var masks []string
  381. var mask string
  382. var err error
  383. if len(client.vhost) > 0 {
  384. mask, err = Casefold(fmt.Sprintf("%s!%s@%s", client.nick, client.username, client.vhost))
  385. if err == nil {
  386. masks = append(masks, mask)
  387. }
  388. }
  389. mask, err = Casefold(fmt.Sprintf("%s!%s@%s", client.nick, client.username, client.rawHostname))
  390. if err == nil {
  391. masks = append(masks, mask)
  392. }
  393. mask2, err := Casefold(fmt.Sprintf("%s!%s@%s", client.nick, client.username, utils.IPString(client.socket.conn.RemoteAddr())))
  394. if err == nil && mask2 != mask {
  395. masks = append(masks, mask2)
  396. }
  397. return masks
  398. }
  399. // SetNickname sets the very first nickname for the client.
  400. func (client *Client) SetNickname(nickname string) error {
  401. if client.HasNick() {
  402. client.server.logger.Error("nick", fmt.Sprintf("%s nickname already set, something is wrong with server consistency", client.nickMaskString))
  403. return ErrNickAlreadySet
  404. }
  405. err := client.server.clients.Add(client, nickname)
  406. if err == nil {
  407. client.updateNick(nickname)
  408. }
  409. return err
  410. }
  411. // ChangeNickname changes the existing nickname of the client.
  412. func (client *Client) ChangeNickname(nickname string) error {
  413. origNickMask := client.nickMaskString
  414. err := client.server.clients.Replace(client.nick, nickname, client)
  415. if err == nil {
  416. client.server.logger.Debug("nick", fmt.Sprintf("%s changed nickname to %s", client.nick, nickname))
  417. client.server.snomasks.Send(sno.LocalNicks, fmt.Sprintf(ircfmt.Unescape("$%s$r changed nickname to %s"), client.nick, nickname))
  418. client.server.whoWas.Append(client)
  419. client.updateNickMask(nickname)
  420. for friend := range client.Friends() {
  421. friend.Send(nil, origNickMask, "NICK", nickname)
  422. }
  423. }
  424. return err
  425. }
  426. // LoggedIntoAccount returns true if this client is logged into an account.
  427. func (client *Client) LoggedIntoAccount() bool {
  428. return client.account != nil && client.account != &NoAccount
  429. }
  430. // RplISupport outputs our ISUPPORT lines to the client. This is used on connection and in VERSION responses.
  431. func (client *Client) RplISupport() {
  432. for _, tokenline := range client.server.getISupport().CachedReply {
  433. // ugly trickery ahead
  434. client.Send(nil, client.server.name, RPL_ISUPPORT, append([]string{client.nick}, tokenline...)...)
  435. }
  436. }
  437. // Quit sends the given quit message to the client (but does not destroy them).
  438. func (client *Client) Quit(message string) {
  439. client.quitMutex.Lock()
  440. defer client.quitMutex.Unlock()
  441. if !client.quitMessageSent {
  442. quitMsg := ircmsg.MakeMessage(nil, client.nickMaskString, "QUIT", message)
  443. quitLine, _ := quitMsg.Line()
  444. errorMsg := ircmsg.MakeMessage(nil, "", "ERROR", message)
  445. errorLine, _ := errorMsg.Line()
  446. client.socket.SetFinalData(quitLine + errorLine)
  447. client.quitMessageSent = true
  448. client.quitMessage = message
  449. }
  450. }
  451. // destroy gets rid of a client, removes them from server lists etc.
  452. func (client *Client) destroy() {
  453. client.destroyMutex.Lock()
  454. defer client.destroyMutex.Unlock()
  455. if client.isDestroyed {
  456. return
  457. }
  458. client.server.logger.Debug("quit", fmt.Sprintf("%s is no longer on the server", client.nick))
  459. // send quit/error message to client if they haven't been sent already
  460. client.Quit("Connection closed")
  461. client.isDestroyed = true
  462. client.server.whoWas.Append(client)
  463. friends := client.Friends()
  464. friends.Remove(client)
  465. // remove from connection limits
  466. ipaddr := client.IP()
  467. // this check shouldn't be required but eh
  468. if ipaddr != nil {
  469. client.server.connectionLimitsMutex.Lock()
  470. client.server.connectionLimits.RemoveClient(ipaddr)
  471. client.server.connectionLimitsMutex.Unlock()
  472. }
  473. // alert monitors
  474. client.server.monitorManager.AlertAbout(client, false)
  475. // clean up monitor state
  476. client.server.monitorManager.RemoveAll(client)
  477. // clean up channels
  478. client.server.channelJoinPartMutex.Lock()
  479. for channel := range client.channels {
  480. channel.Quit(client, &friends)
  481. }
  482. client.server.channelJoinPartMutex.Unlock()
  483. // clean up server
  484. client.server.clients.Remove(client)
  485. // clean up self
  486. if client.idleTimer != nil {
  487. client.idleTimer.Stop()
  488. }
  489. if client.quitTimer != nil {
  490. client.quitTimer.Stop()
  491. }
  492. client.socket.Close()
  493. // send quit messages to friends
  494. for friend := range friends {
  495. if client.quitMessage == "" {
  496. client.quitMessage = "Exited"
  497. }
  498. friend.Send(nil, client.nickMaskString, "QUIT", client.quitMessage)
  499. }
  500. if !client.exitedSnomaskSent {
  501. client.server.snomasks.Send(sno.LocalQuits, fmt.Sprintf(ircfmt.Unescape("%s$r exited the network"), client.nick))
  502. }
  503. }
  504. // SendSplitMsgFromClient sends an IRC PRIVMSG/NOTICE coming from a specific client.
  505. // Adds account-tag to the line as well.
  506. func (client *Client) SendSplitMsgFromClient(msgid string, from *Client, tags *map[string]ircmsg.TagValue, command, target string, message SplitMessage) {
  507. if client.capabilities.Has(caps.MaxLine) {
  508. client.SendFromClient(msgid, from, tags, command, target, message.ForMaxLine)
  509. } else {
  510. for _, str := range message.For512 {
  511. client.SendFromClient(msgid, from, tags, command, target, str)
  512. }
  513. }
  514. }
  515. // SendFromClient sends an IRC line coming from a specific client.
  516. // Adds account-tag to the line as well.
  517. func (client *Client) SendFromClient(msgid string, from *Client, tags *map[string]ircmsg.TagValue, command string, params ...string) error {
  518. // attach account-tag
  519. if client.capabilities.Has(caps.AccountTag) && from.account != &NoAccount {
  520. if tags == nil {
  521. tags = ircmsg.MakeTags("account", from.account.Name)
  522. } else {
  523. (*tags)["account"] = ircmsg.MakeTagValue(from.account.Name)
  524. }
  525. }
  526. // attach message-id
  527. if len(msgid) > 0 && client.capabilities.Has(caps.MessageTags) {
  528. if tags == nil {
  529. tags = ircmsg.MakeTags("draft/msgid", msgid)
  530. } else {
  531. (*tags)["draft/msgid"] = ircmsg.MakeTagValue(msgid)
  532. }
  533. }
  534. return client.Send(tags, from.nickMaskString, command, params...)
  535. }
  536. var (
  537. // these are all the output commands that MUST have their last param be a trailing.
  538. // this is needed because silly clients like to treat trailing as separate from the
  539. // other params in messages.
  540. commandsThatMustUseTrailing = map[string]bool{
  541. "PRIVMSG": true,
  542. "NOTICE": true,
  543. RPL_WHOISCHANNELS: true,
  544. RPL_USERHOST: true,
  545. }
  546. )
  547. // Send sends an IRC line to the client.
  548. func (client *Client) Send(tags *map[string]ircmsg.TagValue, prefix string, command string, params ...string) error {
  549. // attach server-time
  550. if client.capabilities.Has(caps.ServerTime) {
  551. t := time.Now().UTC().Format("2006-01-02T15:04:05.999Z")
  552. if tags == nil {
  553. tags = ircmsg.MakeTags("time", t)
  554. } else {
  555. (*tags)["time"] = ircmsg.MakeTagValue(t)
  556. }
  557. }
  558. // force trailing, if message requires it
  559. var usedTrailingHack bool
  560. if commandsThatMustUseTrailing[strings.ToUpper(command)] && len(params) > 0 {
  561. lastParam := params[len(params)-1]
  562. // to force trailing, we ensure the final param contains a space
  563. if !strings.Contains(lastParam, " ") {
  564. params[len(params)-1] = lastParam + " "
  565. usedTrailingHack = true
  566. }
  567. }
  568. // send out the message
  569. message := ircmsg.MakeMessage(tags, prefix, command, params...)
  570. maxlenTags, maxlenRest := client.maxlens()
  571. line, err := message.LineMaxLen(maxlenTags, maxlenRest)
  572. if err != nil {
  573. // try not to fail quietly - especially useful when running tests, as a note to dig deeper
  574. // log.Println("Error assembling message:")
  575. // spew.Dump(message)
  576. // debug.PrintStack()
  577. message = ircmsg.MakeMessage(nil, client.server.name, ERR_UNKNOWNERROR, "*", "Error assembling message for sending")
  578. line, _ := message.Line()
  579. client.socket.Write(line)
  580. return err
  581. }
  582. // is we used the trailing hack, we need to strip the final space we appended earlier
  583. if usedTrailingHack {
  584. line = line[:len(line)-3] + "\r\n"
  585. }
  586. client.server.logger.Debug("useroutput", client.nick, " ->", strings.TrimRight(line, "\r\n"))
  587. client.socket.Write(line)
  588. return nil
  589. }
  590. // Notice sends the client a notice from the server.
  591. func (client *Client) Notice(text string) {
  592. limit := 400
  593. if client.capabilities.Has(caps.MaxLine) {
  594. limit = client.server.getLimits().LineLen.Rest - 110
  595. }
  596. lines := wordWrap(text, limit)
  597. for _, line := range lines {
  598. client.Send(nil, client.server.name, "NOTICE", client.nick, line)
  599. }
  600. }