mIRC Homepage
Posted By: schaefer31 Help with $read - 27/08/05 11:02 PM
Hi, I have a text file that keeps a list of username and ips. In the list are some duplicate names but with a different ip. I have a command !search <name> that searches the text for the name and then messages the result to the channel. But the problem is that it only returns the first result found and not all of them. How can it be changed to return all results found?

Example

[Joe] !search Joe
[Me] 3 results found for 'Joe'
[Me] Joe - 127.0.0.1
[Me] Joe - 192.168.1.1
[Me] Joe - 192.168.254.254
[Me] Finished search database.

Here is my code

on 1:text:!search *:#mychannel:{
msg #mychannel $read(database.txt,w,$2)
}

I looked in mirc help but I did not find anything that helps, I am not very good at mirc scripting.


Also, how can I make an autoresponse in query if the first character of a message is not an exclamation point ( ! ).

I tried this but it doesn't work

on 1:text:*:?:{
if ($right($1,1) != $chr(33)) { msg $nick You are talking to a bot. }
}
Posted By: bynw Re: Help with $read - 27/08/05 11:54 PM
I'm not sure if that can be done with $read. As it only finds the 1st instance of the search parameter. You COULD put all of the ips after one another in the text file.

Like joe - 127.0.0.1, 192.168.1.1, 192.168.254.254

Then it would return all of them.
Posted By: schaefer31 Re: Help with $read - 28/08/05 12:05 AM
Well if not $read then is there something else that can do it? I would rather keep the format the same rather than put all IP in one line. The list is big and only gets bigger, I would have to manually change the format because it puts the IP in the list automatically on join.
Posted By: Lpfix5 Re: Help with $read - 28/08/05 03:23 AM
Quote:
Well if not $read then is there something else that can do it? I would rather keep the format the same rather than put all IP in one line. The list is big and only gets bigger, I would have to manually change the format because it puts the IP in the list automatically on join.


Maybe not read but $readini yes for example you would write a new ini or too ini with this

/writeini -n <inifile> <section> <item> <value>

example

/writeini -n test.ini JOE 1 194.492.49.34
/writeini -n test.ini JOE 2 293.492.49.32

you can make that a script if you want smile so now you've create a ini with a Section called [JOE] and with 2 values in it which is the 2 ips you wrote.....

then to read the whole list back to channel you create a varible script with ini calc and $readini script as follows, note this is a cheap one just to show you the principal of it, im sure your a good script'er(sp?) you know the drill change channel names or make variables etc...
Code:
on *:TEXT:!search*:*: {
  %iplist.inc = 1
  %iplist.name = $2
  %iplist.inicalc = $ini(test.ini,%iplist.name,0)
  msg #mychan Scanning ( $+ %iplist.name $+ ) $+ ... please wait!
  .timernoresult 1 5 //msg #mychan No Results found for ( $+ %iplist.name $+ ) $+ .
  :NEXT
  msg #mychan $readini(test.ini,%iplist.name,%iplist.inc)
  if (%iplist.inc = %iplist.inicalc) { goto END }
  else {
    .timernoresult off
    inc %iplist.inc
    goto NEXT
  }
  :END
  msg #mychan ( $+ %iplist.inicalc $+ ) results were found on ( $+ %iplist.name $+ )
  unset %iplist*
}


***************UNTESTED***************
im almost next to certain its what your aiming for :P
*******************************************
Posted By: schaefer31 Re: Help with $read - 28/08/05 04:39 AM
Your script works how I described, but not entirely.

It works only if you first do the /writeini which is a problem for me, because there are already many many lines of names and ip in a text file, and I would have to convert them all to the format you're using. It also does not auto-increment the line # and I'd have to remember what the next line # is. This would be a tedious process. I'd rather keep the format I already have.

I know there must be some way to do this, as I've seen similar things in other channels but the people were not very willing to help.

Sorry, I do not know much about mirc scripting so I am a bit clueless as to what to do.
Posted By: Lpfix5 Re: Help with $read - 28/08/05 05:08 AM
Quote:
Your script works how I described, but not entirely.

It works only if you first do the /writeini which is a problem for me, because there are already many many lines of names and ip in a text file, and I would have to convert them all to the format you're using. It also does not auto-increment the line # and I'd have to remember what the next line # is. This would be a tedious process. I'd rather keep the format I already have.

I know there must be some way to do this, as I've seen similar things in other channels but the people were not very willing to help.

Sorry, I do not know much about mirc scripting so I am a bit clueless as to what to do.


thats why i said you can make an alias for add example... to have an alias incremiment the line and all you do is type

/ipadd nickname iphere

Code:
alias ipadd {
  %ipadd.nick = $1
  %ipadd.inicalc = $ini(test.ini,%ipadd.nick,0)
  if (%ipadd.inicalc = 0) { writeini test.ini %ipadd.nick 1 $2- }
  elseif (%ipadd.inicalc &gt; 0) { %ipadd.inicalc = $calc( 1 + $ini(test.ini,%ipadd.nick,0)) 
    writeini test.ini %ipadd.nick %ipadd.inicalc $2-
  }


again this can be put in a longer script another example

this is for a bot just an example again

on 100:text:*:?: {
if ($1 == ADD) { $ipadd $2 $3 }
}

theres various ways to do it...

a text file is bulky plus if.. you do writeini it writes new sections in brackets making it easier to read
Posted By: Kelder Re: Help with $read - 28/08/05 02:18 PM
Code:
on 1:text:!search *:#mychannel:{
  var %rn = 1
  while ($read(database.txt,w,$2, %rn)) {
    msg $chan $v1
    var %rn = $readn + 1
  }
  msg $chan Finished database search.
}


Doesn't give you the number of results though.
Posted By: schaefer31 Re: Help with $read - 28/08/05 03:46 PM
Lpfix5, I think I've found a way to adapt the database to the script you've made, but have a couple questions.

When I do /ipadd for a Name that does not already exist in the list, it adds the [Name] section but then adds the IP twice, so it does

[Name]
0=123.123.123.123
1=123.123.123.123

I don't really understand your script fully so I don't know how to fix this.


Also Is it possible to check for duplicates when doing the /ipadd ? If there's a duplicate then it sends a notice to self and does not add the ip.
Posted By: Lpfix5 Re: Help with $read - 28/08/05 04:02 PM
Quote:
Lpfix5, I think I've found a way to adapt the database to the script you've made, but have a couple questions.

When I do /ipadd for a Name that does not already exist in the list, it adds the [Name] section but then adds the IP twice, so it does

[Name]
0=123.123.123.123
1=123.123.123.123

I don't really understand your script fully so I don't know how to fix this.


Also Is it possible to check for duplicates when doing the /ipadd ? If there's a duplicate then it sends a notice to self and does not add the ip.


Yeah there is btw i like people like atleast you profile says newbie but you try smile!

here you go mate

Code:
alias ipadd {
  %ipadd.nick = $1
  %ipadd.inicalc = $ini(test.ini,%ipadd.nick,0)
  %ipadd.ip = $2
  if (%ipadd.ip iswm $readini(test.ini,%ipadd.nick,%ipadd.inicalc)) { echo -a --**IP is already found in list**-- }
  if (%ipadd.inicalc = 0) { writeini test.ini %ipadd.nick 1 $2- }
  elseif (%ipadd.inicalc &gt; 0) { %ipadd.inicalc = $calc( 1 + $ini(test.ini,%ipadd.nick,0)) 
    writeini test.ini %ipadd.nick %ipadd.inicalc $2-
  }
unset %ipadd*
}


O i c you just edited your post, anyhoo it shouldnt duplicate if you have my recent code and not the first one i posted which you may have because i edited my post i forgot something important..

see this is what happens when i

/ipadd JOE 304.24.4.4
/ipadd david 203.233.532.55
/ipadd doug 320.320.244.9
/ipadd andre 034.42.42.42

it returns ini ini file

[JOE]
1=304.24.4.4
[david]
1=203.233.532.55
[doug]
1=320.320.244.9
[andre]
1=034.42.42.42

however if i do those commands again i get a notice because of same ip

but... if i add a new ip to nickname then it becomes second line not duplicate
Posted By: schaefer31 Re: Help with $read - 28/08/05 04:20 PM
Hey, it still has small problem. When adding a duplicate IP it does give a notice, but it still writes the duplicate IP to the ini file. Everything else works nice!

I do try to fix if I know how, but I am only just starting to learn mirc scripting, so I don't know much yet.

Thanks for helping.
Posted By: Lpfix5 Re: Help with $read - 28/08/05 04:27 PM
oops i didnt click on edit button it created a second file sorry
Posted By: Lpfix5 Re: Help with $read - 28/08/05 04:28 PM
Here's a breakdown of my script for you smile note all my commands that are written below the values are meant for on top of the note like alias ipadd {
; sflkfsajfsalkfasfas will be meant for alias ipadd {

alias ipadd {
; create a new alias named ipadd to be used with /ipadd
%ipadd.nick = $1
; Creates a variable with the first word said after /ipadd in this case its /ipadd nickname
%ipadd.inicalc = $ini(test.ini,%ipadd.nick,0)
; Calculates the lines that are in the test.ini at section %ipadd.nick which is the variable on top which means again its the nick you type by adding a ,0 after it it caculates the lines total only in that section
%ipadd.ip = $2
; This is the new section you wanted , variable created so it captures ip ex:. /ipadd nickname 127.0.0.1
if (%ipadd.ip iswm $readini(test.ini,%ipadd.nick,%ipadd.inicalc)) { echo -a --**IP is already found in list**-- }
; this means if ip is wildcard match in the test.ini in section [nickname] it echo's in active window --**IP is already found in list**-- <<<fully customizable btw.
if (%ipadd.inicalc = 0) { writeini test.ini %ipadd.nick 1 $2- }
; if the calculation of test.ini has NO nickname registered when you do /ipadd completenewnickname iphere. it then at this times write's the test.ini with nickname and automatically assigns it position 1 in a new section
elseif (%ipadd.inicalc > 0) { %ipadd.inicalc = $calc( 1 + $ini(test.ini,%ipadd.nick,0))
writeini test.ini %ipadd.nick %ipadd.inicalc $2-
}
; else... if my ini calculation has a value 1 2 3 4 .... like
[nick]
1=blah
2=blah
and is set has if larger then 0 << by looking at the other if command on top you will understand where 0 comes from, then perform this command... so it takes the calculation of lines in the section of the ini example 3 and does a simple 1st grade calculation 3 + 1 which 3 would be the lines that already exist, then by having it automatically calculate one it will write the new 4th value...

unset %ipadd*
; unsets all variables that begin with %ipadd
}

enjoy and have fun scripting

um it shouldnt overwrite... the only reason why i assume it overwrites if your mIRC is sending 2 text at same time do to maybe something like a ON input script or... you have 2 identical scripts make sure alias ipadd is only one... just to try a test rename alias ipadd to ipaddtest and try /ipaddtest ............
Posted By: schaefer31 Re: Help with $read - 28/08/05 04:44 PM
Thanks much for explaining!

But I think maybe you misunderstood me.

Your script works great except for when I try to do /ipadd Name IP.

If the IP is a duplicate IP, it echos the message that it is a duplicate however it will still write the duplicate IP to the ini file.
Posted By: Lpfix5 Re: Help with $read - 28/08/05 04:49 PM
Quote:
Thanks much for explaining!

But I think maybe you misunderstood me.

Your script works great except for when I try to do /ipadd Name IP.

If the IP is a duplicate IP, it echos the message that it is a duplicate however it will still write the duplicate IP to the ini file.



oh i c i just noticed why just looking at real quickly this is the original line
Code:
if (%ipadd.ip iswm $readini(test.ini,%ipadd.nick,%ipadd.inicalc)) { echo -a --**IP is already found in list**-- }


it should be halted within the same command sorry about that

new code
[code]
if (%ipadd.ip iswm $readini(test.ini,%ipadd.nick,%ipadd.inicalc)) { echo -a --**IP is already found in list**-- | halt }
Posted By: schaefer31 Re: Help with $read - 28/08/05 04:58 PM
Thank you very much! It works exactly how I need it to now smile

Your help is very much appreciated.
Posted By: Lpfix5 Re: Help with $read - 28/08/05 04:59 PM
Quote:
Thank you very much! It works exactly how I need it to now smile

Your help is very much appreciated.


Your very much welcomed smile happy scripting
Posted By: schaefer31 Re: Help with $read - 28/08/05 09:13 PM
Hmm, I've just encountered another problem with it adding duplicates.

When the /ipadd is performed, it appears to check only the newest entry and if it matches the newest entry then it does not add the IP. But if the newest entry is not the same, but entries before the new are match the one being added, it will still add it as a duplicate.

Lol, I don't know if you can understand this or not as I just read it to myself and it's quite confusing. So just in case I'll try a better explanation:

In the database.ini file is the following (example)

[Joe]
1=123.123.123.123
2=1.1.1.1
3=1.2.3.4

When I do the /ipadd it only checks if the 3= is a duplicate of the one being added, and does not check any of the entries before it. So for example if I were to do /ipadd Joe 1.1.1.1 it would add it (even though it already exists as the 2nd entry) because the 3rd entry is not 1.1.1.1 and it completely ignores the first 2 entries.

I've not made any changes to the code other than the ini file name and the channel.
Posted By: Lpfix5 Re: Help with $read - 28/08/05 09:48 PM
Sorry about that this should work better we will use the $read only this time and do a if ip is in text file which is the ini in theory then return echo blah blah

here is the full alias script

Code:
alias ipadd {
  %ipadd.nick = $1
  %ipadd.inicalc = $ini(test.ini,%ipadd.nick,0)
  %ipadd.ip = $2
  if (%ipadd.ip isin $read(test.ini,w, * $+ %ipadd.ip $+ *)) { echo -a --**IP is already found in list**-- | halt }
  if (%ipadd.inicalc = 0) { writeini test.ini %ipadd.nick 1 $2- }
  elseif (%ipadd.inicalc &gt; 0) { %ipadd.inicalc = $calc( 1 + $ini(test.ini,%ipadd.nick,0)) 
    writeini test.ini %ipadd.nick %ipadd.inicalc $2-
  }
  unset %ipadd*
}


and thanks for pointing that out i was using something similar i just built awhile ago
Posted By: Riamus2 Re: Help with $read - 29/08/05 01:55 PM
Just as a note, from your original script for your original text file of data, you can just do this:

Code:
on 1:text:!search *:#mychannel:{
  var %lines = $lines(database.txt)
  var %currentline = 1
  var %total = 0
  while (%currentline &lt;= %lines) {
    var %data = $read(database.txt,w,$2 $+ *,%currentline)
    if (%data != $null) {
      msg #mychannel %data
      inc %total
    }
    var %currentline = $calc($readn + 1)
  }
  msg $mychannel %total found.
}


Using the 4th parameter (a line number) in $read, lets you continue your search starting from a specific line. $readn tells you the most recent line you $read. So, adding 1 to that in your loop will let you continue on.
Posted By: schaefer31 Re: Help with $read - 29/08/05 04:11 PM
Thanks for suggestion, I saved it in case I might have use for it later.

Though, I did a brief test and it caused an excess flood after returning the first line.
Posted By: Riamus2 Re: Help with $read - 29/08/05 04:19 PM
Try changing msg to echo and see what your output is. Maybe you just have too many results trying to be displayed too quickly. In that case, you'd want to play the results.
Posted By: schaefer31 Re: Help with $read - 29/08/05 04:41 PM
I thought it might be too many also, so I made a temp file with only a few results and it still caused excess flood.

When I try echo it locks up the mirc process.

Edit:
I don't know if it matters but the lines of the text file are in this format

Name - IP
Name2 - IP2
Name3 - IP3
etc etc
Posted By: Riamus2 Re: Help with $read - 29/08/05 04:59 PM
Code:
on 1:text:!search *:#mychannel:{
  var %search = $2
  var %lines = $lines(database.txt)
  var %currentline = 1
  var %total = 0
  while (%currentline &lt;= %lines) {
    var %data = $read(database.txt,w,%search $+ *,%currentline)
    if (%data != $null) {
      msg #mychannel %data
      inc %total
    }
    var %currentline = $calc($readn + 1)
  }
  msg $mychannel %total found.
}


That will fix it. I forgot that $N's don't work in a loop.
Posted By: schaefer31 Re: Help with $read - 29/08/05 05:31 PM
It still causes excess flood/locks up. I was able to quickly switch to the mirc process which the trigger is on and notice it endlessly echo'd the same results. I had to end process on it to kill it.
Posted By: Riamus2 Re: Help with $read - 29/08/05 05:40 PM
Can you give me an example of how you're searching and of what lines you're expecting to see from the search? It's working fine here, though it is definitely fast enough to cause an excess flood if you're getting more than a few results.

Here's the code with /play so that you won't get excess flood with too many results being found. Though, something isn't right if you're getting it stuck on one thing. You do have the $readn line in there, right?

Code:
on 1:text:!search *:#mychannel:{
  var %search = $2
  var %lines = $lines(database.txt)
  var %currentline = 1
  var %total = 0
  while (%currentline &lt;= %lines) {
    var %data = $read(database.txt,w,%search $+ *,%currentline)
    if (%data != $null) {
      write temp.txt %data
      inc %total
    }
    var %currentline = $calc($readn + 1)
  }
  write temp.txt %total found.
  play temp.txt 1500
  .remove temp.txt
}


Also, you can do this just for testing purposes... right after the inc %total line, add:

if (%total >= 10) { halt }

Then, it will stop after 10 rounds. You can then check your out from echo/msg, or you can open temp.txt if you're using /play from this updated script.

Let me know your search command you used, the results you expect to see, and the results you do see after using the halt line above.
Posted By: schaefer31 Re: Help with $read - 29/08/05 08:32 PM
As previously said, in the text file the lines are formatted like so:

Name1 - IP1
Name1 - IP2
Name1 - IP3
Name2 - IP1
Name2 - IP2

I used a temp file which only has these 5 lines.

In the channel I do the trigger '!search Name1' (I expect it to return any lines which contain 'Name1' in the first field) and it returns the first result 'Name1 - IP1' from the text file and then immediately afterwards is flooded off the server.

From your code, I made only the obvious change (the channel name). I replaced all 3 instances of 'mychannel' with the actual channel.
Posted By: Riamus2 Re: Help with $read - 29/08/05 09:03 PM
Ok, I'm beginning to feel stupid today... this makes 2 scripts with stupid mistakes in them. That's what I get for trying to code snippets in the middle of writing a mIRC RPG. smile

This will work for you. I wasn't incrementing properly. I also didn't give the proper /play syntax.

Code:
on 1:!search *:#mychannel: {
  var %search = $2
  var %lines = $lines(database.txt)
  var %currentline = 1
  var %total = 0
  while (%currentline &lt;= %lines) {
    var %data = $read(database.txt,w,%search $+ *,%currentline)
    if (%data != $null) {
      write temp.txt %data
      inc %total
      var %currentline = $readn
    }
    inc %currentline
  }
  write temp.txt %total found.
  play $chan temp.txt 1500
  .remove temp.txt
}
Posted By: schaefer31 Re: Help with $read - 29/08/05 09:10 PM
Hehe, you forgot to add on 1: text :

But I noticed and corrected it. Thanks to you also for helping, I will find ways to use this for sure.
Posted By: Lpfix5 Re: Help with $read - 29/08/05 10:20 PM
Quote:
Thanks for suggestion, I saved it in case I might have use for it later.

Though, I did a brief test and it caused an excess flood after returning the first line.


Did the script work for you the last one i wrote?
Posted By: schaefer31 Re: Help with $read - 29/08/05 10:22 PM
Yes, both scripts work just fine. Thanks again.
Posted By: Riamus2 Re: Help with $read - 30/08/05 02:05 AM
Quote:
Hehe, you forgot to add on 1: text :

But I noticed and corrected it. Thanks to you also for helping, I will find ways to use this for sure.


Lol. I was using it as an alias to test it since I couldn't connect to IRC from where I was writing it and I missed that when converting it back to on text. smile
Posted By: schaefer31 Re: Help with $read - 30/08/05 10:22 PM
One more question...

Assuming the same format of the database in the text file:

Name1 - IP
Name1 - IP2
Name1 - IP3
Name2 - IP1
Name2 - IP2
etc etc

If I do "!search ame" is it possible to make it search only the first field (names) for any line that contains "ame" and return then as a result? It's important that it only searches for "ame" in the first field and ignores the " - IP" part as I could do "!search 2" and it would return any line that contains a 2 in the IP. I don't want it to do that. I only want it to return lines that contain a 2 in the NAME.
Posted By: Riamus2 Re: Help with $read - 01/09/05 12:46 PM
Code:
on 1:text:!search *:#mychannel: {
  var %search = $2
  var %lines = $lines(database.txt)
  var %currentline = 1
  var %total = 0
  while (%currentline &lt;= %lines) {
    var %data = $read(database.txt,w,* $+ %search $+ *-*,%currentline)
    if (%data != $null) {
      write temp.txt %data
      inc %total
      var %currentline = $readn
    }
    inc %currentline
  }
  write temp.txt %total found.
  play $chan temp.txt 1500
  .remove temp.txt
}


That code will allow you to do what you want.

Examples:

Text file includes:

this is a test - 123.345.567.789
this is not a test - 234.546.123.56
neither is this - 546.234.123.64
44 but this is a test - 354.78.23.1
487 - 784.354.487.64

!search this
* returns the first 4 lines

!search test
* returns lines 1,2,4

!search 4
* returns lines 4,5

As a note, however, your first section cannot contain a hypen in it like this:

let's-go-do-this - 234.36.123.34

If you want to allow hyphens in the first section, change the - divider between the two sections to something that won't be used in the first section and then change the hyphen in *-* to whatever you're using.
© mIRC Discussion Forums