Commits

Ivan Vučica  committed bb25216

STARTTLS.

  • Participants
  • Parent commits 1a58287

Comments (0)

Files changed (1)

File connection.go

 	"net/textproto"
 	"fmt"
 	"strings"
+	"crypto/tls"
 )
 
 func HandleConnection(conn net.Conn) {
+	defer conn.Close()
 	textconn := textproto.NewConn(conn)
-	defer textconn.Close()
+
+	cert, err := tls.LoadX509KeyPair("/tmp/goimapd.crt", "/tmp/goimapd.key")
+	if err != nil {
+		fmt.Printf("loadkeys: %s\n", err)
+		return
+	}
+	tlsconfig := &tls.Config {
+		Certificates: []tls.Certificate{cert},
+	}
 
 	textconn.PrintfLine("* OK goimapd ready")
 	for {
 				textconn.PrintfLine("%s OK LOGOUT complete.", tag)
 				return
 			case "CAPABILITY":
-				textconn.PrintfLine("* CAPABILITY IMAP4rev1")
+				textconn.PrintfLine("* CAPABILITY IMAP4rev1 STARTTLS")
 				textconn.PrintfLine("%s OK CAPABILITY complete.", tag)
 			case "LOGIN":
 				textconn.PrintfLine("%s OK LOGIN complete.", tag)
 				for i := 0; i < 5; i++ {
 					textconn.PrintfLine("* %d FETCH (UID %d FLAGS (\\Seen))", i+1, i+1)
 				}
-				textconn.PrintfLine("%s OK ID FETCH complete.", tag)
+				textconn.PrintfLine("%s OK UID FETCH complete.", tag)
 			case "FETCH":
 				for i := 0; i < 5; i++ {
 					textconn.PrintfLine("* %d FETCH (UID %d FLAGS (\\Flagged) BODY[HEADER.FIELDS (From To Cc Bcc Subject Date Message-ID Priority X-Priority References Newsgroups In-Reply-To Content-Type Reply-To)] {73}", i+1, i+1)
 				}
 			case "CREATE":
 				textconn.PrintfLine("%s NO No mailbox creation supported.", tag)
+			case "STARTTLS":
+				textconn.PrintfLine("%s OK STARTTLS commencing.", tag)
+				conn = tls.Server(conn, tlsconfig)
+				textconn = textproto.NewConn(conn)
 			default:
 				textconn.PrintfLine("%s BAD No idea what %s is.", tag, cmd)
 				fmt.Printf("Unknown command: %v\n", msg)