Class: Heathrow::Sources::Imap
- Inherits:
-
Object
- Object
- Heathrow::Sources::Imap
- Defined in:
- lib/heathrow/sources/imap.rb
Instance Method Summary collapse
- #can_reply? ⇒ Boolean
- #description ⇒ Object
- #fetch_messages ⇒ Object
-
#initialize(source) ⇒ Imap
constructor
A new instance of Imap.
- #name ⇒ Object
- #send_message(to, subject, body, in_reply_to = nil) ⇒ Object
- #test_connection ⇒ Object
Constructor Details
#initialize(source) ⇒ Imap
Returns a new instance of Imap.
11 12 13 |
# File 'lib/heathrow/sources/imap.rb', line 11 def initialize(source) @source = source end |
Instance Method Details
#can_reply? ⇒ Boolean
170 171 172 173 174 |
# File 'lib/heathrow/sources/imap.rb', line 170 def can_reply? config = @source.config.is_a?(String) ? JSON.parse(@source.config) : @source.config # Check if we have SMTP configuration or can use gmail_smtp config['smtp_server'] || config['username']&.include?('@') end |
#description ⇒ Object
19 20 21 |
# File 'lib/heathrow/sources/imap.rb', line 19 def description 'Fetch emails from any IMAP server using username/password' end |
#fetch_messages ⇒ Object
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 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 |
# File 'lib/heathrow/sources/imap.rb', line 23 def server = @source.config['imap_server'] port = @source.config['imap_port'] || 993 username = @source.config['username'] password = @source.config['password'] folder = @source.config['folder'] || 'INBOX' fetch_limit = @source.config['fetch_limit'] || 50 mark_as_read = @source.config['mark_as_read'] || false use_ssl = @source.config['use_ssl'] != false # Default to true = [] begin # Connect to IMAP server if use_ssl imap = Net::IMAP.new(server, port, usessl = true, certs = nil, verify = false) else imap = Net::IMAP.new(server, port) end # Login with username/password imap.login(username, password) imap.select(folder) # Get state file to track which messages we've already seen heathrow_home = File.('~/.heathrow') state_file = File.join(heathrow_home, 'state', "imap_#{@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 unseen messages unseen_uids = imap.search(["UNSEEN"]) # Get UIDs not already processed new_uids = unseen_uids - seen_uids new_uids = new_uids.first(fetch_limit) if new_uids.length > fetch_limit new_uids.each do |uid| msg = imap.uid_fetch(uid, ["ENVELOPE", "BODY[TEXT]", "FLAGS"]).first next unless msg envelope = msg.attr["ENVELOPE"] body = msg.attr["BODY[TEXT]"] flags = msg.attr["FLAGS"] from = envelope.from&.first sender = from ? "#{from.name || ''} <#{from.mailbox}@#{from.host}>" : "Unknown" is_unread = !flags.include?(:Seen) # Mark as read if configured to do so if mark_as_read && is_unread imap.uid_store(uid, "+FLAGS", [:Seen]) end = { source_id: @source.id, source_type: 'imap', external_id: "imap_#{username}_#{uid}", sender: sender, recipient: username, subject: envelope.subject || "(no subject)", content: body || "", raw_data: { envelope: envelope, flags: flags, uid: uid }.to_json, attachments: nil, timestamp: envelope.date || Time.now, is_read: flags.include?(:Seen) ? 1 : 0 } << seen_uids << uid end # Update state file File.write(state_file, JSON.generate({ seen_uids: seen_uids, last_fetch: Time.now.to_s })) imap.logout imap.disconnect rescue Net::IMAP::NoResponseError => e # Authentication failed return [{ source_id: @source.id, source_type: 'imap', sender: 'IMAP', subject: 'Authentication Failed', content: "Failed to login to #{server}: #{e.}", timestamp: Time.now.to_s, is_read: 0 }] rescue => e # Other errors return [{ source_id: @source.id, source_type: 'imap', sender: 'IMAP', subject: 'Connection Error', content: "Error connecting to #{server}: #{e.}", timestamp: Time.now.to_s, is_read: 0 }] end end |
#name ⇒ Object
15 16 17 |
# File 'lib/heathrow/sources/imap.rb', line 15 def name 'IMAP Email' end |
#send_message(to, subject, body, in_reply_to = nil) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/heathrow/sources/imap.rb', line 176 def (to, subject, body, in_reply_to = nil) config = @source.config.is_a?(String) ? JSON.parse(@source.config) : @source.config from = config['username'] # Use the SmtpSender module require_relative '../smtp_sender' # SmtpSender will automatically detect OAuth2 domains and use gmail_smtp # or fall back to SMTP server configuration result = Heathrow::SmtpSender.( from, to, subject, body, in_reply_to, config ) result end |
#test_connection ⇒ Object
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 |
# File 'lib/heathrow/sources/imap.rb', line 140 def test_connection config = @source.config.is_a?(String) ? JSON.parse(@source.config) : @source.config server = config['imap_server'] port = config['imap_port'] || 993 username = config['username'] password = config['password'] use_ssl = config['use_ssl'] != false begin if use_ssl imap = Net::IMAP.new(server, port, usessl = true, certs = nil, verify = false) else imap = Net::IMAP.new(server, port) end imap.login(username, password) # Get folder list to verify connection folders = imap.list('', '*') folder_count = folders&.size || 0 imap.logout imap.disconnect { success: true, message: "Connected to #{server} (#{folder_count} folders)" } rescue => e { success: false, message: "Connection failed: #{e.}" } end end |