File: irccat

package info (click to toggle)
python-irclib 0.4.8-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze, wheezy
  • size: 188 kB
  • ctags: 249
  • sloc: python: 1,462; makefile: 63
file content (64 lines) | stat: -rw-r--r-- 1,457 bytes parent folder | download | duplicates (2)
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
#! /usr/bin/env python
#
# Example program using irclib.py.
#
# This program is free without restrictions; do anything you like with
# it.
#
# Joel Rosdahl <joel@rosdahl.net>

import irclib
import sys

def on_connect(connection, event):
    if irclib.is_channel(target):
        connection.join(target)
    else:
        while 1:
            line = sys.stdin.readline()
            if not line:
                break
            connection.privmsg(target, line)
        connection.quit("Using irclib.py")

def on_join(connection, event):
    while 1:
        line = sys.stdin.readline()
        if not line:
            break
        connection.privmsg(target, line)
    connection.quit("Using irclib.py")

if len(sys.argv) != 4:
    print "Usage: irccat <server[:port]> <nickname> <target>"
    print "\ntarget is a nickname or a channel."
    sys.exit(1)

def on_disconnect(connection, event):
    sys.exit(0)

s = sys.argv[1].split(":", 1)
server = s[0]
if len(s) == 2:
    try:
        port = int(s[1])
    except ValueError:
        print "Error: Erroneous port."
        sys.exit(1)
else:
    port = 6667
nickname = sys.argv[2]
target = sys.argv[3]

irc = irclib.IRC()
try:
    c = irc.server().connect(server, port, nickname)
except irclib.ServerConnectionError, x:
    print x
    sys.exit(1)

c.add_global_handler("welcome", on_connect)
c.add_global_handler("join", on_join)
c.add_global_handler("disconnect", on_disconnect)

irc.process_forever()