appservice.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. package appservice
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "gopkg.in/yaml.v2"
  7. "maunium.net/go/maulogger"
  8. "strings"
  9. "net/http"
  10. "errors"
  11. "maunium.net/go/gomatrix"
  12. "regexp"
  13. )
  14. // EventChannelSize is the size for the Events channel in Appservice instances.
  15. var EventChannelSize = 64
  16. // Create a blank appservice instance.
  17. func Create() *AppService {
  18. return &AppService{
  19. LogConfig: CreateLogConfig(),
  20. clients: make(map[string]*gomatrix.Client),
  21. intents: make(map[string]*IntentAPI),
  22. StateStore: NewBasicStateStore(),
  23. }
  24. }
  25. // Load an appservice config from a file.
  26. func Load(path string) (*AppService, error) {
  27. data, readErr := ioutil.ReadFile(path)
  28. if readErr != nil {
  29. return nil, readErr
  30. }
  31. var config = &AppService{}
  32. yaml.Unmarshal(data, config)
  33. return config, nil
  34. }
  35. // QueryHandler handles room alias and user ID queries from the homeserver.
  36. type QueryHandler interface {
  37. QueryAlias(alias string) bool
  38. QueryUser(userID string) bool
  39. }
  40. type QueryHandlerStub struct{}
  41. func (qh *QueryHandlerStub) QueryAlias(alias string) bool {
  42. return false
  43. }
  44. func (qh *QueryHandlerStub) QueryUser(userID string) bool {
  45. return false
  46. }
  47. // AppService is the main config for all appservices.
  48. // It also serves as the appservice instance struct.
  49. type AppService struct {
  50. HomeserverDomain string `yaml:"homeserver_domain"`
  51. HomeserverURL string `yaml:"homeserver_url"`
  52. RegistrationPath string `yaml:"registration"`
  53. Host HostConfig `yaml:"host"`
  54. LogConfig LogConfig `yaml:"logging"`
  55. Registration *Registration `yaml:"-"`
  56. Log maulogger.Logger `yaml:"-"`
  57. lastProcessedTransaction string
  58. Events chan *gomatrix.Event `yaml:"-"`
  59. QueryHandler QueryHandler `yaml:"-"`
  60. StateStore StateStore `yaml:"-"`
  61. server *http.Server
  62. botClient *gomatrix.Client
  63. botIntent *IntentAPI
  64. clients map[string]*gomatrix.Client
  65. intents map[string]*IntentAPI
  66. }
  67. // HostConfig contains info about how to host the appservice.
  68. type HostConfig struct {
  69. Hostname string `yaml:"hostname"`
  70. Port uint16 `yaml:"port"`
  71. TLSKey string `yaml:"tls_key,omitempty"`
  72. TLSCert string `yaml:"tls_cert,omitempty"`
  73. }
  74. // Address gets the whole address of the Appservice.
  75. func (hc *HostConfig) Address() string {
  76. return fmt.Sprintf("%s:%d", hc.Hostname, hc.Port)
  77. }
  78. // Save saves this config into a file at the given path.
  79. func (as *AppService) Save(path string) error {
  80. data, err := yaml.Marshal(as)
  81. if err != nil {
  82. return err
  83. }
  84. return ioutil.WriteFile(path, data, 0644)
  85. }
  86. // YAML returns the config in YAML format.
  87. func (as *AppService) YAML() (string, error) {
  88. data, err := yaml.Marshal(as)
  89. if err != nil {
  90. return "", err
  91. }
  92. return string(data), nil
  93. }
  94. func (as *AppService) BotMXID() string {
  95. return fmt.Sprintf("@%s:%s", as.Registration.SenderLocalpart, as.HomeserverDomain)
  96. }
  97. var MatrixUserIDRegex = regexp.MustCompile("^@([^:]+):(.+)$")
  98. func ParseUserID(mxid string) (string, string) {
  99. match := MatrixUserIDRegex.FindStringSubmatch(mxid)
  100. if match != nil && len(match) == 3 {
  101. return match[1], match[2]
  102. }
  103. return "", ""
  104. }
  105. func (as *AppService) Intent(userID string) *IntentAPI {
  106. intent, ok := as.intents[userID]
  107. if !ok {
  108. localpart, homeserver := ParseUserID(userID)
  109. if len(localpart) == 0 || homeserver != as.HomeserverDomain {
  110. return nil
  111. }
  112. intent = as.NewIntentAPI(localpart)
  113. as.intents[userID] = intent
  114. }
  115. return intent
  116. }
  117. func (as *AppService) BotIntent() *IntentAPI {
  118. if as.botIntent == nil {
  119. as.botIntent = as.NewIntentAPI(as.Registration.SenderLocalpart)
  120. }
  121. return as.botIntent
  122. }
  123. func (as *AppService) Client(userID string) *gomatrix.Client {
  124. client, ok := as.clients[userID]
  125. if !ok {
  126. var err error
  127. client, err = gomatrix.NewClient(as.HomeserverURL, userID, as.Registration.AppToken)
  128. if err != nil {
  129. as.Log.Fatalln("Failed to create gomatrix instance:", err)
  130. return nil
  131. }
  132. client.Syncer = nil
  133. client.Store = nil
  134. client.AppServiceUserID = userID
  135. client.Logger = as.Log.Sub(userID)
  136. as.clients[userID] = client
  137. }
  138. return client
  139. }
  140. func (as *AppService) BotClient() *gomatrix.Client {
  141. if as.botClient == nil {
  142. var err error
  143. as.botClient, err = gomatrix.NewClient(as.HomeserverURL, as.BotMXID(), as.Registration.AppToken)
  144. if err != nil {
  145. as.Log.Fatalln("Failed to create gomatrix instance:", err)
  146. return nil
  147. }
  148. as.botClient.Syncer = nil
  149. as.botClient.Store = nil
  150. as.botClient.Logger = as.Log.Sub("Bot")
  151. }
  152. return as.botClient
  153. }
  154. // Init initializes the logger and loads the registration of this appservice.
  155. func (as *AppService) Init() (bool, error) {
  156. as.Events = make(chan *gomatrix.Event, EventChannelSize)
  157. as.QueryHandler = &QueryHandlerStub{}
  158. as.Log = maulogger.Create()
  159. as.LogConfig.Configure(as.Log)
  160. as.Log.Debugln("Logger initialized successfully.")
  161. if len(as.RegistrationPath) > 0 {
  162. var err error
  163. as.Registration, err = LoadRegistration(as.RegistrationPath)
  164. if err != nil {
  165. return false, err
  166. }
  167. }
  168. as.Log.Debugln("Appservice initialized successfully.")
  169. return true, nil
  170. }
  171. // LogConfig contains configs for the logger.
  172. type LogConfig struct {
  173. Directory string `yaml:"directory"`
  174. FileNameFormat string `yaml:"file_name_format"`
  175. FileDateFormat string `yaml:"file_date_format"`
  176. FileMode uint32 `yaml:"file_mode"`
  177. TimestampFormat string `yaml:"timestamp_format"`
  178. RawPrintLevel string `yaml:"print_level"`
  179. PrintLevel int `yaml:"-"`
  180. }
  181. type umLogConfig LogConfig
  182. func (lc *LogConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
  183. err := unmarshal((*umLogConfig)(lc))
  184. if err != nil {
  185. return err
  186. }
  187. switch strings.ToUpper(lc.RawPrintLevel) {
  188. case "DEBUG":
  189. lc.PrintLevel = maulogger.LevelDebug.Severity
  190. case "INFO":
  191. lc.PrintLevel = maulogger.LevelInfo.Severity
  192. case "WARN", "WARNING":
  193. lc.PrintLevel = maulogger.LevelWarn.Severity
  194. case "ERR", "ERROR":
  195. lc.PrintLevel = maulogger.LevelError.Severity
  196. case "FATAL":
  197. lc.PrintLevel = maulogger.LevelFatal.Severity
  198. default:
  199. return errors.New("invalid print level " + lc.RawPrintLevel)
  200. }
  201. return err
  202. }
  203. func (lc *LogConfig) MarshalYAML() (interface{}, error) {
  204. switch {
  205. case lc.PrintLevel >= maulogger.LevelFatal.Severity:
  206. lc.RawPrintLevel = maulogger.LevelFatal.Name
  207. case lc.PrintLevel >= maulogger.LevelError.Severity:
  208. lc.RawPrintLevel = maulogger.LevelError.Name
  209. case lc.PrintLevel >= maulogger.LevelWarn.Severity:
  210. lc.RawPrintLevel = maulogger.LevelWarn.Name
  211. case lc.PrintLevel >= maulogger.LevelInfo.Severity:
  212. lc.RawPrintLevel = maulogger.LevelInfo.Name
  213. default:
  214. lc.RawPrintLevel = maulogger.LevelDebug.Name
  215. }
  216. return lc, nil
  217. }
  218. // CreateLogConfig creates a basic LogConfig.
  219. func CreateLogConfig() LogConfig {
  220. return LogConfig{
  221. Directory: "./logs",
  222. FileNameFormat: "%[1]s-%02[2]d.log",
  223. TimestampFormat: "Jan _2, 2006 15:04:05",
  224. FileMode: 0600,
  225. FileDateFormat: "2006-01-02",
  226. PrintLevel: 10,
  227. }
  228. }
  229. // GetFileFormat returns a mauLogger-compatible logger file format based on the data in the struct.
  230. func (lc LogConfig) GetFileFormat() maulogger.LoggerFileFormat {
  231. path := lc.FileNameFormat
  232. if len(lc.Directory) > 0 {
  233. path = lc.Directory + "/" + path
  234. }
  235. return func(now string, i int) string {
  236. return fmt.Sprintf(path, now, i)
  237. }
  238. }
  239. // Configure configures a mauLogger instance with the data in this struct.
  240. func (lc LogConfig) Configure(log maulogger.Logger) {
  241. basicLogger := log.(*maulogger.BasicLogger)
  242. basicLogger.FileFormat = lc.GetFileFormat()
  243. basicLogger.FileMode = os.FileMode(lc.FileMode)
  244. basicLogger.FileTimeFormat = lc.FileDateFormat
  245. basicLogger.TimeFormat = lc.TimestampFormat
  246. basicLogger.PrintLevel = lc.PrintLevel
  247. }