patch d280db07c18bbe0bee399bd33a23c813c6215823
Author: Enrique D. Bosch <presidev@AT@gmail.com>
Date:   Tue Dec  4 23:41:31 CET 2018
  * Add support for SNI
patch a960193b96b178a92741ceb58850f17b29629c95
Author: Enrique D. Bosch <presidev@AT@gmail.com>
Date:   Tue Dec  4 21:12:31 CET 2018
  * Simplify parser of configuration file
patch 3994fdd764ce973cd13650e4fd820083d9e8b2b5
Author: Enrique D. Bosch <presidev@AT@gmail.com>
Date:   Tue Dec  4 20:53:19 CET 2018
  * Add title to password dialog
patch e8534a5cb083b3d82aff297783e2c5d2d683c8d3
Author: Enrique D. Bosch <presidev@AT@gmail.com>
Date:   Mon Sep 17 16:50:09 CEST 2018
  * Reset client keepalive when server keepalive (untagged "OK still here" like response) is detected
patch 0b7e378659f9aec5eff13490775050929649bda2
Author: Enrique D. Bosch <presidev@AT@gmail.com>
Date:   Sat Sep  8 21:34:57 CEST 2018
  * Set icon to hourglass when the connection is lost and reconnecting
patch 459541a83671f7510008f6add9eed8e5502ea6d3
Author: Enrique D. Bosch <presidev@AT@gmail.com>
Date:   Thu Sep  6 01:00:58 CEST 2018
  * Move reset idle to exit_idle
patch bd7e29cb071eb7be716e9bb785540882402845f3
Author: Enrique D. Bosch <presidev@AT@gmail.com>
Date:   Wed Sep  5 19:21:16 CEST 2018
  * Set icon to hourglass when checking mail (searching) and waiting for logout
patch 54015fb789a93e0b2f72a72ded21190f68fc4f59
Author: Enrique D. Bosch <presidev@AT@gmail.com>
Date:   Tue Sep  4 21:38:00 CEST 2018
  * Replace question mark icon with hourglass
patch 0e5aa389db90fbbc6e6b9515acd3aa92c94731a7
Author: Enrique D. Bosch <presidev@AT@gmail.com>
Date:   Tue Sep  4 19:32:19 CEST 2018
  * Move non-logout code to cleanup function
patch 3eb89bff44a913b4c504d590cf62cd9c47dd3936
Author: Enrique D. Bosch <presidev@AT@gmail.com>
Date:   Tue Sep  4 19:17:08 CEST 2018
  * For logout, exit from idle mode only when it is
patch 7eb7e2ff46a284866aa04fa8679ec731279c63c7
Author: Enrique D. Bosch <presidev@AT@gmail.com>
Date:   Sun Sep  2 21:20:08 CEST 2018
  * Implement correctly LOGOUT command according to RFC 3501, add logout_timeout config option
patch dc30edb7135409c41683a29f0a23684e7805693c
Author: Enrique D. Bosch <presidev@AT@gmail.com>
Date:   Sun Sep  2 19:44:50 CEST 2018
  * Use defaulted argument in imap_command_tagged function, ifempty function not necessary
patch 1fa87126792bb5a286620f7604fe9c0affd81637
Author: Enrique D. Bosch <presidev@AT@gmail.com>
Date:   Fri Aug 31 22:07:57 CEST 2018
  * Simplify ifempty function
patch c10ef6fde35c32a02b4d6ceb7731589dd6e207a0
Author: Enrique D. Bosch <presidev@AT@gmail.com>
Date:   Fri Aug 31 21:32:14 CEST 2018
  * Simplify interface for imap_command_tagged function
patch 564fe5aa95af90b3fb9e3d522c114fecd249b914
Author: Enrique D. Bosch <presidev@AT@gmail.com>
Date:   Fri Aug 31 00:48:30 CEST 2018
  * Set some defaults for missing config options
patch 6cb864e02d9646202d1c02fce406e57ba2059f46
Author: Enrique D. Bosch <presidev@AT@gmail.com>
Date:   Thu Aug 30 00:52:43 CEST 2018
  * Add imap_trace option to show IMAP protocol dialog
patch 55c1cbaf4c675e24f783478cf8aaa869449f3c17
Author: Enrique D. Bosch <presidev@AT@gmail.com>
Date:   Fri Aug 24 22:40:19 CEST 2018
  * Add error wrapper, later can be a window dialog or something
patch c074e25fdf925ae76c798e8303f50d1509e2ac79
Author: Enrique D. Bosch <presidev@AT@gmail.com>
Date:   Thu Sep  7 00:08:44 CEST 2017
  * Simplify or, and procs
patch d6716f5f719e0e6b712139717e6df63ca44fd4db
Author: Enrique D. Bosch <presidev@AT@gmail.com>
Date:   Tue Sep  5 22:19:09 CEST 2017
  * Fix style to avoid double substitution
patch dd4031d5b1d6adb28dec8dd4ebef7e6da146ce12
Author: Enrique D. Bosch <presidev@AT@gmail.com>
Date:   Thu Aug 17 02:18:12 CEST 2017
  * Add tag to LOGIN command in debug omiting the credentials (commented)
patch 16034f2b6f19369d43dc6b1c91c184734f75185a
Author: Enrique D. Bosch <presidev@AT@gmail.com>
Date:   Wed Jan 11 01:00:25 CET 2017
  * Change expressions from infix to prefix notation
patch 2b11c67e9dd039c4f3bce6bf120363cfc66bd5d4
Author: Enrique D. Bosch <presidev@AT@gmail.com>
Date:   Tue Jan  3 20:44:31 CET 2017
  * Improve capabilities parsing, remove initial noop by default
patch c70c6d824c3dc25edbb1417483f26cbb303dbc80
Author: Enrique D. Bosch <presidev@AT@gmail.com>
Date:   Tue Jan  3 19:53:17 CET 2017
  * Improve parsing in IDLE mode
patch 6c43ff2e14364f2547e281a6e97b9ddbe1645f81
Author: Enrique D. Bosch <presidev@AT@gmail.com>
Date:   Tue Dec 13 23:21:49 CET 2016
  * Simplify regular expressions
patch 61926820804dbe5661d868d10142ef8ce839735b
Author: Enrique D. Bosch <presidev@AT@gmail.com>
Date:   Tue Dec 13 22:03:31 CET 2016
  * Fix end-of-line character handling
patch 3d92c90730176e9d354ceba7423397306f7196f3
Author: Enrique D. Bosch <presidev@AT@gmail.com>
Date:   Mon Nov 28 00:54:07 CET 2016
  * Correct tls_validate_cert config option (was forced to 1 even when set to 0)
diff -rN -u old-idlebiff-1/.idlebiffrc new-idlebiff-1/.idlebiffrc
--- old-idlebiff-1/.idlebiffrc	2025-10-31 11:07:43.984145990 +0100
+++ new-idlebiff-1/.idlebiffrc	2025-10-31 11:07:43.996145974 +0100
@@ -10,8 +10,10 @@
 mailbox=INBOX
 tls=1
 port=993
+initial_noop=0
 keepalive=1500
 timeout=20
+timeout_logout=2
 png_up=up.png
 png_down=down.png
 png_chk=check.png
@@ -19,6 +21,7 @@
 pos_y=1
 tip=1
 nodecor=1
+imap_trace=0
 criteria=
 tls_validate_cert=1
 tls_ca_dir=
Binary files old-idlebiff-1/check.png and new-idlebiff-1/check.png differ
diff -rN -u old-idlebiff-1/idlebiff new-idlebiff-1/idlebiff
--- old-idlebiff-1/idlebiff	2025-10-31 11:07:43.996145974 +0100
+++ new-idlebiff-1/idlebiff	2025-10-31 11:07:43.996145974 +0100
@@ -26,10 +26,22 @@
 package require png
 package require tooltip
 
+namespace path ::tcl::mathop
+proc or args { foreach x $args { if { $x } { return 1 } }; return 0 }
+proc and args { foreach x $args { if { $x } continue  { return 0 } }; return 1 }
+
+proc setappend { var args } { upvar 1 $var v; set v [ join $args {} ] }
+
+proc check_conf_and_set_default { opt default } \
+{
+  variable ::shared::conf
+
+  if {![ info exists conf($opt) ]} { set conf($opt) $default }
+}
 
 namespace eval static \
 {
-  variable tag 1
+  variable ntag 1
   variable ccsp 0
   variable pmsgs 0
   variable search_resp 0
@@ -41,13 +53,15 @@
   variable idle 0
   array set cap \
   {
-    idle 0
     logindisabled 0
+    logindisabled_chk 0
+    chk 0
+    idle 0
     esearch 0
   }
   variable search
+  variable rexp_chk
   variable rexpsearch
-  variable criteria
   variable sock
   variable conf
   array set conf {}
@@ -60,9 +74,10 @@
   variable img_down
   variable img_chk
   variable new_messages 0
+  variable tag
 }
 
-proc define_pixmaps { } \
+proc define_pixmaps {} \
 {
   variable ::shared::img_up
   variable ::shared::img_down
@@ -74,7 +89,7 @@
   set img_chk [ image create photo -format png -file $conf(png_chk) ]
 }
 
-proc icon { } \
+proc icon {} \
 {
   variable ::shared::img_up
   variable ::shared::img_down
@@ -93,13 +108,13 @@
   set png_chk_w [ dict get $pnginfo width ]
   set png_chk_h [ dict get $pnginfo height ]
 
-  set size_not_same [ expr $png_up_w!=$png_down_w || $png_down_w!=$png_chk_w || $png_up_h!=$png_down_h || $png_down_h!=$png_chk_h ]
-  if $size_not_same { error }
+  set size_not_same [ or [ != $png_up_w $png_down_w ] [ != $png_down_w $png_chk_w ] [ != $png_up_h $png_down_h ] [ != $png_down_h $png_chk_h ] ]
+  if { $size_not_same } { emit_error size }
 
   wm title . idlebiff
   wm iconname . idlebiff
-  append geom [ expr $png_up_w + 10 ] x [ expr $png_up_h + 10 ]
-  if { $conf(pos_x) && $conf(pos_y) } { append geom + $conf(pos_x) + $conf(pos_y) }
+  append geom [ + $png_up_w 10 ] x [ + $png_up_h 10 ]
+  if {[ and $conf(pos_x) $conf(pos_y) ]} { append geom + $conf(pos_x) + $conf(pos_y) }
   wm geometry . $geom
   wm resizable . 0 0
   wm overrideredirect . $conf(nodecor)
@@ -125,11 +140,12 @@
   variable ::shared::conf
   upvar #0 pass pass
 
-  if $bad { set textbad "WRONG PASSWORD\n" } \
-  else { set textbad "" }
-  if !$conf(tls) { set nv "\nWARNING: INSECURE CONNECTION" }
+  if { $bad } { set textbad "WRONG PASSWORD\n" } \
+  else { set textbad {} }
+  if { !$conf(tls) } { set nv "\nWARNING: INSECURE CONNECTION" }
   
   toplevel .tl
+  wm title .tl {idlebiff: password}
   message .tl.msg -bg black -fg yellow -width 400 -justify center \
           -text "${textbad}Enter password for\n$conf(user) at $conf(host)"
   pack .tl.msg -side top -fill both
@@ -153,55 +169,61 @@
   variable ::shared::new_messages
 
   .frame.canvas.button configure -image $img
-  if $conf(tip) { tooltip::tooltip .frame.canvas $new_messages }
+  if { $conf(tip) } { tooltip::tooltip .frame.canvas $new_messages }
 }
 
-proc read_conf { } \
+proc read_conf {} \
 {
   upvar #0 argv arg argc argn
   variable ::shared::conf
 
-  if { $argn == 1 } { set config $arg } else { set config "$::env(HOME)/.idlebiffrc" }
+  if {[ == $argn 1 ]} { set config $arg } else { set config "$::env(HOME)/.idlebiffrc" }
   set cf [ open $config ]
-  while { [ gets $cf lin ] >= 0 } \
+  while {[ >= [ gets $cf lin ] 0 ]} \
   {
-    if { [ string index [ string trim $lin ] 0 ] == "#" } continue
-    set sep [ string first = $lin ]
-    set key [ string range $lin 0 [ expr $sep - 1 ] ]
-    set val [ string range $lin [ expr $sep + 1 ] end ]
+    if {[ == [ string index [ string trim $lin ] 0 ] # ]} continue
+    set lconf [ split $lin = ]
+    set val [ join [ lassign $lconf key ] = ]
     set conf([ string trim $key]) [ string trim $val ]
   }
 }
 
-proc set_params { } \
+proc set_params {} \
 {
-  variable ::shared::conf
-  variable ::shared::criteria
+  variable ::shared::rexp_chk
 
-  if { [ info exists conf(criteria) ] && $conf(criteria)!="" } { set criteria $conf(criteria) } \
-  else { set criteria UNSEEN }
+  check_conf_and_set_default criteria UNSEEN
+  check_conf_and_set_default nodecor 1
+  check_conf_and_set_default tip 0
+  check_conf_and_set_default tls 0
+  check_conf_and_set_default tls_validate_cert 1
+  check_conf_and_set_default initial_noop 0
+  check_conf_and_set_default imap_trace 0
+  set rexp_chk {(?: |\[)capability }
 }
 
-proc set_search_params { } \
+proc set_search_params {} \
 {
   variable ::shared::cap
   variable ::shared::search
   variable ::shared::rexpsearch
-  variable ::shared::criteria
+  variable ::shared::conf
 
-  if $cap(esearch) \
+  if { $cap(esearch) } \
   {
-    set search "SEARCH RETURN (COUNT) $criteria"
-    set rexpsearch {^\*\s+esearch\s+\(tag\s*[^)]*\)\s+count\s+(.*)$}
+    set search "SEARCH RETURN (COUNT) $conf(criteria)"
+    set rexpsearch {^\* esearch \(tag [^)]*\) count (.*)$}
   } \
   else \
   {
-    set search "SEARCH $criteria"
-    set rexpsearch {^\*\s+search\s+(.*)$}
+    set search "SEARCH $conf(criteria)"
+    set rexpsearch {^\* search((?: .*|$))}
   }
 }
 
-proc set_timeout { } \
+proc emit_error args { error $args }
+
+proc set_timeout {} \
 {
   variable ::shared::conf
   variable ::shared::tevent
@@ -209,6 +231,7 @@
 
   set tevent($teidf) [ after ${conf(timeout)}000 \
                        {
+                         update_img $::shared::img_chk
                          cancel_timeout
                          close $con
                          init
@@ -217,12 +240,12 @@
   incr teidf  
 }
 
-proc cancel_timeout { } \
+proc cancel_timeout {} \
 {
   variable ::shared::tevent
   variable ::shared::teidi
 
-  if [ info exists tevent($teidi) ] \
+  if {[ info exists tevent($teidi) ]} \
   {
     after cancel $tevent($teidi)
     unset tevent($teidi)
@@ -232,7 +255,7 @@
 
 proc exec_terse { com args } \
 {
-  if [ catch { set res [ eval "$com [ join $args]" ] } ] \
+  if {[ catch { set res [ eval "$com [ join $args]" ] } ]} \
   {
     set terse [ split $::errorInfo "\n" ]
     puts [ join [ lrange $terse 0 0 ] ]
@@ -245,8 +268,10 @@
 {
   variable ::shared::ka
   variable ::shared::idle
+  variable ::shared::img_chk
 
   after cancel $ka
+  update_img $img_chk
   set idle 0
   catch { close $con }
   after 3000 init
@@ -254,8 +279,14 @@
 
 proc imap_command { con com } \
 {
-#  if [regexp {.*LOGIN.*} $com] {puts "C: LOGIN"} else {puts "C: $com"}
-  if [ catch { puts -nonewline $con "$com\r\n" } ] \
+  variable ::shared::conf
+
+  if { $conf(imap_trace) } \
+  { if {[ regexp {.*LOGIN.*} $com ]} { puts "C: [string range $com 0 10]" } else { puts "C: $com" } }
+
+  set r [ catch { puts $con $com } ]
+  if {[ eq $com LOGOUT ]} return
+  if { $r } \
   {
     recon $con
     return
@@ -263,29 +294,32 @@
   set_timeout
 }
 
-proc imap_command_tagged { con com } \
+proc imap_command_tagged { con com { prefix A } } \
 {
-  variable ::static::tag
+  variable ::static::ntag
+  variable ::shared::tag
 
-  if { $tag == 10000 } { set tag 1 } 
-  append tcom A [ format %04d $tag ] " " $com
+  if {[ == $ntag 10000 ]} { set ntag 1 }
+  setappend tag $prefix [ format %04d $ntag ]
+  append tcom $tag { } $com
   imap_command $con $tcom
-  incr tag
+  incr ntag
 }
 
-proc ok_tagged linea \
+proc ok_tagged { linea { tag {A\d+} } } \
 {
-  return [ regexp -lineanchor -nocase {^A\d+\s+ok.*$} $linea ]
+  append rexp {^} $tag { ok.*$}
+  return [ regexp -lineanchor -nocase $rexp $linea ]
 }
 
 proc ok_untagged linea \
 {
-  return [ regexp -lineanchor -nocase {^\*\s+ok.*$} $linea ]
+  return [ regexp -lineanchor -nocase {^\* ok.*$} $linea ]
 }
 
 proc no_tagged linea \
 {
-  return [ regexp -lineanchor -nocase {^A\d+\s+no.*$} $linea ]
+  return [ regexp -lineanchor -nocase {^A\d+ no.*$} $linea ]
 }
 
 proc login { con retr } \
@@ -293,27 +327,40 @@
   variable ::shared::conf
   variable ::shared::cap
 
-  if $cap(logindisabled) { error logindisabled }
+  if { $cap(logindisabled) } { emit_error logindisabled }
 
-  if $retr { unset conf(pass) }
-  if ![ info exists conf(pass) ] { set conf(pass) [ get_password $retr ] }
+  if { $retr } { unset conf(pass) }
+  if { ![ info exists conf(pass) ] } { set conf(pass) [ get_password $retr ] }
 
   imap_command_tagged $con "LOGIN $conf(user) $conf(pass)"
 }
 
 proc logout con \
 {
+  variable ::shared::conf
+
+  fileevent $con readable { parse_logout $con }
+  after ${conf(timeout_logout)}000 \
+  {
+    close $con
+    exit 0
+  }
   imap_command_tagged $con LOGOUT
-  close $con
 }
 
 proc cleanup con \
 {
+  variable ::shared::ka
+  variable ::shared::img_chk
+  variable ::shared::idle
+
+  update_img $img_chk
+  after cancel $ka
+  if { $idle } { exit_idle $con }
   logout $con
-  exit 0
 }
 
-proc force_down { } \
+proc force_down {} \
 {
   variable ::shared::img_down
 
@@ -322,11 +369,14 @@
 
 proc enter_idle con \
 {
-  imap_command_tagged $con IDLE  
+  imap_command_tagged $con IDLE
 }
 
 proc exit_idle con \
 {
+  variable ::shared::idle
+
+  set idle 0
   imap_command $con DONE
 }
 
@@ -345,31 +395,40 @@
 proc do_search con \
 {
   variable ::shared::search
+  variable ::shared::img_chk
 
+  update_img $img_chk
   imap_command_tagged $con $search
 }
 
-proc check_cap { linea capname } \
+proc check_cap { linea args } \
 {
   variable ::shared::cap
+  variable ::shared::rexp_chk
 
-  if !$cap($capname) \
+  if {[ regexp -nocase -lineanchor $rexp_chk $linea ]} \
   {
-    set rexp ".*capability .*${capname}(?: .*|].*|$)"
-    set cap($capname) [ regexp -nocase -lineanchor $rexp $linea ]
+    if {[ eq $args logindisabled ]} { set cap(logindisabled_chk) 1 } \
+    else { set cap(chk) 1 }
+    foreach capname $args \
+    {
+      if { !$cap($capname) } \
+      {
+        set rexp_cap " ${capname}(?: |]|$)"
+        set cap($capname) [ regexp -nocase -lineanchor $rexp_cap $linea ]
+      }
+    }
   }
 }
 
 proc idle_cycle { con check } \
 {
-  variable ::shared::idle
   variable ::shared::conf
   variable ::shared::ka
 
   after cancel $ka
-  set idle 0
   exit_idle $con
-  if $check { do_search $con } \
+  if { $check } { do_search $con } \
   else { enter_idle $con }
   set ka [ after ${conf(keepalive)}000 { idle_cycle $con 0 } ]
 }
@@ -378,7 +437,9 @@
 {
   variable ::shared::conf
   variable ::shared::ka
+  variable ::shared::cap
 
+  if { !$cap(idle) } { emit_error noidle }
   fileevent $con readable { parse_idle_check $con }
   enter_mailbox $con
   set_search_params
@@ -388,19 +449,38 @@
 
 proc parse_greeting con \
 {
+  variable ::shared::cap
+
   set linea [ read_line $con ]
   check_cap $linea logindisabled
-  if [ ok_untagged $linea ] { fileevent $con readable { parse_noop $con } }
+  if {[ ok_untagged $linea ]} \
+  {
+    if { $cap(logindisabled_chk) } \
+    {
+      fileevent $con readable { parse_login $con }
+      login $con 0
+    } \
+    else \
+    {
+      fileevent $con readable { parse_logindisabled $con }
+      imap_command_tagged $con CAPABILITY
+    }
+  }
 }
 
-proc parse_noop con \
+proc parse_logindisabled con \
 {
+  variable ::shared::cap
+
   set linea [ read_line $con ]
   check_cap $linea logindisabled
-  if [ ok_tagged $linea ] \
+  if {[ ok_tagged $linea ]} \
   {
-    fileevent $con readable { parse_login $con }
-    login $con 0
+    if { $cap(logindisabled_chk) } \
+    {
+      fileevent $con readable { parse_login $con }
+      login $con 0
+    }
   }
 }
 
@@ -409,42 +489,47 @@
   variable ::shared::cap
 
   set linea [ read_line $con ]
-  check_cap $linea idle
-  check_cap $linea esearch
-  if [ ok_tagged $linea ] \
+  check_cap $linea idle esearch
+  if {[ ok_tagged $linea ]} \
   {
-    if $cap(idle) { init_idle $con } \
+    if { $cap(chk) } { init_idle $con } \
     else \
     {
       fileevent $con readable { parse_cap $con }
       imap_command_tagged $con CAPABILITY
     }
   } \
-  elseif [ no_tagged $linea ] { login $con 1 }
+  elseif {[ no_tagged $linea ]} { login $con 1 }
 }
 
 proc parse_cap con \
 {
-  variable ::shared::cap
-
   set linea [ read_line $con ]
-  check_cap $linea idle
-  check_cap $linea esearch
-  if [ ok_tagged $linea ] \
-  {
-    if $cap(idle) { init_idle $con } \
-    else { error no idle capability } \
-  } 
+  check_cap $linea idle esearch
+  if {[ ok_tagged $linea ]} { init_idle $con }
 }
 
 proc parse_idle_check con \
 {
   variable ::shared::idle
+  variable ::shared::ka
+  variable ::shared::conf
 
-  if [ catch { set linea [ read_line $con ] } ] return
+  if {[ catch { set linea [ read_line $con ] } ]} return
   set prim [ string range $linea 0 0 ]
-  if { $prim eq "+" } { set idle 1 } \
-  elseif { $idle == 1 } { idle_cycle $con 1 } \
+  if {[ eq $prim + ]} { set idle 1 } \
+  elseif { $idle } \
+  {
+    if {[ eq $prim * ]} \
+    {
+      if {[ ok_untagged $linea ]} \
+      {
+        after cancel $ka
+        set ka [ after ${conf(keepalive)}000 { idle_cycle $con 0 } ]
+      } \
+      else { idle_cycle $con 1 } \
+    } \
+  } \
   else { parse_search $con $linea }
 }
 
@@ -459,46 +544,63 @@
   variable ::static::pmsgs
   variable ::static::search_resp
 
-  if [ regexp -lineanchor -nocase $rexpsearch $linea -> res ]  \
+  if {[ regexp -lineanchor -nocase $rexpsearch $linea -> res ]} \
   {
-    if $cap(esearch) { set pmsgs $res } else { incr pmsgs [ llength $res ] }
+    if { $cap(esearch) } { set pmsgs $res } else { incr pmsgs [ llength $res ] }
     set search_resp 1
   }
-  if { $search_resp && [ ok_tagged $linea ] } \
+  if {[ and $search_resp [ ok_tagged $linea ] ]} \
   {
     enter_idle $con
     set new_messages $pmsgs
     set pmsgs 0
     set search_resp 0
-    if { $new_messages > 0 } \
+    if {[ > $new_messages 0 ]} \
          { set current $img_up } \
     else { set current $img_down }
     update_img $current
   }
-  if [ no_tagged $linea ] { error search }
+  if {[ no_tagged $linea ]} { emit_error search }
+}
+
+proc parse_logout con \
+{
+  variable ::shared::conf
+  variable ::shared::tag
+
+  if {[ catch { gets $con linea } ]} return
+  if { $conf(imap_trace) } { puts "S: $linea" }
+  if {[ ok_tagged $linea $tag ]} \
+  {
+    close $con
+    exit 0
+  }
 }
 
 proc read_line con \
 {
+  variable ::shared::conf
+
   cancel_timeout
-  if [ catch { set r [ gets $con line ] } ] \
+  if {[ catch { set r [ gets $con line ] } ]} \
   {
     recon $con
     return -code error
   }
-  if [ regexp -lineanchor -nocase {^\*\s+bye .*$} $line ] \
+  if {[ regexp -lineanchor -nocase {^\* bye(?: .*|$)} $line ]} \
   {
     recon $con
     return -code error
   }
-  if { $r < 1 && [ eof $con ] } \
+  if {[ and [ < $r 1 ] [ eof $con ] ]} \
   {
     recon $con
     return -code error
   } \
   else \
   {
-#    puts "S: $line"
+    if { $conf(imap_trace) } { puts "S: $line" }
+
     return $line
   }
 }
@@ -517,52 +619,61 @@
   variable ::static::ccsp
 
   array set ca $cert
-  if { $conf(tls_ca_subject) ne "" && $type eq "verify" && $ca(subject) eq $ca(issuer) } \
+  if {[ info exists conf(tls_ca_subject) ]} \
   {
-    array set csu [ parse_cert_subject $ca(subject) ]
-    if !$ccsp \
+    if {[ and [ ne $conf(tls_ca_subject) {} ] [ eq $type verify ] [ eq $ca(subject) $ca(issuer) ] ]} \
     {
-      array set ccsu [ parse_cert_subject $conf(tls_ca_subject) ]
-      set ccsp 1
+      array set csu [ parse_cert_subject $ca(subject) ]
+      if { !$ccsp } \
+      {
+        array set ccsu [ parse_cert_subject $conf(tls_ca_subject) ]
+        set ccsp 1
+      }
+      foreach i [ array names ccsu ] \
+      {
+        if { ![ info exists csu($i) ] } { emit_error cert }
+        if {[ ne $csu($i) $ccsu($i) ]} { emit_error cert }
+      }
     }
-    foreach i [ array names ccsu ] \
-    { if { ![ info exists csu($i) ] || $csu($i)!=$ccsu($i) } { error cert } }
   }
 }
 
-proc init_tls { } \
+proc init_tls {} \
 {
   variable ::shared::conf
   variable ::shared::sock
 
-  if $conf(tls) \
+  if { $conf(tls) } \
   {
     set tlsv [ package require tls ]
-    if { "$tlsv" >= "1.6.4" } { set tls1x {-tls1.1 1 -tls1.2 1} } \
+    if {[ >= $tlsv 1.6.4 ]} { set tls1x {-tls1.1 1 -tls1.2 1} } \
     else { set tls1x {} }
+    if {[ info exists conf(sni) ]} { set sni "-servername $conf(sni)" } \
+    else { set sni {} }
     set sock "::tls::socket -ssl2 0 -ssl3 0 -tls1 1 $tls1x \
       -cafile   \"$conf(tls_ca_file)\" \
       -cadir    \"$conf(tls_ca_dir)\" \
       -certfile \"$conf(tls_client_cert)\" \
       -keyfile  \"$conf(tls_client_key)\" \
-      -request  1 \
+      -request  \"$conf(tls_validate_cert)\" \
       -require  \"$conf(tls_validate_cert)\" \
       -cipher   \"$conf(tls_ciphers)\" \
+      $sni \
       -command  tls_cb"
   } \
   else { set sock socket }
-}  
+}
 
-proc init { } \
+proc init {} \
 {
   upvar #0 con con
   variable ::shared::conf
   variable ::shared::sock
 
   set con [ exec_terse $sock -async $conf(host) $conf(port) ]
-  fconfigure $con -blocking 0 -buffering line
+  fconfigure $con -blocking 0 -buffering line -translation {crlf crlf}
   fileevent $con readable { parse_greeting $con }
-  imap_command_tagged $con NOOP
+  if { $conf(initial_noop) } { imap_command_tagged $con NOOP N }
 }
 
 read_conf