Class: Fluent::JabberOutput

Inherits:
Output
  • Object
show all
Defined in:
lib/fluent/plugin/out_jabber.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#jidObject (readonly)

Returns the value of attribute jid.



41
42
43
# File 'lib/fluent/plugin/out_jabber.rb', line 41

def jid
  @jid
end

#passwordObject (readonly)

Returns the value of attribute password.



42
43
44
# File 'lib/fluent/plugin/out_jabber.rb', line 42

def password
  @password
end

Instance Method Details

#configure(conf) ⇒ Object

Raises:

  • (Fluent::ConfigError)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/fluent/plugin/out_jabber.rb', line 44

def configure(conf)
  super

  raise Fluent::ConfigError, "jid/password and pit_id is exclusive!!" if (@jid || @password) && @pit_id

  if @pit_id
     = Pit.get(@pit_id, require: {
      'jid' => 'jid',
      'password' => 'password',
    })
    @jid = ['jid']
    @password = ['password']
  end

  Jabber.debug = true if jabber_debug_log
  Jabber.warnings = true if jabber_warnings_log
end

#emit(tag, es, chain) ⇒ Object



78
79
80
81
82
83
# File 'lib/fluent/plugin/out_jabber.rb', line 78

def emit(tag, es, chain)
  es.each do|time, record|
    send_message plain_text_format(time, record), xhtml_format(time, record)
  end
  chain.next
end

#escape_xhtml(data) ⇒ Object



113
114
115
# File 'lib/fluent/plugin/out_jabber.rb', line 113

def escape_xhtml(data)
  REXML::Text.new(data.to_s, true, nil, false).to_s
end

#format_with(format_string, time, record, need_escape) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/fluent/plugin/out_jabber.rb', line 93

def format_with(format_string, time, record, need_escape)
  return nil unless format_string
  format_string.gsub(/\\n/, "\n").gsub(/\\{sharp}/,'#').gsub(/\${([\w.]+)(?:\|([\w]+))?}/) {
    data = $1.split('.').inject(record) {|r,k| (r||{})[k]}
    filter = $2
    case filter
    when nil
      data = escape_xhtml(data) if need_escape
    when 'br'
      data = escape_xhtml(data).gsub(/\n/, '<br />')
    when 'uri_component'
      data = URI.escape(data, Regexp.union(URI::UNSAFE, /[?&=+]/))
      data = escape_xhtml(data) if need_escape
    else
      raise "Unknown filter: #{filter}"
    end
    data
  }
end

#plain_text_format(time, record) ⇒ Object



85
86
87
# File 'lib/fluent/plugin/out_jabber.rb', line 85

def plain_text_format(time, record)
  format_with(@format, time, record, false)
end

#send_message(plain_text, xhtml_text) ⇒ Object



117
118
119
120
121
122
# File 'lib/fluent/plugin/out_jabber.rb', line 117

def send_message(plain_text, xhtml_text)
  message = Jabber::Message.new(@room, plain_text.force_encoding(Encoding::UTF_8))
  set_xhtml_message(message, xhtml_text) if xhtml_text

  @muc_client.send message
end

#set_xhtml_message(message, xhtml_text) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/fluent/plugin/out_jabber.rb', line 124

def set_xhtml_message(message, xhtml_text)
  # http://devblog.famundo.com/articles/2006/10/18/ruby-and-xmpp-jabber-part-3-adding-html-to-the-messages
  # Create the html part
  h = REXML::Element::new("html")
  h.add_namespace('http://jabber.org/protocol/xhtml-im')

  # The body part with the correct namespace
  b = REXML::Element::new("body")
  b.add_namespace('http://www.w3.org/1999/xhtml')

  # This suggested method not works for me:
  #   REXML::Text.new( message, false, nil, true, nil, %r/.^/ )
  # So I try alternative.
  REXML::Document.new("<div>#{xhtml_text}</div>").children.each do|c|
    b.add c
  end

  h.add(b)

  # Add the html element to the message
  message.add_element(h)
end

#shutdownObject



74
75
76
# File 'lib/fluent/plugin/out_jabber.rb', line 74

def shutdown
  @client.close
end

#startObject



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/fluent/plugin/out_jabber.rb', line 62

def start
  jid = Jabber::JID.new(@jid)
  @client = Jabber::Client.new(jid)
  @client.connect
  @client.auth(@password)

  @muc_client = Jabber::MUC::MUCClient.new(@client)
  @muc_client.join(@room)

  $log.info("out_jabber plugin initialized(jid: #{self.jid}, room: #{self.room})")
end

#xhtml_format(time, record) ⇒ Object



89
90
91
# File 'lib/fluent/plugin/out_jabber.rb', line 89

def xhtml_format(time, record)
  format_with(@xhtml_format, time, record, true)
end