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.

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. "runtime/debug"
  9. "strconv"
  10. "strings"
  11. "time"
  12. "github.com/oragono/oragono/irc/history"
  13. "github.com/oragono/oragono/irc/utils"
  14. )
  15. const (
  16. histservHelp = `HistServ provides commands related to history.`
  17. )
  18. func histservEnabled(config *Config) bool {
  19. return config.History.Enabled
  20. }
  21. func historyComplianceEnabled(config *Config) bool {
  22. return config.History.Enabled && config.History.Persistent.Enabled && config.History.Retention.EnableAccountIndexing
  23. }
  24. var (
  25. histservCommands = map[string]*serviceCommand{
  26. "forget": {
  27. handler: histservForgetHandler,
  28. help: `Syntax: $bFORGET <account>$b
  29. FORGET deletes all history messages sent by an account.`,
  30. helpShort: `$bFORGET$b deletes all history messages sent by an account.`,
  31. capabs: []string{"history"},
  32. enabled: histservEnabled,
  33. minParams: 1,
  34. maxParams: 1,
  35. },
  36. "delete": {
  37. handler: histservDeleteHandler,
  38. help: `Syntax: $bDELETE [target] <msgid>$b
  39. DELETE deletes an individual message by its msgid. The target is a channel
  40. name or nickname; depending on the history implementation, this may or may not
  41. be necessary to locate the message.`,
  42. helpShort: `$bDELETE$b deletes an individual message by its msgid.`,
  43. enabled: histservEnabled,
  44. minParams: 1,
  45. maxParams: 2,
  46. },
  47. "export": {
  48. handler: histservExportHandler,
  49. help: `Syntax: $bEXPORT <account>$b
  50. EXPORT exports all messages sent by an account as JSON. This can be used at
  51. the request of the account holder.`,
  52. helpShort: `$bEXPORT$b exports all messages sent by an account as JSON.`,
  53. enabled: historyComplianceEnabled,
  54. capabs: []string{"history"},
  55. minParams: 1,
  56. maxParams: 1,
  57. },
  58. "play": {
  59. handler: histservPlayHandler,
  60. help: `Syntax: $bPLAY <target> [limit]$b
  61. PLAY plays back history messages, rendering them into direct messages from
  62. HistServ. 'target' is a channel name (or 'me' for direct messages), and 'limit'
  63. is a message count or a time duration. Note that message playback may be
  64. incomplete or degraded, relative to direct playback from /HISTORY or
  65. CHATHISTORY.`,
  66. helpShort: `$bPLAY$b plays back history messages.`,
  67. enabled: histservEnabled,
  68. minParams: 1,
  69. maxParams: 2,
  70. },
  71. }
  72. )
  73. func histservForgetHandler(service *ircService, server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  74. accountName := server.accounts.AccountToAccountName(params[0])
  75. if accountName == "" {
  76. service.Notice(rb, client.t("Could not look up account name, proceeding anyway"))
  77. accountName = params[0]
  78. }
  79. server.ForgetHistory(accountName)
  80. service.Notice(rb, fmt.Sprintf(client.t("Enqueued account %s for message deletion"), accountName))
  81. }
  82. func histservDeleteHandler(service *ircService, server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  83. var target, msgid string
  84. if len(params) == 1 {
  85. msgid = params[0]
  86. } else {
  87. target, msgid = params[0], params[1]
  88. }
  89. accountName := "*"
  90. hasPrivs := client.HasRoleCapabs("history")
  91. if !hasPrivs {
  92. accountName = client.AccountName()
  93. if !(server.Config().History.Retention.AllowIndividualDelete && accountName != "*") {
  94. service.Notice(rb, client.t("Insufficient privileges"))
  95. return
  96. }
  97. }
  98. err := server.DeleteMessage(target, msgid, accountName)
  99. if err == nil {
  100. service.Notice(rb, client.t("Successfully deleted message"))
  101. } else {
  102. if hasPrivs {
  103. service.Notice(rb, fmt.Sprintf(client.t("Error deleting message: %v"), err))
  104. } else {
  105. service.Notice(rb, client.t("Could not delete message"))
  106. }
  107. }
  108. }
  109. func histservExportHandler(service *ircService, server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  110. cfAccount, err := CasefoldName(params[0])
  111. if err != nil {
  112. service.Notice(rb, client.t("Invalid account name"))
  113. return
  114. }
  115. config := server.Config()
  116. // don't include the account name in the filename because of escaping concerns
  117. filename := fmt.Sprintf("%s-%s.json", utils.GenerateSecretToken(), time.Now().UTC().Format(IRCv3TimestampFormat))
  118. pathname := config.getOutputPath(filename)
  119. outfile, err := os.Create(pathname)
  120. if err != nil {
  121. service.Notice(rb, fmt.Sprintf(client.t("Error opening export file: %v"), err))
  122. } else {
  123. service.Notice(rb, fmt.Sprintf(client.t("Started exporting data for account %[1]s to file %[2]s"), cfAccount, filename))
  124. }
  125. go histservExportAndNotify(service, server, cfAccount, outfile, filename, client.Nick())
  126. }
  127. func histservExportAndNotify(service *ircService, server *Server, cfAccount string, outfile *os.File, filename, alertNick string) {
  128. defer func() {
  129. if r := recover(); r != nil {
  130. server.logger.Error("history",
  131. fmt.Sprintf("Panic in history export routine: %v\n%s", r, debug.Stack()))
  132. }
  133. }()
  134. defer outfile.Close()
  135. writer := bufio.NewWriter(outfile)
  136. defer writer.Flush()
  137. server.historyDB.Export(cfAccount, writer)
  138. client := server.clients.Get(alertNick)
  139. if client != nil && client.HasRoleCapabs("history") {
  140. 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))
  141. }
  142. }
  143. func histservPlayHandler(service *ircService, server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
  144. items, _, err := easySelectHistory(server, client, params)
  145. if err != nil {
  146. service.Notice(rb, client.t("Could not retrieve history"))
  147. return
  148. }
  149. playMessage := func(timestamp time.Time, nick, message string) {
  150. service.Notice(rb, fmt.Sprintf("%s <%s> %s", timestamp.Format("15:04:05"), NUHToNick(nick), message))
  151. }
  152. for _, item := range items {
  153. // TODO: support a few more of these, maybe JOIN/PART/QUIT
  154. if item.Type != history.Privmsg && item.Type != history.Notice {
  155. continue
  156. }
  157. if len(item.Message.Split) == 0 {
  158. playMessage(item.Message.Time, item.Nick, item.Message.Message)
  159. } else {
  160. for _, pair := range item.Message.Split {
  161. playMessage(item.Message.Time, item.Nick, pair.Message)
  162. }
  163. }
  164. }
  165. service.Notice(rb, client.t("End of history playback"))
  166. }
  167. // handles parameter parsing and history queries for /HISTORY and /HISTSERV PLAY
  168. func easySelectHistory(server *Server, client *Client, params []string) (items []history.Item, channel *Channel, err error) {
  169. target := params[0]
  170. if strings.ToLower(target) == "me" {
  171. target = "*"
  172. }
  173. channel, sequence, err := server.GetHistorySequence(nil, client, target)
  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. }