require 'hipchat' require 'daybreak' require 'shellwords' class String # Like a downcase, de-camelcaser: # test => test # Test => test # OpsOnly => ops_only def downcase_and_snakify self.gsub(/\s+/,'').gsub(/([A-Z])/, '_\1').gsub('-','_').delete('@').sub(/^_/, '').downcase end end class Robut::Plugin::AliasNick include Robut::Plugin def usage '#alias - When mentioning , will be notified' end def handle time, sender_nick, message if message =~ /^#aliases\s+@??(.+)/ handle_aliases "@#{$1}" elsif message =~ /^#aliases\s*$/ handle_aliases elsif message =~ /^#alias\s+@??(\w+?)\W\s*(@.*+)/ handle_alias "@#{$1}", $2.split(' ') elsif sent_to_me?(message) # nop else mentions = message.scan /@\w+/ handle_notification sender_nick, mentions, message end end private def handle_aliases nick=nil if nick puts 'handle_aliases nick=%s' % nick.inspect rooms = db[nick] if rooms.empty? reply "Couldn't find any aliases." else reply rooms.join(', ') end else db.each do |nick, rooms| reply '%s: %s' % [ nick, rooms.join(', ') ] end end end def handle_alias nick, rooms puts 'handle_alias nick=%s rooms=%s' % [ nick.inspect, rooms.inspect ] db.lock { db[nick] = rooms } db.flush reply affirmation end def handle_notification from, mentions, message puts 'handle_notification from=%s mentions=%s message=%s' % [ from.inspect, mentions.inspect, message.inspect ] return if from.include? reply_to.connection.config.nick # self tos = mentions.select { |m| db.keys.include? m } tos.each do |to| rooms = db[to] rroom, hroom = nil, nil rooms.each do |name| if name =~ /^@/ rroom = robut_room name unless rroom next if rroom.nil? hroom = hipchat_room rroom.name unless hroom next if hroom.nil? end subject = '%s mentioned "%s" in "%s"' % [ from, name, current_room ] notification = "%s writes in %s: %s" % [ from, current_room, highlight(message, to) ] if name =~ /^@/ handle_hipchat_notification notification, hroom else handle_email_notification subject, notification, name end end if rroom.nil? reply "Sorry, I'm having trouble accessing the HipChat room..." elsif rooms.empty? reply "Sorry, I'm having trouble accessing HipChat rooms..." else reply "Notified #{to_sentence rooms}." end end end def handle_hipchat_notification notification, hroom puts 'handle_hipchat_notification notification=%s hroom=%s' % [ notification.inspect, hroom.inspect ] hipchat[hroom['name']].send \ reply_to.connection.config.nick, notification, \ message_format: :html, notify: true end def handle_email_notification subject, notification, email_alias puts 'handle_email_notification subject=%s notification=%s email_alias=%s' % [ subject.inspect, notification.inspect, email_alias.inspect ] send_email = 'echo %s | mail -a %s -a %s -s %s %s' % [ Shellwords::escape(notification), Shellwords::escape('From: noreply@bluejeansnet.com'), Shellwords::escape('Content-Type: text/html'), Shellwords::escape(subject), "#{email_alias}@bluejeansnet.com" ] system send_email end ################################################################################ def to_sentence array return if array.empty? return array.first.to_s if array.length == 1 array = array.dup array[-1] = "and #{array[-1]}" array.join(', ').sub(', and', ' and') end def hipchat reply_to.connection.config.hipchat end def hipchat_rooms reply_to.connection.config.hipchat_rooms end def db reply_to.connection.config.nick_db end def affirmation [ 'Aight, then', 'Sure thing', 'No problemo', 'You got it, bud' ].sample(1).shift end def robut_room name robut_room = reply_to.connection.rooms.select do |r| room_name = r.name.gsub('-','_') room_name =~ /^\d+_#{name.downcase_and_snakify}@/ end.shift end def hipchat_room name hipchat_room = hipchat_rooms.select do |r| r['xmpp_jid'] == name end.shift end def current_room hipchat_room = hipchat_rooms.select do |r| r['xmpp_jid'] == reply_to.name end.shift['name'] end def highlight message, nick message.gsub nick, "#{nick}" end end