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.

Search and write file.

Help for those learning Tcl or writing their own scripts.
Post Reply
i
ipone
Voice
Posts: 13
Joined: Mon Mar 20, 2006 7:00 am

Search and write file.

Post by ipone »

Hey, I've just done a simple little code and It's working as planned. Although I would like to be able to have X host in the nicks.txt file so that if that same X joins the channel the new nickname of that X will be put in the same line. (associated with the old nicknames) BUT! here's the catch, if X joins with a nickname that is already in the list, I don't want it to be logged as a new nick in nicks.txt

Code: Select all

set nicklista "nicks.txt"

bind join - * join_onjoin

proc join_onjoin {nick uhost hand chan} {
 global nicklista
  set filename $nicklista
  set txt [open $filename "a"]
  puts $txt "$nick $uhost"
  close $txt
}
Has anyone got an idea?
User avatar
awyeah
Revered One
Posts: 1580
Joined: Mon Apr 26, 2004 2:37 am
Location: Switzerland
Contact:

Post by awyeah »

Let me get this straight, if you have an entries in your database, and someone with a different nick and uhost join the channel (corresponding to an old entry in the database with a different nick but same uhost), then if that is what you want you can do this:

Code: Select all

#read data from file
set fp [open $nicklista r]
set data [read -nonewline $fp]
close $fp
set datalist [split $data "\n"]

#if nick is already present in list, halt
foreach user $datalist {
 if {[string equal -nocase "[lindex [split $user] 0]" $nick]} {
  return 0
  }
}

#check if uhost is present in list
foreach host $datalist {
 if {[string equal -nocase "[lindex [split $host] 1]" $uhost]} {
  set host:found 1; break
  }
}

#if uhost present in list delete that line write the new nick and uhost to file
if {[info exists host:found]} {
 set user "$nick $host"
 set i [lsearch -exact [split [string tolower $datalist]] [split $user]]
 set datalist [lsort -unique [lreplace $datalist $i $i]]
 set fp [open $nicklista w]
 puts $fp [join $nicklista "\n"]
 close $fp
}
·­awyeah·

==================================
Facebook: jawad@idsia.ch (Jay Dee)
PS: Guys, I don't accept script helps or requests personally anymore.
==================================
i
ipone
Voice
Posts: 13
Joined: Mon Mar 20, 2006 7:00 am

Post by ipone »

Was almost that i where looking for. I was wondering something about this code..

Code: Select all

#if uhost present in list delete that line write the new nick and uhost to file
if {[info exists host:found]} {
 set user "$nick $host"
 set i [lsearch -exact [split [string tolower $datalist]] [split $user]]
 set datalist [lsort -unique [lreplace $datalist $i $i]]
 set fp [open $nicklista w]
 puts $fp [join $nicklista "\n"]
 close $fp
} 
That means that the newest nick are going to be in the database?

I want it to store the nicks associated whit the users uhost. for exampel

Iamwithstupid (test@some.host.eu.se) has joined #Testing
then parts and joins with a diffrent nick.
Buzz (test@some.host.eu.se) has joined #Testing

so it would look something like this i the database:
Buzz Iamwithstupid test@some.host.eu.se

Is it even possible? And thank you awyeah for your help so far, iam going to try the code you just wrote and see if i can change it somehow to store all the nicks and uhost on one line.
User avatar
awyeah
Revered One
Posts: 1580
Joined: Mon Apr 26, 2004 2:37 am
Location: Switzerland
Contact:

Post by awyeah »

Well then these won't work if you store 2 nicks and then one host, and then follow up, more nicks and so on for the same host.

Code: Select all

#if nick is already present in list, halt
foreach user $datalist {
 if {[string equal -nocase "[lindex [split $user] 0]" $nick]} {
  return 0
  }
}

#check if uhost is present in list
foreach host $datalist {
 if {[string equal -nocase "[lindex [split $host] 1]" $uhost]} {
  set host:found 1; break
  }
}
Since the database will check the first element for a nick and the second element for a uhost.

You should have a proper format for all database entries, so that they can be differentiated from nicks and uhosts. But then again maybe you can check for the entry with an @, which will be a uhost and then lsearch the entry number in the list and then lindex the list with that entry and get the uhost and then compare it.

However a proper differentiated format would be better. Something like:
nick:$uhost
nick1,nick2:$uhost
nick1,nick2,nick3:$uhost

Code: Select all

#If suppose $data is the total data in the file then read the data as a list
#and you can get the nicks and uhosts by:

set nicklist [list]
foreach entry $data {
 lappend nicklist [lindex [split $entry :] 0]
}

set uhostlist [list]
foreach entry $data {
 lappend uhostlist [lindex [split $entry :] 1]
}

#Then you check if the nick is not already in the list
#if nick is already present in list, halt

foreach user $nicklist {
 if {[string match -nocase "*$user*" $nick]} {
  set nickfound 1; break
  }
}
if {![info exists nickfound]} {
 return 0

#find the entry in the nicklist and uhostlist
} else {
 set findnick [lsearch -glob [split [string tolower $nicklist]] *$nick*]
 set finduhost [lsearch -glob [split [string tolower $uhostlist]] *uhost*]
}

#if everything is ok, then proceed
if {$findnick == $finduhost} {

#
#then you can use lreplace to replace the entry however you want
#
·­awyeah·

==================================
Facebook: jawad@idsia.ch (Jay Dee)
PS: Guys, I don't accept script helps or requests personally anymore.
==================================
i
ipone
Voice
Posts: 13
Joined: Mon Mar 20, 2006 7:00 am

Post by ipone »

thats look pretty sweet, but i dont really understand how the tcl file should look like for it to work? im new at this so can u help me with that? i want i to logg the nicks an uhost like nick1,nick2,nick3@$uhost everytime somone joins the channel.. or if someone else have an ide.
i
ipone
Voice
Posts: 13
Joined: Mon Mar 20, 2006 7:00 am

Post by ipone »

Now i have tested many things to make this work, and it just will not work :/
Post Reply