Improve capabilities parsing, remove initial noop by default
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
diff -rN -u old-idlebiff/.idlebiffrc new-idlebiff/.idlebiffrc
--- old-idlebiff/.idlebiffrc 2024-11-22 16:27:01.432246364 +0100
+++ new-idlebiff/.idlebiffrc 2024-11-22 16:27:01.432246364 +0100
@@ -10,6 +10,7 @@
mailbox=INBOX
tls=1
port=993
+initial_noop=0
keepalive=1500
timeout=20
png_up=up.png
diff -rN -u old-idlebiff/idlebiff new-idlebiff/idlebiff
--- old-idlebiff/idlebiff 2024-11-22 16:27:01.432246364 +0100
+++ new-idlebiff/idlebiff 2024-11-22 16:27:01.432246364 +0100
@@ -41,11 +41,14 @@
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
@@ -177,9 +180,11 @@
{
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 }
+ set rexp_chk {(?: |\[)capability }
}
proc set_search_params { } \
@@ -263,19 +268,20 @@
set_timeout
}
-proc imap_command_tagged { con com } \
+proc imap_command_tagged { con prefix com } \
{
variable ::static::tag
- if { $tag == 10000 } { set tag 1 }
- append tcom A [ format %04d $tag ] " " $com
+ if { $tag == 10000 } { set tag 1 }
+ append tcom $prefix [ format %04d $tag ] " " $com
imap_command $con $tcom
incr tag
}
-proc ok_tagged linea \
+proc ok_tagged { linea prefix } \
{
- return [ regexp -lineanchor -nocase {^A\d+ ok.*$} $linea ]
+ append rexp {^} $prefix {\d+ ok.*$}
+ return [ regexp -lineanchor -nocase $rexp $linea ]
}
proc ok_untagged linea \
@@ -298,12 +304,12 @@
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)"
+ imap_command_tagged $con A "LOGIN $conf(user) $conf(pass)"
}
proc logout con \
{
- imap_command_tagged $con LOGOUT
+ imap_command_tagged $con A LOGOUT
close $con
}
@@ -322,7 +328,7 @@
proc enter_idle con \
{
- imap_command_tagged $con IDLE
+ imap_command_tagged $con A IDLE
}
proc exit_idle con \
@@ -334,29 +340,38 @@
{
variable ::shared::conf
- imap_command_tagged $con "EXAMINE $conf(mailbox)"
+ imap_command_tagged $con A "EXAMINE $conf(mailbox)"
}
proc exit_mailbox con \
{
- imap_command_tagged $con CLOSE
+ imap_command_tagged $con A CLOSE
}
proc do_search con \
{
variable ::shared::search
- imap_command_tagged $con $search
+ imap_command_tagged $con A $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 { $args eq "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 ]
+ }
+ }
}
}
@@ -378,7 +393,9 @@
{
variable ::shared::conf
variable ::shared::ka
+ variable ::shared::cap
+ if !$cap(idle) { error noidle }
fileevent $con readable { parse_idle_check $con }
enter_mailbox $con
set_search_params
@@ -388,19 +405,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 A 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 A ] \
{
- fileevent $con readable { parse_login $con }
- login $con 0
+ if $cap(logindisabled_chk) \
+ {
+ fileevent $con readable { parse_login $con }
+ login $con 0
+ }
}
}
@@ -409,15 +445,14 @@
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 A ] \
{
- if $cap(idle) { init_idle $con } \
+ if $cap(chk) { init_idle $con } \
else \
{
fileevent $con readable { parse_cap $con }
- imap_command_tagged $con CAPABILITY
+ imap_command_tagged $con A CAPABILITY
}
} \
elseif [ no_tagged $linea ] { login $con 1 }
@@ -425,16 +460,9 @@
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 A ] { init_idle $con }
}
proc parse_idle_check con \
@@ -464,7 +492,7 @@
if $cap(esearch) { set pmsgs $res } else { incr pmsgs [ llength $res ] }
set search_resp 1
}
- if { $search_resp && [ ok_tagged $linea ] } \
+ if { $search_resp && [ ok_tagged $linea A ] } \
{
enter_idle $con
set new_messages $pmsgs
@@ -562,7 +590,7 @@
set con [ exec_terse $sock -async $conf(host) $conf(port) ]
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 N NOOP }
}
read_conf