m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/mutt.rb
blob: 7b650b9198a2d674ecda093a280c5d4a088a1b7b (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
77
def make_named_mailboxes account
  mailboxes = account['mailboxes'].filter do |mailbox|
    mailbox['alias']
  end

  if !mailboxes.empty?
    string = mailboxes.map do |mailbox|
      "#{mailbox['alias']} \"+#{mailbox['name']}\""
    end.join ' '

    "named-mailboxes #{string}"
  end
end

def make_unnamed_mailboxes account
  mailboxes = account['mailboxes'].filter do |mailbox|
    !mailbox['alias']
  end

  if !mailboxes.empty?
    string = mailboxes.map do |mailbox|
      "\"+#{mailbox}\""
    end.join ' '

    "mailboxes #{string}"
  end
end

def make_mutt_config account, config
  named_mailboxes = make_named_mailboxes account
  unnamed_mailboxes = make_unnamed_mailboxes account
  mailboxes = "#{named_mailboxes}"
  if !named_mailboxes
    mailboxes = ''
  elsif unnamed_mailboxes
    mailboxes += "\n"
  end
  mailboxes += unnamed_mailboxes || ''

  <<~END.chomp
    # File generated by mmm
    set realname="#{account['real_name'] || config['real_name']}"
    set from=#{account['email']}
    set sendmail="msmtp -a #{account['name']}"
    set folder=#{config['mail_directory']}/#{account['name']}
    set spoolfile=+INBOX
    set record="+#{account['record'] || 'Sent'}"
    set postponed="+#{account['postponed'] || 'Drafts'}"
    set header_cache="~/.cache/mmm/#{account['name']}/headers"
    unmailboxes *
    #{mailboxes}
  END
end

def make_mutt_activator account, i, config
  "macro index i#{i} '<sync-mailbox><enter-command>source #{config['neomutt_profiles_directory']}/#{account['name']}.neomuttrc<enter><change-folder>!<enter>'"
end

def make_mutt_configs config
  configs = {}
  config['accounts'].each do |account|
    configs[account['name']] = make_mutt_config(account, config)
  end
  configs
end

def make_mmm_muttrc config
  activators = config['accounts'].each_with_index.map do |account, i|
    make_mutt_activator account, i + 1, config
  end.join "\n"

  <<~END
    # File generated by mmm
    bind index i noop
    #{activators}
  END
end