summaryrefslogtreecommitdiff
blob: 5621702142b3b69567c616bfc740d6a98e8f478d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
if (fexist(~/.ircrc) == -1 && fexist(~/.epicrc == -1)) {
# set this if you want logging
# assign win_logdir logs

# uncomment this if you don't want time stamping
# timestamp off

# uncomment this if you want ctcp cloaking
# cloak on

# *** key bindings ***

bind ^[1 parse_command window swap 1
bind ^[2 parse_command window swap 2
bind ^[3 parse_command window swap 3
bind ^[4 parse_command window swap 4
bind ^[5 parse_command window swap 5
bind ^[6 parse_command window swap 6
bind ^[7 parse_command window swap 7
bind ^[8 parse_command window swap 8
bind ^[9 parse_command window swap 9
bind ^[0 parse_command window swap 10


# *** variable sets ***

set -continued_line
set dcc_auto_send_rejects off
set dcc_timeout 60
set dcc_sliding_window 1000
set auto_rejoin off
set log_rewrite $log_format($1-)
set mangle_logfiles all
set mirc_broken_dcc_resume on
set status_format 16,12%T [%R] %=%@%N%*%#%S%H%B%Q%A%C%+%I%O%M%F%L %D


# make sure that our logging directory exists

if (!(win_logdir == [])) {
	@mkdir($win_logdir)
}


# create status and msg windows, if necessary

if (winnam(status) == []) {
	window 1 name status level CRAP,PUBLIC,WALLS,WALLOPS,SNOTES,CTCP,USERLOG1,USERLOG2,USERLOG3,USERLOG4
	if (!(win_logdir == [])) {
		^window logfile $win_logdir/status.log log on
	}
	@ setitem(my_wins 0 blank_item)
	@ setitem(my_wins 1 status)
}

if (winnam(queries) == []) {
	window new name queries level msgs,actions,dcc hide
	if (!(win_logdir == [])) {
		^window queries logfile $win_logdir/msg.log log on
	}
	@ setitem(my_wins 2 queries)
}


# *** aliases ***

alias ban {
	if ([$0] == []) {
		xecho -b Usage: /ban <nickname>
		return
	}

	userhost $0 -cmd {
		@:usr_nick = [$0]
		@:usr_id = [$3]
		@:usr_host = [$4]
	}

	@:re = regcomp(^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\$)

	if (regexec($re $usr_host)) {
		@:usr_dom = count(. $usr_host) > 1 ? [*.$after(-2 . $usr_host)] : [*.$usr_host]
	} {
		@:usr_dom = [$before(-1 . $usr_host).*]
	}

	switch ($1) {
		(1) {
			//mode $C +b $usr_nick!*@*
		}
		(2) {
			//mode $C +b *!*@$usr_host
		}
		(3) {
			//mode $C +b *!*$usr_id@$usr_dom
		}
		(4) {
			//mode $C +b *!*@$usr_dom
		}
		(*) {
			ban $0 2
		}
	}
}

alias bans { if ( [$0] ) { mode $0 +b } { mode $C +b } }

alias chanserv { quote chanserv $* }

alias clearall { //clear -all }

alias client {
	ctcp $0 VERSION
}

alias cloak {
	switch ($0) {
		(on) {
			^on ctcp_request -"*"
			^on ^ctcp_request "*"
			^on -ctcp_request ^"% % ACTION *"
			^on -ctcp_request ^"% % DCC *"
			^on -ctcp_request ^"% % PING *"
			xecho -b -w 1 ctcp cloaking is now on
		}
		(all) {
			^on ctcp_request -"*"
			^on ^ctcp_request "*"
			^on -ctcp_request ^"% % ACTION *"
			^on -ctcp_request ^"% % DCC *"
			xecho -b -w 1 complete ctcp cloaking is now on
		}
		(off) {
			^on ctcp_request -"*"
		}
		(*) {
			xecho -b -w 1 you must specify on, off, or all
		}
			
	}
}

alias cs chanserv

alias ctcpreply { eval notice $0 $chr(1)$1-$chr(1)}

alias cycle {
	@:chan_name = C
	@:chan_key = key($C)

	leave *
	wait
	join $chan_name $chan_key
}

alias dccallow { quote dccallow $* }

alias dns {
	userhost $0 -cmd {
		echo Looking up $4 ...
		exec host $4
	}
}

alias dop {
	fe ($*) n1 n2 n3 n4 {
		mode $C -oooo $n1 $n2 $n3 $n4
	}
}

alias dvoice {
	fe ($*) n1 n2 n3 n4 {
		mode $C -vvvv $n1 $n2 $n3 $n4
	}
}

alias finger {
	ctcp $0 FINGER
}

alias format_idle_time {
	if ( [$0] >= 3600 ) {
		return ${[$0] / 3600} hours ${([$0] % 3600) / 60} mins ${[$0] % 60} sec
	}
	if ( [$0] >= 60 ) {
		return ${([$0] % 3600) / 60} mins ${[$0] % 60} sec
	}
	return ${[$0] % 60} seconds
}

alias j jnw

alias jnw {
	@:ref_num = matchitem(my_wins $0))

	if ( (ref_num < 3) || (winnum($ref_num) == -1) ) {
		window new hide
		window swap last
		if (!(win_logdir == [])) {
			^window logfile $win_logdir/$0.log log on
		}
		@ setitem(my_wins $winnum(0) $0)
	} {
		window swap $ref_num
	}
	//join $*
}

alias join jnw

alias k {
	kick $C $*
}

alias kb {
	ban $0
	k $*
}

alias log_format {
	if ( left(1 $0) == [\(] ) {
		return $strftime($time() (%m/%d/%Y %I:%M:%S %P)) $2-
	} else {
		return $strftime($time() (%m/%d/%Y %I:%M:%S %P)) $0-
	}
}

alias memoserv { quote memoserv $* }

alias mdop {
	fe ($remw($N $chops())) n1 n2 n3 n4 {
		mode $C -oooooo $n1 $n2 $n3 $n4
	}
}

alias mop {
	fe ($nochops()) n1 n2 n3 n4 {
		mode $C +oooooo $n1 $n2 $n3 $n4
	}
}

alias names scan

alias nickserv { quote nickserv $* }

alias ns nickserv

alias o onotice

alias onotice {
	quote notice @$C :ops: $*
	xecho -c $ts()-> ops: $*
}

alias op {
	fe ($*) n1 n2 n3 n4 {
		mode $C +oooo $n1 $n2 $n3 $n4
	}
}

alias operserv { quote operserv $* }

alias os operserv

alias q query

alias sc scan

alias scan {
	@:num = 0

	if (ischannel($0)) {
		@:chan = [$0]
	} {
		@:chan = C
	}


	xecho -w $winchan($chan)
	xecho -w $winchan($chan) -b 16Users on $chan:
	fe ($strip(?. $channel($chan))) n1 n2 n3 n4
	{
		xecho -w $winchan($chan) 14|  13$[17]n1 $[17]n2 $[17]n3 $[17]n414  |
		    if (n4) {
		      @:num += 4
		    } {
		      if (n3) {
			@:num += 3
		      } {
			if (n2) {
			  @:num += 2
			} {
			  @:num++
			}
		      }
		    }
	}
	xecho -w $winchan($chan) Total number of users on $chan: $num
	xecho -w $winchan($chan)
}

# sping from wd's anduril
alias sping (server) {
	if (server == []) @:server = builtin_expando(S)

	@a.server.sping #= [$server $utime() ]
	quote ping $server $server
	xecho -w 1 $ts()$banner Sent PING to $server
}

# tdiff3 from wd's anduril
alias tdiff3 {
	if (![$0]) {
		return 0s
	}

	@:y=:m=:w=0
	@:t=[$0]
	^local r

	@:y=floor(${t/31536000})
	@:t%=31536000
	@:m=floor(${t/2419200})
	@:t%=2419200
	@:w=floor(${t/604800})
	@:t%=604800
	@:l=tdiff2($t)

	if (y) {
		@:r=[${y}y ]
	}
	if (m) {
		@:r#=[${m}m ]
	}
	if (w) {
		@r#=[${w}w ]
	}
	return $r$l
}

alias timestamp {
	switch ($0) {
		(off) {
			@time_stamp = [off]
			xecho -b -w 1 Time stamping is now off
		}
		(on) {
			@time_stamp = [on]
			xecho -b -w 1 Time stamping is now on
		}
		(*) {
			if (time_stamp == [off]) {
				xecho -b -w 1 Time stamping is currently off
			} {
				xecho -b -w 1 Time stamping is currently on
			}
		}
	}
}

alias ts {
	if (time_stamp == [off]) {
		return
	} {
		return 10$strftime($time() (%I:%M:%S %P)) 15
	}
}

alias untopic {
	if (ischannel($0)) {
		@:chan = [$0]
	} {
		@:chan = C
	}

	quote TOPIC $chan :
}

alias ver client

alias voice {
	fe ($*) n1 n2 n3 n4 {
		mode $C +vvvv $n1 $n2 $n3 $n4
	}
}

alias wall {
	fe ($remw($N $onchannel()))  n1 {
		quote notice $n1 :wall: $*
	}
	xecho -c -> $C/wall: $*
}

alias wii { whois $0 $0 }

alias wk { window kill }

alias wops {
	fe ($remw($N $chops())) n1 {
		quote notice $n1 :ops: $*
		xecho -c -> $n1: ops: $*
	}
}

alias ws { window swap $0 }


# *** named on hooks ***

on ^action "*" {
	if (rmatch($1 #* &* +*)) {
		if ([$1] == winchan($winchan($1))) {
			xecho -w $winchan($1) $ts()* $0 $2-
		} {
			xecho -w $winchan($1) $ts()* $0/$1 $2-
		}
	} {
		xecho -w queries $ts()* $0 $2-
	}
}

on ^channel_nick "*" {
	xecho -w $winchan($0) $ts()$banner 16Nick: 10$1 15is now known as 8$2
}

on ^channel_signoff "*" {
	xecho -w $winchan($0) $ts()$banner 16Quit: 10$1 15has quit irc \($2-\)
}

on ^ctcp "*" {
	xecho -w 1 $ts()$banner CTCP $2 request from $0 to $1: $3-
}

on ^ctcp_reply "*" {
	xecho -w 1 $ts()$banner CTCP $2 reply from $0 to $1: $3-
}

on ^dcc_chat "*" {
	xecho -w queries $ts()=$0= $1-
}

on ^dcc_connect "*" {
	xecho -w queries $ts()$banner DCC $1 connection with $0[$2:$3] established
}

on ^dcc_lost "*" {
	xecho -w queries $ts()$banner DCC $1 connection with $0 stopped \($2-\)
}

on ^dcc_request "% CHAT *" {
	xecho -w 1 $ts()$banner DCC CHAT request received from $0!$userhost() [$3:$4]
}

on ^dcc_request "% SEND *" {
	xecho -w 1 $ts()$banner DCC SEND \($5 $6\) request received from $0!$userhost() [$3:$4]
}

on ^exec_exit * #

on ^general_notice "% @% *" {
	xecho -w $winchan($rest($1)) -- $ts()-13$0:$115- $2-
}

on ^general_notice "% #% *" {
	xecho -w $winchan($1) -- $ts()13$0:$115- $2-
}

on ^invite "*" {
	xecho -w 1 $ts()$banner 16Invite: 10$0 15invites you to join $1
}

on ^join "*" {
	xecho -w $winchan($1) $ts()$banner 16Join: 10$0 15\(10$215\) has joined $1
}

on ^kick "*" {
	if ( [$0] == N ) {
		xecho $ts()$banner 16Kick: 15You have been kicked from $2 by $1 \($3-\)
	} {
		xecho -w $winchan($2) $ts()$banner 16Kick: 10$0 15has been kicked from $2 by $1 \($3-\)
	}
}

on ^leave "*" {
	if ([$0] == N) {
		xecho -w $winchan($1) $ts()$banner You have left $1 \($3-\)
	 } {
		xecho -w $winchan($1) $ts()$banner 16Part: 10$0 15has left $1 \($3-\)
	}
}

on ^mode "*" {
	if ([$1] == N) {
		xecho -w 1 $ts()$banner 16Mode: 11$1 $2- by $0
	} {
		xecho -w $winchan($1) $ts()$banner 16Mode: 11$1 $2- by $0
	}
}

on ^msg "*" {
	xecho -w queries $ts()*$0* $1-
}

on ^msg "% @% %" {
	xecho -w $winchan($rest($1)) -- $ts()<-$0:$1\-> $2-
}


on ^notice "*" {
	xecho -w 1 -- $ts()-13$015- $1-
}

on ^notify_signoff "*" {
	xecho -w 1 $ts()$banner 16Signoff: 10$015 has left
}

on ^notify_signon "*" {
	xecho -w 1 $ts()$banner 16Signon: 10$0 15\(10$115\) is on
}

# pong on hook from wd's anduril
on ^pong "% % %" {
	@:ind = findw($0 $a.server.sping)
	if (ind == -1) {
		xecho -w 1 $ts()$banner $0\: PONG received from $1 \($2\)
	} else {
		@:ut = utime()
		@:usec = word(${ind + 2} $a.server.sping)
		@:sec = word(${ind + 1} $a.server.sping)
		@a.server.sping = remw($0 $remw($sec $remw($usec $a.server.sping)))
		@:sec = word(0 $ut) - sec
		@:usec = word(1 $ut) - usec

		if (usec < 0) {
			@:usec += 1000000 , sec--
		}
		@:sec = tdiff3($sec)
		if (pass(s $sec) == []) {
			@:sec = [${sec}0.$left(3 $usec)s]
		} else {
			@:sec = [$before(s $sec).$left(3 $usec)s]
		}
		xecho -w 1 $ts()$banner PONG received from $1 in $sec
	}
}

on ^public "*" {
	xecho -w $winchan($1) $ts()14<15$014> 15$2-
}

on ^public_msg "*" {
	xecho -w $winchan($1) $ts()14<15$0:$114> 15$2-
}

on ^public_other "*" {
	xecho -w $winchan($1) $ts()14<15$0:$114> 15$2-
}

on ^send_action "*" {
	if (rmatch($0 #* &* +*) ) {
		xecho -w $winchan($0) $ts()* $N $1-
	} {
		xecho -w queries $ts()*> $0: $N $1-
	}
}

on ^send_dcc_chat "*" {
	xecho -w queries $ts()=$0=> $1-
}

on ^send_msg "*" {
	xecho -w queries $ts()*$0*> $1-
}

on ^send_notice "*" {
	xecho -w 1 -- $ts()-> $0: $1-
}

on ^send_notice "#% *" {
	xecho -w $winchan($rest($0)) -- $ts()-> $0: $1-
}

on ^send_public "*" {
	if ([$0] == C) {
		xecho -w $winchan($0) $ts()14<15$N14> 15$1-
	} {
		xecho -w $winchan($0) $ts()14<15$N:$014> 15$1-
	}
}

on ^topic "*" {
	xecho -w $winchan($1) $ts()$banner 16Topic: 10$0 15has changed the topic on $1 to: $2-
}

on ^window "% % SWAP: Window % is not hidden!"

on ^window "% % SWAP: No such window: %"

on ^yell "*" {
	xecho -w 1 $ts()$banner $*
}

on ^yell ^"Defaulting % to CHANMODE type D"


# *** numeric hooks ***

on ^317 "*" {
	xecho -w 1 $banner $1 has been idle $format_idle_time($2), Signon $strftime($3 %c)
}

on ^331 "*" {
	xecho -w $winchan($1) $ts()$banner $1-
}

on ^332 "*" {
	xecho -w $winchan($1) $ts()$banner Topic for $1: $2-
}

on ^333 "*" {
	xecho -w $winchan($1) $ts()$banner Topic set by $2 $strftime($3 %c)
}

on ^353 "*" {
	xecho -w $winchan($2)
	xecho -w $winchan($2) -b 16Users on $2:
	fe ($3-) n1 n2 n3 n4 {
		xecho -w $winchan($2) 14|  13$[17]n1 $[17]n2 $[17]n3 $[17]n414  |
		if (n4) {
		  @total_nicks += 4
		} {
		  if (n3) {
		    @total_nicks += 3
		  } {
		    if (n2) {
		      @total_nicks += 2
		    } {
		      @total_nicks++
		    }
		  }
		}
	}
}

on ^366 "*" {
	xecho -w $winchan($1) Total number of users on $1: $total_nicks
	xecho -w $winchan($1)
	@total_nicks = 0
}

on ^367 "*" {
	xecho -w 1 $banner $1 $2 $3 $strftime($4 %c)
}
# tabkey.jm: a full-featured tab key module for epic
#
# written by nsx
#
#
# Here's the plan:
#
# First off, the word fragment (which may not be a fragment at all, but
# rather, a complete word, though we have no way of knowing this yet) that
# we're going to attempt to complete is determined.  If the current word
# pointed to by the input line cursor is quoted, then that word becomes the
# word fragment we'll attempt to complete.  If the word is not quoted, then all
# characters from the beginning of the word pointed to by the input line cursor
# to the cursor itself become the fragment we'll try to complete.
#
# We now analyze the input line to see if it matches certain patterns, and
# determine which method of completion we'll use based on which pattern it
# matches.
#
# If our input line matches "/msg <word><space>", we recall the item in our
# nickname history pointed to by our nickname history index.  We then type out
# the command "/msg <nick><space>", where <nick> is that nickname we recalled
# from the history.  If there are no nicknames in the history, nothing at all
# is done.
#
# If our input line matches "/dcc <word>", then dcc command completion is
# attempted.  This matches the word fragment against all known dcc commands.
#
# If our input line begins with "/dcc send <word1> <word2>" or "/load", we
# attempt file name completion.  This matches the word fragment against any
# local path name that exists.
#
# If our input line matches "/<word>" we attempt command completion.  This
# matches the word fragment against both internal commands and aliases.
#
# If our input line is empty, we attempt nickname history cycling as described
# above.
#
# If our input line matches none of the above, we analyze the first character
# of the fragment.  If it begins with a '#' or a '&', channel name completion
# is attempted.  This matches the word fragment against all channels we're
# currently on.
#
# If our fragment does not begin with '#' or '&', nickname completion is
# attempted.  This matches the word fragment against all nicknames in the
# current channel.  If no matches are found, we then match the word fragment
# against all nicknames in all other channels we're on and nicknames in our
# notify list that are presently online.
#
# If there was multiple matches for the word fragment, we replace the word
# fragment with the common prefix for these matches, if any.  We also display
# all of the possible matches on the screen, unless they're the same list of
# possible matches we displayed less than 5 seconds ago.  This way, we hope to
# avoid unnecessary repetition.
#
# If there was only one match for the word fragment, we replace the word
# fragment with that match.  We also append a space unless nickname completion
# took place, and the completed word is the only word in the input line.  Most
# of the time this occurs, I assume people are trying to address somebody.  I
# do this so that the optional colon can be appended after the nickname,
# without having to backspace.
#
# If we perform file name completion and the completed path name is a
# directory, we treat the completed word as if it's a partial completion (in
# that no space is appended), since we cannot be certain whether or not the
# path name is complete, unless it's a file.
#
# If the completed word (partial or complete) needs to be enclosed in
# quotation marks, we meet this provision.  If the completed word is partial,
# we place the cursor on the enclosing quotation mark, so that further
# completion can take place without having to manually reposition the cursor.
#
# For each outgoing msg or outgoing DCC CHAT message, we add the recipient
# to our nickname history.
#
# For each incoming msg or incoming DCC CHAT message, we add the sender to
# our nickname history.
#
# For each DCC CHAT connection that takes place, we add the other party to
# our nickname history.
#
# For each notify signon, we add the nickname that signed on to our nickname
# history.
#
# Each time a nickname is added to our nickname history, we first remove any
# prior occurrences of the nickname that may exist.  We then check to see if
# our nickname history is at its capacity (10 nicknames).  If it is, the oldest
# nickname in our history is discarded.  Next, we add the nickname to the
# history.  Lastly, we set our nickname index to point to the nickname that was
# just added.


# *** global variables ***


# nickname history array
@delarray(nick_hist)

# nickname history index
@nick_index = -1

# the matches from the last tab key press
@last_matches = []

# how far the cursor is from the end of the word fragment
@end_offset = 0

# whether or not the word fragment needs to be quoted (0 or 1)
@word_fragq = 0


bind ^I parse_command do_tabkey

alias do_tabkey {
	@:cur_pos = curpos()
	@:cur_word = word($indextoword($cur_pos $L ) $L )
	@:frag_start = wordtoindex($indextoword($cur_pos $L ) $L )

	if (index("$chr($jot(32 1))" $cur_word) == -1) {
		@word_fragq = 0
		@end_offset = 0
		@:word_frag = mid($frag_start ${cur_pos - frag_start} $L)
	} {
		@word_fragq = 1
		@end_offset = (frag_start + @cur_word - cur_pos) + 2
		@:word_frag = cur_word

		if (end_offset < 0) {
			return
		}
	}

	if (leftw(1 $L) == [/msg] && #L == 2 && mid(${@L - 1} 1 $L) == [ ]) {
		@msg_cycle()
		return
	}

	switch ($L) {
		(/dcc %) {
			@dcc_complete($word_frag)
		}
		(/dcc send % *)
		(/load *) {
			@file_complete($word_frag)
		}
		(/%) {
			@command_complete($right(${@L - 1} $L))
		}
		() {
			@msg_cycle()
		}
		(*) {
			@:firstc = left(1 $word_frag)
			if (firstc == [#] || firstc == [&]) {
				@chan_complete($word_frag)
			} {
				@nick_complete($word_frag)
			}
		}
	}
}

# replace_fragment (frag_len, matches...)
#
# This function replaces the word fragment.  If given a single match, the word
# fragment is replaced with that match.  If multiple matches are given, the
# common prefix for those matches replaces the word fragment.  If no matches
# are given, this function does nothing.
#
# It expects the fragment length (so it can know how many times to backspace)
# to be its first argument, and all matches for the word fragment to be the
# rest of its arguments.  This function also expects the global variable
# end_offset to be set, so that it can position the cursor after the word
# fragment, if needed, before backspacing to clear it.  This needs to be done,
# for example, when the tab key was pressed while the cursor was in the middle
# of a quoted word.

alias replace_fragment {
	@:matches = [$1-]
	@:match_pfx = prefix($1-)
	@:frag_len = word_fragq ? [$0] + 2 : [$0]

	if (#matches && #match_pfx) {
		repeat $end_offset parsekey forward_character
		repeat $frag_len parsekey backspace

		if (index("$chr($jot(32 1))" $match_pfx) != -1) {
			@:match_pfx = ["$match_pfx"]
			@word_fragq = 1
		}

		if (#matches > 1) {
			unless (matches == last_matches) {
				xecho -c Possible matches:
				xecho -c $matches
				xecho -c
				@last_matches = matches
				timer 5 @last_matches = []
			}

			xtype -l $match_pfx

			if (word_fragq) {
				parsekey backward_character
			}
		} {
			xtype -l $matches$chr(32)
		}
	}
}

alias chan_complete {
	@:word_frag = [$*]
	@:frag_matches = pattern("$word_frag*" $mychannels())

	@replace_fragment($@word_frag $frag_matches)
}

alias command_complete {
	@:word_frag = [$*]
	@:frag_matches = getcommands($word_frag*)

	@push(frag_matches $aliasctl(alias pmatch $word_frag*))

	@replace_fragment($@word_frag $frag_matches)
}

alias dcc_complete {
	@:word_frag = [$*]
	@:dcc_cmds = [chat close closeall get list raw rename resume send]
	@:frag_matches = pattern($word_frag% $dcc_cmds)

	@replace_fragment($@word_frag $frag_matches)
}

alias file_complete {
	@:word_frag = [$*]
	@:frag_matches = glob("$word_frag*")

	@replace_fragment($@word_frag $frag_matches)

	if (#frag_matches == 1) {
		@:stat_ret = stat($frag_matches)

		if (left(1 $word(2 $stat_ret)) == 4) {
			parsekey backspace
			if (word_fragq) {
				parsekey backward_character
			}
		}
	}
}

alias nick_complete {
	@:word_frag = [$*]
	@:frag_matches = pattern("$word_frag*" $onchannel())

	if (frag_matches == []) {
		^local nick_list

		fe ($remw($C $mychannels())) nick_name {
			@push(nick_list $onchannel($nick_name))
		}

		fe ($notify(on)) nick_name {
			@push(nick_list $nick_name)
		}

		@:nick_list = uniq($nick_list)
		@:frag_matches = pattern($word_frag% $nick_list)
	}

	@replace_fragment($@word_frag $frag_matches)

	if (#L == 1 && #frag_matches == 1) {
		parsekey backspace
	}
}

alias add_nickname {
	@:nick_items = numitems(nick_hist)
	@:prev_oc = finditem(nick_hist $0)

	if (prev_oc > -1) {
		@delitem(nick_hist $prev_oc)
		@:nick_items--
	}

	if (nick_items > 9) {
		@delitem(nick_hist 0)
		@:nick_items--
	}

	@setitem(nick_hist $nick_items $0)
	@nick_index = nick_items
}

alias msg_cycle {
	@:nick_items = numitems(nick_hist)

	if (!(nick_items)) {
		return
	}

	parsekey erase_line
	xtype -l /msg $getitem(nick_hist $nick_index)$chr(32)
	@nick_index = nick_index == 0 ? nick_items - 1 : nick_index - 1
}

on #^dcc_chat 10 "*" {
	@add_nickname(=$0)
}

on #^dcc_connect 10 "% CHAT *" {
	@add_nickname(=$0)
}

on #^msg 10 "*" {
	@add_nickname($0)
}

on #^notify_signon 10 "*" {
	@add_nickname($0)
}

on #^send_dcc_chat 10 "*" {
	@add_nickname(=$0)
}

on #^send_msg 10 "*" {
	@add_nickname($0)
}
}