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.

using mashost/getchanhost

Help for those learning Tcl or writing their own scripts.
L
Luminous
Op
Posts: 146
Joined: Fri Feb 12, 2010 1:00 pm

using mashost/getchanhost

Post by Luminous »

I have seen people use these two commands in scripts to build the type of mask they want. I have a script I am working on that requires two mask types, and I am having problems setting them. What my script should do, is voice and adduser/chattr a user when a public command is called. So rather than having to voice them, then add them, then give them flag g, I can have a command that will do it all in one go.

Here's the problem... my script works... for me, but no one else. It works for me from webchat and regular clients, but for some reason, it won't do anything but voice other people. .adduser/chattr never happens for them. :( Currently, I am re-using a regexp from another script that outputs a similar mask to what I need, which I assume must be the problem. I am pretty sure there is an easier way to do this with maskhost/getchanhost:

Code: Select all

bind pub fo !voice voice_user
proc voice_user {nick uhost hand chan text} {
    set nicks [lrange $text 0 end]
    foreach n $nicks {
        if {[onchan $n $chan] && ![isvoice $n $chan]} {
               pushmode $chan +v $n
    set host [getchanhost $n $chan]
    set user [string trim $n {0123456789`~!@#$%^&*-_=+}]
        if {[regexp -nocase -- {@[[:alnum:]]+\.[[:alnum:]]+(\..+)} $host]} {
        regexp -nocase -- {@[[:alnum:]]+\.[[:alnum:]]+(\..+)} $host a b
            adduser $user *$user*!*@*$b
            chattr $user +g
             save
        } else {
    regexp -nocase -- {(@[-_\/\.:[:alnum:]]+)} $host a b
       adduser $user *$user*!*$a
       chattr $user +g
       save
            }
        }
    }
}
Now, those regexps... one look for webchat type of a host and the other looks for normal "domain.net" type of hosts. Reason I need similar in this script is because my network uses umode +x by defaul and scrambles hostnames. So sometimes hosts look like this: "webchat@24624475.11de379d.cable.virginmedia.com" and sometimes, they look like this: "Ruerin@3d344119.1377149a.189efabc.29655c0fX", which is why this is not so straight forward... I need one maskhost that will adduser the first type as: "*nick*!*@*.cable.virginmedia.com" and the second type like: "*nick*!*@*.29655c0fX". perhaps maskhos/getchanhost already does it like that, I am not sure...

Anyway... help to either get that regexp working properly or help to understand how to make masks with maskhost/getchanhost is greatly appreciated.

One more thing.. did I do this line properly? Does not seem like I did:

Code: Select all

set user [string trim $n {0123456789`~!@#$%^&*-_=+}]
I want to trim all other chars except A-Z from nicknames before adding them to my bot. So nicks like [123nick123] or similar will just be added as *nick*!*.
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

First off, don't use list operations on strings.. This one is especially pointless, as it returns the very same list you input (though the input in this case is not a list, but a string).

Code: Select all

set nicks [lrange $text 0 end]
If you wish to convert a string into a list, please use the split command.

Regarding your regular expressions, in the second case you don't verify that the pattern matches the user@host, so there's no guarantee $a is defined later on in the code. If you simply want to grab whatever's after (including) the @, I'd consider using something like this:

Code: Select all

if {[regexp -nocase -- {@[[:alnum:]]+\.[[:alnum:]]+(\..+)} $host a b} {
  adduser $user "*$user*!*@*$b"
} else {
  adduser $user "*$user*!*[string range $host [string first @ $host] end]"
}
chattr $user +g
save
Further, you should not escape characters within a bracket expression ([]), the only character requiring special attention is the hyphen (-), which must be first to be treated as a literal character, otherwize it's the range-operator, and brackets, which may be escaped using \

Now, regarding maskhost, the docs are pretty simple, enter a nick!user@xxx.host.tld, and you'll get something along the lines *!user@*.host.tld". Since you'd like to remove the "user" part, I'd suggest you either use string range/string first (like in the code above), or split/lindex to grab the part after the @, and prepend "*$user*!*@":

Code: Select all

set host [maskhost $user![getchanhost $n $chan]]
adduser $user "*$user*!*@[lindex [split $host @] 1]"
... or ...
adduser $user "*$user*!*[string range $host [string first @ $host] end]
Regarding string trim, this will only trim the outermost characters. In your case, you did not include the [] characters, so string trim finds no characters to remove, and you thus get the full nickname. As you might see, even adding those to the trim-chars wont help in the case of nick123name (it will not be trimmed down to nickname for the same reason). You could probably do this with a regsub though:

Code: Select all

set user [regsub -nocase -all --{[^a-z]} $n ""]
NML_375
L
Luminous
Op
Posts: 146
Joined: Fri Feb 12, 2010 1:00 pm

Post by Luminous »

All right, that all makes pretty good sense, thanks. :D Here's my updated code. paste.tclhelp.net does complain about that regsub: http://paste.tclhelp.net/?id=6f7

Something else I can try there, I hope?

Will that first regexp actually do what I want? I need one adduser proc that will make a mask like: *nick*!*@*29655c0fX for addresses like someone@3d344119.1377149a.189efabc.29655c0fX and then one that does a more traditional *nick*!*@*.domain.com for others' addresses that do not have just that one last scrambled IP block.
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Ahh, seems a space got lost in cyberspace...
Add a space between -- and {[... I'll update my post in a sec or two (fix also posted on the pastebin you linked)
NML_375
L
Luminous
Op
Posts: 146
Joined: Fri Feb 12, 2010 1:00 pm

Post by Luminous »

Aha, lol. Thank you very much. :) I'll test it here in a bit, hope it works.
L
Luminous
Op
Posts: 146
Joined: Fri Feb 12, 2010 1:00 pm

Post by Luminous »

Update: so far, so good. I got to test it on an @*.domain.net type of address earlier. So that part definitely works.

Now that I got this working, my !devoice proc is now valid and I can hopefully get this mode +q simulation trigger to work... I will likely need help with that though, having trouble with the details...
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

I noticed some improper/poor list operations in the code in the pastebin as well, posted an update with corrections and comments..
NML_375
L
Luminous
Op
Posts: 146
Joined: Fri Feb 12, 2010 1:00 pm

Post by Luminous »

Ah... thanks. :)

I will fix that.... in the meantime, I have noticed an issue with the regexp...

[!!Me] !voice Rancho
~!~ mode/#mychan [+v Rancho] by Mybot

Address of Rancho is: webchat@d1cf996.23e86861.3682fb09.18377e3cX

Now, when I .match Rancho, I get this:

HANDLE PASS NOTES FLAGS LAST
Rancho no 0 g never (nowhere)
HOSTS: *Rancho*!*@*.3682fb09.18377e3cX
] --- Found 1 match.

That @*.3682fb09.18377e3cX part is a problem, because the 3682fb09 part will change... I need it to be @*.18377e3cX, just the last part, when the address looks like that.
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Well, that's because it matches {@[[:alnum:]]+\.[[:alnum:]]+(\..+)}

I'd believe the best way to find a positive match for a real domain, is to look for a valid tld (2-4 alnum characters). Probably something along these lines:
{@.*(\.[[:alnum:]]+\.[[:alnum:]]{2,4})$}

If it matches, it's a "real" hostname, if it does not, it's a "scrambled" one. In the latter, split on . and get the last list item [lindex [split $host .] end]
NML_375
L
Luminous
Op
Posts: 146
Joined: Fri Feb 12, 2010 1:00 pm

Post by Luminous »

I assume there should be a "$host" where $ is... this seems to work so far for normal hosts so far. :) Wanted to run the script by you and make sure all looked good. There were a few bracket issues I had to work out, hehe: http://paste.tclhelp.net/?id=6fv

Edit: perhaps I did that wrong because it still adds the "scrambled" hosts the same way as it before; retrieving two slots. :(
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

No, that dollar-sign is a part of the regular expression, and represents the end of line. In your code, you've removed it, making the pattern match anything containing two to four digits after a dot, anywhere in the string - not the end of it. The $host should not be a part of the pattern, but a separate parameter to regexp.
NML_375
L
Luminous
Op
Posts: 146
Joined: Fri Feb 12, 2010 1:00 pm

Post by Luminous »

I don't understand what to do with $host then... http://paste.tclhelp.net/?id=6gb Adding it anywhere doesn't seem to work...
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Now you've got unbalanced brackets... Did you check my last post on the pastebin?

Let me separate the various components:

Code: Select all

set expression {@.*(\.[[:alnum:]]+\.[[:alnum:]]{2,4})$}
set string $host
set option1 -nocase
set option2 --
set var1 a
set var2 b

if {[regexp $option1 $option2 $expression $string $var1 $var2]} {
...
#the same as this, which is exactly what I posted on the pastebin:
if {[regexp -nocase -- {@.*(\.[[:alnum:]]+\.[[:alnum:]]{2,4})$} $host a b]} {
}
Obviously, you don't need to put each parameter in a variable, I simply did this to illustrate which part of the command is what... As I've already mentioned, in my later posts I only spoke of the regular expression (pattern), not the whole regexp command. The pattern should NOT include the variable $host, in fact, since it's enclosed with braces {}, there will in fact be no variable substitution (or command substitution for that matter), which further emphasize that you should not add $host to the pattern. As I said, within the pattern the $-sign is used to indicate the end of line, nothing else.
NML_375
L
Luminous
Op
Posts: 146
Joined: Fri Feb 12, 2010 1:00 pm

Post by Luminous »

Oh.. no, I hadn't seen your pastebin until now. :\ Okay, I see what you were saying now...

That is still giving me the same mask type though... @*.something.something.
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Then you are not using the code I posted. I did notice another typo in my earlier post, missing a *. in one of the hostmasks... Update posted now...
NML_375
Post Reply