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.

IRC nick regexp

Help for those learning Tcl or writing their own scripts.
Post Reply
User avatar
arfer
Master
Posts: 436
Joined: Fri Nov 26, 2004 8:45 pm
Location: Manchester, UK

IRC nick regexp

Post by arfer »

I consistently use the following regexp in my scripts to validate a nick entered with a command, for example !op <nick> to trigger a PUB bind that ops channel users.

Code: Select all

bind PUB o !op procname

proc procname {nick uhost hand chan text} {
  set target [string trim $text]
  if {[regexp -- {^[\x41-\x7D][-\d\x41-\x7D]*$} $target]} {
    # normal code here
  } else {
    # error code here
  }
}
This seems to work fine on the network I frequent but I have never really considered whether it is generally true for a wide range of IRC networks and, as a consequence, my scripts would not function as expected in other circumstances.

Is the regexp generally true or are there common networks that have other rules regarding the characters allowed in a nick?
I must have had nothing to do
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

Why do you need a regexp? why not directly use [onchan] on the target?
User avatar
arfer
Master
Posts: 436
Joined: Fri Nov 26, 2004 8:45 pm
Location: Manchester, UK

Post by arfer »

Fair point. Sorry, I seem to have elicited that response due to using an overly simplistic example, though for completeness it would still be reasonable to use both onchan and a nick checking regexp since they communicate to the command user the exact nature of an error.

Suppose I wanted to use a public command to send a network /WHOIS. In this case the target nick does not need to be in the command source channel. The target nick doesn't even need to be online since this would return a RAW 401 or 402 depending on the syntax of the /WHOIS command. In this case it might be useful to validate the target entered by the command user using the regexp.

Same question. Is it generally valid for common IRC networks?
I must have had nothing to do
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

I'd say that, apart from the nick length, most irc networks atleast adhere to rfc2812, some possibly rfc1459 as well.
rfc1459 wrote:<nick> ::= <letter> { <letter> | <number> | <special> }
...
<letter> ::= 'a' ... 'z' | 'A' ... 'Z'
<number> ::= '0' ... '9'
<special> ::= '-' | '[' | ']' | '\' | '`' | '^' | '{' | '}'
rfc2812 wrote:nickname = ( letter / special ) *8( letter / digit / special / "-" )
...
letter = %x41-5A / %x61-7A ; A-Z / a-z
digit = %x30-39 ; 0-9
...
special = %x5B-60 / %x7B-7D
; "[", "]", "", "`", "_", "^", "{", "|", "}"
As such, your regular expression permits more characters than either rfcs. Mainly, you allow characters such as "; : ? @" in your expression, which are not allowed within a nickname.
NML_375
User avatar
arfer
Master
Posts: 436
Joined: Fri Nov 26, 2004 8:45 pm
Location: Manchester, UK

Post by arfer »

I don't really see that those extra characters are allowed in the regexp pattern

; == \x3B
: == \x3A
? == \x3F
@ == \x40

All of which are outside the range \x41-\x7D
I must have had nothing to do
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Darn, you're right!
Must've overlooked that \d in there.. (and way too little coffee today)

As such, your regular expression should be in line with rfc2812 (although not rfc1459). Being rfc2812 the most common standard these days, I'd say it'll work well with most networks (might be worth adding note as to being rfc2812-compliant, if you're worried users will have issues on non-standard servers)
NML_375
User avatar
arfer
Master
Posts: 436
Joined: Fri Nov 26, 2004 8:45 pm
Location: Manchester, UK

[SOLVED] IRC nick regexp

Post by arfer »

Heh, OK. Many thanks. Coffee is a necessary tool for coding.
I must have had nothing to do
Post Reply