regexp.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 utils
  26. import (
  27. "log"
  28. "regexp"
  29. "sync"
  30. )
  31. const (
  32. NewMailIndicator = "^(From\\s).*"
  33. HeaderRegExp = "^([\x21-\x7E^:]+):(.*)"
  34. FoldingRegExp = "^\\s+(.*)"
  35. BoundaryStartRegExp = "^--(.*)"
  36. BoundaryEndRegExp = "^--(.*)--$"
  37. BoundaryRegExp = "boundary=\"(.*)\""
  38. MailboxRegExp = "^/m(\\d+)/?(.*)"
  39. FullNameRegExp = "^[\\w]+[\\w ]*$"
  40. EncodedStringRegExp = "=\\?.+\\?="
  41. )
  42. const (
  43. DomainRegExp = "(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]"
  44. EmailRegExp = "(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\\])"
  45. )
  46. type RegExpUtils regExpUtils
  47. var (
  48. once sync.Once
  49. instance *regExpUtils
  50. )
  51. func RegExpUtilsInstance() *RegExpUtils {
  52. once.Do(func() {
  53. instance, _ = newRegExpUtils()
  54. })
  55. return (*RegExpUtils)(instance)
  56. }
  57. type regExpUtils struct {
  58. MailIndicator *regexp.Regexp
  59. DomainChecker *regexp.Regexp
  60. EmailChecker *regexp.Regexp
  61. HeaderFinder *regexp.Regexp
  62. FoldingFinder *regexp.Regexp
  63. BoundaryStartFinder *regexp.Regexp
  64. BoundaryEndFinder *regexp.Regexp
  65. BoundaryFinder *regexp.Regexp
  66. MailboxFinder *regexp.Regexp
  67. FullNameChecker *regexp.Regexp
  68. EncodedStringFinder *regexp.Regexp
  69. }
  70. func newRegExpUtils() (*regExpUtils, error) {
  71. mailIndicator, err := regexp.Compile(NewMailIndicator)
  72. if err != nil {
  73. log.Fatalf("Invalid regexp %s\n", err)
  74. return nil, err
  75. }
  76. headerFinder, err := regexp.Compile(HeaderRegExp)
  77. if err != nil {
  78. log.Fatalf("Invalid regexp %s\n", err)
  79. return nil, err
  80. }
  81. foldingFinder, err := regexp.Compile(FoldingRegExp)
  82. if err != nil {
  83. log.Fatalf("Invalid regexp %s\n", err)
  84. return nil, err
  85. }
  86. boundaryStartFinder, err := regexp.Compile(BoundaryStartRegExp)
  87. if err != nil {
  88. log.Fatalf("Invalid regexp %s\n", err)
  89. return nil, err
  90. }
  91. boundaryEndFinder, err := regexp.Compile(BoundaryEndRegExp)
  92. if err != nil {
  93. log.Fatalf("Invalid regexp %s\n", err)
  94. return nil, err
  95. }
  96. boundaryFinder, err := regexp.Compile(BoundaryRegExp)
  97. if err != nil {
  98. log.Fatalf("Invalid regexp %s\n", err)
  99. return nil, err
  100. }
  101. domainChecker, err := regexp.Compile(DomainRegExp)
  102. if err != nil {
  103. log.Fatalf("Invalid regexp %s\n", err)
  104. return nil, err
  105. }
  106. emailChecker, err := regexp.Compile(EmailRegExp)
  107. if err != nil {
  108. log.Fatalf("Invalid regexp %s\n", err)
  109. return nil, err
  110. }
  111. mailboxFinder, err := regexp.Compile(MailboxRegExp)
  112. if err != nil {
  113. log.Fatalf("Invalid regexp %s\n", err)
  114. return nil, err
  115. }
  116. fullNameChecker, err := regexp.Compile(FullNameRegExp)
  117. if err != nil {
  118. log.Fatalf("Invalid regexp %s\n", err)
  119. return nil, err
  120. }
  121. encodedString, err := regexp.Compile(EncodedStringRegExp)
  122. if err != nil {
  123. log.Fatalf("Invalid regexp %s\n", err)
  124. return nil, err
  125. }
  126. ru := &regExpUtils{
  127. MailIndicator: mailIndicator,
  128. EmailChecker: emailChecker,
  129. HeaderFinder: headerFinder,
  130. FoldingFinder: foldingFinder,
  131. BoundaryStartFinder: boundaryStartFinder,
  132. BoundaryEndFinder: boundaryEndFinder,
  133. BoundaryFinder: boundaryFinder,
  134. DomainChecker: domainChecker,
  135. MailboxFinder: mailboxFinder,
  136. FullNameChecker: fullNameChecker,
  137. EncodedStringFinder: encodedString,
  138. }
  139. return ru, nil
  140. }