aboutsummaryrefslogtreecommitdiff
path: root/clients/shellclients/anoirc.sh
blob: 4062e1c4f98194820326cdbf8868d5f27391821a (plain) (blame)
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
#!/bin/bash

PATH=''

term_height=0
term_width=0
term_scroll_height=0
status_line_row=0
irc_host=''
irc_channel=''
irc_nick=''
irc_gecos=''


function scroll_bottom() {
	printf '\e[999B'
}

#figure out terminal height, NOTE: moves cursor to bottom of terminal
function term_height() {
	printf '\e[999B\e[6n'
	read -s -r -d'['
	read -s -r -d';' term_height
	read -s -r -d'R' term_width
	printf '\e[999D'
}

# Set the area the terminal is allowed to scroll in
function scroll_helper() {
	term_scroll_height=$((term_height-2))
	status_line_row=$((term_height-1))
    printf '\e[0;%sr' "$term_scroll_height"
}

function bottom_line() {
    printf '\e[%s;0f' "$term_height"
}

function paste_data() {
	printf '\e7\e[%s;0f\n' "$term_scroll_height"
	printf ' %s' "$1"
	printf '\e8'
}

function status_line() {
	printf '\e7\e[%s;0f\e[2K' "$status_line_row"
	printf '\e[4;44mSTATUS: %s in %s  @ %s\e[0m' "$irc_nick" "$irc_channel" "$irc_host"
	printf '\e8'
}

function init_screen() {
	printf '\e[r'
	term_height
	scroll_helper
	bottom_line
}

function net_fd_helper() {
	local buff=''
	
	#close 44 if open, then open read/write
	exec 44>&-
	exec 44<>"$1"
	
	#delay slightly to improve chance of success
	#read -s -r -t1 -u 44

	printf 'NICK %s\r\n' "$irc_nick" >&44
    printf 'USER %s %s %s :%s\r\n' "$irc_nick" "$HOSTNAME" "$USER" "$irc_gecos" >&44
	
	while true
	do
		while IFS='' read -s -r -t1 -u 44
		do
			case "$REPLY" in
			( :irc* ) 
				paste_data "$REPLY"
				;;
			( PING* )
				paste_data "$REPLY"
				buff="${REPLY##*:}"
				paste_data "PONG :$buff"
				printf 'PONG :%s\r\n' "$buff" >&44
				;;
			( * )
				buff="${REPLY%%\!*}"
				REPLY="${REPLY#*:*:}"
				buff="<${buff/:/}> $REPLY"
				paste_data "$buff"
				;;
			esac
		done
	

		while IFS='' read -r -t1
		do 
			status_line
			echo -en '\e[2K\r> '
			case "$REPLY" in
			( :shell* )
				buff="${REPLY#*\ }"
				paste_data "$buff"
				eval "$buff"
				;;
			( :reset )
				init_screen
				break
				;;
			( /join* )
				irc_channel="${REPLY##*\ }"
				printf '%s\r\n' "${REPLY/\//}" >&44
				;;
			( /quit* )
                printf 'QUIT :%s\r\n' "${REPLY#*\ }" >&44
				exec 44>&-
				exit 0
				;;
			( /* )
				printf '%s\r\n' "${REPLY/\//}" >&44
				;;
			( "" )
				break
				;;
			( * )
                printf 'PRIVMSG %s :%s\r\n' "$irc_channel" "$REPLY" >&44
				;;
			esac
			
			buff="<$irc_nick> ${REPLY}"
			paste_data "$buff"
		done
	done
}

function main() {
	local irc_fd=''
	scroll_bottom
	read -p 'IRC Server: ' -e -r irc_host
	read -p 'IRC Nickname: ' -e -r irc_nick
	read -p 'IRC Realname: ' -e -r irc_gecos
	init_screen
	net_fd_helper "/dev/tcp/$irc_host/6667"
    printf '\x1bc'
}

main