sup

A curses threads-with-tags style email client

sup.git

git clone https://supmua.dev/git/sup/
commit 9e3da384bc860437f06f1fed53c933da4b8a25e5
parent 96113d0a62b2deb613cd1d54d59fdc2ad534acb7
Author: Eric Weikl <eric.weikl@gmx.net>
Date:   Tue,  1 Oct 2013 14:40:13 +0200

Track change in syncback flag per source, warn user

Check per source if the sync_back flag changed from false to true. If so,
warn the user. sup-sync-back-maildir now stores the URIs of all sources that
have been synced by it in the file sync-back-ok, which we already write as a
status flag.

We need an extra verification step during startup, since index and sources
need to be initialized. It might be better to load the sources separately,
though, just to keep things nicely separated.

Diffstat:
M bin/sup | 1 +
M bin/sup-sync-back-maildir | 7 ++++---
M lib/sup.rb | 54 ++++++++++++++++++++++++++++++++++++++++--------------
3 files changed, 45 insertions(+), 17 deletions(-)
diff --git a/bin/sup b/bin/sup
@@ -153,6 +153,7 @@ Index.lock_interactively or exit
 begin
   Redwood::start
   Index.load
+  Redwood::check_syncback_settings
   Index.start_sync_worker unless $opts[:no_threads]
 
   $die = false
diff --git a/bin/sup-sync-back-maildir b/bin/sup-sync-back-maildir
@@ -55,6 +55,7 @@ index.load
 $config[:sync_back_to_maildir] = true
 
 begin
+  sync_performed = File.readlines Redwood::SYNC_OK_FN
   sources = []
 
   ## Try to find out sources given in parameters
@@ -100,10 +101,10 @@ begin
         end
       end
       print "\n"
-
-      ## Write a flag file to tell sup that the synchronization has been performed
-      File.open(Redwood::SYNC_OK_FN, 'w') {|f| f.write("OK") }
+      sync_performed << s.uri
     end
+    ## Write a flag file to tell sup that the synchronization has been performed
+    File.open(Redwood::SYNC_OK_FN, 'w') {|f| f.write(sync_performed.join("\n")) }
   end
 rescue Exception => e
   File.open("sup-exception-log.txt", "w") { |f| f.puts e.backtrace }
diff --git a/lib/sup.rb b/lib/sup.rb
@@ -64,6 +64,7 @@ module Redwood
   YAML_DOMAIN = "supmua.org"
   LEGACY_YAML_DOMAIN = "masanjin.net"
   YAML_DATE = "2006-10-01"
+  MAILDIR_SYNC_CHECK_SKIPPED = 'SKIPPED'
 
   ## record exceptions thrown in threads nicely
   @exceptions = []
@@ -177,13 +178,45 @@ module Redwood
 
     return if bypass_sync_check
 
-    if $config[:sync_back_to_maildir] and not File.exists? Redwood::SYNC_OK_FN
-      $stderr.puts <<EOS
+    if $config[:sync_back_to_maildir]
+      if not File.exists? Redwood::SYNC_OK_FN
+        Redwood.warn_syncback <<EOS
+It appears that the "sync_back_to_maildir" option has been changed
+from false to true since the last execution of sup.
+EOS
+        $stderr.puts <<EOS
+
+Should I complain about this again? (Y/n)
+EOS
+        File.open(Redwood::SYNC_OK_FN, 'w') {|f| f.write(Redwood::MAILDIR_SYNC_CHECK_SKIPPED) } if STDIN.gets.chomp.downcase == 'n'
+      elsif not $config[:sync_back_to_maildir] and File.exists? Redwood::SYNC_OK_FN
+        File.delete(Redwood::SYNC_OK_FN)
+      end
+    end
+  end
+
+  def check_syncback_settings
+    active_sync_sources = File.readlines(Redwood::SYNC_OK_FN).collect { |s| s.strip }
+    return if active_sync_sources.length == 1 and active_sync_sources[0] == Redwood::MAILDIR_SYNC_CHECK_SKIPPED
+    sources = SourceManager.sources
+    newly_synced = sources.select { |s| s.is_a? Maildir and s.sync_back_enabled? and not active_sync_sources.include? s.uri }
+    unless newly_synced.empty?
+      Redwood.warn_syncback <<EOS
+It appears that the option "sync_back" of the following source(s)
+has been changed from false to true since the last execution of
+sup:
+
+#{newly_synced.join("\n")}
+EOS
+    end
+  end
+
+  def self.warn_syncback details
+    $stderr.puts <<EOS
 WARNING
 -------
 
-It appears that the "sync_back_to_maildir" option has been changed
-from false to true since the last execution of sup.
+#{details}
 
 It is *strongly* recommended that you run "sup-sync-back-maildir"
 before continuing, otherwise you might lose informations in your
@@ -196,15 +229,7 @@ Please run "sup-sync-back-maildir -h" to see why it is useful.
 
 Are you really sure you want to continue? (y/N)
 EOS
-      abort "Aborted" unless STDIN.gets.chomp.downcase == 'y'
-      $stderr.puts <<EOS
-
-Should I complain about this again? (Y/n)
-EOS
-      File.open(Redwood::SYNC_OK_FN, 'w') {|f| f.write("SKIPPED") } if STDIN.gets.chomp.downcase == 'n'
-    elsif not $config[:sync_back_to_maildir] and File.exists? Redwood::SYNC_OK_FN
-      File.delete(Redwood::SYNC_OK_FN)
-    end
+    abort "Aborted" unless STDIN.gets.chomp.downcase == 'y'
   end
 
   def finish
@@ -336,7 +361,8 @@ EOM
   end
 
   module_function :save_yaml_obj, :load_yaml_obj, :start, :finish,
-                  :report_broken_sources, :load_config, :managers
+                  :report_broken_sources, :load_config, :managers,
+                  :check_syncback_settings
 end
 
 require 'sup/version'