-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriftwood.py
58 lines (44 loc) · 1.94 KB
/
driftwood.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
import weechat
import os
import re
from datetime import datetime
# Root log directory
LOG_DIR = os.path.expanduser("~/logs/weechat-driftwood/")
def sanitize_filename(filename):
# Remove leading and trailing non-alphanumeric characters
filename = re.sub(r'^\W+|\W+$', '', filename)
# Replace non-alphanumeric characters surrounded by alphanumeric characters with a space
filename = re.sub(r'(?<=\w)\W+(?=\w)', ' ', filename)
return filename
def driftwood_logger(data, buffer, date, tags, displayed, highlight, prefix, message):
# Skip logging server buffers
if weechat.buffer_get_string(buffer, "localvar_type") == "server":
return weechat.WEECHAT_RC_OK
# Skip logging join/part/quit messages
if "irc_join" in tags or "irc_part" in tags or "irc_quit" in tags:
return weechat.WEECHAT_RC_OK
# Extract channel or nick from the buffer name
channel = weechat.buffer_get_string(buffer, "short_name")
if not channel:
return weechat.WEECHAT_RC_OK
# Get the nick of the sender
nick = weechat.buffer_get_string(buffer, "localvar_nick")
if not nick:
return weechat.WEECHAT_RC_OK
# Build log file path
timestamp = datetime.fromtimestamp(int(date)).strftime("%Y%m%d")
sanitized_channel = sanitize_filename(channel)
sanitized_nick = sanitize_filename(nick)
log_dir = os.path.join(LOG_DIR, sanitized_nick, sanitized_channel, timestamp)
os.makedirs(log_dir, exist_ok=True)
log_file = os.path.join(log_dir, "log.txt")
# Format log entry
entry = f"{prefix}\t{message}\n"
# Append log entry to file
with open(log_file, "a", encoding="utf-8") as file:
file.write(entry)
return weechat.WEECHAT_RC_OK
if __name__ == "__main__":
weechat.register("driftwood_logger", "@apple-fritter", "1.0", "MIT",
"Log WeeChat buffers natively in Driftwood format", "", "")
weechat.hook_print("", "", "", 1, "driftwood_logger", "")