forked from nemunaire/nemubot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchannel.py
162 lines (131 loc) · 4.39 KB
/
channel.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
# coding=utf-8
# Nemubot is a modulable IRC bot, built around XML configuration files.
# Copyright (C) 2012 Mercier Pierre-Olivier
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import logging
class Channel:
"""A chat room"""
def __init__(self, name, password=None):
"""Initialize the channel
Arguments:
name -- the channel name
password -- the optional password use to join it
"""
self.name = name
self.password = password
self.people = dict()
self.topic = ""
self.logger = logging.getLogger("nemubot.channel." + name)
def treat(self, cmd, msg):
"""Treat a incoming IRC command
Arguments:
cmd -- the command
msg -- the whole message
"""
if cmd == "353":
self.parse353(msg)
elif cmd == "332":
self.parse332(msg)
elif cmd == "MODE":
self.mode(msg)
elif cmd == "JOIN":
self.join(msg.nick)
elif cmd == "NICK":
self.nick(msg.nick, msg.text)
elif cmd == "PART" or cmd == "QUIT":
self.part(msg.nick)
elif cmd == "TOPIC":
self.topic = self.text
def join(self, nick, level=0):
"""Someone join the channel
Argument:
nick -- nickname of the user joining the channel
level -- authorization level of the user
"""
self.logger.debug("%s join", nick)
self.people[nick] = level
def chtopic(self, newtopic):
"""Send command to change the topic
Arguments:
newtopic -- the new topic of the channel
"""
self.srv.send_msg(self.name, newtopic, "TOPIC")
self.topic = newtopic
def nick(self, oldnick, newnick):
"""Someone change his nick
Arguments:
oldnick -- the previous nick of the user
newnick -- the new nick of the user
"""
if oldnick in self.people:
self.logger.debug("%s switch nick to %s on", oldnick, newnick)
lvl = self.people[oldnick]
del self.people[oldnick]
self.people[newnick] = lvl
def part(self, nick):
"""Someone leave the channel
Argument:
nick -- name of the user that leave
"""
if nick in self.people:
self.logger.debug("%s has left", nick)
del self.people[nick]
def mode(self, msg):
"""Channel or user mode change
Argument:
msg -- the whole message
"""
if msg.text[0] == "-k":
self.password = ""
elif msg.text[0] == "+k":
if len(msg.text) > 1:
self.password = ' '.join(msg.text[1:])[1:]
else:
self.password = msg.text[1]
elif msg.text[0] == "+o":
self.people[msg.nick] |= 4
elif msg.text[0] == "-o":
self.people[msg.nick] &= ~4
elif msg.text[0] == "+h":
self.people[msg.nick] |= 2
elif msg.text[0] == "-h":
self.people[msg.nick] &= ~2
elif msg.text[0] == "+v":
self.people[msg.nick] |= 1
elif msg.text[0] == "-v":
self.people[msg.nick] &= ~1
def parse332(self, msg):
"""Parse RPL_TOPIC message
Argument:
msg -- the whole message
"""
self.topic = msg.text
def parse353(self, msg):
"""Parse RPL_ENDOFWHO message
Argument:
msg -- the whole message
"""
for p in msg.text:
p = p.decode()
if p[0] == "@":
level = 4
elif p[0] == "%":
level = 2
elif p[0] == "+":
level = 1
else:
self.join(p, 0)
continue
self.join(p[1:], level)