Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: minor tweaks

In this cookbook recipe, we're going to show how you can raise events to a pico using an external mail service, like mainIn mailin. Transforming a mail message into an event isn't too difficult. The fun part is in parsing the body of the email to take further action. This recipe shows how to do that too. 

...

Code Block
linenumberstrue
  global {
    newline = (13.chr() + "?" + 10.chr()).as("RegExp")
    var = function(s) {
      p = s.split(re#=#);
      {}.put(p[0],p[1])
    }
    vars = function(s) {
      p = s=>s.split(re#&#)|[];
      p.reduce(function(a,b){a.put(var(b))},{})
    }
    parse = function(text,domain) {
      dlpo = domain.length() + 1;
      text.split(newline)
          .filter(function(v){p=v.split(re#/#);p[0]==domain && p.length()==2})
          .map(function(v){p=v.split(re#[?]#);[p[0].substr(dlpo),vars(p[1])]})
    }
  }
  rule test_test {
    select when test test
    foreach parse(event:attr("text"),"aurora") setting(request)
    pre {
      type = request[0].klog("REQUESTED_TYPE")
      attrs = request[1].klog("REQUESTED_ATTRS")
    }
    if request.length() == 2 then noop()
    fired {
      raise aurora event type attributes attrs;
      ent:lastRequest := "aurora/"+type
    }
  }

...