Class: Olm::App

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/olm/app.rb

Instance Method Summary collapse

Constructor Details

#initializeApp



8
9
10
11
12
# File 'lib/olm/app.rb', line 8

def initialize
  @app = WIN32OLE.connect("Outlook.Application")
  @ns = @app.Session
  const_load(self.class)
end

Instance Method Details

#create_forward_message(entry_id) ⇒ Object



112
113
114
115
116
117
118
# File 'lib/olm/app.rb', line 112

def create_forward_message(entry_id)
  m = @ns.GetItemFromID(entry_id)
  r = m.Forward
  r.BodyFormat = OlFormatPlain
  r.Save
  r.EntryID
end

#create_message(io) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/olm/app.rb', line 92

def create_message(io)
  d = read_draft(io)
  m = @app.CreateItem(OlMailItem)
  m.BodyFormat = OlFormatPlain
  m.To = d[:to] if d[:to]
  m.CC = d[:cc] if d[:cc]
  m.BCC = d[:bcc] if d[:bcc]
  m.Subject = d[:subject] if d[:subject]
  m.Body = d[:body] if d[:body]
  m
end

#create_reply_all_message(entry_id) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/olm/app.rb', line 104

def create_reply_all_message(entry_id)
  m = @ns.GetItemFromID(entry_id)
  r = m.ReplyAll
  r.BodyFormat = OlFormatPlain
  r.Save
  r.EntryID
end

#default_folderObject



14
15
16
# File 'lib/olm/app.rb', line 14

def default_folder
  @ns.GetDefaultFolder(OlFolderInbox)
end

#deleted_items_folderObject



18
19
20
# File 'lib/olm/app.rb', line 18

def deleted_items_folder
  @ns.GetDefaultFolder(OlFolderDeletedItems)
end

#execute_refile(io) ⇒ Object



146
147
148
149
150
151
152
# File 'lib/olm/app.rb', line 146

def execute_refile(io)
  io.each_line do |line|
    line.chomp!
    next unless /^(\h+) (\h+)/ =~ line
    move($1, $2)
  end
end

#ls(folder_id = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/olm/app.rb', line 22

def ls(folder_id = nil)
  f = folder_id ? @ns.GetFolderFromID(folder_id) : default_folder
  n = [f.Items.Count, 100].min
  s = f.Items.Count - n + 1
  t = f.Items.Count
  res = []
  s.upto(t) do |i|
    m = f.Items(i)
    unless m.Class == OlMail
      n -= 1
      next
    end
    entry_id = m.EntryID
    received_at = m.ReceivedTime.to_s.split(' ').first
    from = m.SenderName
    subject = m.Subject
    flag = m.IsMarkedAsTask ? '!' : ' '
    res << sprintf("%s %s  %-12.12s  %-20.20s %s",
      entry_id, flag, received_at, from, subject)
  end
  res.unshift(n.to_s)
end

#mark_as_read(entry_id) ⇒ Object



86
87
88
89
90
# File 'lib/olm/app.rb', line 86

def mark_as_read(entry_id)
  m = @ns.GetItemFromID(entry_id)
  m.UnRead = false
  m.Save
end

#message(entry_id) ⇒ Object



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
# File 'lib/olm/app.rb', line 49

def message(entry_id)
  m = @ns.GetItemFromID(entry_id)
  res = [entry_id]
  res << sprintf("From: %s", m.SenderName)
  res << sprintf("To: %s", m.To)
  res << sprintf("Cc: %s", m.CC) if m.CC.to_s.length > 0
  res << sprintf("Subject: %s", m.Subject)
  res << sprintf("ReceivedAt: %s", m.ReceivedTime)
  if m.Attachments.Count > 0
    buf = []
    m.Attachments.each do |a|
      buf << a.DisplayName
    end
    res << sprintf("Attachments: %s", buf.join("; "))
  end
  res << sprintf("---- ")
  if m.BodyFormat != OlFormatPlain
    m2 = m.Copy
    m2.BodyFormat = OlFormatPlain
    res << m2.Body.split("\r\n")
    m2.Move(deleted_items_folder)
  else
    res << m.Body.split("\r\n")
  end
  res
end

#save_attachments(entry_id, path) ⇒ Object



139
140
141
142
143
144
# File 'lib/olm/app.rb', line 139

def save_attachments(entry_id, path)
  m = @ns.GetItemFromID(entry_id)
  m.Attachments.each do |a|
    a.SaveAsFile(path + a.DisplayName)
  end
end

#send_and_receiveObject



45
46
47
# File 'lib/olm/app.rb', line 45

def send_and_receive
  @ns.SendAndReceive(false)
end

#toggle_task_flag(entry_id) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/olm/app.rb', line 76

def toggle_task_flag(entry_id)
  m = @ns.GetItemFromID(entry_id)
  if m.IsMarkedAsTask
    m.ClearTaskFlag()
  else
    m.MarkAsTask(OlMarkNoDate)
  end
  m.Save
end

#update_forward_message_body(io) ⇒ Object



129
130
131
132
133
134
135
136
137
# File 'lib/olm/app.rb', line 129

def update_forward_message_body(io)
  d = read_draft(io)
  m = @ns.GetItemFromID(d[:entry_id])
  m.BodyFormat = OlFormatPlain
  m.Body = d[:body]
  m.To = d[:to] if d[:to]
  m.BCC = d[:bcc] if d[:bcc]
  m
end

#update_message_body(io) ⇒ Object



120
121
122
123
124
125
126
127
# File 'lib/olm/app.rb', line 120

def update_message_body(io)
  d = read_draft(io)
  m = @ns.GetItemFromID(d[:entry_id])
  m.BodyFormat = OlFormatPlain
  m.Body = d[:body]
  m.BCC = d[:bcc] if d[:bcc]
  m
end