### coherent for bitchx! 100% original by shade (shade@snafu.north.wfu.edu)
### hihihi to shewp, dh, suicide, tarotgrl, panasync, fudd, psykotyk &
### everyone else on #bitchx.  people suck.  

### code ripping has always been cause for decapitation.
### the "pipe" & "_userhost" functions were taken from the epic scriptpack.
### the "_trans" function was originally a tcl script by by-tor.

### lets make sure ppl are running the correct juarez
do ${!bitchx()?{echo $G coherent is a bitchx script, therefore it requires bitchx, dumbass.;echo $G get your copy from ftp.bitchx.com.;//quit im a complete moron, i loaded coherent without bitchx!}:{${V<19971107?{echo $G coherent is a state of the art script, and requires bitchx-74;echo $G or better.  get your copy from ftp.bitchx.com.;//quit im a moron, i loaded coherent with old bitchx!}:[]}}}

### client sets
set deop_on_deopflood 20
set kick_on_deopflood 20
set kick_on_kickflood 10
set kick_on_nickflood 10
set kick_on_pubflood 5
set deopflood_time 5
set kickflood_time 5
set nickflood_time 5
set pubflood_time 5
set flood_after 15
set flood_rate 15
set mode_stripper on
set show_channel_names off
^set exec_protection off
set msglog off
set client_information deviate from the absolute
set comment_breakage off

### file paths
@coh.prefix=twiddle($ctoolz_dir)
@coh.memofile=[$coh.prefix/coh.memos]
@coh.msglogfile=[$coh.prefix/coh.msglog]
@coh.ilinesfile=[$coh.prefix/coh.ilines]
@coh.botfile=[$coh.prefix/coh.bots]
@coh.savefile=[$coh.prefix/coh.config]
@coh.fkeys=[$coh.prefix/coh.fkeys]
@coh.helpfile=[$coh.prefix/coh.help]
@coh.url=[$coh.prefix/coh.urls]

### toggles & settings
@repeatlimit=3
@punclimit=5
@coh.urlgrab=[on]
@coh.topicbar=[on]
@coh.msglog=[on]
@coh.pager=[on]
@coh.awaymsg=[the bottle makes the final call]
@coh.backmsg=[you are the sweetest disease for me]
@coh.autoawaymins=10
@coh.autoawaymsg=[autoaway after $coh.autoawaymins\m]
@coh.email=[$USER@$HOSTNAME]
@coh.cpublics=[on]
@coh.mckick=[on]
@coh.autocdccsave=[on]
@coh.recycle=[on]
@fkey[1]=[bhelp index]
@fkey[2]=[user]
@fkey[3]=[sv]
@fkey[4]=[cdcc plist]
@fkey[5]=[readlog]
@fkey[6]=[scan]
@fkey[7]=[scano]
@fkey[8]=[scann]
@dict.server=[miranda.org 2628]

### internal variables
@_lame.versions=[*mirc* *ircle* *ircn*]
@_bitch.ver=[bx-$after(1 - $J)]
@_coherent.ver=[coherent-1.00(final)]
@_coh.atime=A?time():0
@_uhost=hostname?stripansicodes($user@$hostname):X
@_ae=0
@_urlnum=0

### colors
@cl=[\[0m
@blk=[\[0;30m
@blu=[\[0;34m
@grn=[\[0;32m
@cyn=[\[0;36m
@red=[\[0;31m
@mag=[\[0;35m
@yel=[\[0;33m
@wht=[\[0;37m
@hblk=[\[1;30m
@hblu=[\[1;34m
@hgrn=[\[1;32m
@hcyn=[\[1;36m
@hred=[\[1;31m
@hmag=[\[1;35m
@hyel=[\[1;33m
@hwht=[\[1;37m
@bwht=[\[47m
@bmag=[\[45m
@bblu=[\[44m
@bred=[\[41m
@bblk=[\[40m
@bgrn=[\[42m
@byel=[\[43m
@bcyn=[\[46m

### fsets
fset format_public %K(%n$1%K)%n $3-
fset format_public_other %K(%n$1%K:%n$2%K)%n $3-
fset format_public_ar %K(%C$1%K)%n $3-
fset format_public_other_ar %K(%C$1%K:%n$2%K)%n $3-
fset format_send_public %K(%c$2%K)%n $3-
fset format_away  is occupied: $_away.format()
fset format_back has returned: $_a.backmsg ($nospace($tdiff2(${time()-_coh.atime})))
fset format_version $_bitch.ver$_fix.tcl($4$5)/$tolower($2)-$3($after(@ $before(. $_uhost))) + $_coherent.ver
fset format_whois_header %W ÚÄÄÄ[0mÄÄÄ ÄÄ[1mÄÄÄ[0mÄ[1;30mÄ[0mÄÄ ÄÄ[1mÄ[30mÄÄ Ä[0mÄÄ[1;30mÄ[0mÄ  Ä[1;30mÄÄ[0m   [1;30mÄ[0m   [1;30mÄÄ[0m
fset format_whois_nick %K[%W³%K]  %c$[9]0%c:%n $1%K@%n$2 %m(%n$country($2)%m)%n
fset format_whois_name %K[%W³%K]  i%nrcname  %c:%n $*
fset format_whois_channels %K[%W³%K]  c%nhannels %c:%n $*
fset format_whois_server %K[%W³%K]  s%nerver   %c:%n $0 %m(%n$1-%m)
fset format_whois_away %K[%W³%K]  a%nway     %c:%n $*
fset format_whois_idle %K[%W³%K]  i%ndle     %c:%n $_fixitime($*)
fset format_whois_oper %K[%W³%K]  o%nperator %c:%n has no life whatsoever.
fset format_whois_shit %K[%W³%K]  s%nhitlist %c:%n %cl%m/%n$0%K:%n$1 %m(%n$2-%m)
fset format_whois_friend %K[%W³%K]  f%nriend   %c:%n %cl%m/%n$0%K:%n$1-
fset format_whois_footer [0m [1mÀÄÄÄÄÄÄ[0mÄÄÄ[1mÄÄ[0mÄ[1mÄ[0mÄ ÄÄÄÄ[1;30mÄ[0mÄ[1mÄ[0mÄÄ[1;30mÄÄÄÄÄ[0mÄÄ Ä[1;30mÄÄ[0m Ä[1mÄ[30mÄ[0m  [1;30mÄÄÄ[0m   [1;30mÄ[0m   [1;30mÄ[0m[24C[1m
fset format_whowas_header %W ÚÄÄÄ[0mÄÄÄ ÄÄ[1mÄÄÄ[0mÄ[1;30mÄ[0mÄÄ ÄÄ[1mÄ[30mÄÄ Ä[0mÄÄ[1;30mÄ[0mÄ  Ä[1;30mÄÄ[0m   [1;30mÄ[0m   [1;30mÄÄ[0m
fset format_whowas_nick %K[%W³%K]  %c$[9]0%c:%n $1%K@%n$2 %m(%n$country($2)%m)%n
fset format_whowas_footer [0m [1mÀÄÄÄÄÄÄ[0mÄÄÄ[1mÄÄ[0mÄ[1mÄ[0mÄ ÄÄÄÄ[1;30mÄ[0mÄ[1mÄ[0mÄÄ[1;30mÄÄÄÄÄ[0mÄÄ Ä[1;30mÄÄ[0m Ä[1mÄ[30mÄ[0m  [1;30mÄÄÄ[0m   [1;30mÄ[0m   [1;30mÄ[0m[24C[1m
fset format_nonick %W ÚÄ %c$1%m/%n$3-
fset format_msg %K[%C$1%K]%n $3- $_q.addy($1 $2)
fset format_notice %K-%W$1%K-%n $3-
fset format_status1 $*
fset format_status2 $*
fset format_status3 $*
fset format_send_away [ setaway since $strftime($1 %x)@$_fixmtime($1) - email $coh.email ]
fset format_join $G %K[%cjoin%m(%n$3%m)%K] %Cö %K[%c$1%m(%n$2%m)%K]
fset format_leave $G %K[%cleave%m(%n$3%m)%K] %Cö %K[%c$1%m(%n$2%m)%K]
fset format_mode $G %K[%cmode%m(%n$3%m)%K] %Cö %K[%c$1%m(%n$4-%m)%K]
fset format_smode $G %K[%cmode%m(%n$3%m)%K] %Cö %K[%c$1%m(%n$4-%m)%K]
fset format_channel_signoff $G %K[%csignoff%m(%n$3%m)%K] %Cö %K[%c$1%m(%n$4-%m)%K]%n
fset format_send_msg %K[%cmsg%m(%n$1%m)%K]%n $3-
fset format_send_ctcp %K[%cctcp%m(%n$1%m)%K]%n $2-
fset format_send_notice %K[%cnotice%m(%n$1%m)%K]%n $3-
fset format_send_dcc_chat %K[%cdcc%m(%n$1%m)%K]%n $2-
fset format_dcc_connect $_cmd %cdcc%m/%n$1 connection established. %m(%n$4%K:%n$5%m)%n
fset format_dcc_lost $_cmd %cdcc%m/%n$1%K: "%n$2%K" %n$4 %c$5%n complete. %m(%n$6s $7kb/s%m)%n
fset format_dcc_request $_cmd %cdcc%m/%n$1 request %K"%n$2%K"%n %K[%n$8$7%K]%n from %c$3%m(%n$4%m)%n
fset format_ctcp $_cmd %cctcp%m/%n$fixctcp($4-) request from %c$1%m(%n$2%m)%n to $fixcnick($3).
fset format_ctcp_user $_cmd %cctcp%m/%n$fixctcp($4-) request from %c$1%m(%n$2%m)%n to $fixcnick($3).
fset format_ctcp_cloak $_cmd %cctcp%m/%n$fixctcp($4-) request from %c$1%m(%n$2%m)%n to $fixcnick($3).
fset format_ctcp_cloak_func $_cmd %cctcp%m/%n$fixctcp($4-) request from %c$1%m(%n$2%m)%n to $fixcnick($3).
fset format_ctcp_cloak_func_user $_cmd %cctcp%m/%n$fixctcp($4-) request from %c$1%m(%n$2%m)%n to $fixcnick($3).
fset format_ctcp_unknown $_cmd %cctcp%m/%n$fixctcp($4-) request from %c$1%m(%n$2%m)%n to $fixcnick($3).
fset format_ctcp_unknown_user $_cmd %cctcp%m/%n$fixctcp($4-) request from %c$1%m(%n$2%m)%n to $fixcnick($3).
fset format_ctcp_func $_cmd %cctcp%m/%n$fixctcp($4-) request from %c$1%m(%n$2%m)%n to $fixcnick($3).
fset format_ctcp_func_user $_cmd %cctcp%m/%n$fixctcp($4-) request from %c$1%m(%n$2%m)%n to $fixcnick($3).
fset format_ctcp_reply $_cmd %cctcp%m/%n$3 reply from %c$1%K:%n $4-
fset format_topic $_gettopic($*)
fset format_topic_setby $G %ctopic%m/%n$1%K:%n $_top %m(%nsetby $2 on $strftime($3 %x)%K@%n$_fixmtime($3)%m)%n
fset format_userlist    %K[$_uldots($1!$3'$4/$_trans($0)/$2)%K]
fset format_userlist_header    %K[%Wnick!user@host                                  chans/levels/password%K]
fset format_userlist_footer    %K[$ccenter(69 ${cyn}total users${mag}:${cl} $1)%K]
fset format_shitlist    %K[$_uldots($1'$2/$0/$3-)%K]
fset format_shitlist_header    %K[%Wuser@host                                          chans/level/reason%K]
fset format_shitlist_footer    %K[$ccenter(69 ${cyn}total users${mag}:${cl} $1)%K]

### lame echostrs
@G=[${hblk}ö${hcyn}ð${hblk}ö${cl}]
do set show_numerics_str $G
@_cmd=[${hblk}>${cl}>${hwht}>${cl}]

### global aliases and functions
alias ce echo $G $*
alias _gettopic {
	@_top=[$2-]
	@function_return=[]}
alias _trans {
	for (@xx=0, xx<strlen($0), @xx++) {${mid($xx 1 $0)?[@push(function_return $word($xx voice ops ban unban invite dcc kill iops flood bot reop reop deop kick pban pinvite))]:[]}}}
alias _cpublic {
	@function_return=coh.cpublics==[on]?[$*]:stripansicodes($*)}
alias _fixnotime {
	@_blah=[$*];@function_return=[${time()-_blah}]}
alias _fixmtime {
	@function_return=strftime($0 %I:%M)##tolower($strftime($0 %p))}
alias _q.addy {
	@function_return=Q==[$0]?[]:[${hblk}[${cyn}$1${hblk}]${cl}]}
alias _fixitime {
	@function_return=[$3]?[${[$0]!=[0]?[$0h]:[]}${[$1]!=[0]?[$1m]:[]}${[$2]!=[0]?[$2s]:[]}   signon\: $strftime($0 %x)@$_fixmtime($0)]:[${[$0]!=[0]?[$0h]:[]}${[$1]!=[0]?[$1m]:[]}${[$2]!=[0]?[$2s]:[]}]}
alias repeatchr {
	for (@rcnt=0, rcnt<[$0], @rcnt++) {@function_return#=[$1]}}
alias datetime {
	@function_return=[${HBLK}[${HBLU}$strftime($0 %x)${HBLK}@${HBLU}$_fixmtime($0)${HBLK}]${CL}]}
alias nospace {
	@function_return=sar(g/ //$*)}
alias _away.format {
	@function_return=[$_a.msg \(${coh.msglog==[on]?[l]:[]}${coh.pager==[on]?coh.msglog==[on]?[/p]:[p]:[]}\)]}
alias _fix.tcl {
	@function_return=[$0]?tolower(\($*\)):}
alias ccenter {
	@function_return=sar(g&$stripansicodes($1-)&$1-&$pad(-$0 $center($0 $stripansicodes($1-))))}
alias fixctcp {
	@function_return=[$0]==[PING]?[PING]:[$1]==[]||[$1]==[ ]?toupper($0):toupper($*)}
alias fixcnick {
	@function_return=[$0]==[$N]?[you]:[$0]}
alias numchars {
	fec ($1-) _punc.char {${[$_punc.char]==[$0]?[@function_return++]:[]}}}
alias comchan {
	fe ($mychannels()) curchan {${onchannel($0 $curchan)?[@push(function_return $curchan)]:[]}}}
alias _userhost {
	^local blahblah
	wait for {^userhost $* -cmd {bless;push blahblah $3@$4}}
	^return $blahblah}
alias dots {
	@function_return=repeatchr($0 .)} 
alias _ndots {
	^local blah ${[$2]?[$*]:[$0 $_fixmtime($1)]}
        @function_return=[${cl}$word(0 $blah)${hblk}$dots(${70-strlen($blah)})${cl}$restw(1 $blah)]}
alias _idots {
	@function_return=[${hcyn}$0${hblk}$dots(${70-strlen($*)})${cyn}$1-${cl}]}
alias _uldots {
	@function_return=[${cl}$before(' $*)${hblk}$dots(${70-strlen($*)})${cl}$after(' $*)]}
alias _bdots {
	@function_return=[${hmag}$0${hblk}$dots(${70-strlen($*)})${mag}$1-${cl}]}
alias _cdots {
	@function_return=[   ${hblk}[${cyn}$before(| $*)${hblk}$dots(${70-strlen($*)})${hwht}$after(| $*)${hblk}]${cl}]}
alias _fdots {
	@function_return=[   ${hblk}[${mag}fkey $0${hblk}$dots(${65-strlen($*)})${cyn}$1-${hblk}]${cl}]}
alias percentbar {
	@function_return=cparse(${[$0]>=90?["%K±²Û%6%K²±° %C°±²%n"]:[$0]>=80?["%K±²Û%6%K²±° %C°±%0%K°%n"]:[$0]>=70?["%K±²Û%6%K²±° %C°%0%K°°%n"]:[$0]>=60?["%K±²Û%6%K²±° %0%K°°°%n"]:[$0]>=50?["%K±²Û%6%K²±°%0%K°°°°%n"]:[$0]>=40?["%K±²Û%6%K²±%0%K°°°°°%n"]:[$0]>=30?["%K±²Û%6%K²%0%K°°°°°°%n"]:[$0]>=20?["%K±²Û°°°°°°°%n"]:[$0]>=10?["%K±²°°°°°°°°%n"]:["%K±°°°°°°°°°%n]})}
alias ocuhost {
	fe ($chops($0)) _ocu {@push(function_return $_ocu!$uhost($_ocu))}}
alias _se {
	@oldstatus=!_seing?status_format1:oldstatus
	@_seing=1
	^window set status_format1 $cl$cyn$center(80 $*) 
	^timer 5 {
 		^window set status_format1 $oldstatus
		@_seing=0}}
alias fixuptime {
	@function_return=[${[$0]!=[0d]?[$0]:[]}${[$1]!=[0h]?[$1]:[]}$2]}
alias fixeta {
	@function_return=strlen($1)==1?[eta${hblk}\:${cl}$0\:0$1]:[eta${hblk}\:${cl}$0\:$1]}
alias _csort {
	@csort1=[$*]
	@csort2=sar(g/0/o/$tolower($strip(.+@|*[]-/`_\^~ $csort1)))
	fe ($sort($csort2)) sortme {@push(function_return $copattern($sortme csort2 csort1))}
	@function_return=uniq($function_return)}
alias _fixscan {
	@_nw=numwords($*)
	@function_return=_nw==1?ccenter(72 ${mag}\(${cl}$[10]0${mag}\)${cl}):_nw==2?ccenter(72 ${mag}\(${cl}$[10]0${mag}\)                  ${mag}\(${cl}$[10]1${mag}\)${cl}):_nw==3?ccenter(72 ${mag}\(${cl}$[10]0${mag}\) ${hcyn}ö${cl} ${mag}\(${cl}$[10]1${mag}\) ${hcyn}ö${cl} ${mag}\(${cl}$[10]2${mag}\)${cl}):_nw==4?ccenter(72 ${mag}\(${cl}$[10]0${mag}\) ${hcyn}ö${cl} ${mag}\(${cl}$[10]1${mag}\)                  ${mag}\(${cl}$[10]2${mag}\) ${hcyn}ö${cl} ${mag}\(${cl}$[10]3${mag}\)${cl}):ccenter(70 ${mag}\(${cl}$[10]0${mag}\) ${hcyn}ö${cl} ${mag}\(${cl}$[10]1${mag}\) ${hcyn}ö${cl} ${mag}\(${cl}$[10]2${mag}\) ${hcyn}ö${cl} ${mag}\(${cl}$[10]3${mag}\) ${hcyn}ö${cl} ${mag}\(${cl}$[10]4${mag}\)${cl})}
alias _fixstime {
	^stack push set floating_point_math_var
	^set floating_point_math_var on
	@function_return=[$0]<1?[${[$0]*1000}ms]:[$trunc(1 $0)s]
	^stack pop set floating_point_math_var}

### hop's leet pipe alias
alias pipe {
	@pipe.intval++
	^local mypipeval $pipe.intval
	^local mypipedesc pipe$mypipeval
	^local mypiperetval
	^on ^exec "$mypipedesc *" {
		bless
		push mypiperetval $1-}
	^exec -name $mypipedesc $*
	^wait %$mypipedesc
	^on exec -"$mypipedesc *"
	return $mypiperetval}

### status bar (lotsa help from dh here ;>)
set status_server [0m[36mserver[0m[35m([0m%S[0m[35m)[0m
set status_lag [0m[36mlag[0m[35m([0m%L[0m[35m)[0m
set status_users [1m[36mö [0m[36mo[0m[35m/[0m%! [0m[36mn[0m[35m/[0m%! 
set status_away [0m[36maway [1;36mö
set status_umode [0m[35m([1m[30m+[0m%#[0m[35m)[0m
set status_mode [0m[35m([1m[30m+[0m%+[0m[35m)[0m
set status_chanop [1m[36m@[0m
set status_voice [1m[36m+[0m
set status_nick [0m[36m%N[0m
set status_channel [1m[36mö [0m[36m%C[0m
set status_hold [0m[35m([0mmore[0m[35m)[0m [1;36mö 
set status_query [1;36mö [0;36mquery[0m[35m([0m%Q[0m[35m)[0m
set status_mail [0m[36mmail[0m[35m([0m%M[0m[35m) [1;36mö 
set status_topic [1;30m[[0m:[1;30m] [0;36mtopic[0;35m/[0m%- 
do set status_user2 $fixuptime($uptime())
set status_format1 [0m[1;30m[[0m|[1;30m] %@%N%# %C%+ %! %> %H %M %A %L [1;30m[[0m|[1;30m]
set status_format2 [1;30m[[0m³[1;30m][0;36m %T [1m[36mö %S %Q %> [0m[36muptime[0m[35m([0m%2[0m[35m) [1;30m[[0m:[1;30m][0m
set status_format3 %- 
set input_prompt [1;30m [37mÀ>[0m 
do window set status_server $status_server
do window set status_lag $status_lag
do window set status_users $status_users
do window set status_away $status_away
do window set status_umode $status_umode
do window set status_mode $status_mode
do window set status_voice $status_voice
do window set status_chanop $status_chanop
do window set status_nick $status_nick
do window set status_hold $status_hold
do window set status_channel $status_channel
do window set status_query $status_query
do window set status_mail $status_mail
do window set status_topic $status_topic
do window set status_user2 $status_user2
do window set status_format1 $status_format1
do window set status_format2 $status_format2
do window set status_format3 $status_format3
do ^window split $coh.topicbar

### map fkeys
bind meta3-A parse_command dokey 1
bind meta3-B parse_command dokey 2
bind meta3-C parse_command dokey 3
bind meta3-D parse_command dokey 4
bind meta3-E parse_command dokey 5
bind meta3-7 parse_command dokey 6
bind meta3-8 parse_command dokey 7
bind meta3-9 parse_command dokey 8
alias dokey {
  ^bind ~ nothing
  $fkey[$0]
  ^timer 1 ^bind ~ self_insert}

### a better /scan
alias sc scan $*
alias scann scan -n $*
alias scano scan -o $*
alias names scan $*
alias scan {
	@_sf=[$0]==[-o]||[-n]?[$0]:[]
	@_sc=remw($_sf $*)?remw($_sf $*):C
	@_ops=[${[$_sf]!=[-n]?_csort($pattern(*@* $channel($_sc))):[]}]
	@_lamers=[${[$_sf]!=[-o]?_csort($strip(. $filter(*@* $channel($_sc)))):[]}]
	echo $cparse("$format_whois_header")
	${#_ops?{echo ${hblk}[${cl}³${hblk}] $ccenter(70 ${cyn}operators on $_sc${hblk}\: ${cl}$#_ops);fe ($_ops) _o1 _o2 _o3 _o4 _o5 {echo ${hblk}[${cl}³${hblk}]${cl} $sar(g/@/${hblk}@${cl}/$_fixscan($_o1 $_o2 $_o3 $_o4 $_o5))}}:[]}
	${#_lamers?{echo ${hblk}[${cl}|${hblk}] $ccenter(70 ${cyn}non-operators on $_sc${hblk}\: ${cl}$#_lamers);fe ($_lamers) _l1 _l2 _l3 _l4 _l5 {echo ${hblk}[${cl}|${hblk}]${cl} $sar(g/+/${hblk}+${cl}/$_fixscan($_l1 $_l2 $_l3 $_l4 $_l5))}}:[]}
	${_syn!=[]?{echo ${hblk}[${cl}\:${hblk}] $ccenter(72 ${cyn}total users${hblk}\: ${cl}$numonchannel($_sc)  ${cyn}created${hblk}\:${cl} $strftime($_cctime %x)${hblk}@${cl}$_fixmtime($_cctime)  ${cyn}synch${hblk}\:${cl} $_fixstime($_syn));@_syn=[]}:[echo ${hblk}[${cl}\:${hblk}] $ccenter(70 ${cyn}total users on $_sc${hblk}\: ${cl}$numonchannel($_sc))]}}

### a better http grabber
on -public * {
	${coh.urlgrab==[on]&&[$2-]=~[*http\:\/\/\*]||[$2-]=~[*ftp\:\/\/\*]?[^hook _u.write $*]:[]}}
on -public_other * {
	${coh.urlgrab==[on]&&[$2-]=~[*http\:\/\/\*]||[$2-]=~[*ftp\:\/\/\*]?[^hook _u.write $*]:[]}}
on ^hook _u.write* {
	^local xx ${[$3-]=~[*http\:\/\/\*]?[http]:[ftp]}
	${pipe(grep "$word(${match(\*$xx\* $*)-1} $*)" $coh.url)?[]:{@_u.fd=open($coh.url W);@write($_u.fd $datetime($time())${hblk}\: ${cl}$fparse(format_public_other $*));@close($_u.fd);@_urlnum++;ce grabbed url \#$_urlnum to $coh.url\.}}}
alias url {
	${fexist($coh.url)==1?[more $coh.url]:[ce no urls have been saved in $coh.url\.$]}}
alias remurl {
	@unlink($coh.url)
	ce $coh.url has been removed.}

### dcc status meter
on -dcc_connect "*" {
	${[$1]==[SEND]||[$1]==[GET]?{@dccs++;^window set status_format2 ${hblk}[${cl}³${hblk}]${cyn} %T ${hcyn}ö %S %Q %> %4 ${hblk}[${cl}\:${hblk}]${cl};^hook _dcc.stats}:[]}}
on -dcc_lost "*" {
	${[$1]==[SEND]||[$1]==[GET]?{@dccs--;@dccs=dccs<0?0:dccs;^window set status_format2 ${hblk}[${cl}³${hblk}]${cyn} %T ${hcyn}ö %S %Q %> ${cyn}uptime${mag}\(${cl}%2${mag}\) ${hblk}[${cl}\:${hblk}]${cl}}:[]}}
on -dcc_statf * {
	@_dt=[$1];@_dn=[$2];@_ds=[$4]}
on -dcc_statf1 * {
	@_dp=[$0];@_de=fixeta($3 $4)}
on -hook _dcc.stats {
	^dcc
	${_dp!=_ldp?{@_ldp=[$_dp];^window set status_user4 ${cyn}$tolower($_dt)${mag}/${cl}$_dn${hblk}\: ${mag}\(${cl}$percentbar($_dp) ${cl}$_de${mag}\)${cl}}:[]}
	${dccs?[^timer 1 ^hook _dcc.stats]:[]}}

### my /dict client
alias dict {
	${[$[1]0]==[-]?{@_d.fl=strip(- $0);@_d.q=[$1-]}:{@_d.fl=[];@_d.q=[$*]}}
	@_d.db=_d.fl=~[*d*]?shift(_d.q):[*]
	@_d.l=_d.fl=~[*l*]?[l]:[]
	^@_d.fd=connect($dict.server)
	^on ^dcc_lost "$_d.fd *" {^on ^dcc_lost -"$_d.fd *"}
	^on ^dcc_raw "$_d.fd % d *" {${[$3]==[552]?[ce no match for your query.]:[$3]==[220]?_d.l==[l]?{@writesocket($_d.fd show db);@writesocket($_d.fd quit)}:{@writesocket($_d.fd define $_d.db $_d.q);@writesocket($_d.fd quit)}:[$3]==[221]?[^on ^dcc_raw -"$_d.fd % d *"]:[$3]==[550]?{ce ${hblk}\"${cl}$_d.db${hblk}\"${cl} is not a valid database.}:[$3]==[250]?[@writesocket($_d.fd quit)]:[$3]==[110]?{@_ae=1;ce the following databases are valid...}:[$3]==[151]?{@_ae=1;${_d.fl=~[*o*]?[say]:[ce]} from $before(\: $6-)...}:[$3]==[.]?[@_ae=0]:_ae?[${_d.fl=~[*o*]?[send]:[ce]} $3-]:[]}}}

### a better notify handler
@_notify=notify()
^notify -
@delarray(n_off)
@delarray(n_on)
fe ($_notify) nn {
	@setitem(n_off $numitems(n_off) $nn!*@* not yet)}
on ^notify_signon_uh * {
	echo $_cmd ${cyn}signon${mag}/${cl}$0 ${mag}\(${cl}$1${mag}\)${cl} at $_fixmtime($time()).
	@delitem(n_off $matchitem(n_off \*$0\*))
	@setitem(n_on $numitems(n_on) $0!$1 $time())}
on ^notify_signoff_uh * {
	echo $_cmd ${cyn}signoff${mag}/${cl}$0 ${mag}\(${cl}$1${mag}\)${cl} at $_fixmtime($time()).
	@delitem(n_on $matchitem(n_on \*$0\*))
	@setitem(n_off $numitems(n_off) $0!$1 $time())}
alias notify {
	${[$0]?[//notify $0-]:{${numitems(n_on)?[echo    ${hblk}[${cyn}currently online\:                                         signon time${hblk}]]:[]};for (@xx=0, xx<numitems(n_on), @xx++) {echo    ${hblk}[$_ndots($getitem(n_on $xx))${hblk}]};${numitems(n_off)?[echo    ${hblk}[${cyn}currently offline\:                                          last seen${hblk}]]:[]};for (@xx=0, xx<numitems(n_off), @xx++) {echo    ${hblk}[$_ndots($getitem(n_off $xx))${hblk}]}}}}
do ^notify $_notify

### fix up the operview statbar (this is a TOTAL kludge ;>)
on -window_create * {
	timer 1 {
		^wset oper_view status_server ${cyn}operview${mag}/${cl}%S
		^wset oper_view status_oper_kills ${cyn}nk${mag}/${cl}%K ${cyn}ok${mag}/${cl}%K${cl}
		^wset oper_view status_format1 ${cl}${hblk}[${cl}\:${hblk}]${cl} %S %> %K ${hblk}[${cl}|${hblk}]}}
do set server_prompt ${mag}\(${cl}%s${mag}\)${cl}

### automatic stuff (timers, etc.)
on #-idle 23 "*" {
	${[$0]==coh.autoawaymins&!A?[away $coh.autoawaymsg]:[]}}
on #-timer 23 * {
	${mid(3 2 $Z)==[00]?[ce it is now ${cyn}$Z${cl}.]:[]}
	${A?{^quote away \:$fparse(format_away !) [gone $nospace($tdiff2(${time() - _coh.atime}))]}:[]}
	${coh.recycle==[on]?{fe ($mychannels()) _chan {${numonchannel($_chan)==1&!ischanop($N $_chan)?{ce cycling $_chan to regain ops.;cycle $_chan}:[]}}}:[]}
	^window set status_user2 $fixuptime($uptime())}
on ^channel_synch * {
	@_syn=[$1];scan $0;@_c.mb=getmatches(botlist *$0*);@_b.all=[];${_c.mb!=[]?{fe ($_c.mb) _botitem {${before(! $pattern($word(0 $getitem(botlist $_botitem)) $ocuhost($0)))?[@push(_b.all $_botitem)]:[]}};${_b.all!=[]?{@_ac.connect=getitem(botlist $word(0 $_b.all));echo $G requesting ops on $word(1 $_ac.connect) from $before(! $word(0 $_ac.connect)).;^msg $before(! $word(0 $_ac.connect)) op $decode($word(2 $_ac.connect))}:[]}}:[]}}

### new away handler
alias away {
	@_coh.atime=time()
	@_a.msg=[$*]?[$*]:[$coh.awaymsg]
	//away $_a.msg
	^hook dlog startlog message log started.
	@_a.msgs--}
alias back {
	if (A) {
		^hook dlog closelog message log ended.
		@_a.msgs--
		@_a.backmsg=[$0]?[$*]:[$coh.backmsg]}
		ce you were away for $tdiff(${time() - _coh.atime}). $_a.msgs messages saved.
		//^back
		^on ^window -'*$fparse(format_away !)*' 
		echo $fparse(format_action ! $N ! ! $fparse(format_back !))
		if (_a.msgs) {
			input_char "read awaylog now? (y/n): " if ([$0]==[y]) {readlog}
			input_char "erase this logfile? (y/n): " if ([$0]==[y]) {remlog}}}

### leet-ass i:line finder
alias iline {
	switch ($0) {
	(-g) {if (!gettingilines) {
		@gettingilines=1
		@delarray(ilines)
		ce now creating a list of servers you can connect to.
		ce status will be displayed on the upper statusbar so you can
		ce monitor the progress. we will wait 7 minutes for responses
		ce from all servers before stopping.
		^window split on
		^window set status_format3  %5 %> %6 
		^window set -status_user5
		^window set -status_user6
		^hook _i.q $S *
		^timer 420 {
				@gettingilines=0
				^window set status_format3 %- 
				^window split $coh.topicbar
				ce i:line list has been created. use /iline to display,
				ce and /iline -s to save it.}}{
		ce an i:line list is already being created.}}
	(-s) {
		if (numitems(ilines)) {
			if (fexist($coh.ilinesfile)) {
				input_char "replace the existing ilines list? (y/n): " if ([$0]==[y]) {
					@unlink($coh.ilinesfile)
					^hook _i.s}}{
				^hook _i.s}}{
			ce no i:line list has been created. use /iline -g first.}}
	(*) {
		if (numitems(ilines)) {
			^set hold_mode on
			for (@di=0, di<numitems(ilines), @di++) {
				echo $ccenter(78 ${hblk}[$_idots($getitem(ilines $di))${hblk}]${cl})}
			^set hold_mode off}{
			ce no i:line list has been created. use /iline -g first.}}}}

on -hook "_i.s*" {
	@_c.is=open($coh.ilinesfile W)
	@write($_c.is $time() $S)
	for (@cx=0, cx<numitems(ilines), @cx++) {
		@write($_c.is $getitem(ilines $cx))}
	@close($_c.is)
	ce i:line list has been saved in $coh.ilinesfile}

on -hook "_i.q*" {
	^on ^raw_irc "$1 364 *" {
		if ([$[1]3]!=[*]) {
			^on ^raw_irc "$3 215 *" {
				${[$N!$_uhost]=~[$6]?{^hook _i.a $0 ${[$N!$_uhost]=~[$4]?[$4]:[$6]}}:[]}}
			^on ^raw_irc "$3 219 *" {
				^on raw_irc -"$0 215 *"
				^on raw_irc -"$0 219 *"}
			^window set status_user6 ${cyn}getting stats${hblk}: ${cl}$3
			quote stats i $3}{
			^hook _i.q $3 $3}
		sleep 1}
	^on ^raw_irc "$1 365 *" {
		^on raw_irc -"$0 364 *"
		^on raw_irc -"$0 365 *"}
	^window set status_user6 ${cyn}getting links${hblk}: ${cl}$1 $2
	quote links $1 $2}

on -hook "_i.a*" {
	if ([$2]!=[*]) {
		^window set status_user5 ${hcyn}i:line found! ${mag}\(${cl}$1${hblk}\/${cl}$2${mag}\)${cl}
		@_i.an=matchitem(ilines $1 *)
		${_i.an>-1?[@setitem(ilines $_i.an $uniq($getitem(ilines $_i.an) $2))]:[@setitem(ilines $numitems(ilines) $1-)]}}}
	 
### new /addbot for auto-ops on join
alias addbot {
	@_bot.inp=[$*]
	^local _bot.id
	if (word(2 $_bot.inp)) {
		${word(0 $_bot.inp)!~[*!*@*]?[@_bot.id=[$0!$_userhost($word(0 $_bot.inp))]:[@_bot.id=word(0 $_bot.inp)]}
		@_bot.pass=word(2 $_bot.inp)
		@_bot.chan=word(1 $_bot.inp)
		if (after(! $_bot.id)!=[<UNKNOWN>@<UNKNOWN>]) {
			ce adding bot $_bot.id to the botlist for [$_bot.chan].
			@setitem(botlist $numitems(botlist) $_bot.id $_bot.chan $encode($_bot.pass))}{
			ce no match for \"$word(0 $_bot.inp)\".}}{
		_usage addbot}}
alias unbot {
	if ([$0]) {
		@_c.ub=getmatches(botlist *$0*)
		if (_c.ub) {
			ce removing $word(0 $getitem(botlist $word(0 $_c.ub))) from the botlist.
			@delitem(botlist $word(0 $_c.ub))}{
			echo no bots were found matching \"$0\".}}{
		_usage unbot}}
alias botlist {
	${numitems(botlist)?{for (@zc=0, zc < numitems(botlist), @zc++) {echo $ccenter(78 ${hblk}[$_bdots($word(0 $getitem(botlist $zc)) $word(1 $getitem(botlist $zc)))${hblk}])}}:[ce the botlist is empty.]}} 
alias savebot {
	if (numitems(botlist)) {
		@unlink($coh.botfile)
		@_bfd=open($coh.botfile W)
		for (@sb=0, sb<numitems(botlist), @sb++) {
			@write($_bfd $getitem(botlist $sb))}
		@close($_bfd)
		ce the botlist has been saved in $coh.botfile\.}{
		ce the botlist is empty.}}
                          
### ctcp handler
on ^ctcp "% % DCC SEND" #
on ^ctcp "% % CDCC SEND *" #
on ^ctcp "% % PAGE *" {
	${coh.pager==[on]&&[$[1]1!=[#])?{ce ${cyn}you have been paged by ${hcyn}$0 ${hblk}[${hblu}$userhost()${hblk}]  [${hwht}$3-${hblk}];notice $0 Your request has been logged.}:[]}}

### misc & window hooks
on ^window "? % BitchX: *" ce $3-
on ^window "? BitchX: *" ce $2-
on ^window "? % *Auto-getting file % from %" #
on ^window "? % You have been marked as being*" #
on ^window "? % You are no longer marked as being*" #
on ^402 * {
	echo $fparse(format_nonick ! $1 ! $2-)
	echo $fparse(format_whois_footer !)}
on -exit * {
	${[$coh.autocdccsave]==[on]?[cdcc save]:[]}}

### join error handlers
on ^473 * {	
	^on ^329 * {
		^on ^329 -*}
	^on ^raw_irc "% 324 *" {
		ce ${cyn}$3${mag}/${cl}invite only channel. ${mag}\(${cl}$4${mag}\)${cl}
		^on ^raw_irc -"% 324 *"}
	mode $strip([] $1)}
on ^474 "*" {
	@bchan=[$1]
	^on ^raw_irc "% 367 *" {
		${[$N!$_uhost]=~[$4]?[ce ${cyn}$bchan${mag}/${cl}ban setby $before(! $5)${hblk}!${cl}$after(! $5) ${mag}\(${cl}$4${mag}\)${cl}):[]}}
	^on ^raw_irc "* 368 *" {      
		^on ^raw_irc -"% 367 *" 
		^on ^raw_irc -"* 368 *"}
	mode $bchan +b}
on ^475 * {	
	^on ^329 * {
		^on ^329 -*}
	^on ^raw_irc "% 324 *" {
		ce ${cyn}$3${mag}/${cl}bad channel key. ${mag}\(${cl}$4${mag}\)${cl}
		^on ^raw_irc -"% 324 *"}
	mode $strip([] $1)}

### new email handler
on ^mail * {
	ce ${cyn}new email!${cl} from${hblk}:${cl} $pipe(frm|tail -1)}
 
### misc aliases nobody should be without
alias coffers {
	fe ($onchannel(${[$0]?[$0]:[$C]})) nigzor {${[$nigzor]!=[$N]?[^ctcp $nigzor xdcc list]:[]}}}
alias friend {
	${[$0]?[adduser -ppp $0 ${[$1]?[$1]:[*]} friend]:[_usage friend]}}
alias shit {
	${[$0]?[addshit $0 * 3 ${[$1]?[$1-]:[shitlisted, $strftime($time() %X)]}:[_usage shit]}}
alias memo {
	${[$0]?{@memfl=open($coh.memofile W);@write($memfl $datetime($time()) ${CL}$*);@close($memfl);ce memo saved in $coh.memofile\.}:fexist($coh.memofile)==1?{more $coh.memofile;input_char "erase this memofile\? (y|n)\: " if ([$0]==[y]) {@unlink($coh.memofile);ce $coh.memofile has been removed.}}:[ce the memo file is empty.]}}

alias opers trace -o $*

### add color flags to say, topic, and msg
@scolors=[${BLU} ${BLK} ${RED} ${YEL} ${MAG} ${CYN} ${WHT} ${GRN} ${HBLU} ${HBLK} ${HRED} ${HYEL} ${HMAG} ${HCYN} ${HWHT} ${HGRN} ${BBLU} ${BBLK} ${BRED} ${BYEL} ${BMAG} ${BCYN} ${BWHT} ${BGRN} ${CL} ${CL1} ${BLNK}]
@foregrounds=[${BLK} ${WHT} ${RED} ${CYN} ${MAG} ${GRN} ${BLU} ${YEL} ${HBLK} ${HWHT} ${HRED} ${HCYN} ${HMAG} ${HGRN} ${HBLU} ${HYEL}]
@backgrounds=[${BBLK} ${BWHT} ${BRED} ${BCYN} ${BMAG} ${BGRN} ${BBLU} ${BYEL}]
@bluefade=[${BLU} ${CYN} ${HBLU} ${HCYN} ${HWHT}]
@redfade=[${HBLK} ${MAG} ${HMAG} ${HYEL} ${HWHT}]
@greenfade=[${GRN} ${HGRN} ${HYEL} ${HCYN} ${HWHT}]
@crazyfade=[$bluefade $redfade $greenfade]
alias rancols {
	fec ($*) addcolor {
		@function_return#=[$word($rand($#foregrounds) $foregrounds)$word($rand($#backgrounds)$backgrounds)$addcolor]}}
alias rfade {
	fe ($*) colorword {
		@colorindex=0
		fec ($colorword) colorthis {
			@function_return#=[$word($colorindex $redfade)$colorthis]
			@colorindex++}
		@function_return#=[ ]}}
alias bfade {
	fe ($*) colorword {
		@colorindex=0
		fec ($colorword) colorthis {
			@function_return#=[$word($colorindex $bluefade)$colorthis]
			@colorindex++}
		@function_return#=[ ]}}
alias gfade {
	fe ($*) colorword {
		@colorindex=0
		fec ($colorword) colorthis {
			@function_return#=[$word($colorindex $greenfade)$colorthis]
			@colorindex++}
		@function_return#=[ ]}}
alias cfade {
	^local _cnum 0
	fe ($*) cword {
		@cindex=0
		fec ($cword) cchar {
			@function_return#=[$word($cindex ${_cnum==0?bluefade:_cnum==1?redfade:greenfade})$cchar]
			@cindex++}
		@function_return#=[ ]
		@_cnum++
		@_cnum=_cnum<3?_cnum:0}}
alias lparse {
	@function_return=[$0]==[-c]?cfade($1-):[$0]==[-m]?rfade($1-):[$0]==[-b]?bfade($1-):[$0]==[-r]?rancols($1-):[$0]==[-g]?gfade($1-):[$0]==[-e]?eleet($1-):[$0]==[-k]?cool($1-):[$0]==[-a]?annoy($1-):[$0]==[-p]?cparse("$1-"):[$1-]}
alias topic {
	@lts=[$[1]0]==[#]?[$1-]:[$*]
	//topic ${[$[1]0]==[#]?[$0]:C} ${word(0 $lts)=~[-?]?lparse($word(0 $lts) $restw(1 $lts)):lts}}
alias msg {
	//msg $0 ${[$1]=~[-?]?lparse($1 $2-):[$1-]}}
alias say {
	//say ${[$0]=~[-?]?lparse($0 $1-):[$*]}}

### new kicks
on #-public 23 * {
	${repeatlimit?{${[$2-]==_r.txt&&[$0]==_r.nk&!ischanop($0 $1)&&_r.time-time()<=repeatlimit?[kick $1 $0 dont repeat repeat yourself]:{@_r.txt=[$2-];@_r.time=time();@_r.nk=[$0]}}}:[]}
	${punclimit?{${numchars(! $right(8 $2-))>=punclimit||numchars(\? $right(8 $2-))>=punclimit&&ischanop($N $1)&!ischanop($0 $1)?[kick $1 $0 punctuation limit exceeded]:[]}}:[]}
	${coh.mckick=[on]?{${[$2-]=~[**]&&ischanop($N $1)&!ischanop($0 $1)?[kick $1 $0 mirc_colors!=good]:[]}}:[]}}	
on #-mode_stripped 23 * {
	${[$2]==[+b]&&[$N!$_uhost]=~[$4]&&ischanop($N $1)?{mode $1 -ob $0 $3;kick $1 $0 don't ban $3 dickhead}:[]}}
alias boot {
	${[$0]?{fe ($comchan($0)) curchan {${ischanop($N $curchan)?[wait -CMD kick $curchan $0 ${[$1]?[$1-]:getreason($0)}:[]}}}:[_usage boot]}}
alias dbk {
	${[$0]?{ban $C $0;kick $C $0 ${[$1]?[$1-]:getreason($0)};do timer 5 ub $C $0}:[_usage dbk]}}
alias randkick {
	kick $C $word($rand($numonchannel($C)) $filter($N $onchannel($C))) -randomkick-}
alias vkick {
	switch ( $0 ) {
		( -a ) {
			if ([$1]) {
				if (!pattern($1 $_lame.versions)) {
					@push(_lame.versions $1)
					ce added [$1] to the /vkick list}
					{ce [$1] is already in the /vkick list}}}
		( -d ) {
			if ([$1]) {
				if (pattern($1 $_lame.versions)) {
					@_lame.versions=remw($1 $_lame.versions)
				 	ce removed [$1] from the /vkick list}
				 	{ce [$1] is not in the /vkick list}}}
		( -l ) {
			ce /vkick patterns: [$_lame.versions]}
		( * ) {
			@_vkc=[$0]?[$0]:[$C]
			if (ischanop($N $_vkc)) {
				^on ^ctcp_reply "% VERSION *" {
					fe ($_lame.versions) _lv {
						${tolower($2-)=~_lv?[kick $_vkc $0 coh/vkick\: $2-]:[]}}}
				ce commencing vkick for patterns [$_lame.versions]
				do ^ver $_vkc
				^timer 15 ^on ^ctcp_reply -"% VERSION *"}
				{ce you need to be a chanop on $_vkc to use /vkick}}}}

### annoying flash alias ;>
alias flash {
	if ([$0]) {
		^msg $0 $flash(0) $flash(1) $flash(2) $flash(3) $flash(4) $flash(5)
		ce sent vt100 flash to $0}}

### 4v alias ;>
alias 4v {
	${[$0]?[mode $C +vvvv $0 $0 $0 $0]:[_usage 4v]}}

### event handler
on #-send_msg 23 * {
  ^hook rel_add relsm send_msg $Z $0 $0 ! $1-}
on #-send_notice 23 * {
  ^hook rel_add relsn send_notice $Z $0 $0 ! $1-}
on #-ctcp_reply 23 * {
  ^hook rel_add relcr ctcp_reply $Z $0 $0 ! $1-}
on #-wallop 23 * {
  ^hook rel_add relw wall $Z $0 $*}
on #-msg 23 * {
	^hook dlog msg $0 $userhost() $1-
	^hook rel_add relm msg $Z $0 $0 $userhost() $1-}
on #-invite 23 * {
  ^hook dlog invite $*
  ^hook rel_add reli invite $Z $0 $*}
on #-dcc_connect 23 * {
  ^hook dlog dcc_connect $0 $1 ! $2-}
on #-ctcp 23 * {
	${fixcnick($1)==[you]?[^hook dlog ctcp $0 $userhost() $fixcnick($1) $fixctcp($2-)]:[]} 
	^hook rel_add relc ctcp $Z $0 $0 $userhost() $fixcnick($1) $fixctcp($2-)}
on #-notice 23 * {^hook dlog notice $0 $userhost() $1-
  ^hook rel_add reln notice $Z $0 $0 $userhost() $1-}
on #-dcc_error 23 * {
  ^hook dlog dcc_error $*}
on #-connect 23 * {
  ^hook dlog connect $*}
on #-disconnect 23 * {
  ^hook dlog disconnect $*}
on #-kick 23 * {
	${[$0]==[$N]?[^hook dlog kick_user $1 $2 ! $3-]:[]}}
on #-ar_public 23 * {
  ^hook dlog public_other_ar $*}
on #-ar_public_other 23 * {
  ^hook dlog public_other_ar $*}
on #-join 23 * {
  ${[$0]==N?[^hook dlog join $0 $userhost() $1]:[]}}
on #-dcc_chat 23 * {
  ^hook dlog dcc_chat $0 ! $1-
  ^hook rel_add reld dcc_chat $Z $0 $0 ! $1-}
on #-send_dcc_chat 23 * {
  ^hook rel_add relsd send_dcc_chat $Z $0 $*}
on #-mode_stripped 23 * {
	${pattern(*$N* $2-)?[^hook dlog mode $0 ! $1-]:[]}}
alias ctcp {
  if ([$1]) {
    ^hook rel_add relsc send_ctcp $Z $0 $*
  //ctcp $*}}

### fake msg relays
alias frelm {
	${[$1]?{@fakemsg=[$1-];^local _aa $_userhost($0);${_aa!=[<UNKNOWN>@<UNKNOWN>]?[send $_cpublic($fparse(format_msg ! $0 $_aa $fakemsg)):[ce $0 is not on irc.]}}:[_usage frelm]}}
alias freln {
	${[$1]?{@fakemsg=[$1-];^local _aa $_userhost($0);${_aa!=[<UNKNOWN>@<UNKNOWN>]?[send $_cpublic($fparse(format_notice ! $0 $_aa $fakemsg))]:[ce is not on irc.]}}:[_usage freln]}}
alias rctcp {
	${[$1]?{^quote NOTICE $0 :$toupper($1) $2-;ce faked CTCP $toupper($1): $2- sent to $0}:[_usage rctcp]}}

### new relay command
alias relay {
	@prel=[$*]
	@relkinds=[m sm c cr n sn i w sc d sd]
	@relcmds=[relm relsm relc relcr reln relsn reli relw relsc reld relsd]
	if (match($word(0 $prel) m sm c cr n sn i w sc d sd)) {
		@reltype=copattern($shift(prel) relkinds relcmds)
		if (numitems($reltype)) {
			@relflag=shift(prel)
			switch ($relflag) {
			(-t) {
				@relnum=isdigit($[1]prel)?shift(prel):0
				@_rz=numitems($reltype)-relnum
 				@_this.rel=getitem($reltype ${_rz-1})
				${_this.rel?[do topic ${[$[1]prel]==[#]?prel:C} $$stripansicodes($fparse(format_$word(0 $_this.rel) ! $restw(3 $_this.rel)))]:[ce $reltype message $relnum does not exist.]}}
			(-k) {
				@relnum=isdigit($[1]prel)?shift(prel):0
				@_rz=numitems($reltype)-relnum
				@_this.rel=getitem($reltype ${_rz-1})
				${_this.rel?[do boot ${prel?prel:word(2 $_this.rel)} $$stripansicodes($fparse(format_$word(0 $_this.rel) ! $restw(3 $_this.rel)))]:[ce $reltype message $relnum does not exist..}}      
			(-l) {
				for (@pz=0, pz < numitems($reltype), @pz++) {
					@relnumz=numitems($reltype)-pz
					@_this.rel=getitem($reltype ${relnumz-1})
					echo ${hblk}[${hblu}$pz${hblk}@${blu}$word(1 $_this.rel)${hblk}]: ${cl}$fparse(format_$word(0 $_this.rel) ! $restw(3 $_this.rel))}}
			(*) {
				@relnum=isdigit($relflag)?relflag:0
				@relnc=prel?prel:C
				if (relnum<0) {
					@relnum=relnum*-1>numitems($reltype)?numitems($reltype)*-1:relnum
					for (@rx=numitems($reltype)+relnum, rx<=numitems($reltype)-1, @rx++) {
						@_this.rel=getitem($reltype $rx)
						msg $relnc $_cpublic($fparse(format_$word(0 $_this.rel) ! $restw(3 $_this.rel)))}}{
					@_rn=numitems($reltype)-relnum
					@_this.rel=getitem($reltype ${_rn-1})
					${_this.rel?[msg $relnc $_cpublic($fparse(format_$word(0 $_this.rel) ! $restw(3 $_this.rel)))]:[ce $reltype message $relnum does not exist.]}}}}}{
		ce the $reltype list is currently empty.}}{
	_usage relay}}

alias reld relay d $*
alias ld relay d -l
alias reldt relay d -t $*
alias reldk relay d -k $*
alias relsd relay sd $*
alias lsd relay sd -l
alias relsdt relay d -t $*
alias relsdk relay d -k $*
alias relsc relay sc $*
alias lsc relay sc -l
alias relsct relay sc -t $*
alias relsck relay sc -k $*
alias relm relay m $*
alias lm relay m -l
alias relmt relay m -t $*
alias relmk relay m -k $*
alias relsm relay sm $*
alias lsm relay sm -l
alias relmst relay sm -t $*
alias relmsk relay sm -k $*
alias relc relay c $*
alias lc relay c -l
alias relct relay c -t $*
alias relck relay c -k $*
alias relcr relay cr $*
alias lcr relay cr -l
alias relcrt relay cr -t $*
alias relcrk relay cr -k $*
alias reln relay n $*
alias ln relay n -l
alias relnt relay n -t $*
alias relnk relay n -k $*
alias relsn relay sn $*
alias lsn relay sn -l
alias relsnt relay sn -t $*
alias relsnk relay sn -k $*
alias reli relay i $*
alias li relay i -l
alias relit relay i -t $*
alias relik relay i -k $*
alias relw relay w $*
alias lw relay w -l
alias relwt relay w -t $*
alias relwk relay w -k $*
 
on ^hook "rel_add*" {
	${numitems($1)==[10]?[@delitem($1 $0)]:[]}
	@setitem($1 $numitems($1) $2-)}

### new message logger
on ^hook "dlog % *" {
	${A&&coh.msglog==[on]?{@dlf=open($coh.msglogfile W T);${[$1]==[closelog]||[$1]==[startlog]?[@write($dlf $datetime($time())${blu}\:${hblk} $2-)]:[@write($dlf $datetime($time())${BLU}\:${HBLK} $fparse(format_$1 $1-))]};@close($dlf);@_a.msgs++}:[]}}
alias readlog more $coh.msglogfile
alias remlog {
	@_a.msgs=0
	@unlink($coh.msglogfile)
	ce $coh.msglogfile has been removed.}

### make irc windows like screen. i originally wrote this for deep, but
### this is a complete rewrite cuz deep's was lame.
on #-join_me 23 * {
	^window add $0
	^on ^329 * {
		@_cctime=[$2]
		^on ^329 -*}}
on #-leave 23 * {
	${[$0]==N?[^window remove $1]:[]}}
alias _switch_channels {
	@thischan=C
	${[$#winnicklist()]>1?{until (match($C $winnicklist())&&C!=thischan) {parsekey switch_channels}}:[]}}
@_w.tab=_w.tab?_w.tab:[1\:$time()]

bind ^X parse_command _switch_channels
bind ^W meta5_character
bind meta5-p parse_command _w.previous
bind meta5-c parse_command _w.create
bind meta5-h parse_command dhelp window
bind meta5-k parse_command _w.kill
bind meta5-n parse_command _w.next
bind meta5-space parse_command _w.next
bind meta5-u parse_command _w.uptime
bind meta5-w parse_command _w.info
bind meta5-? parse_command dhelp window
bind meta5-1 parse_command _w.swap 1
bind meta5-2 parse_command _w.swap 2
bind meta5-3 parse_command _w.swap 3
bind meta5-4 parse_command _w.swap 4
bind meta5-5 parse_command _w.swap 5
bind meta5-6 parse_command _w.swap 6
bind meta5-7 parse_command _w.swap 7
bind meta5-8 parse_command _w.swap 8
bind meta5-9 parse_command _w.swap 9

alias _w.create {
	^window new hide swap last double on split $coh.topicbar
	@push(_w.tab $winnum():$time())
	_se created new window $winnum() at $_fixmtime($time()).}
alias _w.swap {
	${[$1]!=winnum()?{${pattern($0\?\?\?\?\?\?\?\?\?\? $_w.tab)?{^window swap $0;_se window $winnum() activated.}:[_w.info]}}:[_se this is window $winnum().]}}
alias _w.next {
	${#_w.tab>1?{@_w.tmp=match($winnum()\?\?\?\?\?\?\?\?\?\? $_w.tab)<#_w.tab?word($match($winnum()\?\?\?\?\?\?\?\?\?\? $_w.tab) $_w.tab):word(0 $_w.tab);_w.swap $before(\: $_w.tmp)}:[_se no other windows exist.]}}
alias _w.previous {
	${#_w.tab>1?{@_w.tmp=match($winnum()\?\?\?\?\?\?\?\?\?\? $_w.tab)>1?word(${match($winnum()\?\?\?\?\?\?\?\?\?\? $_w.tab)-2} $_w.tab):word(${#_w.tab-1} $_w.tab);_w.swap $before(\: $_w.tmp)}:[_se no other windows exist.]}}
alias _w.uptime {
	_se window $winnum() created at $_fixmtime($pattern($winnum:????????? $_w.tab)).}
alias _w.info {
	^local _w.ttt
	fe ($_w.tab) _w.tt {
		@push(_w.ttt $[1]_w.tt)}
	_se current active windows: $_w.ttt}
alias _w.kill {
	${winnum()!=1?{${[$winnicklist()]?[^part $sar(g/ /,/$winnicklist())]:};@_w.tmp=winnum();@_w.tab=notw(${match($winnum()\?\?\?\?\?\?\?\?\?\? $_w.tab)-1} $_w.tab);^window kill swap 1;${[$ov]==[on]?[^window show oper_view size 5 refnum 1]:};_se window $_w.tmp killed  -  window $winnum() activated.}:[_se you can't kill the main window.]}}

### script config
alias config {
	echo    ${hblk}[$ccenter(69 ${blu}.  ${hblu}..  ${hcyn}... ${hwht}current coherent configuration${hcyn} ...${hblu}  ..${blu}  .)${hblk}]${cl}
	echo $_cdots(repeat kick limit|$repeatlimit)
	echo $_cdots(punctuation kick limit|$punclimit)
	echo $_cdots(mirc color kick|$coh.mckick)
	echo $_cdots(awaymsg|$coh.awaymsg)
	echo $_cdots(backmsg|$coh.backmsg)
	echo $_cdots(autoaway time|$coh.autoawaymins\m)
	echo $_cdots(use color for relays|$coh.cpublics)
	echo $_cdots(pager|$coh.pager)
	echo $_cdots(topicbar|$coh.topicbar)
	echo $_cdots(autosave cdcc packs on quit|$coh.autocdccsave)
	echo $_cdots(cycle empty channels for ops|$coh.recycle)
	echo $_cdots(improved url grabber|$coh.urlgrab)
	echo $_cdots(email address|$coh.email)
	input_char "change these settings? (y|n): " if ([$0]==[y]) {
		_se press enter to keep the current value
		input "limit for repeat kicking (0 disables): " if ([$0]!=[]) {@repeatlimit=[$0]}
		input "limit for punctuation kick (0 disables): " if ([$0]!=[]) {@punclimit=[$0]}
		input_char "use mirc color kick? (y|n): " if ([$0]==[y]) {@coh.mckick=[on]}{if ([$0]==[n]) {@coh.mckick=[off]}}
		input "default awaymsg: " if ([$0]) {@coh.awaymsg=[$*]}
		input "default backmsg: " if ([$0]) {@coh.backmsg=[$*]}
		input "time for auto setaway: " if ([$0]!=[]) {@coh.autoawaymins=[$0]}
		input_char "use color for relays? (y|n): " if ([$0]==[y]) {@coh.cpublics=[on]}{if ([$0]==[n]) {@coh.cpublics=[off]}}
		input_char "use pager? (y|n): " if ([$0]==[y]) {@coh.pager=[on]}{if ([$0]==[n]) {@coh.pager=[off]}}
		input_char "use topicbar? (y|n): " if ([$0]==[y]) {@coh.topicbar=[on];^window split on}{if ([$0]==[n]) {@coh.topicbar=[off];^window split off}}
		input_char "autosave cdcc packs on quit? (y|n): " if ([$0]==[y]) {@coh.autocdccsave=[on]}{if ([$0]==[n]) {@coh.autocdccsave=[off]}}
		input_char "recycle empty channels for ops? (y|n): " if ([$0]==[y]) {@coh.recycle=[on]}{if ([$0]==[n]) {@coh.recycle=[off]}}
		input_char "use improved url grabber? (y|n): " if ([$0]==[y]) {@coh.urlgrab=[on]}{if ([$0]==[n]) {@coh.urlgrab=[off]}}
		input "your email address? " if ([$0]) {@coh.email=[$*]}
		input_char "save these settings? (y|n): " if ([$0]==[y]) {
			@unlink($coh.savefile)
			@_cs=open($coh.savefile W)
			@write($_cs ce loaded savefile created $strftime($time() %x)@$_fixmtime($time()))
			@write($_cs @repeatlimit=[$repeatlimit])
			@write($_cs @punclimit=[$punclimit])
			@write($_cs @coh.mckick=[$coh.mckick])
			@write($_cs @coh.awaymsg=[$coh.awaymsg])
			@write($_cs @coh.backmsg=[$coh.backmsg])
			@write($_cs @coh.autoawaymins=[$coh.autoawaymins])
			@write($_cs @coh.cpublics=[$coh.cpublics])
			@write($_cs @coh.pager=[$coh.pager])
			@write($_cs @coh.topicbar=[$coh.topicbar])
			@write($_cs @coh.email=[$coh.email])
			@write($_cs @coh.autocdccsave=[$coh.autocdccsave])
			@write($_cs @coh.recycle=[$coh.recycle])
			@write($_cs @coh.urlgrab=[$coh.urlgrab])
			@write($_cs @_lame.versions=[$_lame.versions])
			@close($_cs)
			ce configuration saved in $coh.savefile}}}

### fkey setup
alias fkeys {
	echo    ${hblk}[$ccenter(69 ${hblk}.  ${mag}.. ${cl}coherent functionkey settings${mag} ..  ${hblk}.)${hblk}]
	for (@xx=1, xx<9, @xx++) {
		echo $_fdots($xx $fkey[$xx])}
	input_char "change these settings? (y|n): " if ([$0]==[y]) {
		input "functionkey 1: " if ([$0]) {@fkey[1]=[$0]}
		input "functionkey 2: " if ([$0]) {@fkey[2]=[$0]}
		input "functionkey 3: " if ([$0]) {@fkey[3]=[$0]}
		input "functionkey 4: " if ([$0]) {@fkey[4]=[$0]}
		input "functionkey 5: " if ([$0]) {@fkey[5]=[$0]}
		input "functionkey 6: " if ([$0]) {@fkey[6]=[$0]}
		input "functionkey 7: " if ([$0]) {@fkey[7]=[$0]}
		input "functionkey 8: " if ([$0]) {@fkey[8]=[$0]}
		input_char "save these settings? (y|n): " if ([$0]==[y]) {
			@unlink($coh.fkeys)
			@_fd=open($coh.fkeys W)
			@write($_fd ce loaded fkey settings.)
			for (@xx=1, xx<9, @xx++) {
				@write($_fd @fkey[$xx]=[$fkey[$xx]])}
			@close($_fd)
			ce fkey settings saved in $coh.fkeys\.}}}

### help aliases!@!
alias _usage {
	ce ${cl}USAGE${cyn}:${cl} $getitem(cu_$0 0)}
alias chelp {
	@_ctop=[$0]?numitems(ch_$0)?[$0]:[index]:[index]
		ce help on topic ${cyn}$_ctop${cl}..
		for (@xq=0, xq<numitems(ch_$_ctop), @xq++) {ce $getitem(ch_$_ctop $xq)}}

### load all saved settings/files
do ${fexist($coh.savefile)==1?[load $coh.savefile]:[ce coherent has not been configured. use /config to change settings.]}
do ${fexist($coh.fkeys)==1?[load $coh.fkeys]:[]}
do ${fexist($coh.ilinesfile)==1?{@_c.il=open($coh.ilinesfile R);@_pp=word(0 $read($_c.il));@delarray(ilines);while (!eof($_c.il)) {@turd=read($_c.il);${turd?[@setitem(ilines $numitems(ilines) $turd)]:[]}};@close(_$c.il);ce loaded i\:line list created $strftime($_pp %x)@$_fixmtime($_pp).}:[]}
do ${fexist($coh.botfile==1)?{@delarray(botlist);@_bfd=open($coh.botfile R);while (!eof($_bfd)) {@pewp=read($_bfd);${pewp?[@setitem(botlist $numitems(botlist) $pewp)]:[]}};@close($_bfd);ce loaded $numitems(botlist) botlist entries.}:[]}
do ${coh.autocdccsave==[on]?[cdcc load]:[]}
do ${fexist($coh.helpfile)==1?{@_hfd=open($coh.helpfile R);while (eof($_hfd)==0) {@_hr=read($_hfd);${[$[1]_hr]==[%]?{@_htype=after(% $_hr);@delarray(cu_$_htype);@delarray(ch_$_htype)}:[$[1]_hr]==[&]?[@setitem(cu_$_htype $numitems(cu_$_htype) $after(& $_hr))]:[$[1]_hr]==[.]?[@setitem(ch_$_htype $numitems(ch_$_htype) $after(. $_hr))]:[]}};@close($_hfd);ce loaded coherent helpsystem.}:[]}

### all finished!
do ${coh.urlgrab==[on]?[^set http_grab off]:[]}
do ce $_coherent.ver successfully loaded. /chelp for help!
  

#sjk97
