home · contact · privacy
Improve help text.
[plomlombot-irc.git] / irclog.py
1 #!/usr/bin/python3
2
3 def format_logline(line, channel):
4     if len(line.tokens) > 2:
5         if line.tokens[1] == "JOIN":
6             return "-> " + line.sender + " (" + \
7                 line.tokens[0][len(line.sender)+2:] + ") joins"
8         elif line.tokens[1] == "PART":
9             msg = ""
10             if len(line.tokens) > 3 and line.tokens[3][0] == ":":
11                 msg = " (" + str.join(" ", line.tokens[3:])[1:] + ")"
12             return "<- " + line.sender + " parts" + msg 
13         elif line.tokens[1] == "QUIT":
14             msg = ""
15             if len(line.tokens) > 2 and line.tokens[2][0] == ":":
16                 msg = " (" + str.join(" ", line.tokens[2:])[1:] + ")"
17             return "<- " + line.sender + " quits server" + msg 
18         elif line.tokens[1] == "NICK":
19             return "-- " + line.sender + " changes their name to " + line.receiver 
20         elif len(line.tokens) > 3:
21             if line.tokens[1] in {"PRIVMSG", "NOTICE"}:
22                 if line.tokens[3] == ":\u0001ACTION" and \
23                         line.tokens[-1][-1] == "\u0001":
24                     msg = str.join(" ", line.tokens[4:])[:-1]
25                     if line.receiver == channel:
26                         return " * " + line.sender + " " + msg 
27                 else:
28                     msg = str.join(" ", line.tokens[3:])[1:]
29                     if line.receiver == channel:
30                         return "   <" + line.sender + "> " + msg 
31             elif line.tokens[1] == "TOPIC":
32                 msg = str.join(" ", line.tokens[3:])[1:]
33                 if line.receiver == channel:
34                     return "-- " + line.sender + " sets topic to: " + msg 
35             elif line.tokens[1] == "KICK":
36                 operands = line.tokens[2:]
37                 pairs = {}
38                 for i in range(len(operands)):
39                     if 0 == i % 2:
40                         if operands[i][0] == ":":
41                             msg = " (" + str.join(" ", operands[i:])[1:] + ")"
42                             break
43                     else:
44                         pairs[operands[i - 1]] = operands[i]
45                 if channel in pairs:
46                     return "-- " + line.sender + " kicks " + pairs[channel] + msg
47             elif line.tokens[1] == "MODE":
48                 msg = str.join(" ", line.tokens[3:])
49                 if line.receiver == channel:
50                     return "-- " + line.sender + " sets channel mode " + msg 
51     return None