Skip to content

Commit 894b815

Browse files
committed
abstract_backends: safe() all prepare_command args
1 parent b6d1e9d commit 894b815

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed

sopel/irc/abstract_backends.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def prepare_command(self, *args: str, text: Optional[str] = None) -> str:
128128
and can be sent as-is.
129129
"""
130130
max_length = unicode_max_length = 510
131-
raw_command = ' '.join(args)
131+
raw_command = ' '.join(safe(arg) for arg in args)
132132
if text is not None:
133133
raw_command = '{args} :{text}'.format(args=raw_command,
134134
text=safe(text))
@@ -151,7 +151,7 @@ def send_ping(self, host: str) -> None:
151151
A ``PING`` command should be sent at a regular interval to make sure
152152
the server knows the IRC connection is still active.
153153
"""
154-
self.send_command('PING', safe(host))
154+
self.send_command('PING', host)
155155

156156
def send_pong(self, host: str) -> None:
157157
"""Send a ``PONG`` command to the server.
@@ -161,14 +161,14 @@ def send_pong(self, host: str) -> None:
161161
A ``PONG`` command must be sent each time the server sends a ``PING``
162162
command to the client.
163163
"""
164-
self.send_command('PONG', safe(host))
164+
self.send_command('PONG', host)
165165

166166
def send_nick(self, nick: str) -> None:
167167
"""Send a ``NICK`` command with a ``nick``.
168168
169169
:param nick: nickname to take
170170
"""
171-
self.send_command('NICK', safe(nick))
171+
self.send_command('NICK', nick)
172172

173173
def send_user(self, user: str, mode: str, nick: str, name: str) -> None:
174174
"""Send a ``USER`` command with a ``user``.
@@ -178,14 +178,14 @@ def send_user(self, user: str, mode: str, nick: str, name: str) -> None:
178178
:param nick: nickname associated with this user
179179
:param name: "real name" for the user
180180
"""
181-
self.send_command('USER', safe(user), mode, safe(nick), text=name)
181+
self.send_command('USER', user, mode, nick, text=name)
182182

183183
def send_pass(self, password: str) -> None:
184184
"""Send a ``PASS`` command with a ``password``.
185185
186186
:param password: password for authentication
187187
"""
188-
self.send_command('PASS', safe(password))
188+
self.send_command('PASS', password)
189189

190190
def send_join(self, channel: str, password: Optional[str] = None) -> None:
191191
"""Send a ``JOIN`` command to ``channel`` with optional ``password``.
@@ -194,17 +194,17 @@ def send_join(self, channel: str, password: Optional[str] = None) -> None:
194194
:param password: optional password for protected channels
195195
"""
196196
if password is None:
197-
self.send_command('JOIN', safe(channel))
197+
self.send_command('JOIN', channel)
198198
else:
199-
self.send_command('JOIN', safe(channel), safe(password))
199+
self.send_command('JOIN', channel, password)
200200

201201
def send_part(self, channel: str, reason: Optional[str] = None) -> None:
202202
"""Send a ``PART`` command to ``channel``.
203203
204204
:param channel: the channel to part
205205
:param text: optional text for leaving the channel
206206
"""
207-
self.send_command('PART', safe(channel), text=reason)
207+
self.send_command('PART', channel, text=reason)
208208

209209
def send_quit(self, reason: Optional[str] = None) -> None:
210210
"""Send a ``QUIT`` command.
@@ -228,15 +228,15 @@ def send_kick(
228228
:param nick: nickname to kick from the ``channel``
229229
:param reason: optional reason for the kick
230230
"""
231-
self.send_command('KICK', safe(channel), safe(nick), text=reason)
231+
self.send_command('KICK', channel, nick, text=reason)
232232

233233
def send_privmsg(self, dest: str, text: str) -> None:
234234
"""Send a ``PRIVMSG`` command to ``dest`` with ``text``.
235235
236236
:param dest: nickname or channel name
237237
:param text: the text to send
238238
"""
239-
self.send_command('PRIVMSG', safe(dest), text=text)
239+
self.send_command('PRIVMSG', dest, text=text)
240240

241241
def send_notice(self, dest: str, text: str) -> None:
242242
"""Send a ``NOTICE`` command to ``dest`` with ``text``.

test/irc/test_irc_abstract_backends.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,33 @@ def test_prepare_command_text_too_long():
4848
assert result == expected
4949

5050

51+
def test_prepare_command_command_safe():
52+
backend = MockIRCBackend(BotCollector())
53+
54+
result = backend.prepare_command(
55+
"PRIVMSG\r\nMODE #sopel +o Mallory\r\nPRIVMSG", "#sopel", text="Hello"
56+
)
57+
assert result == "PRIVMSGMODE #sopel +o MalloryPRIVMSG #sopel :Hello\r\n"
58+
59+
60+
def test_prepare_command_target_safe():
61+
backend = MockIRCBackend(BotCollector())
62+
63+
result = backend.prepare_command(
64+
"PRIVMSG", "#sopel\r\nMODE #sopel +o Mallory\r\nPRIVMSG #sopel", text="Hello"
65+
)
66+
assert result == "PRIVMSG #sopelMODE #sopel +o MalloryPRIVMSG #sopel :Hello\r\n"
67+
68+
69+
def test_prepare_command_text_safe():
70+
backend = MockIRCBackend(BotCollector())
71+
72+
result = backend.prepare_command(
73+
"PRIVMSG", "#sopel", text="Hello\r\nMODE #sopel +o Mallory"
74+
)
75+
assert result == "PRIVMSG #sopel :HelloMODE #sopel +o Mallory\r\n"
76+
77+
5178
def test_send_command():
5279
bot = BotCollector()
5380
backend = MockIRCBackend(bot)

0 commit comments

Comments
 (0)