commands.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. package bridge
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/alecthomas/kong"
  6. "maunium.net/go/mautrix/appservice"
  7. "maunium.net/go/mautrix/event"
  8. "maunium.net/go/mautrix/format"
  9. "maunium.net/go/mautrix/id"
  10. "gitlab.com/beeper/discord/consts"
  11. "gitlab.com/beeper/discord/remoteauth"
  12. "gitlab.com/beeper/discord/version"
  13. )
  14. type globals struct {
  15. context *kong.Context
  16. bridge *Bridge
  17. bot *appservice.IntentAPI
  18. portal *Portal
  19. handler *commandHandler
  20. roomID id.RoomID
  21. user *User
  22. replyTo id.EventID
  23. }
  24. func (g *globals) reply(msg string) {
  25. content := format.RenderMarkdown(msg, true, false)
  26. content.MsgType = event.MsgNotice
  27. intent := g.bot
  28. if g.portal != nil && g.portal.IsPrivateChat() {
  29. intent = g.portal.MainIntent()
  30. }
  31. _, err := intent.SendMessageEvent(g.roomID, event.EventMessage, content)
  32. if err != nil {
  33. g.handler.log.Warnfln("Failed to reply to command from %q: %v", g.user.MXID, err)
  34. }
  35. }
  36. type commands struct {
  37. globals
  38. Disconnect disconnectCmd `kong:"cmd,help='Disconnect from Discord'"`
  39. Help helpCmd `kong:"cmd,help='Displays this message.'"`
  40. Login loginCmd `kong:"cmd,help='Log in to Discord.'"`
  41. Logout logoutCmd `kong:"cmd,help='Log out of Discord.'"`
  42. Reconnect reconnectCmd `kong:"cmd,help='Reconnect to Discord'"`
  43. Version versionCmd `kong:"cmd,help='Displays the version of the bridge.'"`
  44. Guilds guildsCmd `kong:"cmd,help='Guild bridging management.'"`
  45. LoginMatrix loginMatrixCmd `kong:"cmd,help='Replace the puppet for your Discord account with your real Matrix account.'"`
  46. LogoutMatrix logoutMatrixCmd `kong:"cmd,help='Switch the puppet for your Discord account back to the default one.'"`
  47. PingMatrix pingMatrixCmd `kong:"cmd,help='check if your double puppet is working properly'"`
  48. }
  49. ///////////////////////////////////////////////////////////////////////////////
  50. // Help Command
  51. ///////////////////////////////////////////////////////////////////////////////
  52. type helpCmd struct {
  53. Command []string `kong:"arg,optional,help='The command to get help on.'"`
  54. }
  55. func (c *helpCmd) Run(g *globals) error {
  56. ctx, err := kong.Trace(g.context.Kong, c.Command)
  57. if err != nil {
  58. return err
  59. }
  60. if ctx.Error != nil {
  61. return err
  62. }
  63. err = ctx.PrintUsage(true)
  64. if err != nil {
  65. return err
  66. }
  67. fmt.Fprintln(g.context.Stdout)
  68. return nil
  69. }
  70. ///////////////////////////////////////////////////////////////////////////////
  71. // Version Command
  72. ///////////////////////////////////////////////////////////////////////////////
  73. type versionCmd struct{}
  74. func (c *versionCmd) Run(g *globals) error {
  75. fmt.Fprintln(g.context.Stdout, consts.Name, version.String)
  76. return nil
  77. }
  78. ///////////////////////////////////////////////////////////////////////////////
  79. // Login Command
  80. ///////////////////////////////////////////////////////////////////////////////
  81. type loginCmd struct{}
  82. func (l *loginCmd) Run(g *globals) error {
  83. if g.user.LoggedIn() {
  84. fmt.Fprintf(g.context.Stdout, "You are already logged in")
  85. return fmt.Errorf("user already logged in")
  86. }
  87. client, err := remoteauth.New()
  88. if err != nil {
  89. return err
  90. }
  91. qrChan := make(chan string)
  92. doneChan := make(chan struct{})
  93. go func() {
  94. code := <-qrChan
  95. _, err := g.user.sendQRCode(g.bot, g.roomID, code)
  96. if err != nil {
  97. fmt.Fprintln(g.context.Stdout, "Failed to generate the qrcode")
  98. return
  99. }
  100. }()
  101. ctx := context.Background()
  102. if err := client.Dial(ctx, qrChan, doneChan); err != nil {
  103. close(qrChan)
  104. close(doneChan)
  105. return err
  106. }
  107. <-doneChan
  108. user, err := client.Result()
  109. if err != nil {
  110. fmt.Fprintln(g.context.Stdout, "Failed to log in")
  111. return err
  112. }
  113. if err := g.user.Login(user.Token); err != nil {
  114. fmt.Fprintln(g.context.Stdout, "Failed to login", err)
  115. return err
  116. }
  117. g.user.Lock()
  118. g.user.ID = user.UserID
  119. g.user.Update()
  120. g.user.Unlock()
  121. fmt.Fprintln(g.context.Stdout, "Successfully logged in")
  122. return nil
  123. }
  124. ///////////////////////////////////////////////////////////////////////////////
  125. // Logout Command
  126. ///////////////////////////////////////////////////////////////////////////////
  127. type logoutCmd struct{}
  128. func (l *logoutCmd) Run(g *globals) error {
  129. if !g.user.LoggedIn() {
  130. fmt.Fprintln(g.context.Stdout, "You are not logged in")
  131. return fmt.Errorf("user is not logged in")
  132. }
  133. err := g.user.Logout()
  134. if err != nil {
  135. fmt.Fprintln(g.context.Stdout, "Failed to log out")
  136. return err
  137. }
  138. fmt.Fprintln(g.context.Stdout, "Successfully logged out")
  139. return nil
  140. }
  141. ///////////////////////////////////////////////////////////////////////////////
  142. // Disconnect Command
  143. ///////////////////////////////////////////////////////////////////////////////
  144. type disconnectCmd struct{}
  145. func (d *disconnectCmd) Run(g *globals) error {
  146. if !g.user.Connected() {
  147. fmt.Fprintln(g.context.Stdout, "You are not connected")
  148. return fmt.Errorf("user is not connected")
  149. }
  150. if err := g.user.Disconnect(); err != nil {
  151. fmt.Fprintln(g.context.Stdout, "Failed to disconnect")
  152. return err
  153. }
  154. fmt.Fprintln(g.context.Stdout, "Successfully disconnected")
  155. return nil
  156. }
  157. ///////////////////////////////////////////////////////////////////////////////
  158. // Reconnect Command
  159. ///////////////////////////////////////////////////////////////////////////////
  160. type reconnectCmd struct{}
  161. func (r *reconnectCmd) Run(g *globals) error {
  162. if g.user.Connected() {
  163. fmt.Fprintln(g.context.Stdout, "You are already connected")
  164. return fmt.Errorf("user is already connected")
  165. }
  166. if err := g.user.Connect(); err != nil {
  167. fmt.Fprintln(g.context.Stdout, "Failed to connect")
  168. return err
  169. }
  170. fmt.Fprintln(g.context.Stdout, "Successfully connected")
  171. return nil
  172. }
  173. ///////////////////////////////////////////////////////////////////////////////
  174. // LoginMatrix Command
  175. ///////////////////////////////////////////////////////////////////////////////
  176. type loginMatrixCmd struct {
  177. AccessToken string `kong:"arg,help='The shared secret to use the bridge'"`
  178. }
  179. func (m *loginMatrixCmd) Run(g *globals) error {
  180. puppet := g.bridge.GetPuppetByID(g.user.ID)
  181. err := puppet.SwitchCustomMXID(m.AccessToken, g.user.MXID)
  182. if err != nil {
  183. fmt.Fprintf(g.context.Stdout, "Failed to switch puppet: %v", err)
  184. return err
  185. }
  186. fmt.Fprintf(g.context.Stdout, "Successfully switched puppet")
  187. return nil
  188. }
  189. ///////////////////////////////////////////////////////////////////////////////
  190. // LogoutMatrix Command
  191. ///////////////////////////////////////////////////////////////////////////////
  192. type logoutMatrixCmd struct{}
  193. func (m *logoutMatrixCmd) Run(g *globals) error {
  194. return nil
  195. }
  196. ///////////////////////////////////////////////////////////////////////////////
  197. // PingMatrix Command
  198. ///////////////////////////////////////////////////////////////////////////////
  199. type pingMatrixCmd struct{}
  200. func (m *pingMatrixCmd) Run(g *globals) error {
  201. puppet := g.bridge.GetPuppetByCustomMXID(g.user.MXID)
  202. if puppet == nil || puppet.CustomIntent() == nil {
  203. fmt.Fprintf(g.context.Stdout, "You have not changed your Discord account's Matrix puppet.")
  204. return fmt.Errorf("double puppet not configured")
  205. }
  206. resp, err := puppet.CustomIntent().Whoami()
  207. if err != nil {
  208. fmt.Fprintf(g.context.Stdout, "Failed to validate Matrix login: %v", err)
  209. return err
  210. }
  211. fmt.Fprintf(g.context.Stdout, "Confirmed valid access token for %s / %s", resp.UserID, resp.DeviceID)
  212. return nil
  213. }
  214. ///////////////////////////////////////////////////////////////////////////////
  215. // Guilds Commands
  216. ///////////////////////////////////////////////////////////////////////////////
  217. type guildsCmd struct {
  218. Status guildStatusCmd `kong:"cmd,help='Show the bridge status for the guilds you are in'"`
  219. }
  220. type guildStatusCmd struct{}
  221. func (c *guildStatusCmd) Run(g *globals) error {
  222. g.user.guildsLock.Lock()
  223. defer g.user.guildsLock.Unlock()
  224. for _, guild := range g.user.guilds {
  225. fmt.Fprintf(g.context.Stdout, "%s %s %t\n", guild.GuildName, guild.GuildID, guild.Bridge)
  226. }
  227. return nil
  228. }