regexp.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. FullNameRegExp = "^[\\w]+[\\w ]*$"
  39. EncodedStringRegExp = "=\\?.+\\?="
  40. )
  41. const (
  42. DomainRegExp = "(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]"
  43. 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])+)\\])"
  44. )
  45. type RegExpUtils regExpUtils
  46. var (
  47. once sync.Once
  48. instance *regExpUtils
  49. )
  50. func RegExpUtilsInstance() *RegExpUtils {
  51. once.Do(func() {
  52. instance, _ = newRegExpUtils()
  53. })
  54. return (*RegExpUtils)(instance)
  55. }
  56. type regExpUtils struct {
  57. MailIndicator *regexp.Regexp
  58. DomainChecker *regexp.Regexp
  59. EmailChecker *regexp.Regexp
  60. HeaderFinder *regexp.Regexp
  61. FoldingFinder *regexp.Regexp
  62. BoundaryStartFinder *regexp.Regexp
  63. BoundaryEndFinder *regexp.Regexp
  64. BoundaryFinder *regexp.Regexp
  65. FullNameChecker *regexp.Regexp
  66. EncodedStringFinder *regexp.Regexp
  67. }
  68. func newRegExpUtils() (*regExpUtils, error) {
  69. mailIndicator, err := regexp.Compile(NewMailIndicator)
  70. if err != nil {
  71. log.Fatalf("Invalid regexp %s\n", err)
  72. return nil, err
  73. }
  74. headerFinder, err := regexp.Compile(HeaderRegExp)
  75. if err != nil {
  76. log.Fatalf("Invalid regexp %s\n", err)
  77. return nil, err
  78. }
  79. foldingFinder, err := regexp.Compile(FoldingRegExp)
  80. if err != nil {
  81. log.Fatalf("Invalid regexp %s\n", err)
  82. return nil, err
  83. }
  84. boundaryStartFinder, err := regexp.Compile(BoundaryStartRegExp)
  85. if err != nil {
  86. log.Fatalf("Invalid regexp %s\n", err)
  87. return nil, err
  88. }
  89. boundaryEndFinder, err := regexp.Compile(BoundaryEndRegExp)
  90. if err != nil {
  91. log.Fatalf("Invalid regexp %s\n", err)
  92. return nil, err
  93. }
  94. boundaryFinder, err := regexp.Compile(BoundaryRegExp)
  95. if err != nil {
  96. log.Fatalf("Invalid regexp %s\n", err)
  97. return nil, err
  98. }
  99. domainChecker, err := regexp.Compile(DomainRegExp)
  100. if err != nil {
  101. log.Fatalf("Invalid regexp %s\n", err)
  102. return nil, err
  103. }
  104. emailChecker, err := regexp.Compile(EmailRegExp)
  105. if err != nil {
  106. log.Fatalf("Invalid regexp %s\n", err)
  107. return nil, err
  108. }
  109. fullNameChecker, err := regexp.Compile(FullNameRegExp)
  110. if err != nil {
  111. log.Fatalf("Invalid regexp %s\n", err)
  112. return nil, err
  113. }
  114. encodedString, err := regexp.Compile(EncodedStringRegExp)
  115. if err != nil {
  116. log.Fatalf("Invalid regexp %s\n", err)
  117. return nil, err
  118. }
  119. ru := &regExpUtils{
  120. MailIndicator: mailIndicator,
  121. EmailChecker: emailChecker,
  122. HeaderFinder: headerFinder,
  123. FoldingFinder: foldingFinder,
  124. BoundaryStartFinder: boundaryStartFinder,
  125. BoundaryEndFinder: boundaryEndFinder,
  126. BoundaryFinder: boundaryFinder,
  127. DomainChecker: domainChecker,
  128. FullNameChecker: fullNameChecker,
  129. EncodedStringFinder: encodedString,
  130. }
  131. return ru, nil
  132. }