Class: Heathrow::Sources::Messenger
- Defined in:
- lib/heathrow/sources/messenger.rb
Constant Summary collapse
- COOKIE_DIR =
File.join(Dir.home, '.heathrow', 'cookies')
- COOKIE_FILE =
File.join(COOKIE_DIR, 'messenger.json')
- FETCH_SCRIPT =
File.join(__dir__, 'messenger_fetch_marionette.py')
- THREAD_SCRIPT =
File.join(__dir__, 'messenger_fetch_thread.py')
- REQUIRED_COOKIES =
Required cookies for authentication
%w[c_user xs]
- OPTIONAL_COOKIES =
%w[datr fr]
- SEND_SCRIPT =
File.join(__dir__, 'messenger_send.py')
Instance Attribute Summary collapse
-
#sync_error ⇒ Object
readonly
Returns the value of attribute sync_error.
Attributes inherited from Base
Class Method Summary collapse
Instance Method Summary collapse
- #fetch ⇒ Object
-
#initialize(name, config, db) ⇒ Messenger
constructor
A new instance of Messenger.
- #save_cookies(cookies = nil) ⇒ Object
- #send_message(thread_id, _subject, body) ⇒ Object
- #sync(source_id) ⇒ Object
-
#sync_thread(source_id, thread_id, thread_name) ⇒ Object
Deep-fetch a single thread: navigate into it and scrape visible messages.
- #valid_cookies? ⇒ Boolean
Methods inherited from Base
#enabled?, #last_fetch, #poll_interval, #save_config, #update_last_fetch
Constructor Details
#initialize(name, config, db) ⇒ Messenger
Returns a new instance of Messenger.
19 20 21 22 |
# File 'lib/heathrow/sources/messenger.rb', line 19 def initialize(name, config, db) super @cookies = config['cookies'] || end |
Instance Attribute Details
#sync_error ⇒ Object (readonly)
Returns the value of attribute sync_error.
60 61 62 |
# File 'lib/heathrow/sources/messenger.rb', line 60 def sync_error @sync_error end |
Class Method Details
.extract_firefox_cookies ⇒ Object
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 |
# File 'lib/heathrow/sources/messenger.rb', line 139 def self. profile_dirs = Dir.glob(File.join(Dir.home, '.mozilla', 'firefox', '*')) = profile_dirs.map { |d| File.join(d, 'cookies.sqlite') } .find { |f| File.exist?(f) } return nil unless tmp = "/tmp/heathrow_ff_cookies_#{$$}.sqlite" system("cp #{Shellwords.escape()} #{Shellwords.escape(tmp)} 2>/dev/null") return nil unless File.exist?(tmp) = {} begin require 'sqlite3' db = SQLite3::Database.new(tmp) db.results_as_hash = true (REQUIRED_COOKIES + OPTIONAL_COOKIES).each do |name| row = db.get_first_row( "SELECT value FROM moz_cookies WHERE name = ? AND (host LIKE '%messenger.com' OR host LIKE '%facebook.com') ORDER BY expiry DESC LIMIT 1", [name] ) [name] = row['value'] if row end db.close ensure File.delete(tmp) if File.exist?(tmp) end end |
Instance Method Details
#fetch ⇒ Object
125 126 127 128 129 130 131 132 |
# File 'lib/heathrow/sources/messenger.rb', line 125 def fetch return [] unless enabled? source = @db.get_source_by_name(@name) return [] unless source sync(source['id']) update_last_fetch [] end |
#save_cookies(cookies = nil) ⇒ Object
171 172 173 174 175 176 177 |
# File 'lib/heathrow/sources/messenger.rb', line 171 def ( = nil) ||= @cookies Dir.mkdir(COOKIE_DIR) unless Dir.exist?(COOKIE_DIR) File.write(COOKIE_FILE, .to_json) File.chmod(0600, COOKIE_FILE) @cookies = end |
#send_message(thread_id, _subject, body) ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/heathrow/sources/messenger.rb', line 181 def (thread_id, _subject, body) return { success: false, message: "No thread ID" } unless thread_id return { success: false, message: "Empty message" } if body.nil? || body.strip.empty? result = `python3 #{Shellwords.escape(SEND_SCRIPT)} #{Shellwords.escape(thread_id)} #{Shellwords.escape(body.strip)} 2>/dev/null` data = JSON.parse(result) rescue nil if data data.transform_keys!(&:to_sym) data else { success: false, message: "Messenger send: no response from script" } end rescue => e { success: false, message: "Messenger send error: #{e.}" } end |
#sync(source_id) ⇒ Object
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 |
# File 'lib/heathrow/sources/messenger.rb', line 24 def sync(source_id) unless @sync_error = "Messenger: invalid cookies" return 0 end begin data = fetch_via_playwright unless data @sync_error = "Messenger: no data (is the tab open?)" return 0 end if data['error'] @sync_error = "Messenger: #{data['error']}" return 0 end threads = data['threads'] || [] if threads.empty? @sync_error = "Messenger: no threads found" return 0 end count = 0 threads.each do |thread| count += process_thread(source_id, thread) end @sync_error = nil count rescue => e @sync_error = "Messenger: #{e.}" 0 end end |
#sync_thread(source_id, thread_id, thread_name) ⇒ Object
Deep-fetch a single thread: navigate into it and scrape visible messages
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 |
# File 'lib/heathrow/sources/messenger.rb', line 63 def sync_thread(source_id, thread_id, thread_name) result = `timeout 15 python3 #{Shellwords.escape(THREAD_SCRIPT)} #{Shellwords.escape(thread_id)} 2>/dev/null` return 0 if result.nil? || result.strip.empty? data = JSON.parse(result) if data['error'] @sync_error = "Messenger thread: #{data['error']}" return 0 end = data['messages'] || [] return 0 if .empty? count = 0 .each_with_index do |msg, i| text = (msg['text'] || '').strip next if text.empty? || text.length < 2 # Skip UI garbage next if text =~ /^(Today|Yesterday) at \d/i next if text =~ /^Enter, Message sent/i next if text =~ /^You (sent|replied|reacted)/i sender = msg['sender'] || '' sender = thread_name if sender.empty? ext_id = "msng_#{thread_id}_d#{Digest::MD5.hexdigest(text)[0..11]}" msg_data = { source_id: source_id, external_id: ext_id, thread_id: thread_id.to_s, sender: sender, sender_name: sender, recipients: [thread_name], subject: thread_name, content: text, html_content: nil, timestamp: Time.now.to_i, received_at: Time.now.to_i, read: true, starred: false, archived: false, labels: ['Messenger'], attachments: nil, metadata: { thread_id: thread_id, message_id: ext_id, platform: 'messenger' }, raw_data: { thread_id: thread_id, name: thread_name } } begin @db.(msg_data) count += 1 rescue SQLite3::ConstraintException # Already exists end end @sync_error = nil count rescue => e @sync_error = "Messenger thread: #{e.}" 0 end |
#valid_cookies? ⇒ Boolean
134 135 136 137 |
# File 'lib/heathrow/sources/messenger.rb', line 134 def return false unless @cookies.is_a?(Hash) REQUIRED_COOKIES.all? { |k| @cookies[k] && !@cookies[k].empty? } end |