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.

Problems with reading from a file

Help for those learning Tcl or writing their own scripts.
Post Reply
S
SL0RD
Voice
Posts: 19
Joined: Sun Oct 05, 2008 11:44 am

Problems with reading from a file

Post by SL0RD »

So basically what I want to do is create a "database" like this:

Code: Select all

User1 http://user1url.com
User2 http://user2url.com
User3 http://user3url.com
User4 http://user4url.com
I want to be able to do this with that database:

1. Add to the "database" && Update an existing User's URL
2. Delete Users from the "database" by just using their nick
3. List all the user's names in the channel
4. If I do !change user3 in my channel i want it to write user3's url to a var so i can input it into a mysql table

I have been trying to get this to work for days and days. here is my attempt feel free to modify it or w/e you need to do and if you have any advice or know of a better way to do this please tell me. Also I want to be able to do all of this from my channel.

Code: Select all

#mysql sever info

set host "HOST"
set user "username"
set passwd "passwd"
set db "Database"

#make sure you have the right package

package require mysqltcl

#set the database file 

set streamers "scripts/stms.txt"
    
#bind your commands
    
bind pub - !addfeed add:feed
bind pub - !delfeed del:feed
bind pub - !feeds list:feed
bind pub - !feed feed:feed

if {![file exists $cmdsfile]} {
 set fileid [open $cmdsfile w]
 close $fileid
}

set feeds [list]
foreach donecmd [split [read [set fileid [open $streamers]]][close $fileid] \n][unset fileid] {
 if {[string match -nocase "Stream name - *" $donecmd]} {
  lappend feeds [lindex [split $donecmd] 3]
 }
}

proc add:feed {nick uhost hand chan text} {
global streamers
  if {$chan == "#sops"} {
  set fs [open $streamers a]
  set name [lindex $text 0]
  set url [join [lrange [split $text] 1 end]]
  puts $fs "Stream name: $name Stream URL: $url"
  close $fs
  }
}

proc del:feed {nick uhost hand chan text} {
global streamers
  if {$chan == "#sops"} {
  set fs [open $streamers "r"]
  set data [read -nonewline $fs]
  close $fs
  set lines [split $data "\n"]
  set name [lindex $text 0]
  set match "Stream name - $name"
  while {[set i [lsearch -glob $lines $match]]>-0} {
    set lines [lreplace $lines $i $i]
    set fs [open $streamers a]
    puts $fs [join $lines "\n"]
    close $fs
    }
  }
}

proc list:feed {nick uhost hand chan text} {
global allcommands
  if {$chan == "#sops"} {
    if {[llength $feeds]} {
      puthelp "privmsg $chan :Available Streamers: [join $feeds {, }]"
    } {
      puthelp "privmsg $chan :No Available Streamers"
    }
  }
}

proc feed:feed { nick uhost chan handle arg } {
global host user passwd db
  if {$chan == "#sops"} {
    if {isop $nick $chan} { 
    set stream [lindex $text 0]
    set feed [list]
foreach donecmd [split [read [set fileid [open $streamers]]][close $fileid] \n][unset fileid] {
 if {[string match -nocase "$stream Stream URL - *" $donecmd]} {
  lappend feed [lindex [split $donecmd] 3]
 }
}
	set con [::mysql::connect -host $host -user $user -password $passwd]
  ::mysql::use $con $db
	::mysql::exec $con "UPDATE `streameronline` SET `StreamerUSC` = 'http://ustream.tv/e06,Q7h6Mbuey8aOxTltiQUyBKjIDFx4.usc'"
	puthelp "privmsg #sops :Update successfull"
        putlog "Update successfull"
      	::mysql::close $con     
        }
      }
    }

putlog "Script loaded: Stream Switcher"
User avatar
tomekk
Master
Posts: 255
Joined: Fri Nov 28, 2008 11:35 am
Location: Oswiecim / Poland
Contact:

Post by tomekk »

Hey

Code: Select all

# Author: tomekk 
# e-mail:  tomekk/@/oswiecim/./eu/./org 
# home page: http://tomekk.oswiecim.eu.org/ 
# 
# Version 0.1 
# 
# This file is Copyrighted under the GNU Public License. 
# http://www.gnu.org/copyleft/gpl.html

# file with users and urls
set db_file "my_db.txt"

############################################################
bind pub - !add add_proc
bind pub - !edit edit_proc
bind pub - !del del_proc
bind pub - !list list_proc
bind pub - !change change_proc

if {![file exists $db_file]} {
	set need_new_one [open $db_file w]
	close $need_new_one
}

proc grab_users { } {
	global db_file

	set db_hand [open $db_file r]
	set all_users [read $db_hand]
	close $db_hand
	
	return $all_users
}

proc check_user { user } {
	global db_file

	set exists 0

	foreach each_line [split [grab_users] "\n"] {
		set user_nick [lindex [split $each_line] 0]

		if {$user == $user_nick} {
			set exists 1
			break
		}
	}

	return $exists
}

proc add_proc { nick uhost hand chan arg } {
	global db_file

	set our_args [split $arg]
	set our_user [lindex $our_args 0]
	set our_user_url [lindex $our_args 1]

	if {$our_user_url != ""} {
		set db_hand [open $db_file a]
		puts $db_hand "$our_user $our_user_url"
		close $db_hand
	}
}

proc edit_proc { nick uhost hand chan arg } {
	global db_file

	set our_args [split $arg]
	set our_user [lindex $our_args 0]
	set our_user_url [lindex $our_args 1]

	if {$our_user_url != ""} {
		if {[check_user $our_user] == 1} {
			set db_users [grab_users]
				
			set new_file [open $db_file w]	
			foreach each_line [split $db_users "\n"] {
				if {$each_line != ""} {
					set split_line [split $each_line]
					set just_nick [lindex $split_line 0]
					set just_url [lindex $split_line 1]

					if {$just_nick == $our_user} {
						puts $new_file "$just_nick $our_user_url"
					} {
						puts $new_file "$just_nick $just_url"
					}
				}
			}
			close $new_file
		}
	}
}

proc del_proc { nick uhost hand chan arg } {
	global db_file

	set our_args [split $arg]
	set our_user [lindex $our_args 0]

	if {$our_user != ""} {
		if {[check_user $our_user] == 1} {
			set db_users [grab_users]

			set new_file [open $db_file w]
			foreach each_line [split $db_users "\n"] {
				if {$each_line != ""} {
					set split_line [split $each_line]
					set just_nick [lindex $split_line 0]
					set just_url [lindex $split_line 1]

					if {$just_nick != $our_user} {
						puts $new_file "$just_nick $just_url"
					}
				}
			}
			close $new_file
		}
	}
}

proc list_proc { nick uhost hand chan arg } {
	global db_file

	set user_list [list]

	foreach each_line [split [grab_users] "\n"] {
		if {$each_line != ""} {
			set split_line [split $each_line]
			set just_nick [lindex $split_line 0]

			lappend user_list $just_nick
		}
	}

	set rdy_list [join $user_list ", "]
	putquick "PRIVMSG $chan :$rdy_list"
}

proc change_proc { nick uhost hand chan arg } {
	global db_file

	set our_args [split $arg]
	set our_user [lindex $our_args 0]

	if {$our_user != ""} {
		if {[check_user $our_user] == 1} {
			foreach each_line [split [grab_users] "\n"] {
				if {$each_line != ""} {
					set split_line [split $each_line]
					set just_nick [lindex $split_line 0]
					set just_url [lindex $split_line 1]

					if {$just_nick == $our_user} {
						# delete this line
						# put here your MySQL query
						putquick "PRIVMSG $chan :$just_nick => $just_url"
					}
				}
			}
                }
	}
}

putlog "tiny-db.tcl ver 0.1 by tomekk loaded"
I just wrote, try it.

manual:
!add user http://somewhere.com/ - adding a user to a file (one by one, line by line)
!edit user http://some.new.url.com/ - updating user's url (if user exists)
!del user - deleting user from db, if there are more than one user with the same then script will delete all of them
!list - shows all users nicks from db on $chan
!change - this is the command for your MySQL

I don't want to install this stuff for MySQL,
I wrote u a comment in source,
just delete line with "putquick" and insert there some MySQL query,
user's url data is in "just_url" variable

Yoy can rename all bind etc.

cheers :)

P.S
or ignore this post if im wrong ;p
Post Reply