Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

histserv.go 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. // Copyright (c) 2020 Shivaram Lingamneni <slingamn@cs.stanford.edu>
  2. // released under the MIT license
  3. package irc
  4. import (
  5. "bufio"
  6. "fmt"
  7. "os"
  8. "strconv"
  9. "time"
  10. "github.com/ergochat/ergo/irc/history"
  11. "github.com/ergochat/ergo/irc/modes"
  12. "github.com/ergochat/ergo/irc/utils"
  13. )
  14. const (
  15. histservHelp = `HistServ provides commands related to history.`
  16. )
  17. func histservEnabled(config *Config) bool {
  18. return config.History.Enabled
  19. }
  20. func historyComplianceEnabled(config *Config) bool {
  21. return config.History.Enabled && config.History.Persistent.Enabled && config.History.Retention.EnableAccountIndexing
  22. }
  23. var (
  24. histservCommands = map[string]*serviceCommand{
  25. "forget": {
  26. handler: histservForgetHandler,
  27. help: `Syntax: $bFORGET <account>$b
  28. FORGET deletes all history messages sent by an account.`,
  29. helpShort: `$bFORGET$b deletes all history messages sent by an account.`,
  30. capabs: []string{"history"},
  31. enabled: histservEnabled,
  32. minParams: 1,
  33. maxParams: 1,
  34. },
  35. "delete": {
  36. handler: histservDeleteHandler,
  37. help: `Syntax: $bDELETE [target] <msgid>$b
  38. DELETE deletes an individual message by its msgid. The target is a channel
  39. name or nickname; depending on the history implementation, this may or may not
  40. be necessary to locate the message.`,
  41. helpShort: `$bDELETE$b deletes an individual message by its msgid.`,
  42. enabled: histservEnabled,
  43. minParams: 1,
  44. maxParams: 2,
  45. },
  46. "export": {
  47. handler: histservExportHandler,
  48. help: `Syntax: $bEXPORT <account>$b
  49. EXPORT exports all messages sent by an account as JSON. This can be used at
  50. the request of the account holder.`,
  51. helpShort: `$bEXPORT$b exports all messages sent by an account as JSON.`,
  52. enabled: historyComplianceEnabled,
  53. capabs: []string{"history"},
  54. minParams: 1,
  55. maxParams: 1,
  56. },
  57. "play": {
  58. handler: histservPlayHandler,
  59. help: `Syntax: $bPLAY <target> [limit]$b
  60. PLAY plays back history messages, rendering them into direct messages from
  61. HistServ. 'target' is a channel name or nickname to query, and 'limit'
  62. is a message count or a time duration. Note that message playback may be
  63. incomplete or degraded, relative to direct playback from /HISTORY or
  64. CHATHISTORY.`,
  65. helpShort: `$bPLAY$b plays back history messages.`,
  66. enabled: histservEnabled,
  67. minParams: 1,
  68. maxParams: 2,
  69. },
  70. }
  71. )
  72. func histservForgetHandler(service *ircService, server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  73. accountName := server.accounts.AccountToAccountName(params[0])
  74. if accountName == "" {
  75. service.Notice(rb, client.t("Could not look up account name, proceeding anyway"))
  76. accountName = params[0]
  77. }
  78. server.ForgetHistory(accountName)
  79. service.Notice(rb, fmt.Sprintf(client.t("Enqueued account %s for message deletion"), accountName))
  80. }
  81. func histservDeleteHandler(service *ircService, server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  82. var target, msgid string
  83. if len(params) == 1 {
  84. msgid = params[0]
  85. } else {
  86. target, msgid = params[0], params[1]
  87. }
  88. // operators can delete; if individual delete is allowed, a chanop or
  89. // the message author can delete
  90. accountName := "*"
  91. isChanop := false
  92. isOper := client.HasRoleCapabs("history")
  93. if !isOper {
  94. if server.Config().History.Retention.AllowIndividualDelete {
  95. channel := server.channels.Get(target)
  96. if channel != nil && channel.ClientIsAtLeast(client, modes.Operator) {
  97. isChanop = true
  98. } else {
  99. accountName = client.AccountName()
  100. }
  101. }
  102. }
  103. if !isOper && !isChanop && accountName == "*" {
  104. service.Notice(rb, client.t("Insufficient privileges"))
  105. return
  106. }
  107. err := server.DeleteMessage(target, msgid, accountName)
  108. if err == nil {
  109. service.Notice(rb, client.t("Successfully deleted message"))
  110. } else {
  111. if isOper {
  112. service.Notice(rb, fmt.Sprintf(client.t("Error deleting message: %v"), err))
  113. } else {
  114. service.Notice(rb, client.t("Could not delete message"))
  115. }
  116. }
  117. }
  118. func histservExportHandler(service *ircService, server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  119. cfAccount, err := CasefoldName(params[0])
  120. if err != nil {
  121. service.Notice(rb, client.t("Invalid account name"))
  122. return
  123. }
  124. config := server.Config()
  125. // don't include the account name in the filename because of escaping concerns
  126. filename := fmt.Sprintf("%s-%s.json", utils.GenerateSecretToken(), time.Now().UTC().Format(IRCv3TimestampFormat))
  127. pathname := config.getOutputPath(filename)
  128. outfile, err := os.Create(pathname)
  129. if err != nil {
  130. service.Notice(rb, fmt.Sprintf(client.t("Error opening export file: %v"), err))
  131. } else {
  132. service.Notice(rb, fmt.Sprintf(client.t("Started exporting data for account %[1]s to file %[2]s"), cfAccount, filename))
  133. }
  134. go histservExportAndNotify(service, server, cfAccount, outfile, filename, client.Nick())
  135. }
  136. func histservExportAndNotify(service *ircService, server *Server, cfAccount string, outfile *os.File, filename, alertNick string) {
  137. defer server.HandlePanic()
  138. defer outfile.Close()
  139. writer := bufio.NewWriter(outfile)
  140. defer writer.Flush()
  141. server.historyDB.Export(cfAccount, writer)
  142. client := server.clients.Get(alertNick)
  143. if client != nil && client.HasRoleCapabs("history") {
  144. client.Send(nil, service.prefix, "NOTICE", client.Nick(), fmt.Sprintf(client.t("Data export for %[1]s completed and written to %[2]s"), cfAccount, filename))
  145. }
  146. }
  147. func histservPlayHandler(service *ircService, server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  148. items, _, err := easySelectHistory(server, client, params)
  149. if err != nil {
  150. service.Notice(rb, client.t("Could not retrieve history"))
  151. return
  152. }
  153. playMessage := func(timestamp time.Time, nick, message string) {
  154. service.Notice(rb, fmt.Sprintf("%s <%s> %s", timestamp.Format("15:04:05"), NUHToNick(nick), message))
  155. }
  156. for _, item := range items {
  157. // TODO: support a few more of these, maybe JOIN/PART/QUIT
  158. if item.Type != history.Privmsg && item.Type != history.Notice {
  159. continue
  160. }
  161. if len(item.Message.Split) == 0 {
  162. playMessage(item.Message.Time, item.Nick, item.Message.Message)
  163. } else {
  164. for _, pair := range item.Message.Split {
  165. playMessage(item.Message.Time, item.Nick, pair.Message)
  166. }
  167. }
  168. }
  169. service.Notice(rb, client.t("End of history playback"))
  170. }
  171. // handles parameter parsing and history queries for /HISTORY and /HISTSERV PLAY
  172. func easySelectHistory(server *Server, client *Client, params []string) (items []history.Item, channel *Channel, err error) {
  173. channel, sequence, err := server.GetHistorySequence(nil, client, params[0])
  174. if sequence == nil || err != nil {
  175. return nil, nil, errNoSuchChannel
  176. }
  177. var duration time.Duration
  178. maxChathistoryLimit := server.Config().History.ChathistoryMax
  179. limit := 100
  180. if maxChathistoryLimit < limit {
  181. limit = maxChathistoryLimit
  182. }
  183. if len(params) > 1 {
  184. providedLimit, err := strconv.Atoi(params[1])
  185. if err == nil && providedLimit != 0 {
  186. limit = providedLimit
  187. if maxChathistoryLimit < limit {
  188. limit = maxChathistoryLimit
  189. }
  190. } else if err != nil {
  191. duration, err = time.ParseDuration(params[1])
  192. if err == nil {
  193. limit = maxChathistoryLimit
  194. }
  195. }
  196. }
  197. if duration == 0 {
  198. items, err = sequence.Between(history.Selector{}, history.Selector{}, limit)
  199. } else {
  200. now := time.Now().UTC()
  201. start := history.Selector{Time: now}
  202. end := history.Selector{Time: now.Add(-duration)}
  203. items, err = sequence.Between(start, end, limit)
  204. }
  205. return
  206. }