settings.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. package email
  2. /*
  3. settings.go
  4. Per-user preferences of the Mail app and the system-wide configuration an
  5. administrator manages (OAuth clients, network policy, size limits).
  6. */
  7. import (
  8. "strings"
  9. )
  10. // UserSettings are one ArozOS user's Mail preferences.
  11. type UserSettings struct {
  12. Theme string `json:"theme"` //system | light | dark
  13. Density string `json:"density"` //comfortable | compact
  14. ReadingPane string `json:"readingPane"` //right | bottom
  15. ShowPreview bool `json:"showPreview"` //Snippet line in the message list
  16. MarkReadDelay int `json:"markReadDelay"` //Seconds, -1 = never mark automatically
  17. RemoteImages string `json:"remoteImages"` //ask | always
  18. TrustedSenders []string `json:"trustedSenders"`
  19. UndoSendSeconds int `json:"undoSendSeconds"` //0 sends immediately
  20. ConfirmDelete bool `json:"confirmDelete"`
  21. DefaultAccount string `json:"defaultAccount"`
  22. ComposeFont string `json:"composeFont"`
  23. ComposeFontSize string `json:"composeFontSize"`
  24. PollMinutes int `json:"pollMinutes"`
  25. Notify bool `json:"notify"`
  26. PageSize int `json:"pageSize"`
  27. AutoCollectContacts bool `json:"autoCollectContacts"`
  28. SendShortcut string `json:"sendShortcut"` //ctrl-enter | none
  29. }
  30. // DefaultUserSettings are applied to anything a user has not chosen.
  31. func DefaultUserSettings() UserSettings {
  32. return UserSettings{
  33. Theme: "system",
  34. Density: "comfortable",
  35. ReadingPane: "right",
  36. ShowPreview: true,
  37. MarkReadDelay: 0,
  38. RemoteImages: "ask",
  39. TrustedSenders: []string{},
  40. UndoSendSeconds: 5,
  41. ConfirmDelete: false,
  42. ComposeFont: "",
  43. ComposeFontSize: "14px",
  44. PollMinutes: 2,
  45. Notify: true,
  46. PageSize: 50,
  47. AutoCollectContacts: true,
  48. SendShortcut: "ctrl-enter",
  49. }
  50. }
  51. // normalise clamps every field into its valid range, so a hand-edited or
  52. // older record can never put the UI into an impossible state.
  53. func (s *UserSettings) normalise() {
  54. defaults := DefaultUserSettings()
  55. if !oneOf(s.Theme, "system", "light", "dark") {
  56. s.Theme = defaults.Theme
  57. }
  58. if !oneOf(s.Density, "comfortable", "compact") {
  59. s.Density = defaults.Density
  60. }
  61. if !oneOf(s.ReadingPane, "right", "bottom") {
  62. s.ReadingPane = defaults.ReadingPane
  63. }
  64. if s.MarkReadDelay < -1 || s.MarkReadDelay > 60 {
  65. s.MarkReadDelay = defaults.MarkReadDelay
  66. }
  67. if !oneOf(s.RemoteImages, "ask", "always") {
  68. s.RemoteImages = defaults.RemoteImages
  69. }
  70. if s.TrustedSenders == nil {
  71. s.TrustedSenders = []string{}
  72. }
  73. cleaned := []string{}
  74. seen := map[string]bool{}
  75. for _, sender := range s.TrustedSenders {
  76. sender = strings.ToLower(strings.TrimSpace(sender))
  77. if sender == "" || seen[sender] {
  78. continue
  79. }
  80. seen[sender] = true
  81. cleaned = append(cleaned, sender)
  82. }
  83. s.TrustedSenders = cleaned
  84. if s.UndoSendSeconds < 0 || s.UndoSendSeconds > 60 {
  85. s.UndoSendSeconds = defaults.UndoSendSeconds
  86. }
  87. if s.PollMinutes < 1 || s.PollMinutes > 60 {
  88. s.PollMinutes = defaults.PollMinutes
  89. }
  90. if s.PageSize < 10 || s.PageSize > 200 {
  91. s.PageSize = defaults.PageSize
  92. }
  93. if !oneOf(s.SendShortcut, "ctrl-enter", "none") {
  94. s.SendShortcut = defaults.SendShortcut
  95. }
  96. if len(s.ComposeFont) > 120 {
  97. s.ComposeFont = ""
  98. }
  99. if len(s.ComposeFontSize) > 12 {
  100. s.ComposeFontSize = defaults.ComposeFontSize
  101. }
  102. }
  103. // IsTrustedSender reports whether remote content of mail from address loads
  104. // without asking: either the exact address or its whole domain is trusted.
  105. func (s *UserSettings) IsTrustedSender(address string) bool {
  106. address = strings.ToLower(strings.TrimSpace(address))
  107. if address == "" {
  108. return false
  109. }
  110. domain := ""
  111. if at := strings.LastIndex(address, "@"); at >= 0 {
  112. domain = address[at+1:]
  113. }
  114. for _, trusted := range s.TrustedSenders {
  115. if trusted == address {
  116. return true
  117. }
  118. if domain != "" && (trusted == domain || trusted == "@"+domain) {
  119. return true
  120. }
  121. }
  122. return false
  123. }
  124. // OAuth sign-in flows an administrator can choose.
  125. const (
  126. FlowRedirect = "redirect" //Popup + redirect back to /Mail/oauth.html (needs HTTPS or localhost)
  127. FlowDevice = "device" //Device code: the user types a code on the provider's site
  128. FlowLoopback = "loopback" //Desktop-app client: the user pastes the 127.0.0.1 redirect URL back
  129. )
  130. // OAuthClient is an OAuth application registered by the administrator.
  131. type OAuthClient struct {
  132. Enabled bool `json:"enabled"`
  133. ClientID string `json:"clientId"`
  134. ClientSecret string `json:"clientSecret,omitempty"` //Sealed at rest, never returned to the browser
  135. Tenant string `json:"tenant,omitempty"` //Microsoft only, defaults to "common"
  136. Flow string `json:"flow"`
  137. }
  138. // AdminConfig is the system-wide Mail configuration.
  139. type AdminConfig struct {
  140. Google OAuthClient `json:"google"`
  141. Microsoft OAuthClient `json:"microsoft"`
  142. AllowPrivateHosts bool `json:"allowPrivateHosts"` //Let non-admins reach LAN / loopback servers
  143. AllowInsecure bool `json:"allowInsecure"` //Let non-admins use unencrypted connections
  144. MaxAttachmentMB int `json:"maxAttachmentMB"` //Total attachment size per outgoing message
  145. MaxAccounts int `json:"maxAccounts"` //Per user, 0 = unlimited
  146. }
  147. // AdminConfigView is what the admin settings page receives.
  148. type AdminConfigView struct {
  149. Google OAuthClientView `json:"google"`
  150. Microsoft OAuthClientView `json:"microsoft"`
  151. AllowPrivateHosts bool `json:"allowPrivateHosts"`
  152. AllowInsecure bool `json:"allowInsecure"`
  153. MaxAttachmentMB int `json:"maxAttachmentMB"`
  154. MaxAccounts int `json:"maxAccounts"`
  155. }
  156. // OAuthClientView hides the client secret behind a flag.
  157. type OAuthClientView struct {
  158. Enabled bool `json:"enabled"`
  159. ClientID string `json:"clientId"`
  160. HasSecret bool `json:"hasSecret"`
  161. Tenant string `json:"tenant,omitempty"`
  162. Flow string `json:"flow"`
  163. }
  164. func defaultAdminConfig() AdminConfig {
  165. return AdminConfig{
  166. Google: OAuthClient{Flow: FlowRedirect},
  167. Microsoft: OAuthClient{Flow: FlowDevice, Tenant: "common"},
  168. MaxAttachmentMB: 25,
  169. }
  170. }
  171. func (c *AdminConfig) normalise() {
  172. if !oneOf(c.Google.Flow, FlowRedirect, FlowLoopback) {
  173. c.Google.Flow = FlowRedirect
  174. }
  175. if !oneOf(c.Microsoft.Flow, FlowRedirect, FlowDevice) {
  176. c.Microsoft.Flow = FlowDevice
  177. }
  178. c.Microsoft.Tenant = strings.TrimSpace(c.Microsoft.Tenant)
  179. if c.Microsoft.Tenant == "" {
  180. c.Microsoft.Tenant = "common"
  181. }
  182. c.Google.Tenant = ""
  183. c.Google.ClientID = strings.TrimSpace(c.Google.ClientID)
  184. c.Microsoft.ClientID = strings.TrimSpace(c.Microsoft.ClientID)
  185. if c.MaxAttachmentMB <= 0 || c.MaxAttachmentMB > 1024 {
  186. c.MaxAttachmentMB = 25
  187. }
  188. if c.MaxAccounts < 0 {
  189. c.MaxAccounts = 0
  190. }
  191. }
  192. func (c *OAuthClient) view() OAuthClientView {
  193. return OAuthClientView{
  194. Enabled: c.Enabled,
  195. ClientID: c.ClientID,
  196. HasSecret: c.ClientSecret != "",
  197. Tenant: c.Tenant,
  198. Flow: c.Flow,
  199. }
  200. }
  201. func oneOf(value string, options ...string) bool {
  202. for _, option := range options {
  203. if value == option {
  204. return true
  205. }
  206. }
  207. return false
  208. }