main.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package main
  2. import (
  3. "bufio"
  4. "flag"
  5. "fmt"
  6. "os"
  7. "strings"
  8. )
  9. var noVersion = "dev"
  10. var version string
  11. func readFromConsole() string {
  12. var text, line string
  13. var err error
  14. counter := 0
  15. reader := bufio.NewReader(os.Stdin)
  16. for counter < 3 {
  17. line, err = reader.ReadString('\n')
  18. if line == "\n" {
  19. counter++
  20. } else {
  21. counter = 0
  22. }
  23. text += fmt.Sprintf("%s\n", line)
  24. }
  25. if err != nil {
  26. Error.F("Error in reading text from console\n%s", err)
  27. os.Exit(1)
  28. }
  29. return strings.TrimRight(text, "\n")
  30. }
  31. func toList(input string) []string {
  32. var result = []string{}
  33. list := strings.Split(input, ",")
  34. for _, element := range list {
  35. if element != "" {
  36. result = append(result, element)
  37. }
  38. }
  39. return result
  40. }
  41. func initializeConfig(configPath, section string) *Config {
  42. if configPath == "" {
  43. configPath = "/etc/sendmail.toml"
  44. // Ignore non existing config only if `-conf` not used on command line.
  45. if _, err := os.Stat(configPath); os.IsNotExist(err) {
  46. return NewConfig()
  47. }
  48. }
  49. config, err := readConfig(configPath, section)
  50. if err != nil {
  51. if os.IsNotExist(err) {
  52. Error.F("Error in parsing the configuration\n%s", err)
  53. os.Exit(-2)
  54. }
  55. }
  56. Debug.F("---\nConfig from %s\n%s", configPath, *config)
  57. return config
  58. }
  59. func main() {
  60. var err error
  61. var configPath, section, serverAddress, user, password, to, cc, bcc, from, subject, text string
  62. var encryption, dbg, versionFlag bool
  63. var serverPortAux int
  64. var serverPort int64
  65. flag.BoolVar(&versionFlag, "version", false, "Prints the version and exits")
  66. flag.StringVar(&configPath, "conf", "", "Path to a config file (defaults to /etc/sendmail.toml)")
  67. flag.StringVar(&section, "section", "default", "Section of the conf to read (defaults to \"default\")")
  68. flag.BoolVar(&dbg, "dbg", false, "Enable debugging output")
  69. flag.StringVar(&serverAddress, "server-address", "", "The SMTP server address")
  70. flag.IntVar(&serverPortAux, "server-port", 0, "The SMTP server")
  71. flag.BoolVar(&encryption, "force-ssl", false, "Force the use of ssl (defalut: false)")
  72. flag.StringVar(&user, "user", "", "The user to authenticate with to the server")
  73. flag.StringVar(&password, "password", "", "The password to authenticate with to the server")
  74. flag.StringVar(&to, "to", "", "Comma-separated list of recipient(s)")
  75. flag.StringVar(&cc, "cc", "", "Comma-separated list of carbon-copy recipient(s)")
  76. flag.StringVar(&bcc, "bcc", "", "Comma-separated list of blind carbon-copy recipient(s)")
  77. flag.StringVar(&from, "from", "", "Sender of the mail (used as default account user to log in on the SMTP server)")
  78. flag.StringVar(&subject, "sub", "", "Subject of the mail")
  79. flag.Parse()
  80. LogInit(dbg)
  81. if versionFlag {
  82. showVersion := noVersion
  83. if version != "" {
  84. showVersion = version
  85. }
  86. Info.F("Version: %s", showVersion)
  87. os.Exit(0)
  88. }
  89. if flag.NArg() == 0 {
  90. text = readFromConsole()
  91. } else {
  92. for _, arg := range flag.Args() {
  93. text += fmt.Sprintf("%s\n", arg)
  94. }
  95. }
  96. serverPort = int64(serverPortAux)
  97. Debug.F(
  98. `
  99. ---
  100. parameters:
  101. conf: %s
  102. dbg: %t
  103. address: %s
  104. port: %d
  105. encryption: %t
  106. user: %s
  107. password: %s
  108. to: %s
  109. from: %s
  110. subject: %s`,
  111. configPath,
  112. dbg,
  113. serverAddress,
  114. serverPort,
  115. encryption,
  116. user,
  117. password,
  118. to,
  119. from,
  120. subject,
  121. )
  122. config := initializeConfig(configPath, section)
  123. config.Server.Merge("Address", serverAddress)
  124. config.Server.Merge("Port", serverPort)
  125. config.Server.Merge("Encryption", encryption)
  126. config.Server.Merge("User", user)
  127. config.Server.Merge("Password", password)
  128. config.Merge("From", from)
  129. config.Merge("To", toList(to))
  130. config.Merge("Cc", toList(cc))
  131. config.Merge("Bcc", toList(bcc))
  132. config.Merge("Subject", subject)
  133. config.Merge("Text", text)
  134. Debug.F("---\nPre-validation config\n%s", config)
  135. err = config.Validate()
  136. if err != nil {
  137. Error.F("Config validation failed:\n%s\n", err)
  138. os.Exit(1)
  139. }
  140. Info.F("Sending mail | to: %s", config.To)
  141. SendMail(config)
  142. }