Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xmppdiff: Persist connection to XMPP server #2418

Merged
1 commit merged into from
Dec 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
- improved Telnet support for enterasys (@jplitza)
- Include "show version" output for enterasys (@jplitza)
- xmppdiff now also shows diffs with only removed or only added lines (@jplitza)
- xmppdiff now persists its connection to the XMPP server and MUC (@jplitza)

### Fixed

Expand Down
68 changes: 45 additions & 23 deletions lib/oxidized/hook/xmppdiff.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,44 @@
require 'xmpp4r/muc/helper/simplemucclient'

class XMPPDiff < Oxidized::Hook
def connect
@client = Jabber::Client.new(Jabber::JID.new(cfg.jid))

log "Connecting to XMPP"
begin
Timeout.timeout(15) do
begin
@client.connect
rescue StandardError => e
log "Failed to connect to XMPP: #{e}"
end
sleep 1

log "Authenticating to XMPP"
@client.auth(cfg.password)
sleep 1

log "Connected to XMPP"

@muc = Jabber::MUC::SimpleMUCClient.new(@client)
@muc.join(cfg.channel + "/" + cfg.nick)

log "Joined #{cfg.channel}"
end
rescue Timeout::Error
log "timed out"
@client = nil
@muc = nil
end

@client.on_exception do
log "XMPP connection aborted, reconnecting"
@client = nil
@muc = nil
connect
end
end

def validate_cfg!
raise KeyError, 'hook.jid is required' unless cfg.has_key?('jid')
raise KeyError, 'hook.password is required' unless cfg.has_key?('password')
Expand All @@ -23,31 +61,15 @@ def run_hook(ctx)
end

if interesting
log "Connecting to XMPP"
client = Jabber::Client.new(Jabber::JID.new(cfg.jid))
client.connect
sleep 1
client.auth(cfg.password)
sleep 1

log "Connected"

m = Jabber::MUC::SimpleMUCClient.new(client)
m.join(cfg.channel + "/" + cfg.nick)

log "Joined"

title = "#{ctx.node.name} #{ctx.node.group} #{ctx.node.model.class.name.to_s.downcase}"
log "Posting diff as snippet to #{cfg.channel}"

m.say(title + "\n\n" + diff[:patch].lines.to_a[4..-1].join)

sleep 1

client.close
connect if @muc.nil?

log "Finished"
# Maybe connecting failed, so only proceed if we actually joined the MUC
unless @muc.nil?
title = "#{ctx.node.name} #{ctx.node.group} #{ctx.node.model.class.name.to_s.downcase}"
log "Posting diff as snippet to #{cfg.channel}"

@muc.say(title + "\n\n" + diff[:patch].lines.to_a[4..-1].join)
end
end
end
rescue Timeout::Error
Expand Down