Beispiel #1
0
func loadConfig() {

	smallcfg.Load(configFile, &Config)

	if Config.App.Debug {
		logger.AddLevel(lumberjack.DEBUG)
	}

	if Config.App.Port <= 0 || Config.App.Port >= 65534 {
		Config.App.Port = 9000
		logger.Warn("Service Port invalid or not specified in config, defaulting to 9000.")
	}

	if Config.App.TimeOut == 0 {
		Config.App.TimeOut = 5
	}

	if Config.App.AtomicBuffer < 1 {
		Config.App.AtomicBuffer = 1
		logger.Warn("Atomic buffer invalid or not specified in config, defaulting to 1.")
	}
}
Beispiel #2
0
func main() {
	log.Println("Loading configuration...")
	err := smallcfg.Load(configFile, &config)
	if err != nil {
		log.Fatalln(err)
	}
	log.Println("Configuration Loaded!")

	clean := flag.Bool("clean", false, "Enables removal of the contents of the learn folders.")
	flag.Parse()

	log.Println("Initiating SpamAsssassin Bayesian Learning for Vesta Control Panel\n============================")
	log.Println("Running SpamAssassin update...")
	err = runCommand("/usr/bin/sa-update")
	checkErr(err, "SpamAssassin update command exited with errors (check Exit Codes documentation)", false)
	log.Println("SpamAssassin update process completed")

	log.Println("Parsing Mailboxes...")
	mboxes := getMailboxes()

	log.Println("Got Mailboxes, sending to SpamAssassin learning commands...")

	for _, mbox := range mboxes {
		junkpath := filepath.Join(mbox, ".Junk")
		hampath := filepath.Join(mbox, ".NotSpam")

		file, err := os.Stat(junkpath)

		var cmd string

		if checkErr(err, fmt.Sprintf("User @ %q does not have a \".Junk\" folder, skipping.", mbox), false) {
			goto skipjunk
		}

		if !file.IsDir() {
			log.Printf("Warning: %q is not a directory, skipping.", mbox)
			goto skipjunk
		}

		log.Printf("Learning junk mail from mailbox: %q", mbox)
		cmd = fmt.Sprintf("/usr/bin/sa-learn --spam %s/{new,cur}", hampath)
		err = runCommand(cmd)
		checkErr(err, "Unable to run SpamAssassin Learn command (likely empty folder)", false)

		if *clean {
			log.Printf("Cleaning messages from %q because 'clean' flag was set...", junkpath)
			curpath := filepath.Join(junkpath, "/cur")
			newpath := filepath.Join(junkpath, "/new")
			delcur := fmt.Sprintf("rm -f %s/*", curpath)
			delnew := fmt.Sprintf("rm -f %s/*", newpath)

			err = runCommand(delcur)
			checkErr(err, fmt.Sprintf("Unable to delete junk from %q", curpath), false)

			err = runCommand(delnew)
			checkErr(err, fmt.Sprintf("Unable to delete junk from %q", newpath), false)
		}

	skipjunk:

		file, err = os.Stat(hampath)

		if checkErr(err, fmt.Sprintf("User @ %q does not have a \".NotSpam\" folder, skipping.", mbox), false) {
			continue
		}

		if !file.IsDir() {
			log.Printf("Warning: %q is not a directory, skipping.", mbox)
			continue
		}

		log.Printf("Learning non-junk mail from mailbox: %q", mbox)
		cmd = fmt.Sprintf("/usr/bin/sa-learn --ham %s/{new,cur}", hampath)
		err = runCommand(cmd)
		checkErr(err, "Unable to run SpamAssassin Learn command (likely empty folder)", false)

		if *clean {
			log.Printf("Cleaning messages from %q because 'clean' flag was set...", hampath)
			curpath := filepath.Join(hampath, "/cur")
			newpath := filepath.Join(hampath, "/new")
			delcur := fmt.Sprintf("rm -f %s/*", curpath)
			delnew := fmt.Sprintf("rm -f %s/*", newpath)

			err = runCommand(delcur)
			checkErr(err, fmt.Sprintf("Unable to delete messages from %q", curpath), false)

			err = runCommand(delnew)
			checkErr(err, fmt.Sprintf("Unable to delete messages from %q", newpath), false)
		}
	}

	log.Println("Running SpamAssassin database sync...")
	err = runCommand("/usr/bin/sa-learn --sync")
	checkErr(err, "Unable to run SpamAssassin Sync command", true)

	log.Println("Restarting SpamAssassin service...")
	err = runCommand("/etc/init.d/spamassassin restart")
	checkErr(err, "Unable to run SpamAssassin service restart command", true)
	log.Println("Done!\n============================")
}