cli

package
v0.0.0-...-2dc86d1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 21, 2017 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CliCommands = []cgCli.Command{
	alias,
	Queue,
	Routes,
	user,
	Rcpthost,
	RelayIP,

	Dkim,
}

CliCommands is a slice of subcomands

View Source
var Dkim = cgCli.Command{

	Name:  "dkim",
	Usage: "Commands to manage DKIM",

	Subcommands: []cgCli.Command{
		{
			Name:        "enable",
			Usage:       "Activate DKIM on domain DOMAIN",
			Description: "To enable DKIM on domain DOMAIN:\n\ttmail dkim enable DOMAIN",
			Action: func(c *cgCli.Context) {
				if len(c.Args()) != 1 {
					cliDieBadArgs(c)
				}
				dkc, err := api.DkimEnable(c.Args().First())
				cliHandleErr(err)
				println("Done !")
				fmt.Printf("It remains for you to create this TXT record on %s._domainkey.%s zone:\n\nv=DKIM1;k=rsa;s=email;h=sha256;p=%s\n\n", dkc.Selector, c.Args().First(), dkc.PubKey)
				println("And... That's all.")

				cliDieOk()
			},
		}, {
			Name:        "disable",
			Usage:       "Disable DKIM on domain DOMAIN",
			Description: "TO disable DKIM on domain DOMAIN\n\ttmail dkim disable DOMAIN",
			Action: func(c *cgCli.Context) {
				if len(c.Args()) != 1 {
					cliDieBadArgs(c)
				}
				err := api.DkimDisable(c.Args().First())
				cliHandleErr(err)
				cliDieOk()
			},
		}, {
			Name:        "getprivkey",
			Usage:       "Return the private key of domain DOMAIN",
			Description: "tmail dkim getprivkey DOMAIN",
			Action: func(c *cgCli.Context) {
				if len(c.Args()) != 1 {
					cliDieBadArgs(c)
				}
				domain := c.Args().First()
				dkc, err := api.DkimGetConfig(domain)
				cliHandleErr(err)
				if dkc != nil {
					println(dkc.PrivKey)
				} else {
					println("DKIM is not enabled for " + domain)
					println("To enable DKIM on " + domain + " run command:")
					println("tmail dkim enable " + domain)
				}

				cliDieOk()
			},
		}, {
			Name:        "getpubkey",
			Usage:       "Return the public key of domain DOMAIN",
			Description: "tmail dkim getpubkey DOMAIN",
			Action: func(c *cgCli.Context) {
				if len(c.Args()) != 1 {
					cliDieBadArgs(c)
				}
				domain := c.Args().First()
				dkc, err := api.DkimGetConfig(domain)
				cliHandleErr(err)
				if dkc != nil {
					println(dkc.PubKey)
				} else {
					println("DKIM is not enabled for " + domain)
					println("To enable DKIM on " + domain + " run command:")
					println("tmail dkim enable " + domain)
				}

				cliDieOk()
			},
		}, {
			Name:        "getdnsrecord",
			Usage:       "Return the DKIM DNS TXT record for domain DOMAIN",
			Description: "tmail dkim getdnsrecord DOMAIN",
			Action: func(c *cgCli.Context) {
				if len(c.Args()) != 1 {
					cliDieBadArgs(c)
				}
				domain := c.Args().First()
				dkc, err := api.DkimGetConfig(domain)
				cliHandleErr(err)
				if dkc != nil {
					fmt.Printf("%s._domainkey.%s zone:\n\nv=DKIM1;k=rsa;s=email;h=sha256;p=%s\n\n", domain, dkc.Selector, dkc.PubKey)
				} else {
					println("DKIM is not enabled for " + domain)
					println("To enable DKIM on " + domain + " run command:")
					println("tmail dkim enable " + domain)
				}

				cliDieOk()
			},
		},
	},
}
View Source
var Queue = cgCli.Command{
	Name:  "queue",
	Usage: "commands to interact with tmail queue",
	Subcommands: []cgCli.Command{

		{
			Name:        "list",
			Usage:       "List messages in queue",
			Description: "tmail queue list",
			Action: func(c *cgCli.Context) {
				var status string
				messages, err := api.QueueGetMessages()
				cliHandleErr(err)
				if len(messages) == 0 {
					println("There is no message in queue.")
				} else {
					fmt.Printf("%d messages in queue.\r\n", len(messages))
					for _, m := range messages {
						switch m.Status {
						case 0:
							status = "Delivery in progress"
						case 1:
							status = "Will be discarded"
						case 2:
							status = "Scheduled"
						case 3:
							status = "Will be bounced"
						}

						msg := fmt.Sprintf("%d - From: %s - To: %s - Status: %s - Added: %v ", m.Id, m.MailFrom, m.RcptTo, status, m.AddedAt)
						if m.Status != 0 {
							msg += fmt.Sprintf("- Next delivery process scheduled at: %v", m.NextDeliveryScheduledAt)
						}
						println(msg)
					}
				}
				os.Exit(0)
			},
		}, {
			Name:        "count",
			Usage:       "count messages in queue",
			Description: "tmail queue count",
			Action: func(c *cgCli.Context) {
				count, err := api.QueueCount()
				cliHandleErr(err)
				println(count)
				os.Exit(0)
			},
		},
		{
			Name:        "discard",
			Usage:       "Discard (delete without bouncing) a message in queue",
			Description: "tmail queue discard MESSAGE_ID",
			Action: func(c *cgCli.Context) {
				if len(c.Args()) != 1 {
					cliDieBadArgs(c)
				}
				id, err := strconv.ParseInt(c.Args()[0], 10, 64)
				cliHandleErr(err)
				cliHandleErr(api.QueueDiscardMsg(id))
				cliDieOk()
			},
		},
		{
			Name:        "bounce",
			Usage:       "Bounce a message in queue",
			Description: "tmail queue bounce MESSAGE_ID",
			Action: func(c *cgCli.Context) {
				if len(c.Args()) != 1 {
					cliDieBadArgs(c)
				}
				id, err := strconv.ParseInt(c.Args()[0], 10, 64)
				cliHandleErr(err)
				cliHandleErr(api.QueueBounceMsg(id))
				cliDieOk()
			},
		},
		{
			Name:        "purge",
			Usage:       "Purge expired message from queue",
			Description: "tmail queue purge",
			Action: func(c *cgCli.Context) {
				cliHandleErr(api.QueuePurge())
				cliDieOk()
			},
		},
	},
}
View Source
var Rcpthost = cgCli.Command{
	Name:  "rcpthost",
	Usage: "commands to manage domains that tmail should handle",
	Subcommands: []cgCli.Command{
		{
			Name:        "add",
			Usage:       "Add a rcpthost",
			Description: "tmail rcpthost add HOSTNAME",
			Flags: []cgCli.Flag{
				cgCli.BoolFlag{
					Name:  "local, l",
					Usage: "Set this flag if it's a remote host.",
				},
			},
			Action: func(c *cgCli.Context) {
				if len(c.Args()) == 0 {
					cliDieBadArgs(c)
				}
				err := api.RcpthostAdd(c.Args().First(), c.Bool("l"), false)
				cliHandleErr(err)
			},
		},

		{
			Name:        "list",
			Usage:       "List rcpthosts",
			Description: "tmail rcpthost list",
			Action: func(c *cgCli.Context) {
				rcpthosts, err := api.RcpthostList()
				cliHandleErr(err)
				if len(rcpthosts) == 0 {
					println("There no rcpthosts.")
				} else {
					for _, host := range rcpthosts {
						line := fmt.Sprintf("%d %s", host.Id, host.Hostname)
						if host.IsLocal {
							line += " local"
						} else {
							line += "remote"
						}
						fmt.Println(line)
					}
				}
			},
		},

		{
			Name:        "del",
			Usage:       "Delete a rcpthost",
			Description: "tmail rcpthost del HOSTNAME",
			Action: func(c *cgCli.Context) {
				if len(c.Args()) == 0 {
					cliDieBadArgs(c)
				}
				err := api.RcpthostDel(c.Args().First())
				cliHandleErr(err)
			},
		},
	},
}

Rcpthost represents commands for dealing with rcpthosts

View Source
var RelayIP = cgCli.Command{
	Name:  "relayip",
	Usage: "commands to authorise IP to relay through tmail",
	Subcommands: []cgCli.Command{

		{
			Name:        "add",
			Usage:       "Add an authorized IP",
			Description: "tmail relayip add IP",
			Action: func(c *cgCli.Context) {
				if len(c.Args()) == 0 {
					cliDieBadArgs(c)
				}
				cliHandleErr(api.RelayIpAdd(c.Args().First()))
			},
		},

		{
			Name:        "list",
			Usage:       "List authorized IP",
			Description: "tmail relayip list",
			Action: func(c *cgCli.Context) {
				ips, err := api.RelayIpGetAll()
				cliHandleErr(err)
				if len(ips) == 0 {
					println("There no athorized IP.")
				} else {
					for _, ip := range ips {
						fmt.Println(fmt.Sprintf("%d %s", ip.Id, ip.Ip))
					}
				}
			},
		},

		{
			Name:        "del",
			Usage:       "Delete an authorized IP",
			Description: "tmail relayip del IP",
			Action: func(c *cgCli.Context) {
				if len(c.Args()) == 0 {
					cliDieBadArgs(c)
				}
				err := api.RelayIpDel(c.Args().First())
				cliHandleErr(err)
			},
		},
	},
}
View Source
var Routes = cgCli.Command{
	Name:  "routes",
	Usage: "commands to manage outgoing SMTP routes",
	Subcommands: []cgCli.Command{
		{
			Name:        "list",
			Usage:       "List routes",
			Description: "tmail routes list",
			Action: func(c *cgCli.Context) {
				routes, err := api.RoutesGet()
				cliHandleErr(err)

				if len(routes) == 0 {
					println("There is no routes configurated, all mails are routed following MX records")
				} else {
					for _, route := range routes {

						line := fmt.Sprintf("%d", route.Id)

						line += " - Destination host: " + route.Host

						if route.MailFrom.Valid && route.MailFrom.String != "" {
							line += " - if mail from: " + route.MailFrom.String
						}

						line += " - Prority: "
						if route.Priority.Valid && route.Priority.Int64 != 0 {
							line += fmt.Sprintf("%d", route.Priority.Int64)
						} else {
							line += "1"
						}

						line += " - Local IPs: "
						if route.LocalIp.Valid && route.LocalIp.String != "" {
							line += route.LocalIp.String
						} else {
							line += "default"
						}

						line += " - Remote host: "
						if route.SmtpAuthLogin.Valid && route.SmtpAuthLogin.String != "" {
							line += route.SmtpAuthLogin.String
							if route.SmtpAuthPasswd.Valid && route.SmtpAuthPasswd.String != "" {
								line += ":" + route.SmtpAuthPasswd.String
							}
							line += "@"
						}

						line += route.RemoteHost
						if route.RemotePort.Valid && route.RemotePort.Int64 != 0 {
							line += fmt.Sprintf(":%d", route.RemotePort.Int64)
						} else {
							line += ":25"
						}

						println(line)
					}
				}
				os.Exit(0)
			},
		},
		{
			Name:        "add",
			Usage:       "Add a route",
			Description: "tmail routes add -d DESTINATION_HOST -rh REMOTE_HOST [-rp REMOTE_PORT] [-p PRORITY] [-l LOCAL_IP] [-u AUTHENTIFIED_USER] [-f MAIL_FROM] [-rl REMOTE_LOGIN] [-rpwd REMOTE_PASSWD]",
			Flags: []cgCli.Flag{
				cgCli.StringFlag{
					Name:  "destination, d",
					Value: "",
					Usage: "hostame destination, eg domain in rcpt user@domain",
				},
				cgCli.StringFlag{
					Name:  "remote host, rh",
					Value: "",
					Usage: "remote host, eg where email should be deliver",
				}, cgCli.IntFlag{
					Name:  "remotePort, rp",
					Value: 25,
					Usage: "Route port",
				},

				cgCli.IntFlag{
					Name:  "priority, p",
					Value: 1,
					Usage: "Route priority. Lowest-numbered priority routes are the most preferred",
				},
				cgCli.StringFlag{
					Name:  "localIp, l",
					Value: "",
					Usage: "Local IP(s) to use. If you want to add multiple IP separate them by | for round-robin or & for failover. Don't mix & and |",
				},
				cgCli.StringFlag{
					Name:  "smtpUser, u",
					Value: "",
					Usage: "Routes for authentified user user.",
				},
				cgCli.StringFlag{
					Name:  "mailFrom, f",
					Value: "",
					Usage: "Routes for MAIL FROM. User need to be authentified",
				},
				cgCli.StringFlag{
					Name:  "remoteLogin, rl",
					Value: "",
					Usage: "SMTPauth login for remote host",
				},
				cgCli.StringFlag{
					Name:  "remotePasswd, rpwd",
					Value: "",
					Usage: "SMTPauth passwd for remote host",
				},
			},
			Action: func(c *cgCli.Context) {

				host := c.String("d")
				if host == "" {
					host = "*"
				}

				err := api.RoutesAdd(host, c.String("l"), c.String("rh"), c.Int("rp"), c.Int("p"), c.String("u"), c.String("f"), c.String("rl"), c.String("rpwd"))
				cliHandleErr(err)
			},
		},
		{
			Name:        "del",
			Usage:       "Delete a route",
			Description: "tmail routes del ROUTE_ID",
			Action: func(c *cgCli.Context) {
				if len(c.Args()) != 1 {
					cliDieBadArgs(c, "you must provide a route ID")
				}
				routeId, err := strconv.ParseInt(c.Args()[0], 10, 64)
				cliHandleErr(err)
				err = api.RoutesDel(routeId)
				cliHandleErr(err)
			},
		},
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL