diff options
| author | Mistivia <i@mistivia.com> | 2025-11-16 15:13:31 +0800 |
|---|---|---|
| committer | Mistivia <i@mistivia.com> | 2025-11-16 15:13:31 +0800 |
| commit | 1bb8abca1549dba9c0c5660e9b3efa81ccb9d781 (patch) | |
| tree | 521903ed909d7f2686d8ce37e1d5c564237bfc7f | |
| parent | 779d76f20c983f4cda0c8912edb8cf7b156b9f09 (diff) | |
update
| -rw-r--r-- | ircbot/.gitignore | 4 | ||||
| -rw-r--r-- | ircbot/deploy.sh | 3 | ||||
| -rw-r--r-- | ircbot/main.py | 87 | ||||
| -rw-r--r-- | irclog/view/index.html | 2 | ||||
| -rw-r--r-- | irclog/view/view7.js | 186 | ||||
| -rw-r--r-- | teleirc/deploy.sh | 9 | ||||
| -rw-r--r-- | teleirc/matterbridge/bridge/telegram/handlers.go | 11 |
7 files changed, 38 insertions, 264 deletions
diff --git a/ircbot/.gitignore b/ircbot/.gitignore index d344ba6..d83a7ad 100644 --- a/ircbot/.gitignore +++ b/ircbot/.gitignore @@ -1 +1,5 @@ config.json +config.json.test +config.json.release +memos +*.pyc
\ No newline at end of file diff --git a/ircbot/deploy.sh b/ircbot/deploy.sh index 3b4ab46..f188af8 100644 --- a/ircbot/deploy.sh +++ b/ircbot/deploy.sh @@ -1,2 +1,3 @@ +cp config.json.release config.json rsync -avz ./ root@raye:/home/user/ircbot/ -ssh root@raye 'nitroctl restart ircbot'
\ No newline at end of file +ssh root@raye 'nitroctl restart ircbot' diff --git a/ircbot/main.py b/ircbot/main.py index 096dec8..c260839 100644 --- a/ircbot/main.py +++ b/ircbot/main.py @@ -7,9 +7,7 @@ import os import datetime import urllib.parse import hashlib -import re -import requests -import html +from urltitle import url_titles connect_time = time.time() @@ -114,6 +112,8 @@ def save_lastseen(): # with open("lastseen", "w") as fp: # json.dump(LAST_SEEN, fp) +NO_GREETINGS = set(config['no_greeting_nicks']) + def get_greeting(chan, sender, args): if time.time() - connect_time < 300: return '' @@ -121,7 +121,7 @@ def get_greeting(chan, sender, args): return '' if not chan in GREETINGS: return '' - if sender in config['no_greeting_nicks']: + if sender in NO_GREETINGS: return '' if chan in LAST_SEEN \ and sender in LAST_SEEN[chan] \ @@ -221,27 +221,17 @@ def choice_command(chan, sender, args): def dice_command(chan, sender, args): if not args: return "请指定要掷的骰子。用法: dice [数量]d[面数]" - - try: - if args[0][0] == 'd': - num_dice = 1 - num_sides = int(args[0][1:]) - else: - num_dice_str, num_sides_str = args[0].split('d') - num_dice = int(num_dice_str) - num_sides = int(num_sides_str) - except ValueError: - return "无效的骰子格式。请使用 [数量]d[面数] 的格式。" - - if num_dice <= 0 or num_sides <= 1: - return "骰子数量必须大于0,面数必须大于1。" - - results = [] - for _ in range(num_dice): - results.append(random.randint(1, num_sides)) - - total = sum(results) - return f"{total}" + query = " ".join(args) + command = ['/usr/bin/python3', '/opt/dice/main.py'] + process = subprocess.Popen( + command, + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True + ) + stdout, stderr = process.communicate(input=query) + return stdout @command("roll") def roll_command(chan, sender, args): @@ -249,53 +239,6 @@ def roll_command(chan, sender, args): # ======================================================================== -url_pattern = re.compile( - r'(\bhttps?:\/\/[-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%?=~_|])', - re.IGNORECASE -) - -title_regex = re.compile( - r'<title[^>]*>([^>]*?)</title>', - re.IGNORECASE | re.DOTALL -) - -def url_titles(text): - found_urls = url_pattern.findall(text) - unique_urls = [] - if len(found_urls) > 0: - url = found_urls[0] - unique_urls = [url] - results = [] - MAX_CONTENT_SIZE = 5 * 1024 * 1024 - for url in unique_urls: - try: - with requests.get( - url, - timeout=10, - allow_redirects=True, - stream=True - ) as response: - if response.status_code != requests.codes.ok: - continue - content_type = response.headers.get('Content-Type', '').lower() - if 'text/html' not in content_type: - continue - content = b'' - for chunk in response.iter_content(chunk_size=8192): - content += chunk - if len(content) >= MAX_CONTENT_SIZE: - continue - encoding = 'utf-8' - html_doc = content.decode(encoding) - match = title_regex.search(html_doc) - if match: - title_content = match.group(1).strip() - if title_content: - results.append(html.unescape(title_content)) - except Exception as e: - print(e) - return results - def cut_string(text, chunk_size=420): chunks = [] current_chunk = [] diff --git a/irclog/view/index.html b/irclog/view/index.html index 41c33da..ceee973 100644 --- a/irclog/view/index.html +++ b/irclog/view/index.html @@ -73,6 +73,6 @@ <div id="log-container"></div> <button onclick="setPreviousDay()">上一天</button> <button onclick="setNextDay()">下一天</button> - <script src="./view7.js"></script> + <script src="./view8.js"></script> </body> </html> diff --git a/irclog/view/view7.js b/irclog/view/view7.js deleted file mode 100644 index 29eed6c..0000000 --- a/irclog/view/view7.js +++ /dev/null @@ -1,186 +0,0 @@ -const logContainer = document.getElementById('log-container'); -const dateInput = document.getElementById('log-date'); - -const queryString = window.location.search; -const urlParams = new URLSearchParams(queryString); -let chan = urlParams.get('chan'); -let year = urlParams.get('y'); -let month = urlParams.get('m'); -let day = urlParams.get('d'); -let hashtag = window.location.hash.substring(1); - -if (chan === 'main') { - document.getElementById('searchlink').style.visibility = 'visible'; -} - -function getDate(year, month, day) { - try { - year = parseInt(year, 10); - month = parseInt(month, 10); - day = parseInt(day, 10); - if (isNaN(year) || isNaN(month) || isNaN(day)) { - throw new Error("date is NaN"); - } - return new Date(year, month - 1, day); - } catch (e) { - console.log(e); - const now = new Date(); - const y = now.toLocaleString('en-US', { timeZone: 'Asia/Shanghai', year: 'numeric' }); - const m = now.toLocaleString('en-US', { timeZone: 'Asia/Shanghai', month: 'numeric' }); - const d = now.toLocaleString('en-US', { timeZone: 'Asia/Shanghai', day: 'numeric' }); - return getDate(y, m, d); - } -} - -function formatDate(date) { - const year = date.getFullYear(); - const month = String(date.getMonth() + 1).padStart(2, '0'); - const day = String(date.getDate()).padStart(2, '0'); - return `${year}-${month}-${day}`; -} - -function setDateState(date) { - year = date.getFullYear(); - month = String(date.getMonth() + 1).padStart(2, '0'); - day = String(date.getDate()).padStart(2, '0'); - dateInput.value = formatDate(getDate(year, month, day)); - let newurl = window.location.origin + window.location.pathname + - '?chan=' + chan + - '&y=' + year + - '&m=' + month + - '&d=' + day; - if (hashtag !== '') { - newurl = newurl + '#' + hashtag; - } - window.history.replaceState(null, '', newurl); -} - -setDateState(getDate(year, month, day)); - -logContainer.innerHTML = ''; - -function escapeHtml(unsafe) { - if (!unsafe) return ''; - return unsafe - .replace(/&/g, "&") - .replace(/</g, "<") - .replace(/>/g, ">") - .replace(/"/g, """) - .replace(/'/g, "'"); -} - -function ircAction(text) { - const regex = - /^(\[[^\]]+\])(\s*<)([^>]+)(>:\s*)(\u0001ACTION\s+)([^\u0001]+)(\u0001)(.*)$/gm; - const replacement = '$1 * $3 $6$8'; - return text.replace(regex, replacement); -} - -function linkify(text) { - const urlRegex = /(\b(https?):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig; - return text.replace(urlRegex, function(url) { - return `<a href="${url}" target="_blank">${url}</a>`; - }); -} - -function sethash(time) { - let newurl = window.location.pathname + window.location.search + '#' + time; - window.history.replaceState(null, '', newurl); -} - -function insertTag(text) { - let res = ''; - let lines = text.split('\n'); - let scrollToInserted = false; - for (let line of lines) { - let time = line.substring(1, 9); - if (time == hashtag && time !== '') { - if (!scrollToInserted) { - res += '<span id="scrollTo"></span>'; - scrollToInserted = true; - } - res += '<a class="logline" href="#' + time + '" style="color:red;">' + line + '</span>\n'; - } else { - res += '<a class="logline" href="#' + time + '">' + line + '</a>\n'; - } - } - hashtag = ''; - return res; -} - -function logProcess(text) { - text = ircAction(text); - text = escapeHtml(text); - text = linkify(text); - text = insertTag(text); - return text; -} - -function loadLog() { - let targetUrl = 'https://raye.mistivia.com/irclog/%23' + chan + '/' + year + '/' + month + '-' + day + '.txt'; - fetch(targetUrl) - .then(response => { - if (!response.ok) { - throw new Error(`HTTP Error: ${response.status} ${response.statusText} for hash: ${urlHash}`); - } - return response.text(); - }) - .then(text => { - logContainer.innerHTML = `<pre>${logProcess(text)}</pre>`; - let scrollTo = document.getElementById('scrollTo'); - if (scrollTo) { - scrollTo.scrollIntoView({behavior: 'smooth', block: 'center'}); - } - }) - .catch(error => { - console.error('Fetch error:', error); - logContainer.innerHTML = ''; - }); -} - -loadLog(); - -function onDateChange() { - var dateInput = this; - var dateValue = dateInput.value; - - if (dateValue && chan) { - var dateParts = dateValue.split('-'); - var year = dateParts[0]; - var month = dateParts[1]; - var day = dateParts[2]; - let date = getDate(year, month, day); - setDateState(date); - loadLog(); - } -} - -document.getElementById("log-date").addEventListener('change', onDateChange, false); - -function changeDate(days) { - let currentDate = getDate(year, month, day); - const currentDay = currentDate.getDate(); - currentDate.setDate(currentDay + days); - setDateState(currentDate); - dateInput.dispatchEvent(new Event('change')); -} - -function setPreviousDay() { - changeDate(-1); - window.scrollTo(0, 0); -} - -function setNextDay() { - changeDate(1); - window.scrollTo(0, 0); -} - -function setToday() { - let date = getDate('', '', ''); - setDateState(date); - dateInput.dispatchEvent(new Event('change')); -} - -function gotosearch() { - window.open('../search/', '_blank'); -}
\ No newline at end of file diff --git a/teleirc/deploy.sh b/teleirc/deploy.sh index e390c1d..f03335f 100644 --- a/teleirc/deploy.sh +++ b/teleirc/deploy.sh @@ -1,2 +1,7 @@ -scp config.toml root@raye:/etc/matterbridge/ -ssh root@raye 'nitroctl restart teleirc' +# cd matterbridge +# go build -tags netgo -ldflags '-extldflags "-static"' && \ +# cd .. && \ +# proxychains -q scp ./matterbridge/matterbridge root@raye:/usr/local/bin/ && \ +proxychains -q ssh root@raye 'nitroctl stop teleirc' && \ +proxychains -q scp config.toml root@raye:/etc/matterbridge/ && \ +proxychains -q ssh root@raye 'nitroctl start teleirc' diff --git a/teleirc/matterbridge/bridge/telegram/handlers.go b/teleirc/matterbridge/bridge/telegram/handlers.go index a9d9bd6..31ca78f 100644 --- a/teleirc/matterbridge/bridge/telegram/handlers.go +++ b/teleirc/matterbridge/bridge/telegram/handlers.go @@ -163,8 +163,15 @@ func (b *Btelegram) handleUsername(rmsg *config.Message, message *tgbotapi.Messa b.handleDownloadAvatar(message.SenderChat.ID, rmsg.Channel) } } - - // if we really didn't find a username, set it to unknown + if rmsg.Username == "" { + rmsg.Username = unknownUser + } + rmsg.Username = strings.TrimSpace(rmsg.Username); + if rmsg.Username == "" { + if message.SenderChat != nil && message.SenderChat.UserName != "" { + rmsg.Username = message.SenderChat.UserName + } + } if rmsg.Username == "" { rmsg.Username = unknownUser } |
