2 # Copyright (C) 2008 Stefan Hajnoczi <stefanha@gmail.com>.
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License as
6 # published by the Free Software Foundation; either version 2 of the
7 # License, or any later version.
9 # This program is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 HOST
= 'irc.freenode.net'
25 NICKSERV_PASSWORD
= None
29 ERRCODE_RE
= re
.compile(r
'(errcode|Error)\s+((0x)?[0-9a-fA-F]{8})')
35 def nick_from_mask(mask
):
36 return (mask
.find('!') > -1 and mask
.split('!', 1)[0]) or mask
41 pmsg('nickserv', 'identify %s' % NICKSERV_PASSWORD
)
48 def privmsg(_
, target
, msg
):
51 if msg
.find(NICK
) == -1:
54 replyto
= nick_from_mask(who
)
55 m
= ERRCODE_RE
.search(msg
)
58 pmsg(replyto
, str(errcode
.Errcode(int(m
.groups()[1], 16))))
61 if msg
.find('help') > -1:
62 pmsg(replyto
, 'I look up gPXE error codes. Message me like this:')
63 pmsg(replyto
, 'errcode 0x12345678 OR Error 0x12345678')
65 def add_handler(command
, handler
, nargs
):
66 handlers
[command
] = (handler
, nargs
)
69 sock
.sendall('%s\r\n' % msg
)
71 def pmsg(target
, msg
):
72 cmd('PRIVMSG %s :%s' % (target
, msg
))
76 if command
in handlers
:
80 elif len(args
) == h
[1]:
85 who
, line
= line
.split(None, 1)
90 while line
and line
[0] != ':' and line
.find(' ') != -1:
91 fields
= line
.split(None, 1)
103 add_handler('376', autojoin
, NO_ARGS
)
104 add_handler('PING', ping
, 2)
105 add_handler('PRIVMSG', privmsg
, 3)
107 sock
= socket
.socket()
108 sock
.connect((HOST
, PORT
))
109 cmd('NICK %s' % NICK
)
110 cmd('USER %s none none :%s' % (IDENT
, REALNAME
))
119 while rbuf
.find('\r\n') != -1:
120 line
, rbuf
= rbuf
.split('\r\n', 1)
123 who
, args
= parse(line
)