egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

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

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Script Requests
View previous topic :: View next topic  
Author Message
Faraibi
Voice


Joined: 16 Oct 2007
Posts: 6

PostPosted: Thu Jan 17, 2008 8:23 am    Post subject: Need a script to display txt files from subdirs to a channel Reply with quote

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 Smile
Back to top
View user's profile Send private message
r0t3n
Owner


Joined: 31 May 2005
Posts: 507
Location: UK

PostPosted: Thu Jan 17, 2008 2:56 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message MSN Messenger
user
 


Joined: 18 Mar 2003
Posts: 1452
Location: Norway

PostPosted: Fri Jan 18, 2008 7:57 am    Post subject: Reply with quote

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:
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
   }

}

_________________
Have you ever read "The Manual"?


Last edited by user on Sat Jan 19, 2008 1:13 am; edited 1 time in total
Back to top
View user's profile Send private message
Faraibi
Voice


Joined: 16 Oct 2007
Posts: 6

PostPosted: Fri Jan 18, 2008 12:57 pm    Post subject: thanks user Reply with quote

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 ?
Back to top
View user's profile Send private message
rix
Halfop


Joined: 21 Sep 2005
Posts: 42
Location: Estonia

PostPosted: Fri Jan 18, 2008 1:28 pm    Post subject: Reply with quote

Is it possible to just list the directory content?
Back to top
View user's profile Send private message
metroid
Owner


Joined: 16 Jun 2004
Posts: 771

PostPosted: Fri Jan 18, 2008 2:01 pm    Post subject: Re: thanks user Reply with quote

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.
Back to top
View user's profile Send private message
Faraibi
Voice


Joined: 16 Oct 2007
Posts: 6

PostPosted: Fri Jan 18, 2008 2:08 pm    Post subject: Reply with quote

Quote:
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"
Back to top
View user's profile Send private message
user
 


Joined: 18 Mar 2003
Posts: 1452
Location: Norway

PostPosted: Sat Jan 19, 2008 1:14 am    Post subject: Reply with quote

There was a stupid regexp bug (missing argument). Try the edited version.
If you want the output in the channel, change
Code:
puthelp "PRIVMSG $nick :$line"
to
Code:
puthelp "PRIVMSG $chan :$line"

_________________
Have you ever read "The Manual"?
Back to top
View user's profile Send private message
Faraibi
Voice


Joined: 16 Oct 2007
Posts: 6

PostPosted: Sat Jan 19, 2008 3:44 am    Post subject: Reply with quote

user thanks a lot
it really worked
the bot is dumping the files on channel now Smile
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 ?
Back to top
View user's profile Send private message
Faraibi
Voice


Joined: 16 Oct 2007
Posts: 6

PostPosted: Sat Jan 19, 2008 6:02 am    Post subject: Reply with quote

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 !
Back to top
View user's profile Send private message
MrStatic
Voice


Joined: 29 Apr 2008
Posts: 3

PostPosted: Tue Apr 29, 2008 4:00 am    Post subject: Reply with quote

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:
               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?t=14021&highlight=delay+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:

               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:

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
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Script Requests All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber