#!/bin/sed -unrf
# read config into hold buffer
h;
# use domain as to
s#^\S+@(\S+)\s.+$##;
p;n;
:wait-for-plain;
/^PLAIN<\/mechanism$/bauth-with-plain;
n;
bwait-for-plain;
:auth-with-plain;
# load config
g;
s#^\S+\s+(\S+)$#\1#;
p;n;
bwait-for-sasl-result;
:wait-for-sasl-result;
/^#;
p;n;
bwait-for-bind;
:sasl-failure;
s#^.+$##;
q1;
:wait-for-bind;
/^#;p;n;
bwait-for-bind-result;
:wait-for-bind-result;
/#;p;q1;
:send-presence;
s#^.+$##;p;n;
bmain-loop;
:main-loop;
/#;
# store result in hold space
h;
bmessage-search-body-loop;
:message-search-body-loop;
# drop message if end-of-message before body
\#\1