appservice.go 7.8 KB

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