-
Notifications
You must be signed in to change notification settings - Fork 0
/
ircclient.py
99 lines (77 loc) · 2.9 KB
/
ircclient.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
#!/usr/bin/env python
#
# IRC Client
#
# Akhmat Safrudin <[email protected]>
"""
Early Warning Disaster Information System
Copyright (C) <2011> Akhmat Safrudin
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
from random import choice
import string
from irclib import SimpleIRCClient, ServerConnectionError, nm_to_n, is_channel
class IRCListner(object):
def on_connect(self):
''' Called when connecting '''
return
def on_welcome(self):
''' Called when connected '''
return
def on_join(self):
''' Called when joining room '''
return
def on_data(self, data, source):
"""Called when new data arrives"""
return
def on_disconnected(self):
"""Called when disconnected"""
return
class IRCClient(SimpleIRCClient):
def __init__(self, config, listner):
SimpleIRCClient.__init__(self)
self.server = config['irc']['server']
self.port = 6667
self.nickname = self._generate_nick()
self.channel = config['irc']['channel']
self.listner = listner
def on_connect(self, connection, event):
self.listner.on_connect()
def on_welcome(self, connection, event):
self.listner.on_welcome()
if is_channel(self.channel):
connection.join(self.channel)
else:
sys.exit(1)
def on_join(self, connection, event):
self.listner.on_join()
def on_pubmsg(self, connection, event):
self.listner.on_data(event.arguments()[0], nm_to_n(event.source()))
def on_disconnect(self, connection, event):
self.listner.on_disconnected()
self.connection.execute_delayed(0, self._reconnect)
def on_nicknameinuse(self, c, e):
c.nick(c.get_nickname() + "_")
def _generate_nick(self):
nick = ''.join([choice(string.letters + string.digits) for i in range(10)])
return nick
def _connect(self):
try:
self.connect(self.server, self.port, self.nickname)
except ServerConnectionError, x:
print x
sys.exit(1)
def _reconnect(self):
if not self.connection.is_connected():
self.connect(server, port, nickname)
def start(self):
self.ircobj.process_once()