-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.py
131 lines (114 loc) · 4.79 KB
/
bot.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
# coding: utf-8
import irc.bot
import irc.client
import irc.connection
import pymongo
import datetime
import config
import urllib2 # only for quote
class SBot(irc.bot.SingleServerIRCBot):
def __init__(self):
if config.USE_SSL:
import ssl
factory = irc.connection.Factory(wrapper=ssl.wrap_socket)
else:
factory = irc.connection.Factory()
irc.bot.SingleServerIRCBot.__init__(self,
[(config.SERVER, config.PORT), ],
config.BOT_NAME,
'm',
connect_factory = factory
)
self.connected = False
connection = pymongo.Connection()
self.db = connection[config.SIRC_DB]
if config.SIRC_DB_USE_AUTH:
self.db.authenticate(config.SIRC_DB_USER, config.SIRC_DB_PASS)
self.db.send.remove()
self._fetch()
def on_welcome(self,c, e):
self.connected = True
for target in self.db.collection_names(): #extract last channels the user used
target = urllib2.unquote(target.encode("utf-8"))
if target.startswith('#'): # TODO: is_channel?
c.join(target) #==self.connect.join(target)
def on_mode(self, c, e):
nick = e.source.nick
target = e.target
if irc.client.is_channel(target):
self._log(target, config.NOTIFY_NAME, '[%s] by <%s>.' % (' '.join(e.arguments), nick))
else:
pass
def on_topic(self, c, e):
nick = e.source.nick
target = e.target
new_topic = e.arguments[0]
self._log(target, config.NOTIFY_NAME, '<%s> changed the topic to: "%s".' % (nick, new_topic))
def on_nick(self, c, e):
before = e.source.nick
after = e.target
for target, ch in self.channels.items():
if ch.has_user(before):
self._log(target, config.NOTIFY_NAME, '<%s> is now known as <%s>.' % (before, after))
def on_join(self, c, e):
nick = e.source.nick
self._log(e.target, config.NOTIFY_NAME, '<%s> has joined.' % nick)
def on_part(self, c, e):
nick = e.source.nick
self._log(e.target, config.NOTIFY_NAME, '<%s> has left.' % nick)
def on_quit(self, c, e):
nick = e.source.nick
for target, ch in self.channels.items():
if ch.has_user(nick):
self._log(target, config.NOTIFY_NAME, '<%s> has quit.' % nick)
def on_kick(self, c, e):
nick_s = e.source.nick
nick_m = e.arguments[0]
because_of = e.arguments[1]
self._log(e.target, config.NOTIFY_NAME, '<%s> was kicked by <%s> because of "%s".' % (nick_m, nick_s, because_of))
#딴사람이 메시지를 썼을때
def on_pubmsg(self, c, e): #c는 커넥트된 서버, 허나 이미 connection이라는 멤버 변수가 있으므로 그렇게 필요가 있을까?=>
nick = e.source.nick# e: Event객체(source와 target이 있음)
target = e.target
message = e.arguments[0]
self._log(target, nick, message)
if self.channels[target].is_oper(c.get_nickname()) and \
message.startswith(config.OPERATOR_COMMAND):
if nick == config.OPERATOR_NAME:
self.connection.mode(target, '+o %s' % nick)
else:
self.connection.kick(target, nick, '...')
def _log(self, target, source, message):
data = {
'datetime': datetime.datetime.now(),
'source': source.decode('utf-8', 'replace'),
'message': message.decode('utf-8', 'replace'),
}
try:
channel = target.lower()
channel = urllib2.quote(channel) # percent encoding to deal with hangul channel
self.db[channel].insert(data)
self.db[channel].ensure_index('datetime')
except:
pass
#send에 들어있는 것=>irc 서버로 전송
#자동으로 불림
def _fetch(self):
if self.connected:
try:
for data in self.db.send.find():
channel = data['channel'].lower().encode('utf-8').lower()
if channel not in self.channels:
self.connection.join(channel)
account = data['account'].encode('utf-8')
message = ('<%s> %s' % (account, data['message'])).encode('utf-8')
self.connection.privmsg(channel, message) #중요: 메시지 보냄
self._log(channel, self._nickname, message)
self.db.send.remove(data)
except irc.client.ServerNotConnectedError:
self.connected = False
self._connect()
self.ircobj.execute_delayed(1, self._fetch) # 1초마다 fetch하는 함수
if __name__ == '__main__':
bot = SBot()
bot.start()