-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathirc.py
176 lines (139 loc) · 4.65 KB
/
irc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import re
class rinIRC():
""" Rin IRC a libary rewriten from the go-irc libray for python
Parameters
-----------
rawLine: :class:`str`
Raw IRC string to prase
Return
-----------
`dict`
returns a dic of parse data
"""
def __init__(self, rawLine) -> None:
self.rawLine = rawLine
def replace_last(self, source_string:str, replace_what:str, replace_with:str) -> str:
"""Replace at the end of a string
Parameters
-----------
source_string: :class:`str`
String to replace
replace_what: :class:`str`
what to replace
replace_with: :class:`str`
what to replace with in string
Return
-----------
`str`
returns a string fixed
Taken from: http://stackoverflow.com/questions/3675318/ddg#3675423
"""
head, _sep, tail = source_string.rpartition(replace_what)
return head + replace_with + tail
def parseSource(self, rawSource:str) -> dict:
"""Parse the source of the IRC message
Parameters
-----------
rawSource: :class:`str`
The unparseed data
Return
-----------
source: ` dict[str, str]`
returns a dict of host, nickname and username
"""
source = {"Host": "", "Nickname": "", "Username": ""}
rawSource.replace(":", "", 1)
regex = re.compile(r"!|@")
splitS = regex.split(rawSource)
if len(splitS) == 0:
return source
if len(splitS) == 1:
source["Host"] = splitS[0]
elif len(splitS) == 2:
source["Nickname"] = splitS[0]
source["Host"] = splitS[1]
else:
source["Nickname"] = splitS[0]
source["Username"] = splitS[1]
source["Host"] = splitS[2]
return source
def parseTags(self, rawTags) -> dict:
"""Parse the tags of a irc message
Parameters
-----------
rawTags: :class:`str`
The unparseed data
Return
-----------
tags: ` dict`
returns a dict of tags sent in a message
"""
tags = {}
rawTags.replace("@", "", 1)
for tag in rawTags.split(";"):
pair = tag.split("=", 2)
key = pair[0]
value = ""
if len(pair) == 2:
value = self.parseIRCTagValue(pair[1])
tags[key] = value
return tags
def parseIRCTagValue(self, rawValue) -> str:
"""Parse the tags values of a irc message
Parameters
-----------
rawValue: :class:`str`
The unparseed data
Return
-----------
rawValue: ` str`
returns a fixed string
"""
tagEscapeCharacters = [("\s", " "), ("\\n", ""),
("\\r", ""), ("\:", ";"), ("\\\\", "\\")]
for escape in tagEscapeCharacters:
rawValue.replace(escape[0], escape[1])
rawValue = self.replace_last(rawValue, "\\", "")
rawValue = rawValue.strip()
return rawValue
def parseIRCMessage(self) -> dict:
"""Parse the raw IRC message
Parameters
-----------
line: :class:`str`
The unparseed data
Return
-----------
rawValue: ` str`
returns a fixed string
"""
message = {"Raw": self.rawLine, "Tags": "", "Params": "",
"Source": "", "Command": "", "Params": ""}
split = self.rawLine.split(" ")
index = 0
params = []
lineSplit = split[index]
if lineSplit.startswith("@"):
message["Tags"] = self.parseTags(split[index])
index += 1
if index >= len(split):
return message, 0 # Error(0): partial message
if split[index].startswith(":"):
message["Source"] = self.parseSource(split[index])
index += 1
if index >= len(split):
return message, 1 # Error(1): no command
message["Command"] = split[index]
index += 1
if index >= len(split):
return message, 2 # Error(2): No Prams
for i, v in enumerate(split[index:]):
if v.startswith(":"):
v = " ".join(split[index+i:])
v.replace(":", "", 1)
params.append(v.replace(":", "", 1))
break
else:
params.append(v.replace(":", "", 1))
message["Params"] = params
return message, None