-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsed.py
171 lines (140 loc) · 5.39 KB
/
sed.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
#!/usr/bin/env python3
# coding=utf-8
# Sed Module for Drastikbot
#
# Replace text using sed.
#
# This module keeps a buffer of the last posted messages
# and when the substitution command is issued it calls sed
# and sends the result.
#
# Depends:
# - sed :: default unix program, should be in the repos
'''
Copyright (C) 2018, 2021 drastik.org
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
import subprocess
import re
from dbot_tools import p_truncate
class Module:
irc_commands = ["PRIVMSG"]
manual = {
"desc": (
"Usage: s/regexp/replacement/flags"
" | Try to match a 'regexp' with one of the previous"
" messages posted and replace it with 'replacement'."
" For flags and a detailed explanation see:"
" https://www.gnu.org/software/sed/manual/html_node/"
"The-_0022s_0022-Command.html"
" | Extensions: \"s///-n\" n is the number of matches to skip"
" If the 'number' flag is used -n should be used after it.")
}
def write(varget, varset, channel, msg):
msgdict = varget('msgdict', defval={channel: [msg]})
try:
msgdict[channel].append(msg)
except KeyError:
msgdict.update({channel: [msg]})
if len(msgdict[channel]) > 50:
del msgdict[channel][0]
varset('msgdict', msgdict)
def read(varget, channel):
return varget('msgdict')[channel]
def call_sed(msg, sed_args):
echo = ['echo', msg]
p = subprocess.run(echo, stdout=subprocess.PIPE)
echo_outs = p.stdout
sed = ['sed', '-r', '--sandbox',
f's/{sed_args[1]}/{sed_args[2]}/{sed_args[3]}']
p = subprocess.run(sed, stdout=subprocess.PIPE, input=echo_outs)
return p.stdout.decode('utf-8')
sed_parse = re.compile('(?<!\\\\)/')
sed_cmd = re.compile('^s/.*/.*')
def main(i, irc):
nickname = i.msg.get_nickname()
msgtarget = i.msg.get_msgtarget()
text = i.msg.get_text()
if not sed_cmd.match(text):
write(i.varget, i.varset, msgtarget, text)
return
sed_out = ""
sed_args = sed_parse.split(text)
if len(sed_args) < 4:
# check if the last / is missed etc.
return
msglist = read(i.varget, msgtarget)
# Extension to allow the user match previous messages.
# It uses the special syntax: "s///-n" where n is the
# number of matches to skip.
# Sideffect: because it matches upto two decimals after -,
# sed string positioning (or other commands
# with decimals in them) should be issued
# before the - command or two characters after
# the - .
# The following code is for parsing the command. It
# edits the "goback" variable which will be used in the
# loop below.
# We use two decimals because the queue save upto 50
# messages.
goback = False
if '-' in sed_args[3]:
s = sed_args[3]
idx = s.index('-')
if len(s[idx:]) > 2 and s[idx + 2].isdecimal():
goback = int(f'{s[idx + 1]}{s[idx + 2]}')
sed_args[3] = f'{s[:idx]}{s[idx + 3:]}'
elif len(s[idx:]) > 1 and s[idx + 1].isdecimal():
goback = int(s[idx + 1])
sed_args[3] = f'{s[:idx]}{s[idx + 2:]}'
n = 1
while n <= 50:
flags = 0
if 'i' in sed_args[3]:
flags |= re.I
try:
db_search = re.search(sed_args[1], msglist[-n], flags)
except re.error as e: # Invalid pattern given
irc.out.notice(nickname, f"sed: regexp error: {e}")
return
if db_search:
if goback:
# Check if the goback command was issued.
goback -= 1
n = n + 1
continue
a = n
break
else:
a = False
n = n + 1
if a:
if "\x01ACTION " in msglist[-a][:8]:
msg_len = irc.msg_len - 9 - len(msgtarget) - 10 - 2
sed_out = call_sed(msglist[-a][8:], sed_args).strip()
sed_out = sed_out.rstrip('\n').replace('\x01', "")
sed_out = p_truncate(sed_out, msg_len, 98, True)
irc.out.privmsg(msgtarget, f"\x01ACTION {sed_out}\x01")
else:
msg_len = irc.msg_len - 9 - len(msgtarget) - 2
sed_out = call_sed(msglist[-a], sed_args).strip()
sed_out = sed_out.rstrip('\n').replace('\x01', "")
sed_out = p_truncate(sed_out, msg_len, 98, True)
irc.out.privmsg(msgtarget, sed_out)
if sed_out:
# We try to limit the string saved in the queue to avoid:
# OSError: [Errno 7] Argument list too long: 'echo'
# when calling 'echo' in @call_sed .
# 512 chars are more than enough, since the bot will
# never be able to send a message with that many.
write(i.varget, i.varset, msgtarget, sed_out)
# write(i.varget, i.varset, msgtarget, text) # save commands