heathrow 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +58 -0
  3. data/README.md +205 -0
  4. data/bin/heathrow +42 -0
  5. data/bin/heathrowd +283 -0
  6. data/docs/ARCHITECTURE.md +1172 -0
  7. data/docs/DATABASE_SCHEMA.md +685 -0
  8. data/docs/DEVELOPMENT_WORKFLOW.md +867 -0
  9. data/docs/DISCORD_SETUP.md +142 -0
  10. data/docs/GMAIL_OAUTH_SETUP.md +120 -0
  11. data/docs/PLUGIN_SYSTEM.md +1370 -0
  12. data/docs/PROJECT_PLAN.md +1022 -0
  13. data/docs/README.md +417 -0
  14. data/docs/REDDIT_SETUP.md +174 -0
  15. data/docs/REPLY_FORWARD.md +182 -0
  16. data/docs/WHATSAPP_TELEGRAM_SETUP.md +306 -0
  17. data/heathrow.gemspec +34 -0
  18. data/heathrowd.service +21 -0
  19. data/img/heathrow.svg +95 -0
  20. data/img/rss_threaded.png +0 -0
  21. data/img/sources.png +0 -0
  22. data/lib/heathrow/address_book.rb +42 -0
  23. data/lib/heathrow/config.rb +332 -0
  24. data/lib/heathrow/database.rb +731 -0
  25. data/lib/heathrow/database_new.rb +392 -0
  26. data/lib/heathrow/event_bus.rb +175 -0
  27. data/lib/heathrow/logger.rb +122 -0
  28. data/lib/heathrow/message.rb +176 -0
  29. data/lib/heathrow/message_composer.rb +399 -0
  30. data/lib/heathrow/message_organizer.rb +774 -0
  31. data/lib/heathrow/migrations/001_initial_schema.rb +248 -0
  32. data/lib/heathrow/notmuch.rb +45 -0
  33. data/lib/heathrow/oauth2_smtp.rb +254 -0
  34. data/lib/heathrow/plugin/base.rb +212 -0
  35. data/lib/heathrow/plugin_manager.rb +141 -0
  36. data/lib/heathrow/poller.rb +93 -0
  37. data/lib/heathrow/smtp_sender.rb +204 -0
  38. data/lib/heathrow/source.rb +39 -0
  39. data/lib/heathrow/sources/base.rb +74 -0
  40. data/lib/heathrow/sources/discord.rb +357 -0
  41. data/lib/heathrow/sources/gmail.rb +294 -0
  42. data/lib/heathrow/sources/imap.rb +198 -0
  43. data/lib/heathrow/sources/instagram.rb +307 -0
  44. data/lib/heathrow/sources/instagram_fetch.py +101 -0
  45. data/lib/heathrow/sources/instagram_send.py +55 -0
  46. data/lib/heathrow/sources/instagram_send_marionette.py +104 -0
  47. data/lib/heathrow/sources/maildir.rb +606 -0
  48. data/lib/heathrow/sources/messenger.rb +212 -0
  49. data/lib/heathrow/sources/messenger_fetch.js +297 -0
  50. data/lib/heathrow/sources/messenger_fetch_marionette.py +138 -0
  51. data/lib/heathrow/sources/messenger_send.js +32 -0
  52. data/lib/heathrow/sources/messenger_send.py +100 -0
  53. data/lib/heathrow/sources/reddit.rb +461 -0
  54. data/lib/heathrow/sources/rss.rb +299 -0
  55. data/lib/heathrow/sources/slack.rb +375 -0
  56. data/lib/heathrow/sources/source_manager.rb +328 -0
  57. data/lib/heathrow/sources/telegram.rb +498 -0
  58. data/lib/heathrow/sources/webpage.rb +207 -0
  59. data/lib/heathrow/sources/weechat.rb +479 -0
  60. data/lib/heathrow/sources/whatsapp.rb +474 -0
  61. data/lib/heathrow/ui/application.rb +8098 -0
  62. data/lib/heathrow/ui/navigation.rb +8 -0
  63. data/lib/heathrow/ui/panes.rb +8 -0
  64. data/lib/heathrow/ui/source_wizard.rb +567 -0
  65. data/lib/heathrow/ui/threaded_view.rb +780 -0
  66. data/lib/heathrow/ui/views.rb +8 -0
  67. data/lib/heathrow/version.rb +3 -0
  68. data/lib/heathrow/wizards/discord_wizard.rb +193 -0
  69. data/lib/heathrow/wizards/slack_wizard.rb +140 -0
  70. data/lib/heathrow.rb +55 -0
  71. metadata +147 -0
@@ -0,0 +1,328 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'json'
5
+
6
+ module Heathrow
7
+ class SourceManager
8
+ attr_reader :sources, :db, :source_instances
9
+
10
+ def initialize(database)
11
+ @db = database
12
+ @sources = {}
13
+ @source_instances = {}
14
+ load_sources
15
+ end
16
+
17
+ def load_sources
18
+ # Load all configured sources from database
19
+ db_sources = @db.get_all_sources
20
+ db_sources.each do |source|
21
+ @sources[source['id']] = source
22
+
23
+ # Create source instance if module exists
24
+ if source['enabled']
25
+ instance = create_source_instance(source)
26
+ @source_instances[source['id']] = instance if instance
27
+ end
28
+ end
29
+ end
30
+
31
+ def reload
32
+ @sources = {}
33
+ @source_instances = {}
34
+ load_sources
35
+ end
36
+
37
+ def create_source_instance(source)
38
+ source_type = (source['plugin_type'] || source['type']).to_s.downcase
39
+
40
+ # Try to load the source module
41
+ begin
42
+ # Handle 'web' as 'webpage' for the module
43
+ module_name = source_type == 'web' ? 'webpage' : source_type
44
+ require_relative module_name
45
+
46
+ # Get the class (e.g., Heathrow::Sources::RSS, Heathrow::Sources::Webpage)
47
+ class_name = case module_name
48
+ when 'rss' then 'RSS'
49
+ when 'webpage' then 'Webpage'
50
+ when 'messenger' then 'Messenger'
51
+ when 'instagram' then 'Instagram'
52
+ when 'weechat' then 'Weechat'
53
+ # Custom source types loaded from ~/.heathrow/plugins/
54
+ else module_name.capitalize
55
+ end
56
+ source_class = Heathrow::Sources.const_get(class_name)
57
+
58
+ # Parse config if it's JSON
59
+ config = source['config']
60
+ config = JSON.parse(config) if config.is_a?(String)
61
+
62
+ # Create instance
63
+ source_class.new(source['name'], config, @db)
64
+ rescue LoadError => e
65
+ STDERR.puts "Source module not found: #{source_type}" if ENV['DEBUG']
66
+ nil
67
+ rescue => e
68
+ STDERR.puts "Error loading source #{source['name']}: #{e.message}" if ENV['DEBUG']
69
+ nil
70
+ end
71
+ end
72
+
73
+ def poll_sources
74
+ messages = []
75
+
76
+ @source_instances.each do |source_id, instance|
77
+ next unless instance.enabled?
78
+
79
+ # Check if enough time has passed since last poll
80
+ if instance.last_fetch
81
+ next if Time.now.to_i - instance.last_fetch < instance.poll_interval
82
+ end
83
+
84
+ begin
85
+ new_messages = instance.fetch
86
+ messages.concat(new_messages) if new_messages
87
+ rescue => e
88
+ STDERR.puts "Error polling #{instance.name}: #{e.message}" if ENV['DEBUG']
89
+ end
90
+ end
91
+
92
+ messages
93
+ end
94
+
95
+ def add_source(source_type, config)
96
+ # Generate unique ID
97
+ source_id = "#{source_type}_#{Time.now.to_i}"
98
+
99
+ # Use default color based on source type if not provided
100
+ default_color = get_default_color_for_type(source_type)
101
+
102
+ # Save to database
103
+ @db.add_source(
104
+ source_id,
105
+ source_type,
106
+ config[:name],
107
+ config,
108
+ config[:polling_interval] || 300,
109
+ config[:color] || default_color,
110
+ true # enabled by default
111
+ )
112
+
113
+ # Add to local cache
114
+ @sources[source_id] = {
115
+ 'id' => source_id,
116
+ 'type' => source_type,
117
+ 'name' => config[:name],
118
+ 'config' => config,
119
+ 'color' => config[:color] || get_default_color_for_type(source_type),
120
+ 'poll_interval' => config[:polling_interval] || 300,
121
+ 'enabled' => true,
122
+ 'last_poll' => nil
123
+ }
124
+
125
+ source_id
126
+ end
127
+
128
+ # Default colors for new sources (stored in DB at creation time).
129
+ # Runtime display colors come from the theme system in application.rb.
130
+ SOURCE_TYPE_COLORS = {
131
+ 'email' => 39, 'gmail' => 33, 'maildir' => 39, 'whatsapp' => 40,
132
+ 'discord' => 99, 'reddit' => 202, 'rss' => 226, 'telegram' => 51,
133
+ 'slack' => 35, 'web' => 208, 'weechat' => 75
134
+ }.freeze
135
+
136
+ def get_default_color_for_type(source_type)
137
+ SOURCE_TYPE_COLORS[source_type.to_s.downcase] || 15
138
+ end
139
+
140
+ def remove_source(source_id)
141
+ @db.execute("DELETE FROM sources WHERE id = ?", source_id)
142
+ @sources.delete(source_id)
143
+ end
144
+
145
+ def toggle_source(source_id)
146
+ source = @sources[source_id]
147
+ return unless source
148
+
149
+ new_status = source['enabled'] ? 0 : 1
150
+ @db.execute("UPDATE sources SET enabled = ? WHERE id = ?", new_status, source_id)
151
+ source['enabled'] = !source['enabled']
152
+ end
153
+
154
+ def get_source_types
155
+ {
156
+ 'gmail' => {
157
+ name: 'Gmail (OAuth2)',
158
+ description: 'Connect to Gmail using OAuth2 (requires setup - see fields below)',
159
+ icon: '✉',
160
+ fields: [
161
+ { key: 'name', label: 'Account Name', type: 'text', required: true },
162
+ { key: 'email', label: 'Gmail Address', type: 'text', required: true, placeholder: 'you@gmail.com' },
163
+ { key: 'safedir', label: 'Safe Directory', type: 'text', required: true, default: File.join(Dir.home, '.heathrow', 'mail'),
164
+ help: 'Dir with OAuth files: email.json (credentials) & email.txt (refresh token)' },
165
+ { key: 'oauth2_script', label: 'OAuth2 Script', type: 'text', default: '~/bin/oauth2.py',
166
+ help: 'Get from: github.com/google/gmail-oauth2-tools/blob/master/python/oauth2.py' },
167
+ { key: 'folder', label: 'Folder', type: 'text', default: 'INBOX' },
168
+ { key: 'fetch_limit', label: 'Max messages per fetch', type: 'number', default: 50 },
169
+ { key: 'mark_as_read', label: 'Mark as read (CAUTION)', type: 'boolean', default: false },
170
+ { key: 'polling_interval', label: 'Check interval (seconds)', type: 'number', default: 300 }
171
+ ]
172
+ },
173
+ 'imap' => {
174
+ name: 'Email (IMAP)',
175
+ description: 'Connect to any IMAP email server with username/password',
176
+ icon: '📧',
177
+ fields: [
178
+ { key: 'name', label: 'Account Name', type: 'text', required: true },
179
+ { key: 'imap_server', label: 'IMAP Server', type: 'text', required: true, placeholder: 'imap.example.com' },
180
+ { key: 'imap_port', label: 'IMAP Port', type: 'number', default: 993 },
181
+ { key: 'username', label: 'Username/Email', type: 'text', required: true },
182
+ { key: 'password', label: 'Password', type: 'password', required: true },
183
+ { key: 'use_ssl', label: 'Use SSL/TLS', type: 'boolean', default: true },
184
+ { key: 'folder', label: 'Folder', type: 'text', default: 'INBOX' },
185
+ { key: 'fetch_limit', label: 'Max messages per fetch', type: 'number', default: 50 },
186
+ { key: 'mark_as_read', label: 'Mark fetched as read', type: 'boolean', default: false },
187
+ { key: 'polling_interval', label: 'Check interval (seconds)', type: 'number', default: 300 }
188
+ ]
189
+ },
190
+ 'whatsapp' => {
191
+ name: 'WhatsApp',
192
+ description: 'Connect via WhatsApp Web (requires API server)',
193
+ icon: '◉',
194
+ fields: [
195
+ { key: 'name', label: 'Account Name', type: 'text', required: true },
196
+ { key: 'api_url', label: 'WhatsApp API URL', type: 'text', default: 'http://localhost:8080',
197
+ help: 'URL of the WhatsApp API server (whatsmeow)' },
198
+ { key: 'use_pairing_code', label: 'Use pairing code instead of QR?', type: 'boolean', default: false,
199
+ help: 'Use 8-digit pairing code instead of QR code scanning' },
200
+ { key: 'phone_number', label: 'Phone Number (for pairing code)', type: 'text', placeholder: '1234567890',
201
+ help: 'Required only if using pairing code method' },
202
+ { key: 'fetch_limit', label: 'Messages per fetch', type: 'number', default: 50 },
203
+ { key: 'incremental_sync', label: 'Incremental sync', type: 'boolean', default: true,
204
+ help: 'Only fetch new messages since last sync' },
205
+ { key: 'polling_interval', label: 'Check interval (seconds)', type: 'number', default: 60 }
206
+ ]
207
+ },
208
+ 'telegram' => {
209
+ name: 'Telegram',
210
+ description: 'Connect to Telegram via Bot or User account',
211
+ icon: '✈',
212
+ fields: [
213
+ { key: 'name', label: 'Account Name', type: 'text', required: true },
214
+ { key: 'bot_token', label: 'Bot Token (if using bot)', type: 'password', required: false,
215
+ help: 'Get from @BotFather in Telegram. Leave empty for user account.' },
216
+ { key: 'api_id', label: 'API ID (if using user account)', type: 'text', required: false,
217
+ help: 'Get from https://my.telegram.org - only for user account' },
218
+ { key: 'api_hash', label: 'API Hash (if using user account)', type: 'password', required: false,
219
+ help: 'Get from https://my.telegram.org - only for user account' },
220
+ { key: 'phone_number', label: 'Phone Number (if using user account)', type: 'text', required: false,
221
+ placeholder: '+1234567890', help: 'Required for user account authentication' },
222
+ { key: 'polling_interval', label: 'Check interval (seconds)', type: 'number', default: 60 }
223
+ ]
224
+ },
225
+ 'discord' => {
226
+ name: 'Discord',
227
+ description: 'Connect to Discord servers and channels',
228
+ icon: '💬',
229
+ fields: [
230
+ { key: 'name', label: 'Account Name', type: 'text', required: true },
231
+ { key: 'token', label: 'Bot Token', type: 'password', required: true,
232
+ help: 'Bot token from Discord Developer Portal (starts with MTM...)' },
233
+ { key: 'is_bot', label: 'Is Bot Token?', type: 'boolean', default: true,
234
+ help: 'Set to true for bot tokens, false for user tokens (not recommended)' },
235
+ { key: 'channels', label: 'Channel IDs', type: 'text', placeholder: '1234567890,0987654321',
236
+ help: 'Comma-separated Discord channel IDs to monitor' },
237
+ { key: 'guilds', label: 'Guild/Server IDs', type: 'text', placeholder: 'Optional',
238
+ help: 'Monitor all channels in these guilds (comma-separated)' },
239
+ { key: 'fetch_limit', label: 'Messages per fetch', type: 'number', default: 20 },
240
+ { key: 'polling_interval', label: 'Check interval (seconds)', type: 'number', default: 60 }
241
+ ]
242
+ },
243
+ 'reddit' => {
244
+ name: 'Reddit',
245
+ description: 'Monitor subreddits and messages',
246
+ icon: '®',
247
+ fields: [
248
+ { key: 'name', label: 'Account Name', type: 'text', required: true },
249
+ { key: 'client_id', label: 'Client ID', type: 'text', required: true },
250
+ { key: 'client_secret', label: 'Client Secret', type: 'password', required: true },
251
+ { key: 'user_agent', label: 'User Agent', type: 'text', required: true, default: 'Heathrow/1.0' },
252
+ { key: 'username', label: 'Username (optional)', type: 'text' },
253
+ { key: 'password', label: 'Password (optional)', type: 'password' },
254
+ { key: 'subreddits', label: 'Subreddits to monitor', type: 'text', placeholder: 'AskReddit,news,technology' },
255
+ { key: 'polling_interval', label: 'Check interval (seconds)', type: 'number', default: 300 }
256
+ ]
257
+ },
258
+ 'rss' => {
259
+ name: 'RSS/Atom Feeds',
260
+ description: 'Subscribe to RSS and Atom feeds',
261
+ icon: '◈',
262
+ fields: [
263
+ { key: 'name', label: 'Feed Collection Name', type: 'text', required: true },
264
+ { key: 'feeds', label: 'Feed URLs (one per line)', type: 'multiline', required: true, placeholder: "https://news.ycombinator.com/rss\nhttps://example.com/feed.xml" },
265
+ { key: 'polling_interval', label: 'Check interval (seconds)', type: 'number', default: 900 }
266
+ ]
267
+ },
268
+ 'web' => {
269
+ name: 'Web Page Monitor',
270
+ description: 'Monitor web pages for changes (use setup_webwatch.rb to configure)',
271
+ icon: '◎',
272
+ fields: [
273
+ { key: 'name', label: 'Collection Name', type: 'text', required: true },
274
+ { key: 'pages', label: 'Pages (JSON array)', type: 'multiline', required: true,
275
+ placeholder: '[{"url": "https://example.com", "title": "Example", "selector": "#content"}]' },
276
+ { key: 'polling_interval', label: 'Check interval (seconds)', type: 'number', default: 3600 }
277
+ ]
278
+ },
279
+ 'messenger' => {
280
+ name: 'Facebook Messenger',
281
+ description: 'Read Messenger DMs via browser cookies (no app needed)',
282
+ icon: '◉',
283
+ fields: [
284
+ { key: 'name', label: 'Account Name', type: 'text', required: true, default: 'Messenger' },
285
+ { key: 'polling_interval', label: 'Check interval (seconds)', type: 'number', default: 300 }
286
+ ]
287
+ },
288
+ 'instagram' => {
289
+ name: 'Instagram DMs',
290
+ description: 'Read Instagram direct messages via browser cookies (no app needed)',
291
+ icon: '◈',
292
+ fields: [
293
+ { key: 'name', label: 'Account Name', type: 'text', required: true, default: 'Instagram' },
294
+ { key: 'polling_interval', label: 'Check interval (seconds)', type: 'number', default: 300 }
295
+ ]
296
+ },
297
+ 'slack' => {
298
+ name: 'Slack',
299
+ description: 'Connect to Slack workspaces',
300
+ icon: '#',
301
+ fields: [
302
+ { key: 'name', label: 'Workspace Name', type: 'text', required: true },
303
+ { key: 'token', label: 'Bot/User Token', type: 'password', required: true, placeholder: 'xoxb-...' },
304
+ { key: 'channels', label: 'Channel IDs (optional)', type: 'text', placeholder: 'C1234567890,C0987654321' },
305
+ { key: 'polling_interval', label: 'Check interval (seconds)', type: 'number', default: 60 }
306
+ ]
307
+ },
308
+ 'weechat' => {
309
+ name: 'WeeChat Relay',
310
+ description: 'Connect to WeeChat relay for IRC, Slack, and other buffers',
311
+ icon: 'W',
312
+ fields: [
313
+ { key: 'name', label: 'Source Name', type: 'text', required: true, default: 'WeeChat' },
314
+ { key: 'host', label: 'Relay Host', type: 'text', required: true, default: 'localhost',
315
+ help: 'Host running WeeChat relay (use SSH tunnel for remote)' },
316
+ { key: 'port', label: 'Relay Port', type: 'number', required: true, default: 8001 },
317
+ { key: 'password', label: 'Relay Password', type: 'password', required: true },
318
+ { key: 'buffer_filter', label: 'Buffer Filter', type: 'text',
319
+ help: 'Comma-separated patterns, e.g.: irc.*,python.slack.* (empty = all)' },
320
+ { key: 'lines_per_buffer', label: 'Lines per buffer', type: 'number', default: 30 },
321
+ { key: 'polling_interval', label: 'Check interval (seconds)', type: 'number', default: 120 }
322
+ ]
323
+ },
324
+ # Additional source types can be added via plugins in ~/.heathrow/plugins/
325
+ }
326
+ end
327
+ end
328
+ end