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.

431 lines
12 KiB

8 years ago
8 years ago
  1. package clients
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "net/http"
  6. "reflect"
  7. "strings"
  8. "sync"
  9. "github.com/matrix-org/go-neb/api"
  10. "github.com/matrix-org/go-neb/database"
  11. "github.com/matrix-org/go-neb/matrix"
  12. "github.com/matrix-org/go-neb/metrics"
  13. "github.com/matrix-org/go-neb/types"
  14. shellwords "github.com/mattn/go-shellwords"
  15. log "github.com/sirupsen/logrus"
  16. "maunium.net/go/mautrix"
  17. "maunium.net/go/mautrix/event"
  18. mevt "maunium.net/go/mautrix/event"
  19. "maunium.net/go/mautrix/id"
  20. )
  21. // A Clients is a collection of clients used for bot services.
  22. type Clients struct {
  23. db database.Storer
  24. httpClient *http.Client
  25. dbMutex sync.Mutex
  26. mapMutex sync.Mutex
  27. clients map[id.UserID]BotClient
  28. }
  29. // New makes a new collection of matrix clients
  30. func New(db database.Storer, cli *http.Client) *Clients {
  31. clients := &Clients{
  32. db: db,
  33. httpClient: cli,
  34. clients: make(map[id.UserID]BotClient), // user_id => BotClient
  35. }
  36. return clients
  37. }
  38. // Client gets a client for the userID
  39. func (c *Clients) Client(userID id.UserID) (*BotClient, error) {
  40. entry := c.getClient(userID)
  41. if entry.Client != nil {
  42. return &entry, nil
  43. }
  44. entry, err := c.loadClientFromDB(userID)
  45. return &entry, err
  46. }
  47. // Update updates the config for a matrix client
  48. func (c *Clients) Update(config api.ClientConfig) (api.ClientConfig, error) {
  49. _, old, err := c.updateClientInDB(config)
  50. return old.config, err
  51. }
  52. // Start listening on client /sync streams
  53. func (c *Clients) Start() error {
  54. configs, err := c.db.LoadMatrixClientConfigs()
  55. if err != nil {
  56. return err
  57. }
  58. for _, cfg := range configs {
  59. if cfg.Sync {
  60. if _, err := c.Client(cfg.UserID); err != nil {
  61. return err
  62. }
  63. }
  64. }
  65. return nil
  66. }
  67. func (c *Clients) getClient(userID id.UserID) BotClient {
  68. c.mapMutex.Lock()
  69. defer c.mapMutex.Unlock()
  70. return c.clients[userID]
  71. }
  72. func (c *Clients) setClient(client BotClient) {
  73. c.mapMutex.Lock()
  74. defer c.mapMutex.Unlock()
  75. c.clients[client.config.UserID] = client
  76. }
  77. func (c *Clients) loadClientFromDB(userID id.UserID) (entry BotClient, err error) {
  78. c.dbMutex.Lock()
  79. defer c.dbMutex.Unlock()
  80. entry = c.getClient(userID)
  81. if entry.Client != nil {
  82. return
  83. }
  84. if entry.config, err = c.db.LoadMatrixClientConfig(userID); err != nil {
  85. if err == sql.ErrNoRows {
  86. err = fmt.Errorf("client with user ID %s does not exist", userID)
  87. }
  88. return
  89. }
  90. if err = c.initClient(&entry); err != nil {
  91. return
  92. }
  93. c.setClient(entry)
  94. return
  95. }
  96. func (c *Clients) updateClientInDB(newConfig api.ClientConfig) (new, old BotClient, err error) {
  97. c.dbMutex.Lock()
  98. defer c.dbMutex.Unlock()
  99. old = c.getClient(newConfig.UserID)
  100. if old.Client != nil && reflect.DeepEqual(old.config, newConfig) {
  101. // Already have a client with that config.
  102. new = old
  103. return
  104. }
  105. new.config = newConfig
  106. if err = c.initClient(&new); err != nil {
  107. return
  108. }
  109. // set the new display name if they differ
  110. if old.config.DisplayName != new.config.DisplayName {
  111. if err := new.SetDisplayName(new.config.DisplayName); err != nil {
  112. // whine about it but don't stop: this isn't fatal.
  113. log.WithFields(log.Fields{
  114. log.ErrorKey: err,
  115. "displayname": new.config.DisplayName,
  116. "user_id": new.config.UserID,
  117. }).Error("Failed to set display name")
  118. }
  119. }
  120. if old.config, err = c.db.StoreMatrixClientConfig(new.config); err != nil {
  121. new.StopSync()
  122. return
  123. }
  124. if old.Client != nil {
  125. old.Client.StopSync()
  126. return
  127. }
  128. c.setClient(new)
  129. return
  130. }
  131. func (c *Clients) onMessageEvent(botClient *BotClient, event *mevt.Event) {
  132. services, err := c.db.LoadServicesForUser(botClient.UserID)
  133. if err != nil {
  134. log.WithFields(log.Fields{
  135. log.ErrorKey: err,
  136. "room_id": event.RoomID,
  137. "service_user_id": botClient.UserID,
  138. }).Warn("Error loading services")
  139. }
  140. message := event.Content.AsMessage()
  141. body := message.Body
  142. if body == "" {
  143. return
  144. }
  145. // filter m.notice to prevent loops
  146. if message.MsgType == mevt.MsgNotice {
  147. return
  148. }
  149. // replace all smart quotes with their normal counterparts so shellwords can parse it
  150. body = strings.Replace(body, ``, `'`, -1)
  151. body = strings.Replace(body, ``, `'`, -1)
  152. body = strings.Replace(body, ``, `"`, -1)
  153. body = strings.Replace(body, ``, `"`, -1)
  154. var responses []interface{}
  155. for _, service := range services {
  156. if body[0] == '!' { // message is a command
  157. args, err := shellwords.Parse(body[1:])
  158. if err != nil {
  159. args = strings.Split(body[1:], " ")
  160. }
  161. if response := runCommandForService(service.Commands(botClient), event, args); response != nil {
  162. responses = append(responses, response)
  163. }
  164. } else { // message isn't a command, it might need expanding
  165. expansions := runExpansionsForService(service.Expansions(botClient), event, body)
  166. responses = append(responses, expansions...)
  167. }
  168. }
  169. for _, content := range responses {
  170. if _, err := botClient.SendMessageEvent(event.RoomID, mevt.EventMessage, content); err != nil {
  171. log.WithFields(log.Fields{
  172. "room_id": event.RoomID,
  173. "content": content,
  174. "sender": event.Sender,
  175. }).WithError(err).Error("Failed to send command response")
  176. }
  177. }
  178. }
  179. // runCommandForService runs a single command read from a matrix event. Runs
  180. // the matching command with the longest path. Returns the JSON encodable
  181. // content of a single matrix message event to use as a response or nil if no
  182. // response is appropriate.
  183. func runCommandForService(cmds []types.Command, event *mevt.Event, arguments []string) interface{} {
  184. var bestMatch *types.Command
  185. for i, command := range cmds {
  186. matches := command.Matches(arguments)
  187. betterMatch := bestMatch == nil || len(bestMatch.Path) < len(command.Path)
  188. if matches && betterMatch {
  189. bestMatch = &cmds[i]
  190. }
  191. }
  192. if bestMatch == nil {
  193. return nil
  194. }
  195. cmdArgs := arguments[len(bestMatch.Path):]
  196. log.WithFields(log.Fields{
  197. "room_id": event.RoomID,
  198. "user_id": event.Sender,
  199. "command": bestMatch.Path,
  200. }).Info("Executing command")
  201. content, err := bestMatch.Command(event.RoomID, event.Sender, cmdArgs)
  202. if err != nil {
  203. if content != nil {
  204. log.WithFields(log.Fields{
  205. log.ErrorKey: err,
  206. "room_id": event.RoomID,
  207. "user_id": event.Sender,
  208. "command": bestMatch.Path,
  209. "args": cmdArgs,
  210. }).Warn("Command returned both error and content.")
  211. }
  212. metrics.IncrementCommand(bestMatch.Path[0], metrics.StatusFailure)
  213. content = mevt.MessageEventContent{
  214. MsgType: mevt.MsgNotice,
  215. Body: err.Error(),
  216. }
  217. } else {
  218. metrics.IncrementCommand(bestMatch.Path[0], metrics.StatusSuccess)
  219. }
  220. return content
  221. }
  222. // run the expansions for a matrix event.
  223. func runExpansionsForService(expans []types.Expansion, event *mevt.Event, body string) []interface{} {
  224. var responses []interface{}
  225. for _, expansion := range expans {
  226. matches := map[string]bool{}
  227. for _, matchingGroups := range expansion.Regexp.FindAllStringSubmatch(body, -1) {
  228. matchingText := matchingGroups[0] // first element is always the complete match
  229. if matches[matchingText] {
  230. // Only expand the first occurance of a matching string
  231. continue
  232. }
  233. matches[matchingText] = true
  234. if response := expansion.Expand(event.RoomID, event.Sender, matchingGroups); response != nil {
  235. responses = append(responses, response)
  236. }
  237. }
  238. }
  239. return responses
  240. }
  241. func (c *Clients) onBotOptionsEvent(client *mautrix.Client, event *mevt.Event) {
  242. // see if these options are for us. The state key is the user ID with a leading _
  243. // to get around restrictions in the HS about having user IDs as state keys.
  244. if event.StateKey == nil {
  245. return
  246. }
  247. targetUserID := id.UserID(strings.TrimPrefix(*event.StateKey, "_"))
  248. if targetUserID != client.UserID {
  249. return
  250. }
  251. // these options fully clobber what was there previously.
  252. opts := types.BotOptions{
  253. UserID: client.UserID,
  254. RoomID: event.RoomID,
  255. SetByUserID: event.Sender,
  256. Options: event.Content.Raw,
  257. }
  258. if _, err := c.db.StoreBotOptions(opts); err != nil {
  259. log.WithFields(log.Fields{
  260. log.ErrorKey: err,
  261. "room_id": event.RoomID,
  262. "bot_user_id": client.UserID,
  263. "set_by_user_id": event.Sender,
  264. }).Error("Failed to persist bot options")
  265. }
  266. }
  267. func (c *Clients) onRoomMemberEvent(client *mautrix.Client, event *mevt.Event) {
  268. if event.StateKey == nil || *event.StateKey != client.UserID.String() {
  269. return // not our member event
  270. }
  271. membership := event.Content.AsMember().Membership
  272. if membership == "invite" {
  273. logger := log.WithFields(log.Fields{
  274. "room_id": event.RoomID,
  275. "service_user_id": client.UserID,
  276. "inviter": event.Sender,
  277. })
  278. logger.Print("Accepting invite from user")
  279. content := struct {
  280. Inviter id.UserID `json:"inviter"`
  281. }{event.Sender}
  282. if _, err := client.JoinRoom(event.RoomID.String(), "", content); err != nil {
  283. logger.WithError(err).Print("Failed to join room")
  284. } else {
  285. logger.Print("Joined room")
  286. }
  287. }
  288. }
  289. func (c *Clients) initClient(botClient *BotClient) error {
  290. config := botClient.config
  291. client, err := mautrix.NewClient(config.HomeserverURL, config.UserID, config.AccessToken)
  292. if err != nil {
  293. return err
  294. }
  295. client.Client = c.httpClient
  296. client.DeviceID = config.DeviceID
  297. if client.DeviceID == "" {
  298. log.Warn("Device ID is not set which will result in E2E encryption/decryption not working")
  299. }
  300. botClient.Client = client
  301. botClient.verificationSAS = &sync.Map{}
  302. syncer := client.Syncer.(*mautrix.DefaultSyncer)
  303. syncer.ParseErrorHandler = func(evt *event.Event, err error) bool {
  304. // Events of type m.room.bot.options will be flagged as errors as this isn't an event type
  305. // recognised by the Syncer, but we need to process them so the bot can accept options
  306. // through this event (see onBotOptionsEvent)
  307. if evt.Type.Type == "m.room.bot.options" {
  308. return true
  309. }
  310. return false
  311. }
  312. nebStore := &matrix.NEBStore{
  313. InMemoryStore: *mautrix.NewInMemoryStore(),
  314. Database: c.db,
  315. ClientConfig: config,
  316. }
  317. client.Store = nebStore
  318. // TODO: Check that the access token is valid for the userID by peforming
  319. // a request against the server.
  320. if err = botClient.InitOlmMachine(client, nebStore); err != nil {
  321. return err
  322. }
  323. // Register sync callback for maintaining the state store and Olm machine state
  324. botClient.Register(syncer)
  325. syncer.OnEventType(mevt.EventMessage, func(_ mautrix.EventSource, event *mevt.Event) {
  326. c.onMessageEvent(botClient, event)
  327. })
  328. syncer.OnEventType(mevt.Type{Type: "m.room.bot.options", Class: mevt.StateEventType}, func(_ mautrix.EventSource, event *mevt.Event) {
  329. c.onBotOptionsEvent(botClient.Client, event)
  330. })
  331. if config.AutoJoinRooms {
  332. syncer.OnEventType(mevt.StateMember, func(_ mautrix.EventSource, event *mevt.Event) {
  333. c.onRoomMemberEvent(client, event)
  334. })
  335. }
  336. // When receiving an encrypted event, attempt to decrypt it using the BotClient's capabilities.
  337. // If successfully decrypted propagate the decrypted event to the clients.
  338. syncer.OnEventType(mevt.EventEncrypted, func(source mautrix.EventSource, evt *mevt.Event) {
  339. encContent := evt.Content.AsEncrypted()
  340. decrypted, err := botClient.DecryptMegolmEvent(evt)
  341. if err != nil {
  342. log.WithFields(log.Fields{
  343. "user_id": config.UserID,
  344. "device_id": encContent.DeviceID,
  345. "session_id": encContent.SessionID,
  346. "sender_key": encContent.SenderKey,
  347. }).WithError(err).Error("Failed to decrypt message")
  348. } else {
  349. if decrypted.Type == mevt.EventMessage {
  350. c.onMessageEvent(botClient, decrypted)
  351. }
  352. log.WithFields(log.Fields{
  353. "type": evt.Type,
  354. "sender": evt.Sender,
  355. "room_id": evt.RoomID,
  356. "state_key": evt.StateKey,
  357. }).Trace("Decrypted event successfully")
  358. }
  359. })
  360. // Ignore events before neb's join event.
  361. eventIgnorer := mautrix.OldEventIgnorer{UserID: config.UserID}
  362. eventIgnorer.Register(syncer)
  363. log.WithFields(log.Fields{
  364. "user_id": config.UserID,
  365. "device_id": config.DeviceID,
  366. "sync": config.Sync,
  367. "auto_join_rooms": config.AutoJoinRooms,
  368. "since": nebStore.LoadNextBatch(config.UserID),
  369. }).Info("Created new client")
  370. if config.Sync {
  371. go botClient.Sync()
  372. }
  373. return nil
  374. }