Class: Heathrow::Sources::Gmail

Inherits:
Object
  • Object
show all
Defined in:
lib/heathrow/sources/gmail.rb

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Gmail

Returns a new instance of Gmail.



13
14
15
# File 'lib/heathrow/sources/gmail.rb', line 13

def initialize(source)
  @source = source
end

Instance Method Details

#can_reply?Boolean

Returns:

  • (Boolean)


269
270
271
# File 'lib/heathrow/sources/gmail.rb', line 269

def can_reply?
  true
end

#config_fieldsObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/heathrow/sources/gmail.rb', line 25

def config_fields
  [
    { name: 'email', label: 'Gmail Address', type: 'text', required: true },
    { name: 'safedir', label: 'Safe Directory', type: 'text', required: true,
      hint: 'Directory containing your .json and .txt OAuth files' },
    { name: 'oauth2_script', label: 'OAuth2 Script Path', type: 'text', required: true,
      hint: 'Path to oauth2.py script' },
    { name: 'folder', label: 'Folder to Monitor', type: 'text', default: 'INBOX' },
    { name: 'fetch_limit', label: 'Max messages per fetch', type: 'number', default: 50 },
    { name: 'check_interval', label: 'Check Interval (seconds)', type: 'number', default: 300 },
    { name: 'mark_as_read', label: 'Mark fetched as read', type: 'boolean', default: false,
      hint: 'CAUTION: Only enable if you want Heathrow to mark emails as read' }
  ]
end

#descriptionObject



21
22
23
# File 'lib/heathrow/sources/gmail.rb', line 21

def description
  'Fetch emails from Gmail using OAuth2'
end

#fetch_messagesObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/heathrow/sources/gmail.rb', line 61

def fetch_messages
  email = @source.config['email']
  safedir = File.expand_path(@source.config['safedir'])
  oauth2_script = File.expand_path(@source.config['oauth2_script'] || '~/bin/oauth2.py')
  folder = @source.config['folder'] || 'INBOX'
  fetch_limit = @source.config['fetch_limit'] || 50
  mark_as_read = @source.config['mark_as_read'] || false
  
  # Read OAuth credentials
  json_file = File.join(safedir, "#{email}.json")
  txt_file = File.join(safedir, "#{email}.txt")
  
  begin
    jparse = JSON.parse(File.read(json_file))
    clientid = jparse["web"]["client_id"]
    clsecret = jparse["web"]["client_secret"]
    refresh_token = File.read(txt_file).chomp
  rescue StandardError => e
    return [{
      source_id: @source.id,
      source_type: 'gmail',
      sender: 'Gmail',
      subject: 'Configuration Error',
      content: "Failed to read OAuth credentials: #{e.message}",
      timestamp: Time.now.to_s,
      is_read: 0
    }]
  end
  
  # Get access token using oauth2.py
  begin
    cmd = "python3 #{oauth2_script} --generate_oauth2_token " \
          "--client_id=#{clientid} --client_secret=#{clsecret} " \
          "--refresh_token=#{refresh_token}"
    
    require 'timeout'
    stdout = stderr = nil
    status = nil
    
    Timeout::timeout(10) do
      stdout, stderr, status = Open3.capture3(cmd)
    end
    
    unless status && status.success?
      raise "OAuth token generation failed: #{stderr || 'No output'}"
    end
    
    # Extract access token from output (format: "Access Token: <token>")
    token_match = stdout.match(/Access Token: (.+?)(\n|$)/)
    unless token_match
      raise "Could not extract access token from output: #{stdout}"
    end
    token = token_match[1].strip
  rescue StandardError => e
    return [{
      source_id: @source.id,
      source_type: 'gmail',
      sender: 'Gmail',
      subject: 'Authentication Error',
      content: "Failed to get access token: #{e.message}",
      timestamp: Time.now.to_s,
      is_read: 0
    }]
  end
  
  messages = []
  
  begin
    # Connect to Gmail IMAP
    imap = Net::IMAP.new('imap.gmail.com', 993, usessl = true, certs = nil, verify = false)
    
    # Authenticate with OAuth2
    imap.authenticate('XOAUTH2', email, token)
    imap.select(folder)
    
    # Get state file to track which messages we've already seen
    heathrow_home = File.expand_path('~/.heathrow')
    state_file = File.join(heathrow_home, 'state', "gmail_#{@source.id}.json")
    FileUtils.mkdir_p(File.dirname(state_file))
    
    seen_uids = if File.exist?(state_file)
      JSON.parse(File.read(state_file))['seen_uids'] || []
    else
      []
    end
    
    # Search for messages (both seen and unseen for testing)
    # In production, you might want to use ["UNSEEN"] only
    search_criteria = mark_as_read ? ["UNSEEN"] : ["ALL"]
    
    # Get UIDs of messages
    uids = imap.uid_search(search_criteria)
    
    # Limit the number of messages to fetch
    new_uids = uids.reject { |uid| seen_uids.include?(uid) }.last(fetch_limit)
    
    new_uids.each do |uid|
      begin
        # Fetch the message envelope and body
        fetch_data = imap.uid_fetch(uid, ['ENVELOPE', 'BODY.PEEK[TEXT]', 'FLAGS'])[0]
        
        envelope = fetch_data.attr['ENVELOPE']
        body = fetch_data.attr['BODY[TEXT]'] || ''
        flags = fetch_data.attr['FLAGS'] || []
        
        # Extract sender
        from = if envelope.from && envelope.from.first
          addr = envelope.from.first
          if addr.name
            "#{addr.name} <#{addr.mailbox}@#{addr.host}>"
          else
            "#{addr.mailbox}@#{addr.host}"
          end
        else
          'Unknown Sender'
        end
        
        # Clean up subject
        subject = envelope.subject || '(No Subject)'
        
        # Parse date
        date = envelope.date ? Time.parse(envelope.date) : Time.now
        
        # Truncate body for preview
        body_preview = body.to_s
          .force_encoding('UTF-8')
          .encode('UTF-8', invalid: :replace, undef: :replace, replace: '?')
          .gsub(/\r\n/, "\n")
          .strip[0..500]
        
        # Check if message is unread
        is_unread = !flags.include?(:Seen)
        
        messages << {
          source_id: @source.id,
          source_type: 'gmail',
          sender: from,
          subject: subject,
          content: body_preview,
          timestamp: date.to_s,
          is_read: is_unread ? 0 : 1,
          metadata: {
            uid: uid,
            folder: folder,
            message_id: envelope.message_id,
            flags: flags.map(&:to_s)
          }.to_json
        }
        
        # Add to seen UIDs list
        seen_uids << uid
        
        # IMPORTANT: Only mark as read if explicitly configured
        if mark_as_read && is_unread
          imap.uid_store(uid, "+FLAGS", [:Seen])
        end
      rescue StandardError => e
        # Log error but continue with other messages
        puts "Error fetching message UID #{uid}: #{e.message}" if ENV['DEBUG']
      end
    end
    
    # Save state
    File.write(state_file, JSON.pretty_generate({
      seen_uids: seen_uids,
      last_check: Time.now.to_s
    }))
    
    # Disconnect
    imap.logout
    imap.disconnect
    
  rescue StandardError => e
    return [{
      source_id: @source.id,
      source_type: 'gmail',
      sender: 'Gmail',
      subject: 'Connection Error',
      content: "Failed to connect to Gmail: #{e.message}",
      timestamp: Time.now.to_s,
      is_read: 0
    }]
  end
  
  messages
end

#nameObject



17
18
19
# File 'lib/heathrow/sources/gmail.rb', line 17

def name
  'Gmail'
end

#send_message(to, subject, body, in_reply_to = nil) ⇒ Object



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/heathrow/sources/gmail.rb', line 273

def send_message(to, subject, body, in_reply_to = nil)
  config = @source.config.is_a?(String) ? JSON.parse(@source.config) : @source.config
  email = config['email']
  
  # Use the SmtpSender module
  require_relative '../smtp_sender'
  
  # SmtpSender handles all the complexity of OAuth2 and gmail_smtp
  result = Heathrow::SmtpSender.send_message(
    email,
    to,
    subject,
    body,
    in_reply_to,
    config
  )
  
  result
end

#testObject



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/heathrow/sources/gmail.rb', line 248

def test
  # Test connection and authentication
  email = @source.config['email']
  
  begin
    # Try to get token
    messages = fetch_messages
    
    if messages.any? { |m| m[:subject] =~ /Error/ }
      { success: false, message: messages.first[:content] }
    else
      { 
        success: true, 
        message: "Successfully connected to Gmail for #{email}. Found #{messages.size} messages."
      }
    end
  rescue StandardError => e
    { success: false, message: "Test failed: #{e.message}" }
  end
end

#validate_config(config) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/heathrow/sources/gmail.rb', line 40

def validate_config(config)
  return { valid: false, error: 'Email is required' } unless config['email']
  return { valid: false, error: 'Safe directory is required' } unless config['safedir']
  
  # Check if OAuth files exist
  email = config['email']
  safedir = File.expand_path(config['safedir'])
  json_file = File.join(safedir, "#{email}.json")
  txt_file = File.join(safedir, "#{email}.txt")
  
  unless File.exist?(json_file)
    return { valid: false, error: "OAuth JSON file not found: #{json_file}" }
  end
  
  unless File.exist?(txt_file)
    return { valid: false, error: "Refresh token file not found: #{txt_file}" }
  end
  
  { valid: true }
end