Class: Heathrow::Sources::Weechat
- Defined in:
- lib/heathrow/sources/weechat.rb
Constant Summary collapse
- CONFIG_DIR =
File.join(Dir.home, '.heathrow', 'cookies')
- CONFIG_FILE =
File.join(CONFIG_DIR, 'weechat.json')
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #fetch ⇒ Object
-
#initialize(name_or_source, config = nil, db = nil) ⇒ Weechat
constructor
A new instance of Weechat.
-
#send_message(to, subject, body) ⇒ Object
Standard send_message interface (used by send_composed_message).
-
#send_to_buffer(buffer_name, text) ⇒ Object
Send a message to a buffer via the relay.
- #sync(source_id) ⇒ Object
- #test_connection ⇒ Object
Methods inherited from Base
#enabled?, #last_fetch, #poll_interval, #save_config, #update_last_fetch
Constructor Details
#initialize(name_or_source, config = nil, db = nil) ⇒ Weechat
Returns a new instance of Weechat.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/heathrow/sources/weechat.rb', line 20 def initialize(name_or_source, config = nil, db = nil) if name_or_source.is_a?(Hash) source = name_or_source config = source['config'] config = JSON.parse(config) if config.is_a?(String) super(source['name'] || 'WeeChat', config, db) else super(name_or_source, config, db) end @host = config['host'] || 'localhost' @port = (config['port'] || 8001).to_i @password = config['password'] || '' @buffer_filter = config['buffer_filter'] # e.g., 'irc.*,python.slack.*' @lines_per_buffer = (config['lines_per_buffer'] || 50).to_i end |
Instance Method Details
#fetch ⇒ Object
89 90 91 92 93 94 95 96 |
# File 'lib/heathrow/sources/weechat.rb', line 89 def fetch return [] unless enabled? source = @db.get_source_by_name(@name) return [] unless source sync(source['id']) update_last_fetch [] end |
#send_message(to, subject, body) ⇒ Object
Standard send_message interface (used by send_composed_message)
135 136 137 |
# File 'lib/heathrow/sources/weechat.rb', line 135 def (to, subject, body) send_to_buffer(to, body) end |
#send_to_buffer(buffer_name, text) ⇒ Object
Send a message to a buffer via the relay
121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/heathrow/sources/weechat.rb', line 121 def send_to_buffer(buffer_name, text) sock = connect_and_auth return { success: false, message: "Auth failed" } unless sock sock.write("input #{buffer_name} #{text}\n") sock.write("quit\n") rescue nil sock.close rescue nil { success: true, message: "Sent to #{buffer_name}" } rescue => e { success: false, message: "Send failed: #{e.}" } end |
#sync(source_id) ⇒ Object
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 |
# File 'lib/heathrow/sources/weechat.rb', line 36 def sync(source_id) sock = connect_and_auth return 0 unless sock count = 0 begin # Request buffer list sock.write("(listbuffers) hdata buffer:gui_buffers(*) number,full_name,short_name,title,type,local_variables\n") buffers_msg = read_and_parse(sock) buffer_list = extract_hdata(buffers_msg) return 0 if buffer_list.empty? buffer_list.each do |buf| full_name = buf['full_name'].to_s # Skip non-message buffers next if full_name == 'core.weechat' next if full_name =~ /^(relay\.|fset\.|script\.|irc\.server\.)/ # Apply buffer filter if @buffer_filter patterns = @buffer_filter.split(',').map(&:strip) next unless patterns.any? { |p| File.fnmatch(p, full_name) } end ptr = buf['__path']&.first next unless ptr # Fetch recent lines (hdata path: buffer:PTR/own_lines/last_line/data) sock.write("(lines_#{ptr}) hdata buffer:#{ptr}/own_lines/last_line(-#{@lines_per_buffer})/data date,prefix,message,tags_array,displayed,highlight,notify_level\n") lines_msg = read_and_parse(sock) next unless lines_msg lines = extract_hdata(lines_msg) lines.each do |line| next unless line['displayed'].to_i == 1 count += process_line(source_id, buf, line) end end ensure sock.write("quit\n") rescue nil sock.close rescue nil end count rescue Errno::ECONNREFUSED STDERR.puts "WeeChat relay not available at #{@host}:#{@port}" if ENV['DEBUG'] 0 rescue => e STDERR.puts "WeeChat error: #{e.}" if ENV['DEBUG'] STDERR.puts e.backtrace.first(3).join("\n") if ENV['DEBUG'] 0 end |
#test_connection ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/heathrow/sources/weechat.rb', line 98 def test_connection sock = connect_and_auth return { success: false, message: "Auth failed at #{@host}:#{@port}" } unless sock sock.write("(version) info version\n") msg = read_and_parse(sock) version = extract_info(msg) sock.write("(buflist) hdata buffer:gui_buffers(*) full_name\n") buf_msg = read_and_parse(sock) buffers = extract_hdata(buf_msg) sock.write("quit\n") rescue nil sock.close rescue nil { success: true, message: "WeeChat #{version} - #{buffers.size} buffers" } rescue Errno::ECONNREFUSED { success: false, message: "Cannot connect to #{@host}:#{@port}" } rescue => e { success: false, message: "Error: #{e.}" } end |