Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

136 linhas
5.7KB

  1. ' Shared configuration code for 32-bit and 64-bit Windows Script Host.
  2. Option Explicit
  3. Function ProjectRoot()
  4. Dim fs
  5. Set fs = CreateObject("Scripting.FileSystemObject")
  6. ProjectRoot = fs.GetParentFolderName(fs.GetParentFolderName(WScript.ScriptFullName))
  7. End Function
  8. Function ConfigFilePath()
  9. Dim fs
  10. Set fs = CreateObject("Scripting.FileSystemObject")
  11. ConfigFilePath = fs.BuildPath(ProjectRoot(), "config.ini")
  12. End Function
  13. Function LoadConfig(configPath)
  14. Dim fs, stream, content, lines, line, equalAt, key, value, settings, i
  15. Set fs = CreateObject("Scripting.FileSystemObject")
  16. If Not fs.FileExists(configPath) Then
  17. Err.Raise vbObjectError + 1001, "MassMailer config", _
  18. "Missing config.ini. Copy config.example.ini to config.ini and edit it."
  19. End If
  20. Set stream = CreateObject("ADODB.Stream")
  21. stream.Type = 2
  22. stream.Charset = "utf-8"
  23. stream.Open
  24. stream.LoadFromFile configPath
  25. content = stream.ReadText
  26. stream.Close
  27. If Len(content) > 0 Then
  28. If Left(content, 1) = ChrW(&HFEFF) Then content = Mid(content, 2)
  29. End If
  30. Set settings = CreateObject("Scripting.Dictionary")
  31. settings.CompareMode = 1
  32. lines = Split(Replace(Replace(content, vbCrLf, vbLf), vbCr, vbLf), vbLf)
  33. For i = 0 To UBound(lines)
  34. line = Trim(lines(i))
  35. If Len(line) > 0 Then
  36. If Left(line, 1) <> "#" And Left(line, 1) <> ";" Then
  37. equalAt = InStr(line, "=")
  38. If equalAt < 2 Then ConfigError "Invalid setting at line " & (i + 1)
  39. key = UCase(Trim(Left(line, equalAt - 1)))
  40. value = Trim(Mid(line, equalAt + 1))
  41. If Not IsAllowedKey(key) Then ConfigError "Unknown setting: " & key
  42. If settings.Exists(key) Then ConfigError "Duplicate setting: " & key
  43. settings.Add key, value
  44. End If
  45. End If
  46. Next
  47. ValidateConfig settings
  48. Set LoadConfig = settings
  49. End Function
  50. Sub ConfigError(message)
  51. Err.Raise vbObjectError + 1002, "MassMailer config", message
  52. End Sub
  53. Function IsAllowedKey(key)
  54. Dim name
  55. IsAllowedKey = False
  56. For Each name In Array("SMTP_HOST", "SMTP_PORT", "SMTP_USER", "SMTP_TLS", _
  57. "SMTP_STARTTLS", "SMTP_FROM", "SMTP_REPLY_TO", "SMTP_TEST_TO", "SMTP_SECRET_ID", _
  58. "IMAP_HOST", "IMAP_PORT", "IMAP_SENT_FOLDER", _
  59. "DB_PATH", "DB_PROVIDER", "DB_BITNESS", "QUEUE_INTERVAL", _
  60. "SEND_MODE", "DISPLAY_TIME_ZONE", "TEMPLATE_ROOT", _
  61. "TEMPLATE_IMAGE_MODE", "TEMPLATE_IMAGE_TYPES", _
  62. "TEMPLATE_IMAGE_MAX_BYTES", "TEMPLATE_MESSAGE_MAX_BYTES", _
  63. "OPT_OUT_REVIEW_HOURS", "BUSINESS_POSTAL_ADDRESS", "REPORT_DIR", "BACKUP_DIR")
  64. If key = name Then IsAllowedKey = True: Exit Function
  65. Next
  66. End Function
  67. Sub ValidateConfig(settings)
  68. Dim name
  69. For Each name In Array("SMTP_HOST", "SMTP_PORT", "SMTP_USER", "SMTP_TLS", _
  70. "SMTP_STARTTLS", "SMTP_FROM", "SMTP_REPLY_TO", "SMTP_SECRET_ID", _
  71. "DB_PATH", "DB_PROVIDER", "DB_BITNESS", "QUEUE_INTERVAL", _
  72. "SEND_MODE", "DISPLAY_TIME_ZONE", "TEMPLATE_ROOT", _
  73. "TEMPLATE_IMAGE_MODE", "TEMPLATE_IMAGE_TYPES", _
  74. "TEMPLATE_IMAGE_MAX_BYTES", "TEMPLATE_MESSAGE_MAX_BYTES", _
  75. "OPT_OUT_REVIEW_HOURS", "BUSINESS_POSTAL_ADDRESS")
  76. If Not settings.Exists(name) Then ConfigError "Missing setting: " & name
  77. If name <> "BUSINESS_POSTAL_ADDRESS" Then
  78. If Len(settings(name)) = 0 Then ConfigError "Empty setting: " & name
  79. End If
  80. Next
  81. CheckWholeNumber settings, "SMTP_PORT", 1, 65535
  82. CheckWholeNumber settings, "QUEUE_INTERVAL", 60, 86400
  83. CheckWholeNumber settings, "TEMPLATE_IMAGE_MAX_BYTES", 1, 10485760
  84. CheckWholeNumber settings, "TEMPLATE_MESSAGE_MAX_BYTES", 1, 52428800
  85. CheckWholeNumber settings, "OPT_OUT_REVIEW_HOURS", 1, 168
  86. If CLng(settings("TEMPLATE_IMAGE_MAX_BYTES")) > CLng(settings("TEMPLATE_MESSAGE_MAX_BYTES")) Then
  87. ConfigError "Image limit exceeds message limit"
  88. End If
  89. If LCase(settings("SMTP_TLS")) <> "on" Or LCase(settings("SMTP_STARTTLS")) <> "on" Then
  90. ConfigError "SMTP_TLS and SMTP_STARTTLS must both be on"
  91. End If
  92. If LCase(settings("DB_BITNESS")) <> "x86" And LCase(settings("DB_BITNESS")) <> "x64" Then
  93. ConfigError "DB_BITNESS must be x86 or x64"
  94. End If
  95. Select Case LCase(settings("DB_PROVIDER"))
  96. Case "microsoft.jet.oledb.4.0", "microsoft.ace.oledb.12.0", "microsoft.ace.oledb.16.0"
  97. Case Else: ConfigError "Unsupported DB_PROVIDER"
  98. End Select
  99. If LCase(settings("SEND_MODE")) <> "always" Then ConfigError "SEND_MODE must be always"
  100. If LCase(settings("DISPLAY_TIME_ZONE")) <> "local" Then ConfigError "DISPLAY_TIME_ZONE must be local"
  101. If LCase(settings("TEMPLATE_IMAGE_MODE")) <> "inline_cid" Then ConfigError "TEMPLATE_IMAGE_MODE must be inline_cid"
  102. End Sub
  103. Sub CheckWholeNumber(settings, name, minimum, maximum)
  104. Dim re, value
  105. Set re = CreateObject("VBScript.RegExp")
  106. re.Pattern = "^[0-9]+$"
  107. If Not re.Test(settings(name)) Then ConfigError name & " must be a whole number"
  108. If Len(settings(name)) > 10 Then ConfigError name & " is too large"
  109. value = CDbl(settings(name))
  110. If value < minimum Or value > maximum Then
  111. ConfigError name & " must be between " & minimum & " and " & maximum
  112. End If
  113. End Sub
  114. Function ResolveDbPath(settings)
  115. Dim fs, path
  116. Set fs = CreateObject("Scripting.FileSystemObject")
  117. path = settings("DB_PATH")
  118. If Left(path, 2) = "\\" Then ConfigError "DB_PATH must be on a local drive"
  119. If fs.GetDriveName(path) = "" Then path = fs.BuildPath(ProjectRoot(), path)
  120. path = fs.GetAbsolutePathName(path)
  121. If LCase(fs.GetExtensionName(path)) <> "mdb" Then ConfigError "DB_PATH must end in .mdb"
  122. ResolveDbPath = path
  123. End Function

Powered by TurnKey Linux.