db.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2020 Alexey Edelev <semlanik@gmail.com>
  5. *
  6. * This file is part of gostfix project https://git.semlanik.org/semlanik/gostfix
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of this
  9. * software and associated documentation files (the "Software"), to deal in the Software
  10. * without restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  12. * to permit persons to whom the Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all copies
  16. * or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  19. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  20. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  21. * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. package db
  26. import (
  27. "context"
  28. "crypto/sha1"
  29. "encoding/hex"
  30. "errors"
  31. "log"
  32. "time"
  33. common "git.semlanik.org/semlanik/gostfix/common"
  34. bcrypt "golang.org/x/crypto/bcrypt"
  35. bson "go.mongodb.org/mongo-driver/bson"
  36. "go.mongodb.org/mongo-driver/bson/primitive"
  37. mongo "go.mongodb.org/mongo-driver/mongo"
  38. options "go.mongodb.org/mongo-driver/mongo/options"
  39. config "git.semlanik.org/semlanik/gostfix/config"
  40. )
  41. type Storage struct {
  42. db *mongo.Database
  43. usersCollection *mongo.Collection
  44. tokensCollection *mongo.Collection
  45. emailsCollection *mongo.Collection
  46. allEmailsCollection *mongo.Collection
  47. }
  48. func qualifiedMailCollection(user string) string {
  49. sum := sha1.Sum([]byte(user))
  50. return "mb" + hex.EncodeToString(sum[:])
  51. }
  52. func NewStorage() (s *Storage, err error) {
  53. fullUrl := "mongodb://"
  54. if config.ConfigInstance().MongoUser != "" {
  55. fullUrl += config.ConfigInstance().MongoUser
  56. if config.ConfigInstance().MongoPassword != "" {
  57. fullUrl += ":" + config.ConfigInstance().MongoPassword
  58. }
  59. fullUrl += "@"
  60. }
  61. fullUrl += config.ConfigInstance().MongoAddress
  62. client, err := mongo.NewClient(options.Client().ApplyURI(fullUrl))
  63. if err != nil {
  64. return nil, err
  65. }
  66. ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
  67. defer cancel()
  68. err = client.Connect(ctx)
  69. if err != nil {
  70. return nil, err
  71. }
  72. db := client.Database("gostfix")
  73. index := mongo.IndexModel{
  74. Keys: bson.M{
  75. "user": 1,
  76. },
  77. Options: options.Index().SetUnique(true),
  78. }
  79. s = &Storage{
  80. db: db,
  81. usersCollection: db.Collection("users"),
  82. tokensCollection: db.Collection("tokens"),
  83. emailsCollection: db.Collection("emails"),
  84. allEmailsCollection: db.Collection("allEmails"),
  85. }
  86. //Initial database setup
  87. s.usersCollection.Indexes().CreateOne(context.Background(), index)
  88. s.tokensCollection.Indexes().CreateOne(context.Background(), index)
  89. s.emailsCollection.Indexes().CreateOne(context.Background(), index)
  90. return
  91. }
  92. func (s *Storage) AddUser(user, password, fullName string) error {
  93. hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
  94. if err != nil {
  95. return err
  96. }
  97. hashString := string(hash)
  98. userInfo := bson.M{
  99. "user": user,
  100. "password": hashString,
  101. "fullName": fullName,
  102. }
  103. _, err = s.usersCollection.InsertOne(context.Background(), userInfo)
  104. if err != nil {
  105. return err
  106. }
  107. err = s.addEmail(user, user, true)
  108. if err != nil {
  109. s.usersCollection.DeleteOne(context.Background(), bson.M{"user": user})
  110. return err
  111. }
  112. //TODO: Update postfix virtual map here
  113. return nil
  114. }
  115. func (s *Storage) AddEmail(user string, email string) error {
  116. return s.addEmail(user, email, false)
  117. }
  118. func (s *Storage) addEmail(user string, email string, upsert bool) error {
  119. result := struct {
  120. User string
  121. }{}
  122. err := s.usersCollection.FindOne(context.Background(), bson.M{"user": user}).Decode(&result)
  123. if err != nil {
  124. return err
  125. }
  126. emails, err := s.GetAllEmails()
  127. if err != nil {
  128. return err
  129. }
  130. for _, existingEmail := range emails {
  131. if existingEmail == email {
  132. return errors.New("Email exists")
  133. }
  134. }
  135. _, err = s.emailsCollection.UpdateOne(context.Background(),
  136. bson.M{"user": user},
  137. bson.M{"$addToSet": bson.M{"email": email}},
  138. options.Update().SetUpsert(upsert))
  139. //TODO: Update postfix virtual map here
  140. return err
  141. }
  142. func (s *Storage) RemoveEmail(user string, email string) error {
  143. _, err := s.emailsCollection.UpdateOne(context.Background(),
  144. bson.M{"user": user},
  145. bson.M{"$pull": bson.M{"email": email}})
  146. //TODO: Update postfix virtual map here
  147. return err
  148. }
  149. func (s *Storage) CheckUser(user, password string) error {
  150. log.Printf("Check user: %s %s", user, password)
  151. result := struct {
  152. User string
  153. Password string
  154. }{}
  155. err := s.usersCollection.FindOne(context.Background(), bson.M{"user": user}).Decode(&result)
  156. if err != nil {
  157. return errors.New("Invalid user or password")
  158. }
  159. if bcrypt.CompareHashAndPassword([]byte(result.Password), []byte(password)) != nil {
  160. return errors.New("Invalid user or password")
  161. }
  162. return nil
  163. }
  164. func (s *Storage) AddToken(user, token string) error {
  165. log.Printf("add token: %s, %s", user, token)
  166. s.tokensCollection.UpdateOne(context.Background(),
  167. bson.M{"user": user},
  168. bson.M{
  169. "$addToSet": bson.M{
  170. "token": bson.M{
  171. "token": token,
  172. "expire": time.Now().Add(time.Hour * 96).Unix(),
  173. },
  174. },
  175. },
  176. options.Update().SetUpsert(true))
  177. return nil
  178. }
  179. func (s *Storage) CheckToken(user, token string) error {
  180. log.Printf("Check token: %s %s", user, token)
  181. if token == "" {
  182. return errors.New("Invalid token")
  183. }
  184. cur, err := s.tokensCollection.Aggregate(context.Background(),
  185. bson.A{
  186. bson.M{"$match": bson.M{"user": user}},
  187. bson.M{"$unwind": "$token"},
  188. bson.M{"$match": bson.M{"token.token": token}},
  189. bson.M{"$project": bson.M{"_id": 0, "token.expire": 1}},
  190. })
  191. if err != nil {
  192. log.Fatalln(err)
  193. return err
  194. }
  195. defer cur.Close(context.Background())
  196. if cur.Next(context.Background()) {
  197. result := struct {
  198. Token struct {
  199. Expire int64
  200. }
  201. }{}
  202. err = cur.Decode(&result)
  203. if err == nil && result.Token.Expire >= time.Now().Unix() {
  204. log.Printf("Check token %s expire: %d", user, result.Token.Expire)
  205. return nil
  206. }
  207. }
  208. return errors.New("Token expired")
  209. }
  210. func (s *Storage) SaveMail(email, folder string, m *common.Mail) error {
  211. result := &struct {
  212. User string
  213. }{}
  214. s.emailsCollection.FindOne(context.Background(), bson.M{"email": email}).Decode(result)
  215. mailsCollection := s.db.Collection(qualifiedMailCollection(result.User))
  216. mailsCollection.InsertOne(context.Background(), &struct {
  217. Email string
  218. Mail *common.Mail
  219. Folder string
  220. Read bool
  221. }{
  222. Email: email,
  223. Mail: m,
  224. Folder: folder,
  225. Read: false,
  226. }, options.InsertOne().SetBypassDocumentValidation(true))
  227. return nil
  228. }
  229. func (s *Storage) RemoveMail(user string, messageId string) error {
  230. mailsCollection := s.db.Collection(qualifiedMailCollection(user))
  231. oId, err := primitive.ObjectIDFromHex(messageId)
  232. if err != nil {
  233. return err
  234. }
  235. _, err = mailsCollection.DeleteOne(context.Background(), bson.M{"_id": oId})
  236. return err
  237. }
  238. func (s *Storage) MailList(user, email, folder string, frame common.Frame) ([]*common.MailMetadata, error) {
  239. mailsCollection := s.db.Collection(qualifiedMailCollection(user))
  240. request := bson.A{
  241. bson.M{"$match": bson.M{"email": email}},
  242. bson.M{"$sort": bson.M{"mail.header.date": 1}},
  243. }
  244. if frame.Skip > 0 {
  245. request = append(request, bson.M{"$skip": frame.Skip})
  246. }
  247. if frame.Limit > 0 {
  248. request = append(request, bson.M{"$limit": frame.Limit})
  249. }
  250. cur, err := mailsCollection.Aggregate(context.Background(), request)
  251. if err != nil {
  252. return nil, err
  253. }
  254. var headers []*common.MailMetadata
  255. for cur.Next(context.Background()) {
  256. result := &common.MailMetadata{}
  257. err = cur.Decode(result)
  258. if err != nil {
  259. log.Printf("Unable to read database mail record: %s", err)
  260. continue
  261. }
  262. log.Printf("Add message: %s", result.Id)
  263. headers = append(headers, result)
  264. }
  265. log.Printf("Mails read from database: %v", headers)
  266. return headers, nil
  267. }
  268. func (s *Storage) GetUserInfo(user string) (*common.UserInfo, error) {
  269. result := &common.UserInfo{}
  270. err := s.usersCollection.FindOne(context.Background(), bson.M{"user": user}).Decode(result)
  271. return result, err
  272. }
  273. func (s *Storage) GetEmailStats(user string, email string) (unread, total int, err error) {
  274. mailsCollection := s.db.Collection(qualifiedMailCollection(user))
  275. result := &struct {
  276. Total int
  277. Unread int
  278. }{}
  279. cur, err := mailsCollection.Aggregate(context.Background(), bson.A{bson.M{"$match": bson.M{"email": email, "read": false}}, bson.M{"$count": "unread"}})
  280. if err == nil && cur.Next(context.Background()) {
  281. cur.Decode(result)
  282. } else {
  283. return 0, 0, err
  284. }
  285. cur, err = mailsCollection.Aggregate(context.Background(), bson.A{bson.M{"$match": bson.M{"email": email}}, bson.M{"$count": "total"}})
  286. if err == nil && cur.Next(context.Background()) {
  287. cur.Decode(result)
  288. } else {
  289. return 0, 0, err
  290. }
  291. return result.Unread, result.Total, err
  292. }
  293. func (s *Storage) GetMail(user string, id string) (m *common.Mail, err error) {
  294. mailsCollection := s.db.Collection(qualifiedMailCollection(user))
  295. oId, err := primitive.ObjectIDFromHex(id)
  296. if err != nil {
  297. return nil, err
  298. }
  299. m = &common.Mail{}
  300. result := &struct {
  301. Mail *common.Mail
  302. }{
  303. Mail: m,
  304. }
  305. err = mailsCollection.FindOne(context.Background(), bson.M{"_id": oId}).Decode(result)
  306. if err != nil {
  307. return nil, err
  308. }
  309. return result.Mail, nil
  310. }
  311. func (s *Storage) SetRead(user string, id string, read bool) error {
  312. mailsCollection := s.db.Collection(qualifiedMailCollection(user))
  313. oId, err := primitive.ObjectIDFromHex(id)
  314. if err != nil {
  315. return err
  316. }
  317. _, err = mailsCollection.UpdateOne(context.Background(), bson.M{"_id": oId}, bson.M{"$set": bson.M{"read": read}})
  318. return err
  319. }
  320. func (s *Storage) GetAttachment(user string, attachmentId string) (filePath string, err error) {
  321. return "", nil
  322. }
  323. func (s *Storage) GetUsers() (users []string, err error) {
  324. return nil, nil
  325. }
  326. func (s *Storage) GetEmails(user []string) (emails []string, err error) {
  327. return nil, nil
  328. }
  329. func (s *Storage) GetAllEmails() (emails []string, err error) {
  330. cur, err := s.allEmailsCollection.Find(context.Background(), bson.M{})
  331. if cur.Next(context.Background()) {
  332. result := struct {
  333. Emails []string
  334. }{}
  335. err = cur.Decode(&result)
  336. if err == nil {
  337. return result.Emails, nil
  338. }
  339. }
  340. return nil, err
  341. }