Skip to content

Instantly share code, notes, and snippets.

@ishan-marikar
Last active August 29, 2015 14:26
Show Gist options
  • Save ishan-marikar/86ab61df308ed5316ab4 to your computer and use it in GitHub Desktop.
Save ishan-marikar/86ab61df308ed5316ab4 to your computer and use it in GitHub Desktop.
A regular expression that parses RFC2812 (IRC protocol)
# original work by Michael F. Lamb. License: GPLv3.
RFC2812Matcher = ///
^ # We'll match the whole line. Start.
# Optional prefix and the space that separates it
# from the next thing. Prefix can be a servername,
# or nick[[!user]@host]
(?::( # This whole set is optional but if it's
# here it begins with : and ends with space
([^@!\ ]*) # nick
(?: # then, optionally user/host
(?: # but user is optional if host is given
!([^@]*) # !user
)? # (user was optional)
@([^\ ]*) # @host
)? # (host was optional)
)\ )? # ":nick!user@host " ends
([^\ ]+) # IRC command (required)
# Optional args, max 15, space separated. Last arg is
# the only one that may contain inner spaces. More than
# 15 words means remainder of words are part of 15th arg.
# Last arg may be indicated by a colon prefix instead.
# Pull the leading and last args out separately; we have
# to split the former on spaces.
(
(?:
\ [^:\ ][^\ ]* # space, no colon, non-space characters
){0,14} # repeated up to 14 times
) # captured in one reference
(?:\ :?(.*))? # the rest, does not capture colon.
$ # EOL
///
parse = (line) ->
res = RFC2812Matcher.exec line
if (! res) return invalid: line
raw: line # Whole line
prefix: res[1] # complete prefix
nick: res[2] # or servername
username: res[3]
hostname: res[4]
command: res[5]
params: res[6]
.split ' '
.slice 1 # First char of args is always ' '
.concat if res[7] then res[7] else []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment