### CTCP PING routines rewritten to be more informative.
### If you ping yourself, that information goes into the (lag) indicator.
### The (lag) indicator is an estimate of the minimum lag to the rest
### of the world; the fastest ping should be from yourself to yourself;
### fsclient puts this number into (lag).  If there's no (lag), there's
### no lag.
### If you get pinged, fsclient attempts to calculate the length of time
### ago that the ping was sent out.  Unless you're on the same system,
### this number is not likely to be accurate because not all systems are
### perfectly synced.

on ^ctcp_reply "% ping *" if ([$0]==[$N])
   { if (time()-[$2] > 0)
     { set status_user1  \(lag ${time()-[$2]}\) }
     { set -status_user1 }
   }
   { if (time()-[$2] == 1)
     { echo *** CTCP PING reply from $0: ${time()-[$2]} second }
     { echo *** CTCP PING reply from $0: ${time()-[$2]} seconds }
   }

on ^ctcp "% % ping *" if ([$0]!=[$N])
   { if ([$1]==[$N])
     { if ((time()-[$3] < 2) || (time()-[$3] > 10000))
       { echo *** $0 just pinged you. }
       { echo *** $0 pinged you ${time()-[$3]} seconds ago.  This number may be inaccurate. }
     }
     { if ((time()-[$3] < 2) || (time()-[$3] > 10000))
       { echo *** $0 just pinged $1\. }
       { echo *** $0 pinged $1\ ${time()-[$3]} seconds ago.  This number may be inaccurate.}
     }
   }

### autopings must be /notice'd otherwise you get /away responses

on ^notice "% autoping *" if ([$0]==[$N])
   { if (time()-[$2] > 0)
     { set status_user1  \(lag ${time()-[$2]}\) }
     { set -status_user1 }
#     echo *** AUTOPING noticed.
   }

alias autoping {
        ^set status_user1  \(lag ?\)
        ^eval notice $N autoping $time()
}

alias ping {
      if ([$0]==[$N])
        {
        ^echo *** selfpings are reflected in the (lag) indicator.
        ^autoping
        }
        { //ping $0 }
}

### Every minute, update the (lag) indicator.

### on #^timer *0 autoping

on #^timer 50 * autoping
