This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Need a script to display txt files from subdirs to a channel

Requests for complete scripts or modifications/fixes for scripts you didn't write. Response not guaranteed, and no thread bumping!
Post Reply
F
Faraibi
Voice
Posts: 6
Joined: Tue Oct 16, 2007 3:40 pm

Need a script to display txt files from subdirs to a channel

Post by Faraibi »

Hi all,
i searched the whole day for a script on the egghelp.org tcl archive and on this forum also, i found some scripts very useful but dint get what i need actually.

actually what i have is subdirectories in a directory, so i want to specify different subdirectories in a single directory for different contents in the script:

e.g
sports "base_dir/sports/"
games "base_dir/games/"

and the text files will be in these subdirectories. when the user do this on a channel !menu or !options or any trigger the bot displays the available subdirectories (sports, games etc) and then the user can get the list !list sports or !list games, that will list the files in that subdirectory and when the user do this !display sports cricket, the bot will display the contents of cricket.txt, line by line (every next line after some seconds 5..6 seconds), that is in sports subdirectory.

and a mastershutdown of this tcl would help .. so that users dont get the bot to flood...


I know it's a bit complicated and thanks for reading this request :)
r
r0t3n
Owner
Posts: 507
Joined: Tue May 31, 2005 6:56 pm
Location: UK

Post by r0t3n »

Take a look at http://forum.egghelp.org/viewtopic.php?t=6885 for basic file operations, then modify one of the scripts you have found to your requirements. Learn the file commands to grab the contents of a dir and display the list of subdirs, and the contents of the file(s) you want to display along with foreach loop for your line-to-line output.
r0t3n @ #r0t3n @ Quakenet
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

Tosser^^ wrote:Take a look at http://forum.egghelp.org/viewtopic.php?t=6885 for basic file operations, then modify one of the scripts you have found to your requirements. Learn the file commands to grab the contents of a dir and display the list of subdirs, and the contents of the file(s) you want to display along with foreach loop for your line-to-line output.
This is not the scripting help forum. Making this script is not as easy as it sounds and could be dangerous if done by a newbie (directory traversal bugs)

Here's a mockup (not tested). I packed all the features into a single command (the number of arguments determine what action is performed)

Code: Select all

namespace eval ::txthing {

	bind pub - !display ::txthing::pubcmd
	variable basedir "~/"
	variable fileext ".txt"

	proc pubcmd {nick uhost hand chan arg} {
		# scan takes care of two things; stripping whitespace and counting the words
		switch -- [scan $arg %s%s%s 0 1 2] {
			-1 - 0 {
				# 0 args - list dirs
				if {![llength [set dirs [list_dirs]]]} {
					puthelp "PRIVMSG $chan :$nick: Configuration error: basedir does not exist or is empty."
				} {
					puthelp "PRIVMSG $chan :$nick: [join $dirs ", "]"
				}
			}
			1 {
				# 1 arg - list files in a dir
				if {[catch {list_files $0} files]} {
					puthelp "PRIVMSG $chan :$nick: Error: $files"
				} {
					puthelp "PRIVMSG $chan :$nick: [join $files ", "]"
				}
			}
			2 {
				# 2 args - fetch file contents
				if {[catch {get_file "$0 $1"} lines]} {
					puthelp "PRIVMSG $chan :$nick: Error: $lines"
				} {
					foreach line $lines {
						# puthelp will give you about 4 seconds delay between lines
						puthelp "PRIVMSG $nick :$line"
					}
				}
			}
			default {
				# 3 or more args
				puthelp "PRIVMSG $chan :$nick: usage: $::lastbind ?category? ?subcategory?"
			}
		}
	}

	proc list_dirs {} {
		variable basedir
		set list {}
		foreach dir [glob -nocomplain -type d -dir $basedir *] {
			lappend list [file tail $dir]
		}
		set list
	}

	proc list_files dir {
		variable basedir
		variable fileext
		# no . or / allowed in names (quick hack to prevent directory traversal)
		if {![regexp {^[^\./]+$} $dir]} {
			error "invalid category"
		}
		set dir [file join $basedir $dir]
		if {![file isdir $dir]} {
			error "invalid category"
		}
		set list {}
		foreach file [glob -nocomplain -type f -dir $dir *$fileext] {
			lappend list [file root [file tail $file]]
		}
		set list
	}
	
	proc get_file arg {
		variable basedir
		variable fileext
		# no . or / allowed in names (quick hack to prevent directory traversal)
		if {![regexp {^([^\./ ]+)\s+([^\./ ]+)$} $arg arg dir file]} {
			error "invalid category or subcategory"
		}
		set dir [file join $basedir $dir]
		if {![file isdir $dir]} {
			error "invalid category"
		}
		set file [file join $dir $file$fileext]
		if {![file isfile $file]} {
			error "invalid subcategory"
		}
		split [read -nonewline [set file [open $file]]][close $file] \n
	}

}
Last edited by user on Sat Jan 19, 2008 1:13 am, edited 1 time in total.
Have you ever read "The Manual"?
F
Faraibi
Voice
Posts: 6
Joined: Tue Oct 16, 2007 3:40 pm

thanks user

Post by Faraibi »

thanks user and tosser.

user i tried your tcl.
it works fine for listing directory. But it is not dumping the text file.

<TuN>!display
<Shugal> TuN: sports, games
<TuN>!display sports
<Shugal>TuN: cricket, playingminds
<TuN>!display cricket
<Shugal> TuN: Error: invalid category

I made cricket.txt inside sports directory. it displays the name
but when i do this !display cricket
or !display cricket.txt the result is:

<Shugal> TuN: Error: invalid category

(Shugal is my botnick)

I also did "!display sports cricket" and "!display sports cricket.txt" but it dint work, the same error "<Shugal> TuN: Error: invalid category". So, any idea what is the problem ?
r
rix
Halfop
Posts: 42
Joined: Wed Sep 21, 2005 1:04 pm
Location: Estonia

Post by rix »

Is it possible to just list the directory content?
m
metroid
Owner
Posts: 771
Joined: Wed Jun 16, 2004 2:46 am

Re: thanks user

Post by metroid »

Faraibi wrote:thanks user and tosser.

user i tried your tcl.
it works fine for listing directory. But it is not dumping the text file.

<TuN>!display
<Shugal> TuN: sports, games
<TuN>!display sports
<Shugal>TuN: cricket, playingminds
<TuN>!display cricket
<Shugal> TuN: Error: invalid category

I made cricket.txt inside sports directory. it displays the name
but when i do this !display cricket
or !display cricket.txt the result is:

<Shugal> TuN: Error: invalid category

(Shugal is my botnick)

I also did "!display sports cricket" and "!display sports cricket.txt" but it dint work, the same error "<Shugal> TuN: Error: invalid category". So, any idea what is the problem ?
You need to use "!display sports cricket" to display the contents. The bot can't read your mind thus it won't know what you want unless you tell it what you want.
F
Faraibi
Voice
Posts: 6
Joined: Tue Oct 16, 2007 3:40 pm

Post by Faraibi »

You need to use "!display sports cricket" to display the contents. The bot can't read your mind thus it won't know what you want unless you tell it what you want.
That's what i was asking, it is not displaying the file contents (dumping the file in the channel)
what it do is:

<TuN> !display sports cricket
<Shugal> TuN: Error: invalid category

and cricket.txt file is in sports directory, and the bot lists cricket file when i do this "!display sports" it lists cricket file. but when i do this "!display sports cricket" it gives

"<Shugal> TuN: Error: invalid category"
User avatar
user
&nbsp;
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

There was a stupid regexp bug (missing argument). Try the edited version.
If you want the output in the channel, change

Code: Select all

puthelp "PRIVMSG $nick :$line"
to

Code: Select all

puthelp "PRIVMSG $chan :$line"
Have you ever read "The Manual"?
F
Faraibi
Voice
Posts: 6
Joined: Tue Oct 16, 2007 3:40 pm

Post by Faraibi »

user thanks a lot
it really worked
the bot is dumping the files on channel now :)
thank you for this..


one more thing, can i add timer to this ? like if the file is big, having 50..60 lines, the bot displays every next line after some seconds, 10 or more ? so that bot wont flood.
10+ seconds for next line
is this possible ?
F
Faraibi
Voice
Posts: 6
Joined: Tue Oct 16, 2007 3:40 pm

Post by Faraibi »

and one more thing, if there are a number of files in the subdirectory, the bot only displays the names which come in one line, the bot doesn't display the rest in 2nd line..

can we have some thing like, the bot displays some number of files in first, and some in 2nd and some in third line.

so that all the file names can be displayed which are in the sub-directory

thankyou !
M
MrStatic
Voice
Posts: 3
Joined: Tue Apr 29, 2008 3:56 am

Post by MrStatic »

Heh I realize this is an old post but the code still works wonders but I am attempting to add to it and I am having issues. I am looking to delay the output in the foreach loop

Code: Select all

               foreach line $lines {
                  # puthelp will give you about 4 seconds delay between lines
                  puthelp "PRIVMSG $chan :$line"
               }
I have searched the forums and I found http://forum.egghelp.org/viewtopic.php? ... lay+output
but I am still having issues making it work, any insights would be helpful as well if there is an easier way to pull this off. I am looking for say 30 seconds between each line or 60 seconds.

Added

Code: Select all

               foreach line $lines {
                  # puthelp will give you about 4 seconds delay between lines
                  utimer 30 [ putserv "PRIVMSG $chan :$line" ]
               }
Works but does not actually delay by 30 seconds, maybe If I put it in an array and play that out with a timer? I have no idea how to do that tho in tcl

******
Another edit, got it finally

Code: Select all

set time 0
            if {[catch {get_file "$0 $1"} lines]} {
               puthelp "PRIVMSG $chan :$nick: Error: $lines"
            } {
               foreach line $lines {
                  # puthelp will give you about 4 seconds delay between lines
                  utimer [incr time 30] [list putserv "PRIVMSG $chan :$line"]
               }
            }
Now to try and see if I can pull the first part of the file for a variable like have it say 0 Text then 30 text tp represent time for timers
Post Reply