Many changes:

*) rename imap to mail, sort pop3/imap functions
*) smtp auth support
*) pop3 starttls only
*) fix segfault if cram-md5 was used without apop
diff --git a/auto/make b/auto/make
index 029918c..b97d868 100644
--- a/auto/make
+++ b/auto/make
@@ -6,7 +6,7 @@
          $NGX_OBJS/src/os/unix $NGX_OBJS/src/os/win32 \
          $NGX_OBJS/src/http $NGX_OBJS/src/http/modules \
 	 $NGX_OBJS/src/http/modules/perl \
-         $NGX_OBJS/src/imap
+         $NGX_OBJS/src/mail
 
 
 ngx_objs_dir=$NGX_OBJS$ngx_regex_dirsep
@@ -41,7 +41,7 @@
 
 # ALL_INCS, required by the addons and by OpenWatcom C precompiled headers
 
-ngx_incs=`echo $CORE_INCS $NGX_OBJS $HTTP_INCS $IMAP_INCS\
+ngx_incs=`echo $CORE_INCS $NGX_OBJS $HTTP_INCS $MAIL_INCS\
     | sed -e "s/  *\([^ ][^ ]*\)/$ngx_regex_cont$ngx_include_opt\1/g" \
           -e "s/\//$ngx_regex_dirsep/g"`
 
@@ -101,26 +101,26 @@
 fi
 
 
-# the imap dependences and include pathes
+# the mail dependences and include pathes
 
-if [ $IMAP = YES ]; then
+if [ $MAIL = YES ]; then
 
-    ngx_all_srcs="$ngx_all_srcs $IMAP_SRCS"
+    ngx_all_srcs="$ngx_all_srcs $MAIL_SRCS"
 
-    ngx_deps=`echo $IMAP_DEPS \
+    ngx_deps=`echo $MAIL_DEPS \
         | sed -e "s/  *\([^ ][^ ]*\)/$ngx_regex_cont\1/g" \
               -e "s/\//$ngx_regex_dirsep/g"`
 
-    ngx_incs=`echo $IMAP_INCS \
+    ngx_incs=`echo $MAIL_INCS \
         | sed -e "s/  *\([^ ][^ ]*\)/$ngx_regex_cont$ngx_include_opt\1/g" \
               -e "s/\//$ngx_regex_dirsep/g"`
 
     cat << END                                                >> $NGX_MAKEFILE
 
-IMAP_DEPS = $ngx_deps
+MAIL_DEPS = $ngx_deps
 
 
-IMAP_INCS = $ngx_include_opt$ngx_incs
+MAIL_INCS = $ngx_include_opt$ngx_incs
 
 END
 
@@ -279,17 +279,17 @@
 fi
 
 
-# the imap sources
+# the mail sources
 
-if [ $IMAP = YES ]; then
+if [ $MAIL = YES ]; then
 
     if test -n "$NGX_PCH"; then
         ngx_cc="\$(CC) $ngx_compile_opt \$(CFLAGS) $ngx_use_pch \$(ALL_INCS)"
     else
-        ngx_cc="\$(CC) $ngx_compile_opt \$(CFLAGS) \$(CORE_INCS) \$(IMAP_INCS)"
+        ngx_cc="\$(CC) $ngx_compile_opt \$(CFLAGS) \$(CORE_INCS) \$(MAIL_INCS)"
     fi
 
-    for ngx_src in $IMAP_SRCS
+    for ngx_src in $MAIL_SRCS
     do
         ngx_src=`echo $ngx_src | sed -e "s/\//$ngx_regex_dirsep/g"`
         ngx_obj=`echo $ngx_src \
@@ -300,7 +300,7 @@
 
         cat << END                                            >> $NGX_MAKEFILE
 
-$ngx_obj:	\$(CORE_DEPS) \$(IMAP_DEPS)$ngx_cont$ngx_src
+$ngx_obj:	\$(CORE_DEPS) \$(MAIL_DEPS)$ngx_cont$ngx_src
 	$ngx_cc$ngx_tab$ngx_objout$ngx_obj$ngx_tab$ngx_src$NGX_AUX
 
 END
diff --git a/auto/modules b/auto/modules
index a298210..dfd6f12 100644
--- a/auto/modules
+++ b/auto/modules
@@ -302,10 +302,10 @@
 fi
 
 
-if [ $IMAP_SSL = YES ]; then
-    IMAP_DEPS="$IMAP_DEPS $IMAP_SSL_DEPS"
-    IMAP_SRCS="$IMAP_SRCS $IMAP_SSL_SRCS"
-    have=NGX_IMAP_SSL . auto/have
+if [ $MAIL_SSL = YES ]; then
+    MAIL_DEPS="$MAIL_DEPS $MAIL_SSL_DEPS"
+    MAIL_SRCS="$MAIL_SRCS $MAIL_SSL_SRCS"
+    have=NGX_MAIL_SSL . auto/have
     USE_OPENSSL=YES
 fi
 
@@ -331,18 +331,18 @@
 fi
 
 
-if [ $IMAP = YES ]; then
-    modules="$modules $IMAP_MODULES"
+if [ $MAIL = YES ]; then
+    modules="$modules $MAIL_MODULES"
 
-    if [ $IMAP_SSL = YES ]; then
-        modules="$modules $IMAP_SSL_MODULE"
+    if [ $MAIL_SSL = YES ]; then
+        modules="$modules $MAIL_SSL_MODULE"
     fi
 
-    modules="$modules $IMAP_AUTH_HTTP_MODULE"
-    IMAP_SRCS="$IMAP_SRCS $IMAP_AUTH_HTTP_SRCS"
+    modules="$modules $MAIL_AUTH_HTTP_MODULE"
+    MAIL_SRCS="$MAIL_SRCS $MAIL_AUTH_HTTP_SRCS"
 
-    modules="$modules $IMAP_PROXY_MODULE"
-    IMAP_SRCS="$IMAP_SRCS $IMAP_PROXY_SRCS"
+    modules="$modules $MAIL_PROXY_MODULE"
+    MAIL_SRCS="$MAIL_SRCS $MAIL_PROXY_SRCS"
 fi
 
 
diff --git a/auto/options b/auto/options
index c0bc3b4..379bb23 100644
--- a/auto/options
+++ b/auto/options
@@ -77,8 +77,8 @@
 # STUB
 HTTP_STUB_STATUS=NO
 
-IMAP=NO
-IMAP_SSL=NO
+MAIL=NO
+MAIL_SSL=NO
 
 NGX_ADDONS=
 
@@ -182,8 +182,11 @@
         # STUB
         --with-http_stub_status_module)  HTTP_STUB_STATUS=YES       ;;
 
-        --with-imap)                     IMAP=YES                   ;;
-        --with-imap_ssl_module)          IMAP_SSL=YES               ;;
+        --with-mail)                     MAIL=YES                   ;;
+        --with-mail_ssl_module)          MAIL_SSL=YES               ;;
+        # STUB
+        --with-imap)                     MAIL=YES                   ;;
+        --with-imap_ssl_module)          MAIL_SSL=YES               ;;
 
         --add-module=*)                  NGX_ADDONS="$NGX_ADDONS $value" ;;
 
@@ -292,8 +295,8 @@
 
   --without-http                     disable HTTP server
 
-  --with-imap                        enable IMAP4/POP3 proxy module
-  --with-imap_ssl_module             enable ngx_imap_ssl_module
+  --with-mail                        enable IMAP4/POP3/SMTP proxy module
+  --with-mail_ssl_module             enable ngx_mail_ssl_module
 
   --with-cc=PATH                     set path to C compiler
   --with-cpp=PATH                    set path to C preprocessor
diff --git a/auto/sources b/auto/sources
index 0ff1ece..0632d39 100644
--- a/auto/sources
+++ b/auto/sources
@@ -408,23 +408,23 @@
 HTTP_UPSTREAM_IP_HASH_SRCS=src/http/modules/ngx_http_upstream_ip_hash_module.c
 
 
-IMAP_INCS="src/imap"
+MAIL_INCS="src/mail"
 
-IMAP_DEPS="src/imap/ngx_imap.h"
+MAIL_DEPS="src/mail/ngx_mail.h"
 
-IMAP_MODULES="ngx_imap_module ngx_imap_core_module"
+MAIL_MODULES="ngx_mail_module ngx_mail_core_module"
 
-IMAP_SRCS="src/imap/ngx_imap.c \
-           src/imap/ngx_imap_core_module.c \
-           src/imap/ngx_imap_handler.c \
-           src/imap/ngx_imap_parse.c"
+MAIL_SRCS="src/mail/ngx_mail.c \
+           src/mail/ngx_mail_core_module.c \
+           src/mail/ngx_mail_handler.c \
+           src/mail/ngx_mail_parse.c"
 
-IMAP_SSL_MODULE="ngx_imap_ssl_module"
-IMAP_SSL_DEPS="src/imap/ngx_imap_ssl_module.h"
-IMAP_SSL_SRCS="src/imap/ngx_imap_ssl_module.c"
+MAIL_SSL_MODULE="ngx_mail_ssl_module"
+MAIL_SSL_DEPS="src/mail/ngx_mail_ssl_module.h"
+MAIL_SSL_SRCS="src/mail/ngx_mail_ssl_module.c"
 
-IMAP_AUTH_HTTP_MODULE="ngx_imap_auth_http_module"
-IMAP_AUTH_HTTP_SRCS="src/imap/ngx_imap_auth_http_module.c"
+MAIL_AUTH_HTTP_MODULE="ngx_mail_auth_http_module"
+MAIL_AUTH_HTTP_SRCS="src/mail/ngx_mail_auth_http_module.c"
 
-IMAP_PROXY_MODULE="ngx_imap_proxy_module"
-IMAP_PROXY_SRCS="src/imap/ngx_imap_proxy_module.c"
+MAIL_PROXY_MODULE="ngx_mail_proxy_module"
+MAIL_PROXY_SRCS="src/mail/ngx_mail_proxy_module.c"