#################################################################################
#
# Eggdrop Script for basic unix cmmmands from IRC
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 (YES v2 explicitly defined here.)
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# cmd_unix.tcl v 0.0.1
#	(tcl@kofee.org)
#################################################################################

#########################
#	Changelog	#
#########################
#
# 27/02/2006: 	ø script started, v 0.0.1
#		» ls	cat
#		» df	du
#		» uptime
#





#################################################################################
#	Would be nice not to change those "Copyrights" things ...		#
#################################################################################
set cmd_unix_script			"cmd_unix.tcl"
set cmd_unix_script_version		"v 0.0.1"
set cmd_unix_script_date		"27/02/2006"
set cmd_unix_script_author		"Kofee"
set cmd_unix_script_author_mail		"tcl@kofee.org"

#########################
#	Config Start	#
#########################

# The user account the eggdrop is running under
set user "kofee"
# PATH to eggdrop's directory
set eggpath "/home/$user/.eggdrop/$botnick"


#### Some answers included in the commands
## First general configuration.

# answer the bot will have in the channel in case of a deny access
set deny_cmd_unix "Go fuck yourself"
# log message in the partyline for a denied access
set deny_cmd_unix_pl "Access denied for"
# log message in the partyline for a granted access
set granted_cmd_unix_pl "Access granted for"
# How to call a directory in the script ? :)
set cmd_unix_directory "Directory"
# How to call a folder in the script ? :)
set cmd_unix_folder "Folder"



#########################
#	Config End	#
#################################################################################
#	BindZ		#
#########################

bind pub -	~ls		cmd_ls
bind pub -	~cat		cmd_cat
bind pub -	~df		cmd_df
bind pub -	~du		cmd_du
bind pub -	~uptime		cmd_uptime

## fun ;)
bind pub - 	~fortune	cmd_fortune

#################################################################################

proc cmd_fortune {nick host handle chan arg} {
	global user deny_cmd_unix deny_cmd_unix_pl granted_cmd_unix_pl

	if {![matchattr $handle +f $chan] && ![matchattr $handle +o $chan]} {
		putlog "\[cmd_fortune\] $deny_cmd_unix_pl $nick @ $chan"
		putserv "PRIVMSG $chan :$deny_cmd_unix $nick !"
		return 0
	}
	putlog "\[fortune_check_auth\] $granted_cmd_unix_pl $nick ."

	set fortune_opts ""

	if { [string match "-*" [lindex $arg 0]] == 1 } {
		putlog "OPTS to parse."
		if { [string match "-*a*" [lindex $arg 0]] == 1 } {
			set fortune_opts "-a $fortune_opts"
		}
		if { [string match "-*c*" [lindex $arg 0]] == 1 } {
			set fortune_opts "-c $fortune_opts"
		}
		if { [string match "-*e*" [lindex $arg 0]] == 1 } {
			set fortune_opts "-e $fortune_opts"
		}

		if { [string match "-*f*" [lindex $arg 0]] == 1 } {
			set fortune_opts "-f $fortune_opts"
		}
		if { [string match "-*l*" [lindex $arg 0]] == 1 } {
			set fortune_opts "-l $fortune_opts"
		}
		if { [string match "-*o*" [lindex $arg 0]] == 1 } {
			set fortune_opts "-o $fortune_opts"
		}

		if { [string match "-*s*" [lindex $arg 0]] == 1 } {
			set fortune_opts "-s $fortune_opts"
		}
	}

	if { ![catch {eval exec "/usr/bin/fortune $fortune_opts" } results] } {
		set lines [split $results "\n"]
		foreach l $lines {
			if {[string compare $l ""] != 0} {
				putserv "PRIVMSG $chan :$l"
			}
       		}
	return 0
	} else {
		set lines [split $results "\n"]
		foreach l $lines {
			if {[string compare $l ""] != 0} {
				putserv "PRIVMSG $nick :$l"
			}
       		}
	}
	return 0
}

#################################################################################

proc cmd_uptime {nick host handle chan arg} {
	global user deny_cmd_unix deny_cmd_unix_pl granted_cmd_unix_pl
	# Is the guy authorized to call this proc ? :)
	if {![matchattr $handle +f $chan] && ![matchattr $handle +o $chan]} {
		putlog "\[cmd_uptime\] $deny_cmd_unix_pl $nick @ $chan"
		putserv "PRIVMSG $chan :$deny_cmd_unix $nick !"
		return 0
	}
	putlog "\[uptime_check_auth\] $granted_cmd_unix_pl $nick ."

	if { ![catch {eval exec "/usr/bin/uptime" } results] } {
		set lines [split $results "\n"]
		foreach l $lines {
			if {[string compare $l ""] != 0} {
				putserv "PRIVMSG $chan :$l"
			}
       		}
	} else {
		set lines [split $results "\n"]
		foreach l $lines {
			if {[string compare $l ""] != 0} {
				putserv "PRIVMSG $nick :$l"
			}
       		}
	}
	return 0
}

#################################################################################

proc cmd_du {nick host handle chan arg} {
	global user deny_cmd_unix deny_cmd_unix_pl granted_cmd_unix_pl cmd_unix_folder cmd_unix_directory

	# Is the guy authorized to call this proc ? :)
	if {![matchattr $handle +n $chan]} {
		putlog "\[cmd_du\] $deny_cmd_unix_pl $nick @ $chan"
		putserv "PRIVMSG $chan :$deny_cmd_unix $nick !"
		return 0
	}
	putlog "\[du_check_auth\] $granted_cmd_unix_pl $nick ."

	# Is it an empty call ?
	if { [lindex $arg 0] == "" || [lindex $arg 0] == "--help"} {
		putserv "PRIVMSG $chan :USAGE \[1\]: ~du <OPTS> \[$cmd_unix_folder \| $cmd_unix_directory\]"
		return 0
	}
	putlog "\[du_check_string\] non empty request by $nick"
	# Have we got any option to parse ? :)
	if { [string match "-*" [lindex $arg 0]] == 1 } {
		putlog "OPTS to parse."
		set du_opts ""
		if { [string match "-*a*" [lindex $arg 0]] == 1 } {
			set du_opts "-a $du_opts"
		}
		if { [string match "-*b*" [lindex $arg 0]] == 1 } {
			set du_opts "-b $du_opts"
		}
		if { [string match "-*c*" [lindex $arg 0]] == 1 } {
			set du_opts "-c $du_opts"
		}
		if { [string match "-*D*" [lindex $arg 0]] == 1 } {
			set du_opts "-D $du_opts"
		}
		if { [string match "-*h*" [lindex $arg 0]] == 1 } {
			set du_opts "-h $du_opts"
		}
		if { [string match "-*H*" [lindex $arg 0]] == 1 } {
			set du_opts "-H $du_opts"
		}
		if { [string match "-*k*" [lindex $arg 0]] == 1 } {
			set du_opts "-k $du_opts"
		}
		if { [string match "-*l*" [lindex $arg 0]] == 1 } {
			set du_opts "-l $du_opts"
		}
		if { [string match "-*L*" [lindex $arg 0]] == 1 } {
			set du_opts "-L $du_opts"
		}
		if { [string match "-*m*" [lindex $arg 0]] == 1 } {
			set du_opts "-m $du_opts"
		}
		if { [string match "-*s*" [lindex $arg 0]] == 1 } {
			set du_opts "-s $du_opts"
		}
		if { [string match "-*S*" [lindex $arg 0]] == 1 } {
			set du_opts "-S $du_opts"
		}
		if { [string match "-*x*" [lindex $arg 0]] == 1 } {
			set du_opts "-x $du_opts"
		}

		# We've got OPTS so have we got a directory/folder ? :)
		if { [lindex $arg 1] == "" } {
			putserv "PRIVMSG $chan :USAGE \[2\]: ~du <OPTS> \[$cmd_unix_folder \| $cmd_unix_directory\]"
			return 0
		} else {
			set du_folder "[lindex $arg 1]"
			putlog "\[du_exec\] du $du_opts$du_folder"
			if { ![catch {eval exec "/bin/du $du_opts$du_folder" } results] } {
				set lines [split $results "\n"]
				foreach l $lines {
					if {[string compare $l ""] != 0} {
						putserv "PRIVMSG $chan :$l"
					}
        			}
			} else {
				set lines [split $results "\n"]
				foreach l $lines {
					if {[string compare $l ""] != 0} {
						putserv "PRIVMSG $nick :$l"
					}
        			}
			}
		}
	return 0
	} else {
		set du_folder "[lindex $arg 0]"
		putlog "\[du_exec\] du $du_folder"
		if { ![catch {eval exec "/bin/du $du_folder" } results] } {
			set lines [split $results "\n"]
			foreach l $lines {
				if {[string compare $l ""] != 0} {
					putserv "PRIVMSG $chan :$l"
				}
        		}
		} else {
			set lines [split $results "\n"]
			foreach l $lines {
				if {[string compare $l ""] != 0} {
					putserv "PRIVMSG $nick :$l"
				}
        		}
		}
	}
	return 0
}




#################################################################################

proc cmd_df {nick host handle chan arg} {
	global user deny_cmd_unix deny_cmd_unix_pl granted_cmd_unix_pl

	# Is the guy authorized to call this proc ? :)
	if {![matchattr $handle +n $chan]} {
		putlog "\[cmd_df\] $deny_cmd_unix_pl $nick @ $chan"
		putserv "PRIVMSG $chan :$deny_cmd_unix $nick !"
		return 0
	}
	putlog "\[df_check_auth\] $granted_cmd_unix_pl $nick ."
	# need help ? :)
	if {[lindex $arg 0] != ""} {
		if {[lindex $arg 0] == "--help"} {
			putserv "PRIVMSG $chan :USAGE \[1\]: ~df <OPTS>"
			return 0
		}
		putlog "OPTS to parse."
		set df_opts ""
		if { [string match "-*a*" [lindex $arg 0]] == 1 } {
			set df_opts "-a $df_opts"
		}
		if { [string match "-*h*" [lindex $arg 0]] == 1 } {
			set df_opts "-h $df_opts"
		}
		if { [string match "-*H*" [lindex $arg 0]] == 1 } {
			set df_opts "-H $df_opts"
		}
		if { [string match "-*i*" [lindex $arg 0]] == 1 } {
			set df_opts "-i $df_opts"
		}
		if { [string match "-*k*" [lindex $arg 0]] == 1 } {
			set df_opts "-k $df_opts"
		}
		if { [string match "-*l*" [lindex $arg 0]] == 1 } {
			set df_opts "-l $df_opts"
		}
		if { [string match "-*m*" [lindex $arg 0]] == 1 } {
			set df_opts "-m $df_opts"
		}
		if { [string match "-*P*" [lindex $arg 0]] == 1 } {
			set df_opts "-P $df_opts"
		}
		if { [string match "-*t*" [lindex $arg 0]] == 1 } {
			set df_opts "-t $df_opts"
		}
		if { [string match "-*T*" [lindex $arg 0]] == 1 } {
			set df_opts "-T $df_opts"
		}
		if { ![catch {eval exec "/bin/df $df_opts" } results] } {
			set lines [split $results "\n"]
			foreach l $lines {
				if {[string compare $l ""] != 0} {
					putserv "PRIVMSG $chan :$l"
				}
        		}
		} else {
			set lines [split $results "\n"]
			foreach l $lines {
				if {[string compare $l ""] != 0} {
					putserv "PRIVMSG $nick :$l"
				}
        		}
		}
	} else {
		if { ![catch {eval exec "/bin/df" } results] } {
			set lines [split $results "\n"]
			foreach l $lines {
				if {[string compare $l ""] != 0} {
					putserv "PRIVMSG $nick :$l"
				}
			}
		} else {
			set lines [split $results "\n"]
			foreach l $lines {
				if {[string compare $l ""] != 0} {
					putserv "PRIVMSG $nick :$l"
				}
        		}
		}
	}
	return 0
}

#################################################################################

proc cmd_cat {nick host handle chan arg} {
	global user deny_cmd_unix deny_cmd_unix_pl granted_cmd_unix_pl cmd_unix_folder

	# Is the guy authorized to call this proc ? :)
	if {![matchattr $handle +n $chan]} {
		putlog "\[cmd_cat\] $deny_cmd_unix_pl $nick @ $chan"
		putserv "PRIVMSG $chan :$deny_cmd_unix $nick !"
		return 0
	}
	putlog "\[cat_check_auth\] $granted_cmd_unix_pl $nick ."
	# Is it an empty call ?
	if { [lindex $arg 0] == "" || [lindex $arg 0] == "--help"} {
		putserv "PRIVMSG $chan :USAGE \[1\]: ~cat <OPTS> \[$cmd_unix_folder\]"
		return 0
	}
	putlog "\[cat_check_string\] non empty request by $nick"
	# Have we got any option to parse ? :)
	if { [string match "-*" [lindex $arg 0]] == 1 } {
		putlog "OPTS to parse."
		set cat_opts ""
		if { [string match "-*A*" [lindex $arg 0]] == 1 } {
			set cat_opts "-A $cat_opts"
		}
		if { [string match "-*b*" [lindex $arg 0]] == 1 } {
			set cat_opts "-b $cat_opts"
		}
		if { [string match "-*e*" [lindex $arg 0]] == 1 } {
			set cat_opts "-e $cat_opts"
		}
		if { [string match "-*E*" [lindex $arg 0]] == 1 } {
			set cat_opts "-E $cat_opts"
		}
		if { [string match "-*n*" [lindex $arg 0]] == 1 } {
			set cat_opts "-n $cat_opts"
		}
		if { [string match "-*s*" [lindex $arg 0]] == 1 } {
			set cat_opts "-s $cat_opts"
		}
		if { [string match "-*t*" [lindex $arg 0]] == 1 } {
			set cat_opts "-t $cat_opts"
		}
		if { [string match "-*T*" [lindex $arg 0]] == 1 } {
			set cat_opts "-T $cat_opts"
		}
		if { [string match "-*u*" [lindex $arg 0]] == 1 } {
			set cat_opts "-u $cat_opts"
		}
		if { [string match "-*v*" [lindex $arg 0]] == 1 } {
			set cat_opts "-v $cat_opts"
		}
		# We've got OPTS so have we got a folder ? :)
		if { [lindex $arg 1] == "" } {
			putserv "PRIVMSG $chan :USAGE \[2\]: ~cat <OPTS> \[$cmd_unix_folder\]"
			return 0
		} else {
			set cat_folder "[lindex $arg 1]"
			putlog "\[cat_exec\] cat $cat_opts$cat_folder"
			if { ![catch {eval exec "/bin/cat $cat_opts$cat_folder" } results] } {
				set lines [split $results "\n"]
				foreach l $lines {
					if {[string compare $l ""] != 0} {
						putserv "PRIVMSG $chan :$l"
					}
        			}
			} else {
				set lines [split $results "\n"]
				foreach l $lines {
					if {[string compare $l ""] != 0} {
					putserv "PRIVMSG $nick :$l"
					}
        			}
			}
		}
	return 0
	} else {
		set cat_folder "[lindex $arg 0]"
		putlog "\[cat_exec\] cat $cat_folder"
		if { ![catch {eval exec "/bin/cat $cat_folder" } results] } {
			set lines [split $results "\n"]
			foreach l $lines {
				if {[string compare $l ""] != 0} {
					putserv "PRIVMSG $chan :$l"
				}
        		}
		} else {
			set lines [split $results "\n"]
			foreach l $lines {
				if {[string compare $l ""] != 0} {
					putserv "PRIVMSG $nick :$l"
				}
        		}
		}
	}
	return 0
}


#################################################################################

proc cmd_ls {nick host handle chan arg} {
	global user deny_cmd_unix deny_cmd_unix_pl granted_cmd_unix_pl cmd_unix_directory

	# Is the guy authorized to call this proc ? :)
	if {![matchattr $handle +n $chan]} {
		putlog "\[cmd_ls\] $deny_cmd_unix_pl $nick @ $chan"
		putserv "PRIVMSG $chan :$deny_cmd_unix $nick !"
		return 0
	}
	putlog "\[ls_check_auth\] $granted_cmd_unix_pl $nick ."
	# Is it an empty call ?
	if { [lindex $arg 0] == "" || [lindex $arg 0] == "--help"} {
		putserv "PRIVMSG $chan :USAGE \[1\]: ~ls <OPTS> \[$cmd_unix_directory\]"
		return 0
	}
	putlog "\[ls_check_string\] non empty request by $nick"

	# Have we got any option to parse ? :)
	if { [string match "-*" [lindex $arg 0]] == 1 } {
		putlog "OPTS to parse."
		set ls_opts ""
		if { [string match "-*l*" [lindex $arg 0]] == 1 } {
			set ls_opts "-l $ls_opts"
		}
		if { [string match "-*h*" [lindex $arg 0]] == 1 } {
			set ls_opts "-h $ls_opts"
		}
		putlog "OPTS parsed: $ls_opts"
		if { [string match "-*r*" [lindex $arg 0]] == 1 } {
			set ls_opts "-r $ls_opts"
		}
		if { [string match "-*R*" [lindex $arg 0]] == 1 } {
			set ls_opts "-R $ls_opts"
		}
		if { [string match "-*u*" [lindex $arg 0]] == 1 } {
			set ls_opts "-u $ls_opts"
		}
		if { [string match "-*t*" [lindex $arg 0]] == 1 } {
			set ls_opts "-t $ls_opts"
		}
		if { [string match "-*C*" [lindex $arg 0]] == 1 } {
			set ls_opts "-C $ls_opts"
		}
		if { [string match "-*b*" [lindex $arg 0]] == 1 } {
			set ls_opts "-b $ls_opts"
		}
		if { [string match "-*F*" [lindex $arg 0]] == 1 } {
			set ls_opts "-F $ls_opts"
		}
		if { [string match "-*a*" [lindex $arg 0]] == 1 } {
			set ls_opts "-a $ls_opts"
		}
		if { [string match "-*d*" [lindex $arg 0]] == 1 } {
			set ls_opts "-d $ls_opts"
		}
		if { [string match "-*i*" [lindex $arg 0]] == 1 } {
			set ls_opts "-i $ls_opts"
		}

		# We've got OPTS so have we got a directory ? :)
		if { [lindex $arg 1] == "" } {
			putserv "PRIVMSG $chan :USAGE \[2\]: ~ls <OPTS> \[$cmd_unix_directory\]"
			return 0
		} else {
			set ls_dir "[lindex $arg 1]"
			putlog "\[ls_exec\] ls $ls_opts$ls_dir"
			if { ![catch {eval exec "/bin/ls $ls_opts$ls_dir" } results] } {
				set lines [split $results "\n"]
				foreach l $lines {
					if {[string compare $l ""] != 0} {
						putserv "PRIVMSG $chan :$l"
					}
        			}
			} else {
				set lines [split $results "\n"]
				foreach l $lines {
					if {[string compare $l ""] != 0} {
						putserv "PRIVMSG $nick :$l"
					}
	        		}
			}
		}
	return 0
	} else {
		set ls_dir "[lindex $arg 0]"
		putlog "\[ls_exec\] ls $ls_dir"
		if { ![catch {eval exec "/bin/ls -C -b $ls_dir" } results] } {
			set lines [split $results "\n"]
			foreach l $lines {
				if {[string compare $l ""] != 0} {
					putserv "PRIVMSG $chan :$l"
				}
        		}
		} else {
			set lines [split $results "\n"]
			foreach l $lines {
				if {[string compare $l ""] != 0} {
					putserv "PRIVMSG $nick :$l"
				}
        		}
		}
	}
	return 0
}



#################################################################################
putlog "$cmd_unix_script $cmd_unix_script_version release $cmd_unix_script_date \
	by $cmd_unix_script_author ( $cmd_unix_script_author_mail ) loaded"
################################  EOF  ##########################################