Skip to content

Instantly share code, notes, and snippets.

@AwwCookies
Created April 23, 2015 23:50
Show Gist options
  • Save AwwCookies/d4791602a825e2f1b9a4 to your computer and use it in GitHub Desktop.
Save AwwCookies/d4791602a825e2f1b9a4 to your computer and use it in GitHub Desktop.
irc parse
#!/usr/bin/env python
# -*- coding: utf-8 -*-
privmsg_msg = ':[email protected] PRIVMSG ##Aww-Test :Hello, World!'
join_msg = ':[email protected] JOIN ##Aww-Test'
part_msg = ':[email protected] PART ##Aww-Test :"good bye"'
quit_msg = ':[email protected] QUIT :Read error: Connection reset by peer'
notice_msg = ':[email protected] NOTICE Aww :Hello, World!'
mode_msg = ':[email protected] MODE ##Aww-Test +v Aww'
action_msg = ':[email protected] PRIVMSG ##Aww-Test :\x01ACTION Jumps\x01'
ctcp_msg = ':[email protected] PRIVMSG Aww :\x01VERSION\x01'
def parse(s):
try:
type = s.split()[1]
if type == "PRIVMSG":
if '\x01' in s:
if '\x01ACTION' in s:
return {
"nick": s.split('!')[0][1:],
"host": s.split('!')[1].split()[0],
"indent": s.split('!')[1].split()[0].split("@")[0],
"domain": s.split('!')[1].split()[0].split("@")[1],
"type": "ACTION",
"channel": s.split()[2],
"message": ' '.join(s.split()[3:])[1:],
}
else:
return {
"nick": s.split('!')[0][1:],
"host": s.split('!')[1].split()[0],
"indent": s.split('!')[1].split()[0].split("@")[0],
"domain": s.split('!')[1].split()[0].split("@")[1],
"type": "CTCP",
"channel": s.split()[2],
"message": ' '.join(s.split()[3:])[1:],
}
else:
return {
"nick": s.split('!')[0][1:],
"host": s.split('!')[1].split()[0],
"indent": s.split('!')[1].split()[0].split("@")[0],
"domain": s.split('!')[1].split()[0].split("@")[1],
"type": s.split()[1],
"channel": s.split()[2],
"message": ' '.join(s.split()[3:])[1:],
}
elif type == "JOIN":
return {
"nick": s.split('!')[0][1:],
"host": s.split('!')[1].split()[0],
"indent": s.split('!')[1].split()[0].split("@")[0],
"domain": s.split('!')[1].split()[0].split("@")[1],
"type": s.split()[1],
"channel": s.split()[2],
}
elif type == "PART":
return {
"nick": s.split('!')[0][1:],
"host": s.split('!')[1].split()[0],
"indent": s.split('!')[1].split()[0].split("@")[0],
"domain": s.split('!')[1].split()[0].split("@")[1],
"type": s.split()[1],
"channel": s.split()[2],
"message": ' '.join(s.split()[3:])[1:],
}
elif type == "QUIT":
return {
"nick": s.split('!')[0][1:],
"host": s.split('!')[1].split()[0],
"indent": s.split('!')[1].split()[0].split("@")[0],
"domain": s.split('!')[1].split()[0].split("@")[1],
"type": s.split()[1],
"message": ' '.join(s.split()[2:])[1:],
}
elif type == "NOTICE":
return {
"nick": s.split('!')[0][1:],
"host": s.split('!')[1].split()[0],
"indent": s.split('!')[1].split()[0].split("@")[0],
"domain": s.split('!')[1].split()[0].split("@")[1],
"type": s.split()[1],
"channel": s.split()[2],
"message": ' '.join(s.split()[3:])[1:],
}
else:
return {
"type": "UNKNOWN"
}
except:
return {
"type": "UNKNOWN"
}
# TEST
def test_privmsg():
assert parse(privmsg_msg)["nick"] == "Aww"
assert parse(privmsg_msg)["host"] == "[email protected]"
assert parse(privmsg_msg)["indent"] == "aww"
assert parse(privmsg_msg)["domain"] == "allyourcookiearebelongto.us"
assert parse(privmsg_msg)["type"] == "PRIVMSG"
assert parse(privmsg_msg)["channel"] == "##Aww-Test"
assert parse(privmsg_msg)["message"] == "Hello, World!"
def test_action():
assert parse(action_msg)["nick"] == "Aww"
assert parse(action_msg)["host"] == "[email protected]"
assert parse(action_msg)["indent"] == "aww"
assert parse(action_msg)["domain"] == "allyourcookiearebelongto.us"
assert parse(action_msg)["type"] == "ACTION"
assert parse(action_msg)["channel"] == "##Aww-Test"
assert parse(action_msg)["message"] == "\x01ACTION Jumps\x01"
def test_ctcp():
assert parse(ctcp_msg)["nick"] == "Aww"
assert parse(ctcp_msg)["host"] == "[email protected]"
assert parse(ctcp_msg)["indent"] == "aww"
assert parse(ctcp_msg)["domain"] == "allyourcookiearebelongto.us"
assert parse(ctcp_msg)["type"] == "CTCP"
assert parse(ctcp_msg)["channel"] == "Aww"
assert parse(ctcp_msg)["message"] == "\x01VERSION\x01"
def test_notice():
assert parse(notice_msg)["nick"] == "Aww"
assert parse(notice_msg)["host"] == "[email protected]"
assert parse(notice_msg)["indent"] == "aww"
assert parse(notice_msg)["domain"] == "allyourcookiearebelongto.us"
assert parse(notice_msg)["type"] == "NOTICE"
assert parse(notice_msg)["channel"] == "Aww"
assert parse(notice_msg)["message"] == "Hello, World!"
def test_join():
assert parse(join_msg)["nick"] == "Aww"
assert parse(join_msg)["host"] == "[email protected]"
assert parse(join_msg)["indent"] == "aww"
assert parse(join_msg)["domain"] == "allyourcookiearebelongto.us"
assert parse(join_msg)["type"] == "JOIN"
assert parse(join_msg)["channel"] == "##Aww-Test"
def test_part():
assert parse(part_msg)["nick"] == "Aww"
assert parse(part_msg)["host"] == "[email protected]"
assert parse(part_msg)["indent"] == "aww"
assert parse(part_msg)["domain"] == "allyourcookiearebelongto.us"
assert parse(part_msg)["type"] == "PART"
assert parse(part_msg)["channel"] == "##Aww-Test"
assert parse(part_msg)["message"] == "\"good bye\""
def test_quit():
assert parse(quit_msg)["nick"] == "Aww"
assert parse(quit_msg)["host"] == "[email protected]"
assert parse(quit_msg)["indent"] == "aww"
assert parse(quit_msg)["domain"] == "allyourcookiearebelongto.us"
assert parse(quit_msg)["type"] == "QUIT"
assert parse(quit_msg)["message"] == "Read error: Connection reset by peer"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment