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