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.

Script to read numbered line of text

Requests for complete scripts or modifications/fixes for scripts you didn't write. Response not guaranteed, and no thread bumping!
Post Reply
M
Mabus4444
Halfop
Posts: 51
Joined: Mon Oct 30, 2006 7:40 pm

Script to read numbered line of text

Post by Mabus4444 »

My channel has a rules page with numbered rules for example;

1) no killing people
2) no playing barry manilow songs
3) no used disco balls

As I have it now, if someone types "!rules" in the channel the bot will respond with a url for the rules page. Nice and simple.

What I would LIKE to do however, is so that when an op types "!rule 1" in the channel, the bot replies by going to the webpage and reads that rule back to the channel.

The way I have it imagined in my head, I could have the html set like so,
<!-- rule 1 start -->
1) no killing people
<!-- rule 1 end -->

When looking a the webpage all people would see is the rule itself, and you could have the bot start and stop reading with the remmed out tags.

Any help with this would be greatly appreciated
User avatar
tomekk
Master
Posts: 255
Joined: Fri Nov 28, 2008 11:35 am
Location: Oswiecim / Poland
Contact:

Post by tomekk »

you can make second file in the same directory or something, text file with rules 1 by 1

rules.txt
1. rule /1st line
2 .rule /2nd line
3. rule /3rd line
n. rule /n line

I can write TCL for you if want, but with txt file (its less work for me and for sure that will always work, 2nd thing, you can mod your html source without changing the TCL script code because its based on "clear" text file)
M
Mabus4444
Halfop
Posts: 51
Joined: Mon Oct 30, 2006 7:40 pm

Post by Mabus4444 »

tomekk wrote:you can make second file in the same directory or something, text file with rules 1 by 1
Yeah that's what I'm doing at the moment, but it creates a ton of different files. Would be much cleaner (as you said) if a script like this could just read off a single text file. Also I imagine it could be adapted to many other things and have many other uses.
tomekk wrote:I can write TCL for you if want, but with txt file (its less work for me and for sure that will always work, 2nd thing, you can mod your html source without changing the TCL script code because its based on "clear" text file)
If you could do it so it reads a text file off the web that would work great.
User avatar
tomekk
Master
Posts: 255
Joined: Fri Nov 28, 2008 11:35 am
Location: Oswiecim / Poland
Contact:

Post by tomekk »

try:

Code: Select all

# tomekk, http://forum.egghelp.org/viewtopic.php?t=16942

# commands channels
set file_web_channels {#channel #channel2}

# syntax: "channel_command_name|http://address/somefile"
set file_web_links {
	"!rule|http://localhost/rules.txt"
	"!test|http://localhost/test.txt"
}
#############################################################
bind pubm -|- * pub_proc

package require http

proc pub_proc { nick uhost hand chan arg } {
	global file_web_links file_web_channels

	if {[lsearch $file_web_channels $chan] == -1} {
		return                         
	} 

	set spt_args [split $arg]
	set command_name [lindex $spt_args 0]
	set command_data [lindex $spt_args 1]

	if {![regexp {^([0-9]+)$} $command_data]} {
		return
	} {
		set command_data [expr $command_data - 1]

		if {$command_data <= -1} {
			return
		}
	}

	set the_url ""

	foreach link_line $file_web_links {
		if {$link_line != ""} {
			set sep_rows [split $link_line "|"]
			set ch_cmd_name [string trim [lindex $sep_rows 0]]
			set ch_cmd_url [string trim [lindex $sep_rows 1]]

			if {$command_name == $ch_cmd_name} {
				set the_url $ch_cmd_url
			}
		}
	}

	if {$the_url != ""} {
		if {![catch {set http_handle [http::geturl $the_url -timeout 10000]}]} {
			set http_status [http::status $http_handle]
			if {$http_status == "ok"} {
				if {[http::ncode $http_handle] == 200} {
					set http_data [http::data $http_handle]

					set line_by_line [split $http_data "\n"]

					if {$command_data <= [llength $line_by_line]} {
						putquick "PRIVMSG $chan :[lindex $line_by_line $command_data]"
					}
				}
			} elseif {$http_status == "timeout"} {
				putquick "NOTICE $nick :error: connection timed out"
			} 
		} {
			putquick "NOTICE $nick :error: connection failed"
		}
	}
}

putlog "file-grabber.tcl ver 0.1 by tomekk loaded"
20:31:19 <@tomekk> !rule 2
20:31:24 < botty> rules file line 2
20:31:29 <@tomekk> !test 3
20:31:29 < botty> test file line 3
two diffrent files, with data line by line:
[tomekk@zonk]:/home/www# cat rules.txt test.txt
rules file line 1
rules file line 2
rules file line 3

test file line 1
test file line 2
test file line 3
M
Mabus4444
Halfop
Posts: 51
Joined: Mon Oct 30, 2006 7:40 pm

Post by Mabus4444 »

First off, thanks for taking a shot at this. I appreciate it.

That said, the first attempt didn't t seem to work for me. Just to summarise, so as to make sure I did not misunderstand anything. I created two files. One was called rule.txt and the other was test.txt

I was not sure what text was supposed to go where, so I put the full text I saw in your output into both files like so;

rules file line 1
rules file line 2
rules file line 3

test file line 1
test file line 2
test file line 3

The above is the full contents of both text files.

As far as the settings are concerned, I changed them to the following;

# commands channels
set file_web_channels {#mabusbottest #channel2}

# syntax: "channel_command_name|http://address/somefile"
set file_web_links {
"!rule|http://www.dalnetdebates.com/rules.txt"
"!test|http://www.dalnetdebates.com/test.txt"
}

I restarted the bot, to make sure it loaded the script (not merely rehashed).

In the channel I typed
!rule
and
!test

The result of both was silence. No errors of any kind, but no response either. Any idea what the problem could be?
User avatar
tomekk
Master
Posts: 255
Joined: Fri Nov 28, 2008 11:35 am
Location: Oswiecim / Poland
Contact:

Post by tomekk »

can be without #channel2, its example:

Code: Select all

set file_web_channels {#mabusbottest}
and use:
!rule <line number>
example, i used your host in my script ("!rule|http://www.dalnetdebates.com/rules.txt"):
09:22:59 <@tomekk> !rule 1
09:23:01 < botty> rules file line 1
09:23:03 <@tomekk> !rule 2
09:23:04 < botty> rules file line 2
09:23:06 <@tomekk> !rule 5
09:23:07 < botty> test file line 1
because, your 'rule.txt' file:
rules file line 1 //line num 1
rules file line 2 //line num 2
rules file line 3 //line num 3
//line num 4
test file line 1 //line num 5
test file line 2 //line num 6
test file line 3 //line num 7
its working OK, remember, like u said, <command> <line number>

:>
M
Mabus4444
Halfop
Posts: 51
Joined: Mon Oct 30, 2006 7:40 pm

Post by Mabus4444 »

It WORKS!!!!

The problem turned out to be the channels setting. I got rid of the second channel somehow that worked, anyway it works perfectly now. Thanks a lot for all your help :)
M
Mabus4444
Halfop
Posts: 51
Joined: Mon Oct 30, 2006 7:40 pm

Post by Mabus4444 »

One question, if it's not too much trouble. The script stops (does not spit out the line) if the line is too long. Is there an easy way to make it split a sentence up into two parts if it's too long?
User avatar
tomekk
Master
Posts: 255
Joined: Fri Nov 28, 2008 11:35 am
Location: Oswiecim / Poland
Contact:

Post by tomekk »

works for me even with 20k chars in one line, but of course on irc see only IRC max in one line,

could you pm me with this file? i will look into it, maybe its something else
M
Mabus4444
Halfop
Posts: 51
Joined: Mon Oct 30, 2006 7:40 pm

Post by Mabus4444 »

I sent this to you in PM as you asked for, but thought I'd also update it here as well;

Thanks for your help with this.

The file is just a text file called rules.txt The contents of the file are;

1) Threats of any kind of violence towards any member of our channel is subject to immediate and permanent removal from the channel. This includes any stalking behaviour that occurs on our, or other channels our members happen to go on. This includes users following our members into our channel from other channels to harrass them. Attempts to uncover personal information about our users to use personal offline information about our users to in any way embarrass or harm a user is strictly prohibited.
2) Negative remarks directed at our members beliefs or claims are protected provided they remain reasonable, however offensive remarks directed specifically and deliberately at the character of a fellow channel member him or herself is strictly prohibited. This includes swearing at the person or any other derogatory comments of of a similar nature. Attack the beliefs, and not the individual holding the beliefs.


Just two very long lines. The first line does not display at all, the bot just sits there silently. The second line displays perfectly.

If you want/need to download the textfile itself you can download it at
http://www.dalnetdebates.com/rules/rules.txt

Thanks again for your help
User avatar
tomekk
Master
Posts: 255
Joined: Fri Nov 28, 2008 11:35 am
Location: Oswiecim / Poland
Contact:

Post by tomekk »

try:

Code: Select all

# tomekk, http://forum.egghelp.org/viewtopic.php?t=16942

# commands channels
set file_web_channels {#channel #channel2}

# max "words" per line (it means: test test on. <- space separator, "test" "test" "on.", 3 "words" here)
set max_per_line 30

# syntax: "channel_command_name|http://address/somefile"
set file_web_links {
	"!rule|http://localhost/rules.txt"
	"!test|http://localhost/test.txt"
}
#############################################################
bind pubm -|- * pub_proc

package require http

proc pub_proc { nick uhost hand chan arg } {
	global file_web_links file_web_channels max_per_line

	if {[lsearch $file_web_channels $chan] == -1} {
		return                         
	} 

	set spt_args [split $arg]
	set command_name [lindex $spt_args 0]
	set command_data [lindex $spt_args 1]

	if {![regexp {^([0-9]+)$} $command_data]} {
		return
	} {
		set command_data [expr $command_data - 1]

		if {$command_data <= -1} {
			return
		}
	}

	set the_url ""

	foreach link_line $file_web_links {
		if {$link_line != ""} {
			set sep_rows [split $link_line "|"]
			set ch_cmd_name [string trim [lindex $sep_rows 0]]
			set ch_cmd_url [string trim [lindex $sep_rows 1]]

			if {$command_name == $ch_cmd_name} {
				set the_url $ch_cmd_url
			}
		}
	}

	if {$the_url != ""} {
		if {![catch {set http_handle [http::geturl $the_url -timeout 10000]}]} {
			set http_status [http::status $http_handle]
			if {$http_status == "ok"} {
				if {[http::ncode $http_handle] == 200} {
					set http_data [http::data $http_handle]

					set line_by_line [split $http_data "\n"]

					if {$command_data <= [llength $line_by_line]} {
						set line_list [split [lindex $line_by_line $command_data]]
						set line_length [llength $line_list]

						if {$line_length > $max_per_line} {
							set parts [expr $line_length / $max_per_line]

							if {[expr $parts * $max_per_line] < $line_length} {
								incr parts 1
							}

							set start_idx 0
							set end_idx [expr $max_per_line - 1]

							for {set i 0} {$i < $parts} {incr i 1} {
								putquick "PRIVMSG $chan :[join [lrange $line_list $start_idx $end_idx]]"
								incr start_idx $max_per_line
								incr end_idx $max_per_line
							}
						} {
							putquick "PRIVMSG $chan :[lindex $line_by_line $command_data]"
						}
					}
				}
			} elseif {$http_status == "timeout"} {
				putquick "NOTICE $nick :error: connection timed out"
			} 
		} {
			putquick "NOTICE $nick :error: connection failed"
		}
	}
}

putlog "file-grabber.tcl ver 0.1 by tomekk loaded"
10:39:21 <@tomekk> !rule 1
10:39:28 < botty> 1) Threats of any kind of violence towards any member of our channel is subject to immediate
and permanent removal from the channel. This includes any stalking behaviour that occurs
10:39:31 < botty> on our, or other channels our members happen to go on. This includes users following our
members into our channel from other channels to harrass them. Attempts to uncover personal
10:39:34 < botty> information about our users to use personal offline information about our users to in any way
embarrass or harm a user is strictly prohibited.
User avatar
speechles
Revered One
Posts: 1398
Joined: Sat Aug 26, 2006 10:19 pm
Location: emerald triangle, california (coastal redwoods)

Post by speechles »

There is a better way than counting words. Doing it the "count word" way there will be VAST differences in the length of each "split" line because the length of words can vary so much. This looks very sketchy and amateur.

Instead you can use this proc by user and it does the job much more elegantly.

Code: Select all

# LINE_WRAP
# takes a long line in, and chops it before the specified length
# http://forum.egghelp.org/viewtopic.php?t=6690
#
proc line_wrap {str {splitChr { }}} { 
  set out [set cur {}]
  set i 0
  set len $::lineSplit
  foreach word [split [set str][set str ""] $splitChr] { 
    if {[incr i [string length $word]] > $len} { 
      lappend out [join $cur $splitChr] 
      set cur [list $word] 
      set i [string length $word] 
    } else { 
      lappend cur $word 
    } 
    incr i 
  } 
  lappend out [join $cur $splitChr] 
}
Then add this var to the top of your script:

Code: Select all

variable lineSplit 340
Then to use it in your script would work like so:

Code: Select all

               if {$command_data <= [llength $line_by_line]} {
                  foreach splitline [line_wrap [lindex $line_by_line $command_data]] {
                    putserv "PRIVMSG $chan :$splitline"
                  }
               } 
User r0x! ;)
User avatar
tomekk
Master
Posts: 255
Joined: Fri Nov 28, 2008 11:35 am
Location: Oswiecim / Poland
Contact:

Post by tomekk »

speechles wrote:This looks very sketchy and amateur.
Nah, it's just other way of doing the same action.
Both are working OK, but of course there are diffrent words in text.

User's proc is simple and working great.

Mabus4444 choose one, i fixed it for you.
Thanks speechles.

Code: Select all

# tomekk, http://forum.egghelp.org/viewtopic.php?t=16942

# commands channels
set file_web_channels {#channel #channel2}

# max chars per line (with full words, without spliting in the middle like: word wo\nrd word, will be: word word\nword, if possible)
variable lineSplit 340

# syntax: "channel_command_name|http://address/somefile"
set file_web_links {
	"!rule|http://localhost/rules.txt"
	"!test|http://localhost/test.txt"
}
#############################################################
bind pubm -|- * pub_proc

package require http

# LINE_WRAP 
# takes a long line in, and chops it before the specified length 
# http://forum.egghelp.org/viewtopic.php?t=6690 
# 
proc line_wrap {str {splitChr { }}} { 
	set out [set cur {}] 
	set i 0 
	set len $::lineSplit 
	foreach word [split [set str][set str ""] $splitChr] { 
		if {[incr i [string length $word]] > $len} { 
			lappend out [join $cur $splitChr] 
			set cur [list $word] 
			set i [string length $word] 
		} else { 
			lappend cur $word 
		} 
		incr i 
	} 
	lappend out [join $cur $splitChr] 
}

proc pub_proc { nick uhost hand chan arg } {
	global file_web_links file_web_channels max_per_line

	if {[lsearch $file_web_channels $chan] == -1} {
		return                         
	} 

	set spt_args [split $arg]
	set command_name [lindex $spt_args 0]
	set command_data [lindex $spt_args 1]

	if {![regexp {^([0-9]+)$} $command_data]} {
		return
	} {
		set command_data [expr $command_data - 1]

		if {$command_data <= -1} {
			return
		}
	}

	set the_url ""

	foreach link_line $file_web_links {
		if {$link_line != ""} {
			set sep_rows [split $link_line "|"]
			set ch_cmd_name [string trim [lindex $sep_rows 0]]
			set ch_cmd_url [string trim [lindex $sep_rows 1]]

			if {$command_name == $ch_cmd_name} {
				set the_url $ch_cmd_url
			}
		}
	}

	if {$the_url != ""} {
		if {![catch {set http_handle [http::geturl $the_url -timeout 10000]}]} {
			set http_status [http::status $http_handle]
			if {$http_status == "ok"} {
				if {[http::ncode $http_handle] == 200} {
					set http_data [http::data $http_handle]

					set line_by_line [split $http_data "\n"]

					if {$command_data <= [llength $line_by_line]} {
						foreach splitline [line_wrap [lindex $line_by_line $command_data]] { 
							putserv "PRIVMSG $chan :$splitline" 
						}
					}
				}
			} elseif {$http_status == "timeout"} {
				putquick "NOTICE $nick :error: connection timed out"
			} 
		} {
			putquick "NOTICE $nick :error: connection failed"
		}
	}
}

putlog "file-grabber.tcl ver 0.1 by tomekk loaded"
;)
M
Mabus4444
Halfop
Posts: 51
Joined: Mon Oct 30, 2006 7:40 pm

Post by Mabus4444 »

Hey guys, It works perfectly now. Thanks a bunch :)
Post Reply