Skip to content

Commit 2f8c068

Browse files
committed
clone_scanner.py 1.5: make script compatible with Python 3
1 parent f15287e commit 2f8c068

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

python/clone_scanner.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@
126126
# * Updated advertise option to include /script install clone_scanner.py
127127
# * Updated min version to 0.3.6, though this will prob change in the next version
128128
#
129+
### 2024-08-18: Sébastien Helleu:
130+
# * version 1.5: make script compatible with Python 3
131+
#
129132
## Acknowledgements:
130133
# * Sebastien "Flashcode" Helleu, for developing the kick-ass chat/IRC
131134
# client WeeChat
@@ -170,19 +173,18 @@
170173
#
171174
SCRIPT_NAME = "clone_scanner"
172175
SCRIPT_AUTHOR = "Filip H.F. 'FiXato' Slagter <fixato [at] gmail [dot] com>"
173-
SCRIPT_VERSION = "1.4"
176+
SCRIPT_VERSION = "1.5"
174177
SCRIPT_LICENSE = "MIT"
175178
SCRIPT_DESC = "A Clone Scanner that can manually scan channels and automatically scans joins for users on the channel with multiple nicknames from the same host."
176179
SCRIPT_COMMAND = "clone_scanner"
177180
SCRIPT_CLOSE_CB = "cs_close_cb"
178181

179-
import_ok = True
180-
181182
try:
182183
import weechat
184+
IMPORT_OK = True
183185
except ImportError:
184-
print "This script must be run under WeeChat."
185-
import_ok = False
186+
print("This script must be run under WeeChat.")
187+
IMPORT_OK = False
186188

187189
import re
188190
cs_buffer = None
@@ -262,7 +264,7 @@ def on_join_scan_cb(data, signal, signal_data):
262264
return weechat.WEECHAT_RC_OK
263265

264266
joined_nick = weechat.info_get("irc_nick_from_host", signal_data)
265-
join_match_data = re.match(':[^!]+!([^@]+@(\S+)) JOIN :?([#&]\S*)', signal_data)
267+
join_match_data = re.match(r':[^!]+!([^@]+@(\S+)) JOIN :?([#&]\S*)', signal_data)
266268
parsed_ident_host = join_match_data.group(1).lower()
267269
parsed_host = join_match_data.group(2).lower()
268270
if OPTIONS["compare_idents"] == "on":
@@ -274,7 +276,7 @@ def on_join_scan_cb(data, signal, signal_data):
274276
network_chan_name = "%s.%s" % (network, chan_name)
275277
chan_buffer = weechat.info_get("irc_buffer", "%s,%s" % (network, chan_name))
276278
if not chan_buffer:
277-
print "No IRC channel buffer found for %s" % network_chan_name
279+
weechat.prnt("", "No IRC channel buffer found for %s" % network_chan_name)
278280
return weechat.WEECHAT_RC_OK
279281

280282
if OPTIONS["display_join_messages"] == "on":
@@ -293,8 +295,8 @@ def on_join_scan_cb(data, signal, signal_data):
293295
if clones:
294296
key = get_validated_key_from_config("clone_onjoin_alert_key")
295297

296-
filtered_clones = filter(lambda clone: clone['nick'] != joined_nick, clones[hostkey])
297-
match_strings = map(lambda m: format_from_config(m[key], "colors.onjoin_alert.matches"), filtered_clones)
298+
filtered_clones = [clone for clone in clones[hostkey] if clone['nick'] != joined_nick]
299+
match_strings = [format_from_config(m[key], "colors.onjoin_alert.matches") for m in filtered_clones]
298300

299301
join_string = format_from_config(' and ',"colors.onjoin_alert.message")
300302
masks = join_string.join(match_strings)
@@ -347,7 +349,7 @@ def get_channel_from_buffer_args(buffer, args):
347349
if not channel_name:
348350
channel_name = weechat.buffer_get_string(buffer, "localvar_channel")
349351

350-
match_data = re.match('\A(irc.)?([^.]+)\.([#&]\S*)\Z', channel_name)
352+
match_data = re.match(r'\A(irc.)?([^.]+)\.([#&]\S*)\Z', channel_name)
351353
if match_data:
352354
channel_name = match_data.group(3)
353355
server_name = match_data.group(2)
@@ -360,7 +362,7 @@ def get_clones_for_buffer(infolist_buffer_name, hostname_to_match=None):
360362
infolist = weechat.infolist_get("irc_nick", "", infolist_buffer_name)
361363
while(weechat.infolist_next(infolist)):
362364
ident_hostname = weechat.infolist_string(infolist, "host")
363-
host_matchdata = re.match('([^@]+)@(\S+)', ident_hostname)
365+
host_matchdata = re.match(r'([^@]+)@(\S+)', ident_hostname)
364366
if not host_matchdata:
365367
continue
366368

@@ -388,7 +390,7 @@ def get_clones_for_buffer(infolist_buffer_name, hostname_to_match=None):
388390
weechat.infolist_free(infolist)
389391

390392
#Select only the results that have more than 1 match for a host
391-
return dict((k, v) for (k, v) in matches.iteritems() if len(v) > 1)
393+
return dict((k, v) for (k, v) in matches.items() if len(v) > 1)
392394

393395
def report_clones(clones, scanned_buffer_name, target_buffer=None):
394396
# Default to clone_scanner buffer
@@ -405,7 +407,7 @@ def report_clones(clones, scanned_buffer_name, target_buffer=None):
405407
clone_report_header = format_from_config(clone_report_header, "colors.clone_report.header.message")
406408
weechat.prnt(target_buffer, clone_report_header)
407409

408-
for (host, clones) in clones.iteritems():
410+
for (host, clones) in clones.items():
409411
host_message = "%s %s %s %s" % (
410412
format_from_config(host, "colors.clone_report.subheader.host"),
411413
format_from_config("is online from", "colors.clone_report.subheader.message"),
@@ -476,7 +478,7 @@ def remove_hooks():
476478
weechat.unhook(hook)
477479
hooks = set([])
478480

479-
if __name__ == "__main__" and import_ok:
481+
if __name__ == "__main__" and IMPORT_OK:
480482
if weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, SCRIPT_DESC, SCRIPT_CLOSE_CB, ""):
481483
version = weechat.info_get("version_number", "") or 0
482484
if int(version) >= 0x00030600:

0 commit comments

Comments
 (0)