[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [sup-talk] Hooks - some examples appreciated



On Fri, Nov 23, 2012 at 3:54 AM, Andre Tann <atann@alphasrv.net> wrote:
> as this Website http://sup.rubyforge.org/wiki/wiki.pl?Hooks is not
> really informative ;) … does anybody here have some examples for sup
> hooks?

Sadly the wiki has been almost completely spammed out of existance; it
doesn't store a long-enough revision history to get reverted correctly
any more.

You probably should be doing tagging in before-add.rb

In my before-add hook I was doing a lot of tagging, so much that I
built a small function to help me add and remove tags in one
invocation. A bunch of standard actions help.

First, extract the useful fields -- because a message can have
multiple recipients and I want to check any/all of them (To, Cc, Bcc).
Doing this means I don't have to check the array of recpipents every
time, I can just use string matching on a long flat list of
recipients.

----
# Build up an array of recipients for this message
recipients=[]
message.recipients.each {|rp| recipients << rp.email }
recips = recipients.join(' ')
----

Then I use a case statement to separate out all the different checks I
want to do:

----
# Nest a set of mutually exclusive cases ...
case
# Nagios messages
        when (message.from.email =~ /nagios@MYDOMAIN/ \
          and recips =~ /MYDOMAIN/) :
                tagit(message,"auto nagios -inbox")
# Logwatch messages
        when (message.from.email =~ /^logwatch@/ \
          and message.subj =~ /^Logwatch for/) :
                tagit(message,"auto logwatch -inbox")
# Legato messages
        when message.from.email =~ /^legato@/ :
                tagit(message,"auto legato -inbox")
----

Those three cases check the sender, adding the tags "auto" and
"nagios"/"logwatch"/"legato" as appropriate, and remove the inbox tag,
so I don't see them in my default view.

I'm using a function "tagit" to do this, it's just a helper function
to make the rules read a little easier. It's defined at the beginning
of my before-add.rb :-

----
def tagit(message,labels)
        actions=[]
        labels.split(' ').each {|l|
                # Check the first character of each label.
                # If it starts with -, remove the label
                # otherwise add it.
                minus=l.match('^-(.*)$')
                if minus
                        message.remove_label minus[1]
                        actions << "Del #{minus[1]}"
                else
                        message.add_label l
                        actions << "Add #{l}"
                end
        }
        log "Tagit: #{labels} -> #{message.id} : #{actions.join(',')}"
end
----

Here are a few more tests that I do :-

----
# EDUCAUSE Security
        when message.raw_header =~ /^List-Owner:
<mailto:SECURITY-request@LISTSERV.EDUCAUSE.EDU>$/ :
                tagit(message,"list educause -inbox")
# rss2email blog messages
        when message.raw_header =~ /X-rss2email/ :
                tagit(message,"blog -inbox")
                log " blog from #{message.from.inspect}"
                log " blog name #{message.from.name}"
                case message.from.name
                        when /Shipping Container House/
                                tagit(message,"blog.marek")
                        when /cortesi/
                                tagit(message,"blog.cortesi")
                        when /RISKS/
                                tagit(message,"blog.risks")
                        when /WTF/
                                tagit(message,"blog.wtf")
                        when /Schneier on Security/
                                tagit(message,"blog.schneier")
                        when /Krebs/
                                tagit(message,"blog.krebs")
                end
----

So in general I'm looking at these fields :-
* sender address (message.from.email)
* sender name (message.from.name)
* recipient (handy when it's to a list address, using a copy of
message.recipients.each)
* subject (message.subj)
* unusual headers (message.raw_header)

and using decisions based on those to change the tags, using
message.add_label and message.remove_label -- but I'm doing those via
my helper function tagit()

Hope that helps :-)

-jim
_______________________________________________
sup-talk mailing list
sup-talk@rubyforge.org
http://rubyforge.org/mailman/listinfo/sup-talk