forked from circuits/circuits
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathircclient.py
executable file
·197 lines (141 loc) · 5.05 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Example IRC Client
A basic IRC client with a very basic console interface.
For usage type:
./ircclient.py --help
"""
from __future__ import print_function
import os
from optparse import OptionParser
from socket import gethostname
from circuits import Component, Debugger, __version__ as systemVersion, handler
from circuits.io import stdin
from circuits.net.events import connect
from circuits.net.sockets import TCPClient
from circuits.protocols.irc import IRC, JOIN, NICK, PRIVMSG, USER
USAGE = "%prog [options] host [port]"
VERSION = "%prog v" + systemVersion
def parse_options():
parser = OptionParser(usage=USAGE, version=VERSION)
parser.add_option(
"-n", "--nick",
action="store", default=os.environ["USER"], dest="nick",
help="Nickname to use"
)
parser.add_option(
"-d", "--debug",
action="store_true", default=False, dest="debug",
help="Enable debug verbose logging",
)
parser.add_option(
"-c", "--channel",
action="store", default="#circuits", dest="channel",
help="Channel to join"
)
opts, args = parser.parse_args()
if len(args) < 1:
parser.print_help()
raise SystemExit(1)
return opts, args
class Client(Component):
# Set a separate channel in case we want multiple ``Client`` instances.
channel = "ircclient"
def init(self, host, port=6667, opts=None):
self.host = host
self.port = port
self.opts = opts
self.hostname = gethostname()
self.nick = opts.nick
self.ircchannel = opts.channel
# Add TCPClient and IRC to the system.
TCPClient(channel=self.channel).register(self)
IRC(channel=self.channel).register(self)
# Enable Debugging?
if opts.debug:
Debugger().register(self)
def ready(self, component):
"""ready Event
This event is triggered by the underlying ``TCPClient`` Component
when it is ready to start making a new connection.
"""
self.fire(connect(self.host, self.port))
def connected(self, host, port):
"""connected Event
This event is triggered by the underlying ``TCPClient`` Component
when a successfully connection has been made.
"""
print("Connected to %s:%d" % (host, port))
nick = self.nick
hostname = self.hostname
name = "%s on %s using circuits/%s" % (nick, hostname, systemVersion)
self.fire(NICK(nick))
self.fire(USER(nick, nick, self.hostname, name))
def disconnected(self):
"""disconnected Event
This event is triggered by the underlying ``TCPClient`` Component
when the connection has been disconnected.
"""
print("Disconnecetd from %s:%d" % (self.host, self.port))
raise SystemExit(0)
def numeric(self, source, numeric, *args):
"""numeric Event
This event is triggered by the ``IRC`` Protocol Component when we have
received an IRC Numberic Event from server we are connected to.
"""
if numeric == 1:
self.fire(JOIN(self.ircchannel))
elif numeric == 433:
self.nick = newnick = "%s_" % self.nick
self.fire(NICK(newnick))
def join(self, source, channel):
"""join Event
This event is triggered by the ``IRC`` Protocol Component when a
user has joined a channel.
"""
if source[0].lower() == self.nick.lower():
print("Joined %s" % channel)
else:
print(
"--> %s (%s) has joined %s" % (
source[0], "@".join(source[1:]), channel
)
)
def notice(self, source, target, message):
"""notice Event
This event is triggered by the ``IRC`` Protocol Component for each
notice we receieve from the server.
"""
print("-%s- %s" % (source[0], message))
def privmsg(self, source, target, message):
"""privmsg Event
This event is triggered by the ``IRC`` Protocol Component for each
message we receieve from the server.
"""
if target[0] == "#":
print("<%s> %s" % (source[0], message))
else:
print("-%s- %s" % (source[0], message))
@handler("read", channel="stdin")
def stdin_read(self, data):
"""read Event (on channel ``stdin``)
This is the event handler for ``read`` events specifically from the
``stdin`` channel. This is triggered each time stdin has data that
it has read.
"""
data = data.strip().decode("utf-8")
print("<{0:s}> {1:s}".format(self.nick, data))
self.fire(PRIVMSG(self.ircchannel, data))
def main():
opts, args = parse_options()
host = args[0]
if len(args) > 1:
port = int(args[1])
else:
port = 6667
# Configure and run the system.
client = Client(host, port, opts=opts)
stdin.register(client)
client.run()
if __name__ == "__main__":
main()