@ _r.types = [m sm n sn d sd c sc cr w ow p sp i si]
@ _r.formats = [msg send_msg notice send_notice dcc_chat send_dcc_chat ctcp send_ctcp ctcp_reply bwall wall public send_public invite invite_user]
@ _r.lit = [message sent_message notice sent_notice dcc_chat sent_dcc_chat ctcp sent_ctcp ctcp_reply channel_wall wallop public sent_public invite sent_invite]
@ _r.flags = [k t bk ok w ow p n m me]
@ _r.cmds = [k topic bk kill wall wallops silmsg notice silmsg memo]

alias silmsg {
  ^msg $*
  ${!ischannel($0) ? [addtab /m $0] :}
  echo ${!ischannel($0) ? fparse(format_send_msg $tolower($Z) $0 ! $1-) : ([$0] == C ? fparse(format_send_public $tolower($Z) $C $N $1-) : fparse(format_send_public_other $tolower($Z) $0 $N $1-))}}

fe ($_r.types) aa {
  ^alias rel$aa relay $aa $*
  ^alias l$aa rcat $aa
  ^alias rel$aa\k relay $aa -k $*
  ^alias rel$aa\t relay $aa -t $*}

on ^hook ra* {
  @ numitems(rel_$1)==10 ? delitem(rel_$1 0) :
  @ setitem(rel_$1 $numitems(rel_$1) $time() $2-)}

alias relay {
  if (match($0 $_r.types)&&[$1]!=[-l]) {
    ^local aa $_rparse($*)
    if (word(1 $aa) >= 0) {
      if (word(1 $aa) < numitems(rel_$word(0 $aa))) {
        ^local bb $getitem(rel_$word(0 $aa) ${numitems(rel_$word(0 $aa)) - word(1 $aa) + 1})
        $copattern($word(2 $aa) _r.flags _r.cmds) $sar(g/&/$word(1 $bb)/$word(3 $aa)) ${match($word(2 $aa) k bk t)?stripansicodes($_fparse(format_$copattern($word(0 $aa) _r.types _r.formats) $restw(2 $bb))):_fparse(format_$fixrpub($copattern($word(0 $aa) _r.types _r.formats) $restw(4 $bb)) $restw(2 $bb))}} else {
        ge item #$word(1 $aa) does not exist.}} else {
      if (word(1 $aa) * -1 <= numitems(rel_$word(0 $aa))) {
        for (@xx=numitems(rel_$word(0 $aa)) + word(1 $aa), xx < numitems(rel_$word(0 $aa)), @xx++) {
          ^local bb $getitem(rel_$word(0 $aa) $xx)
          $copattern($word(2 $aa) _r.flags _r.cmds) $sar(g/&/$word(1 $bb)/$word(3 $aa)) $_fparse(format_$fixrpub($copattern($word(0 $aa) _r.types _r.formats) $restw(4 $bb)) $restw(2 $bb))}} else {
        ge multiple relay out of range.}}} else {
    if ([$1]==[-l]) {
      rcat $0} else {
      ausage relay}}}

alias fixrpub {
  if ([$0]!=[public]) {
    ^return $0} else {
    @function_return=[$1]==C?[public]:[public_other]
    @function_return#=[$2-]=~[*$N*]?[_ar]:}}
     
alias rcat {
  if (match($0 $_r.types)) {
    if (numitems(rel_$0)) {
      ^set continued_line $box.echo()$repeat(5  )
      echo $box.head()
      echo $box.echo_head($sar(g/_/ /$copattern($0 _r.types _r.lit)) buffer$(c.hk).$(cl). use /rel$0 $(c.hk)[$(c.lc)#$(c.hk)]$(cl) to relay.)
      if (flag(a.rlasc)) {
        for (@xx=0, xx < numitems(rel_$0), @xx++) {
          ^local yy $getitem(rel_$0 ${numitems(rel_$0) - xx + 1})
          echo $box.echo($(c.hk)[$(c.hw)$xx$(c.hk)] [$(c.lc)$_fixmtime($word(0 $yy))$(c.hk)]$(cl) $fparse(format_$fixrpub($copattern($0 _r.types _r.formats) $restw(4 $yy)) $restw(2 $yy)))}} else {
        for (@xx=numitems(rel_$0) - 1, xx >= 0 , @xx--) {
          ^local yy $getitem(rel_$0 ${numitems(rel_$0) - xx + 1})
          echo $box.echo($(c.hk)[$(c.hw)$xx$(c.hk)] [$(c.lc)$_fixmtime($word(0 $yy))$(c.hk)]$(cl) $fparse(format_$fixrpub($copattern($0 _r.types _r.formats) $restw(4 $yy)) $restw(2 $yy)))}}
      echo $box.footer()
      ^set -continued_line} else {
      ge the $sar(g/_/ /$copattern($0 _r.types _r.lit)) buffer is empty.}} else {
    ausage relay}}
