-
Notifications
You must be signed in to change notification settings - Fork 235
/
yourls.py
166 lines (140 loc) · 4.81 KB
/
yourls.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
"""yourls url shortener for irssi"""
import asyncio
import cProfile
import json
import re
import threading
from urllib import request, parse
import irssi
DEBUG = False
__version__ = "1.0.0"
IRSSI = {
"authors": "terminaldweller",
"contact": "https://terminaldweller.com",
"name": "yourls",
"description": "uses yourls to shorten urls",
"license": "GPL3 or newer",
"url": "https://github.com/irssi/scripts.irssi.org",
}
def yourls_request(req, target: bytes, content: str) -> None:
"""async wrapper for urllib.request.urlopen"""
timeout = irssi.settings_get_int(b"yourls_timeout")
with request.urlopen(req, timeout=timeout) as resp:
json_response = json.load(resp)
if DEBUG:
print(json_response)
short_url = json_response["shorturl"]
if DEBUG:
print(short_url)
url_pattern = r"(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))"
new_content = re.sub(url_pattern, short_url, content)
window = irssi.window_find_item(target)
window.prnt(new_content.encode("utf-8"))
def yourls_command_handler(content: bytes, target: bytes) -> None:
"""handle the command"""
yourls_url = irssi.settings_get_str(b"yourls_server_url").decode("utf-8")
yourls_token = irssi.settings_get_str(b"yourls_secret_sig_token").decode("utf-8")
yourls_min_length = irssi.settings_get_int(b"yourls_min_length")
yourls_format = irssi.settings_get_str(b"yourls_format").decode("utf-8")
yourls_action = irssi.settings_get_str(b"yourls_action").decode("utf-8")
url_pattern = r"(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))"
result = re.findall(url_pattern, content.decode("utf-8"))
if len(result) == 0:
return
if len(result[0][0]) < yourls_min_length:
return
form_fields = {
"url": result[0][0],
}
data = parse.urlencode(form_fields).encode("utf-8")
params = {
"signature": yourls_token,
"action": yourls_action,
"format": yourls_format,
"url": result[0][0],
}
url = yourls_url + "?" + parse.urlencode(params, doseq=True, safe=":/?&=")
if DEBUG:
print(url)
req = request.Request(url, method="POST")
# yourls_request(req, target, content.decode("utf-8"))
threading.Thread(
target=yourls_request(req, target, content.decode("utf-8"))
).start()
def yourls_signal_handler(*args, **kwargs) -> None:
"""handle the message signal"""
server = args[0]
msg = args[1]
_ = args[2]
address = args[3]
target = args[4]
yourls_url = irssi.settings_get_str(b"yourls_server_url").decode("utf-8")
yourls_operation_mode = irssi.settings_get_str(b"yourls_operation_mode").decode(
"utf-8"
)
yourls_names = irssi.settings_get_str(b"yourls_name_list").decode("utf-8")
yourls_name_list = yourls_names.split(" ")
current = server.tag.decode("utf-8") + "/" + target.decode("utf-8")
if yourls_url == "":
return
for name in yourls_name_list:
if re.match(name, current):
if yourls_operation_mode == "whitelist":
break
elif yourls_operation_mode == "blacklist":
return
else:
print("invalid operation mode: must be whitelist or blacklist")
return
if DEBUG:
profiler = cProfile.Profile()
profiler.runctx("yourls_command_handler(msg, target)", globals(), locals())
profiler.print_stats()
else:
threading.Thread(target=yourls_command_handler(msg, target)).start()
yourls_command_handler(msg, target)
def run_on_script_load() -> None:
"""setup the script"""
irssi.settings_add_str(
b"misc",
b"yourls_server_url",
b"",
)
irssi.settings_add_str(
b"misc",
b"yourls_secret_sig_token",
b"",
)
irssi.settings_add_str(
b"misc",
b"yourls_format",
b"json",
)
irssi.settings_add_str(
b"misc",
b"yourls_action",
b"shorturl",
)
irssi.settings_add_str(
b"misc",
b"yourls_operation_mode",
b"whitelist",
)
irssi.settings_add_str(
b"misc",
b"yourls_name_list",
b"",
)
irssi.settings_add_int(
b"misc",
b"yourls_min_length",
30,
)
irssi.settings_add_int(
b"misc",
b"yourls_timeout",
15,
)
irssi.signal_add(b"message public", yourls_signal_handler)
irssi.signal_add(b"message private", yourls_signal_handler)
run_on_script_load()