Class: Heathrow::SourceWizard

Inherits:
Object
  • Object
show all
Includes:
Rcurses, Rcurses::Cursor, Rcurses::Input
Defined in:
lib/heathrow/ui/source_wizard.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_manager, bottom_pane, right_pane) ⇒ SourceWizard

Returns a new instance of SourceWizard.



12
13
14
15
16
17
18
# File 'lib/heathrow/ui/source_wizard.rb', line 12

def initialize(source_manager, bottom_pane, right_pane)
  @source_manager = source_manager
  @panes = {
    bottom: bottom_pane,
    right: right_pane
  }
end

Instance Attribute Details

#panesObject (readonly)

Returns the value of attribute panes.



10
11
12
# File 'lib/heathrow/ui/source_wizard.rb', line 10

def panes
  @panes
end

#source_managerObject (readonly)

Returns the value of attribute source_manager.



10
11
12
# File 'lib/heathrow/ui/source_wizard.rb', line 10

def source_manager
  @source_manager
end

Instance Method Details

#configure_instagramObject

Instagram: extract cookies from Firefox or manual entry



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/heathrow/ui/source_wizard.rb', line 236

def configure_instagram
  configure_meta_source(
    'INSTAGRAM DMs',
    'instagram',
    'instagram.com',
    %w[sessionid csrftoken],
    %w[ds_user_id ig_did mid],
    [
      "This connects to Instagram DMs using your browser session.",
      "",
      "HOW IT WORKS:".bd.fg(39),
      "  Heathrow reads your Firefox cookies to authenticate",
      "  with instagram.com — no password needed, no extra app.",
      "",
      "IMPORTANT:".bd.fg(196),
      "  - Cookies expire when you log out of Instagram",
      "  - Sessions typically last 30-90 days",
      "  - If messages stop appearing, refresh cookies",
      "  - Two-factor auth stays active — this is read-only",
      "",
      "PRIVACY:".bd.fg(226),
      "  Cookies are stored locally in ~/.heathrow/cookies/",
      "  with restricted permissions (owner-only read/write).",
    ]
  )
end

#configure_messengerObject

Messenger: extract cookies from Firefox or manual entry



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
# File 'lib/heathrow/ui/source_wizard.rb', line 207

def configure_messenger
  configure_meta_source(
    'FACEBOOK MESSENGER',
    'messenger',
    'messenger.com',
    %w[c_user xs],
    %w[datr fr],
    [
      "This connects to Messenger using your browser session.",
      "",
      "HOW IT WORKS:".bd.fg(39),
      "  Heathrow reads your Firefox cookies to authenticate",
      "  with messenger.com — no password needed, no extra app.",
      "",
      "IMPORTANT:".bd.fg(196),
      "  - Cookies expire when you log out of Facebook",
      "  - Sessions typically last 30-90 days",
      "  - If messages stop appearing, refresh cookies",
      "  - To refresh: press S → select Messenger → e → r",
      "",
      "PRIVACY:".bd.fg(226),
      "  Cookies are stored locally in ~/.heathrow/cookies/",
      "  with restricted permissions (owner-only read/write).",
      "  They never leave your machine.",
    ]
  )
end

#configure_meta_source(title, source_type, domain, required_cookies, optional_cookies, help_lines) ⇒ Object

Shared Meta cookie setup for Messenger/Instagram



343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/heathrow/ui/source_wizard.rb', line 343

def configure_meta_source(title, source_type, domain, required_cookies, optional_cookies, help_lines)
  @panes[:right].clear
  @panes[:right].text = ([
    "#{title} SETUP".bd.fg(226),
    "",
  ] + help_lines).join("\n")
  @panes[:right].refresh

  # Try auto-extraction from Firefox
  @panes[:bottom].clear
  @panes[:bottom].text = " Checking Firefox for #{domain} cookies...".fg(226)
  @panes[:bottom].refresh

  require_relative '../sources/messenger'
  cookies = if source_type == 'messenger'
              Heathrow::Sources::Messenger.extract_firefox_cookies
            else
              Heathrow::Sources::Messenger.extract_instagram_cookies_from_firefox
            end

  loop do
    if cookies && required_cookies.all? { |k| cookies[k] && !cookies[k].empty? }
      @panes[:bottom].clear
      @panes[:bottom].text = " Found #{domain} cookies in Firefox!".fg(156)
      @panes[:bottom].refresh
      sleep(1)
      save_meta_cookies(source_type, cookies)
      return { name: title.split(' ').first.capitalize, cookies: cookies, enabled: true }
    end

    # Not found — ask user to log in and retry
    @panes[:right].clear
    @panes[:right].text = ([
      "#{title} SETUP".bd.fg(226),
      "",
    ] + help_lines + [
      "",
      "NEXT STEP:".bd.fg(39),
      "  1. Open #{domain} in Firefox and log in",
      "  2. Come back here and press Enter to retry",
      "",
      "Heathrow will automatically read your",
      "Firefox cookies — no copying needed.",
    ]).join("\n")
    @panes[:right].refresh

    choice = @panes[:bottom].ask("Press Enter after logging in (ESC to cancel): ", "")
    return nil if choice.nil?

    @panes[:bottom].clear
    @panes[:bottom].text = " Checking Firefox for #{domain} cookies...".fg(226)
    @panes[:bottom].refresh

    cookies = if source_type == 'messenger'
                Heathrow::Sources::Messenger.extract_firefox_cookies
              else
                Heathrow::Sources::Messenger.extract_instagram_cookies_from_firefox
              end
  end
end

#configure_rssObject

RSS: prompts for feed URLs with optional title/tags



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
# File 'lib/heathrow/ui/source_wizard.rb', line 163

def configure_rss
  feeds = []

  @panes[:right].clear
  @panes[:right].text = [
    "RSS/ATOM FEED SETUP".bd.fg(226),
    "",
    "Add feeds one at a time.",
    "Leave URL empty when done.",
    "",
    "Feeds from newsboat can be imported",
    "with: ruby setup_rss.rb",
  ].join("\n")
  @panes[:right].refresh

  loop do
    url = @panes[:bottom].ask("Feed URL (Enter when done, ESC cancel): ", "")
    return nil if url.nil?
    break if url.empty?

    title = @panes[:bottom].ask("Title (optional): ", "")
    return nil if title.nil?

    tags_input = @panes[:bottom].ask("Tags (comma-separated, optional): ", "")
    return nil if tags_input.nil?
    tags = tags_input.empty? ? [] : tags_input.split(',').map(&:strip)

    feed = { 'url' => url }
    feed['title'] = title unless title.empty?
    feed['tags'] = tags unless tags.empty?
    feeds << feed

    @panes[:bottom].clear
    @panes[:bottom].text = " Added: #{title.empty? ? url : title} (#{feeds.size} total)".fg(156)
    @panes[:bottom].refresh
    sleep(0.5)
  end

  return nil if feeds.empty?

  { name: 'RSS Feeds', feeds: feeds, enabled: true }
end

#configure_source(source_type, source_info) ⇒ Object

Generic source configuration via field definitions



413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# File 'lib/heathrow/ui/source_wizard.rb', line 413

def configure_source(source_type, source_info)
  config = {}

  form_text = []
  form_text << "CONFIGURE #{source_info[:name].upcase}".bd.fg(226)
  form_text << ""
  form_text << source_info[:description]
  form_text << ""

  source_info[:fields].each do |field|
    form_text << "#{field[:label]}:".bd.fg(39)
    form_text << "  #{field[:help]}".fg(240) if field[:help]

    @panes[:right].clear
    @panes[:right].text = form_text.join("\n")
    @panes[:right].refresh

    value = get_field_value(field)
    return nil if value.nil?

    # Store value
    unless value.empty?
      case field[:type]
      when 'number'
        config[field[:key].to_sym] = value.to_i
      when 'boolean'
        config[field[:key].to_sym] = %w[y yes true 1].include?(value.downcase)
      else
        config[field[:key].to_sym] = value
      end
    end

    # Use default if empty
    if config[field[:key].to_sym].nil? && field[:default]
      config[field[:key].to_sym] = field[:default]
    end

    display_val = field[:type] == 'password' ? '****' : (value.empty? ? "(default: #{field[:default]})" : value)
    form_text << "#{display_val}".fg(156)
    form_text << ""
  end

  # Validate required fields
  missing = source_info[:fields].select { |f| f[:required] && config[f[:key].to_sym].nil? }
  unless missing.empty?
    @panes[:bottom].clear
    @panes[:bottom].text = " Missing required: #{missing.map { |f| f[:label] }.join(', ')}".fg(196)
    @panes[:bottom].refresh
    sleep(2)
    return nil
  end

  config
end

#configure_webwatchObject

Web Watch: simple prompts for URL, title, selector



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
# File 'lib/heathrow/ui/source_wizard.rb', line 109

def configure_webwatch
  pages = []

  @panes[:right].clear
  @panes[:right].text = [
    "WEB PAGE MONITOR SETUP".bd.fg(226),
    "",
    "Add pages to monitor for changes.",
    "Each page is checked on refresh (C-R).",
    "",
    "Optional CSS selector limits monitoring",
    "to a specific part of the page:",
    "  #content  - element with id=\"content\"",
    "  .main     - elements with class=\"main\"",
    "  article   - all <article> elements",
    "",
    "Enter pages one at a time.",
    "Leave URL empty when done.",
  ].join("\n")
  @panes[:right].refresh

  loop do
    url = @panes[:bottom].ask("URL (Enter when done, ESC cancel): ", "")
    return nil if url.nil?
    break if url.empty?

    title = @panes[:bottom].ask("Title (optional): ", "")
    return nil if title.nil?

    selector = @panes[:bottom].ask("CSS selector (optional): ", "")
    return nil if selector.nil?

    tags_input = @panes[:bottom].ask("Tags (comma-separated, optional): ", "")
    return nil if tags_input.nil?
    tags = tags_input.empty? ? [] : tags_input.split(',').map(&:strip)

    page = { 'url' => url }
    page['title'] = title unless title.empty?
    page['selector'] = selector unless selector.empty?
    page['tags'] = tags unless tags.empty?
    pages << page

    @panes[:bottom].clear
    @panes[:bottom].text = " Added: #{title.empty? ? url : title} (#{pages.size} total)".fg(156)
    @panes[:bottom].refresh
    sleep(0.5)
  end

  return nil if pages.empty?

  { name: 'Web Watch', pages: pages, enabled: true }
end

#configure_weechatObject

WeeChat Relay setup with connection test



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/heathrow/ui/source_wizard.rb', line 264

def configure_weechat
  @panes[:right].clear
  @panes[:right].text = [
    "WEECHAT RELAY SETUP".bd.fg(226),
    "",
    "Connect to a running WeeChat instance via",
    "its relay protocol to read IRC, Slack, and",
    "other chat buffers.",
    "",
    "PREREQUISITES:".bd.fg(39),
    "  WeeChat must have a relay enabled:",
    "  /relay add weechat 8001",
    "  /set relay.network.password \"yourpassword\"",
    "",
    "For remote servers, use an SSH tunnel:",
    "  ssh -L 8001:localhost:8001 user@host",
    "",
    "BUFFER FILTER:".bd.fg(39),
    "  Comma-separated glob patterns to select",
    "  which buffers to import:",
    "  irc.*           - all IRC",
    "  python.slack.*  - all Slack",
    "  irc.oftc.*      - just OFTC network",
    "  (empty = import all message buffers)",
  ].join("\n")
  @panes[:right].refresh

  host = @panes[:bottom].ask("Relay host [localhost]: ", "localhost")
  return nil if host.nil?
  host = 'localhost' if host.empty?

  port = @panes[:bottom].ask("Relay port [8001]: ", "8001")
  return nil if port.nil?
  port = port.empty? ? 8001 : port.to_i

  password = @panes[:bottom].ask("Relay password: ", "")
  return nil if password.nil?

  buffer_filter = @panes[:bottom].ask("Buffer filter (empty = all): ", "")
  return nil if buffer_filter.nil?

  # Test connection
  @panes[:bottom].clear
  @panes[:bottom].text = " Testing connection to #{host}:#{port}...".fg(226)
  @panes[:bottom].refresh

  require_relative '../sources/weechat'
  test_config = { 'host' => host, 'port' => port, 'password' => password }
  test_instance = Heathrow::Sources::Weechat.new('WeeChat', test_config, @source_manager.db)
  result = test_instance.test_connection

  if result[:success]
    @panes[:bottom].clear
    @panes[:bottom].text = " #{result[:message]}".fg(156)
    @panes[:bottom].refresh
    sleep(1)

    config = {
      name: 'WeeChat',
      host: host,
      port: port,
      password: password,
      enabled: true
    }
    config[:buffer_filter] = buffer_filter unless buffer_filter.empty?
    config
  else
    @panes[:bottom].clear
    @panes[:bottom].text = " Connection failed: #{result[:message]}".fg(196)
    @panes[:bottom].refresh
    sleep(2)

    choice = @panes[:bottom].ask("Retry? (Enter = retry, ESC = cancel): ", "")
    return nil if choice.nil?
    configure_weechat  # Retry
  end
end

#run_wizardObject



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
# File 'lib/heathrow/ui/source_wizard.rb', line 20

def run_wizard
  # Show source type selection with arrow navigation
  source_type = select_source_type
  return nil unless source_type

  # Get source configuration
  source_info = @source_manager.get_source_types[source_type]
  return nil unless source_info

  # Run type-specific wizard
  config = case source_type
           when 'web'
             configure_webwatch
           when 'rss'
             configure_rss
           when 'messenger'
             configure_messenger
           when 'instagram'
             configure_instagram
           when 'weechat'
             configure_weechat
           else
             configure_source(source_type, source_info)
           end
  return nil unless config

  # Add the source
  source_id = create_source(source_type, config)

  @panes[:bottom].clear
  @panes[:bottom].text = " Source '#{config[:name] || config['name']}' added!".fg(156)
  @panes[:bottom].refresh
  sleep(1)

  source_id
end

#save_meta_cookies(source_type, cookies) ⇒ Object



404
405
406
407
408
409
410
# File 'lib/heathrow/ui/source_wizard.rb', line 404

def save_meta_cookies(source_type, cookies)
  cookie_dir = File.join(Dir.home, '.heathrow', 'cookies')
  Dir.mkdir(cookie_dir) unless Dir.exist?(cookie_dir)
  file = File.join(cookie_dir, "#{source_type}.json")
  File.write(file, cookies.to_json)
  File.chmod(0600, file)
end

#select_source_typeObject



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
# File 'lib/heathrow/ui/source_wizard.rb', line 57

def select_source_type
  source_types = @source_manager.get_source_types
  keys = source_types.keys
  selected = 0

  loop do
    # Render list with arrow marker
    lines = []
    lines << "ADD SOURCE".bd.fg(226)
    lines << ""

    keys.each_with_index do |key, i|
      info = source_types[key]
      marker = i == selected ? "" : "  "
      name_line = "#{marker}#{info[:icon]} #{info[:name]}"
      desc_line = "    #{info[:description]}"

      if i == selected
        lines << name_line.bd.fg(39)
        lines << desc_line.fg(245)
      else
        lines << name_line.fg(250)
        lines << desc_line.fg(240)
      end
      lines << ""
    end

    lines << "j/k or arrows to navigate, Enter to select, ESC to cancel".fg(245)

    @panes[:right].clear
    @panes[:right].text = lines.join("\n")
    @panes[:right].refresh

    @panes[:bottom].clear
    @panes[:bottom].text = " Select source type...".fg(245)
    @panes[:bottom].refresh

    chr = getchr
    case chr
    when 'j', 'DOWN'
      selected = (selected + 1) % keys.size
    when 'k', 'UP'
      selected = (selected - 1) % keys.size
    when 'ENTER', "\r", "\n"
      return keys[selected]
    when 'ESC', "\e", 'q'
      return nil
    end
  end
end