m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/mbsync.rb
blob: 511c9247af100261dc685813a0108b2548d28858 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
class MbsyncAccountConfig
  def initialize account, config
    @config = config
    @account = account
    make_imap_account
    make_imap_store
    make_maildir_store
    make_channel
  end

  def make_imap_account
    @imap_account = <<~END.chomp
      IMAPAccount #{@account['name']}
      Host #{@account['imap']}
      User #{@account['email']}
      Port 993
      SSLType IMAPS
      PassCmd "pass show #{@account['pass_tag'] || @account['name']}"
      CertificateFile /etc/ssl/certs/ca-certificates.crt
      AuthMechs *
    END
  end

  def make_imap_store
    @imap_store = <<~END.chomp
      IMAPStore #{@account['name']}-remote
      Account #{@account['name']}
    END
  end

  def make_maildir_store
    @maildir_store = <<~END.chomp
      MaildirStore #{@account['name']}-local
      Path #{@config['mail_directory']}/#{@account['name']}/
      Inbox #{@config['mail_directory']}/#{@account['name']}/INBOX
      Subfolders Verbatim
      Flatten .
    END
  end

  def make_channel
    @channel = <<~END.chomp
      Channel #{@account['name']}
      Master :#{@account['name']}-remote:
      Slave :#{@account['name']}-local:
      Patterns * !"[Gmail]/All Mail" !"[Gmail]/Wszystkie"
      Create Slave
      SyncState *
      ExpireUnread yes
      MaxMessages #{@account['max_messages'] || '0'}
    END
  end

  def to_s
    <<~END.chomp
      # BEGIN profile generated by mmm
      #{@imap_account}

      #{@imap_store}

      #{@maildir_store}

      #{@channel}
      # END profile generated by mmm
    END
  end
end

def make_mbsync_config config
  accounts = config['accounts'].map do |account_config|
    account = MbsyncAccountConfig.new account_config, config
    account.to_s
  end

  accounts.join "\n\n"
end