Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

gpxebot.py 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/usr/bin/env python
  2. # Copyright (C) 2008 Stefan Hajnoczi <stefanha@gmail.com>.
  3. #
  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.
  8. #
  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.
  13. #
  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.
  17. import re
  18. import socket
  19. import errcode
  20. HOST = 'irc.freenode.net'
  21. PORT = 6667
  22. NICK = 'ipxebot'
  23. CHAN = '#etherboot'
  24. NICKSERV_PASSWORD = None
  25. IDENT = 'ipxebot'
  26. REALNAME = 'iPXE bot'
  27. ERRCODE_RE = re.compile(r'(errcode|Error)\s+((0x)?[0-9a-fA-F]{8})')
  28. NO_ARGS = -1
  29. handlers = {}
  30. def nick_from_mask(mask):
  31. return (mask.find('!') > -1 and mask.split('!', 1)[0]) or mask
  32. def autojoin():
  33. del handlers['376']
  34. if NICKSERV_PASSWORD:
  35. pmsg('nickserv', 'identify %s' % NICKSERV_PASSWORD)
  36. if CHAN:
  37. cmd('JOIN %s' % CHAN)
  38. def ping(_, arg):
  39. cmd('PONG %s' % arg)
  40. def privmsg(_, target, msg):
  41. if target == CHAN:
  42. replyto = target
  43. if msg.find(NICK) == -1:
  44. return
  45. elif target == NICK:
  46. replyto = nick_from_mask(who)
  47. m = ERRCODE_RE.search(msg)
  48. if m:
  49. try:
  50. pmsg(replyto, str(errcode.Errcode(int(m.groups()[1], 16))))
  51. except ValueError:
  52. pass
  53. if msg.find('help') > -1:
  54. pmsg(replyto, 'I look up iPXE error codes. Message me like this:')
  55. pmsg(replyto, 'errcode 0x12345678 OR Error 0x12345678')
  56. def add_handler(command, handler, nargs):
  57. handlers[command] = (handler, nargs)
  58. def cmd(msg):
  59. sock.sendall('%s\r\n' % msg)
  60. def pmsg(target, msg):
  61. cmd('PRIVMSG %s :%s' % (target, msg))
  62. def dispatch(args):
  63. command = args[0]
  64. if command in handlers:
  65. h = handlers[command]
  66. if h[1] == NO_ARGS:
  67. h[0]()
  68. elif len(args) == h[1]:
  69. h[0](*args)
  70. def parse(line):
  71. if line[0] == ':':
  72. who, line = line.split(None, 1)
  73. who = who[1:]
  74. else:
  75. who = None
  76. args = []
  77. while line and line[0] != ':' and line.find(' ') != -1:
  78. fields = line.split(None, 1)
  79. if len(fields) == 1:
  80. fields.append(None)
  81. arg, line = fields
  82. args.append(arg)
  83. if line:
  84. if line[0] == ':':
  85. args.append(line[1:])
  86. else:
  87. args.append(line)
  88. return who, args
  89. add_handler('376', autojoin, NO_ARGS)
  90. add_handler('PING', ping, 2)
  91. add_handler('PRIVMSG', privmsg, 3)
  92. sock = socket.socket()
  93. sock.connect((HOST, PORT))
  94. cmd('NICK %s' % NICK)
  95. cmd('USER %s none none :%s' % (IDENT, REALNAME))
  96. rbuf = ''
  97. while True:
  98. r = sock.recv(4096)
  99. if not r:
  100. break
  101. rbuf += r
  102. while rbuf.find('\r\n') != -1:
  103. line, rbuf = rbuf.split('\r\n', 1)
  104. if not line:
  105. continue
  106. who, args = parse(line)
  107. dispatch(args)