aboutsummaryrefslogtreecommitdiffstats
path: root/IRC/asybot.py
blob: 6a37daf17fe8c879835c0242dafadf8d882db856 (plain)
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
#! /usr/bin/env python
#
# //Reaktor/IRC/asybot.py
#

from __future__ import print_function

def is_executable(x):
  import os
  return os.path.exists(x) and os.access(x, os.X_OK)

from asynchat import async_chat as asychat
from asyncore import loop
from socket import AF_INET, SOCK_STREAM
from signal import SIGALRM, signal, alarm
from datetime import datetime as date, timedelta
from sys import exit
from re import split, search
class asybot(asychat):
  def __init__(self, server, port, nickname, targets, **kwargs):
    asychat.__init__(self)
    self.server = server
    self.port = port
    self.nickname = nickname
    self.targets = targets
    self.username = kwargs['username'] if 'username' in kwargs else nickname
    self.hostname = kwargs['hostname'] if 'hostname' in kwargs else nickname
    self.ircname = kwargs['ircname'] if 'ircname' in kwargs else nickname
    self.realname = kwargs['realname'] if 'realname' in kwargs else nickname
    self.data = ''
    self.set_terminator('\r\n')
    self.create_socket(AF_INET, SOCK_STREAM)
    self.connect((self.server, self.port))
    self.alarm_timeout = 300
    self.kill_timeout = 360
    self.last_activity = date.now()
    signal(SIGALRM, lambda signum, frame: self.alarm_handler())
    alarm(self.alarm_timeout)
    loop()

  def alarm_handler(self):
    delta = date.now() - self.last_activity
    if delta > timedelta(seconds=self.kill_timeout):
      print('kill alarm %s' % delta)
      exit()
    else:
      print('alarm %s' % delta)
      self.push('PING :asybot')
      alarm(self.alarm_timeout)

  def collect_incoming_data(self, data):
    self.data += data

  def found_terminator(self):
    print('< %s' % self.data)
    self.last_activity = date.now()

    message = self.data
    self.data = ''

    _, prefix, command, params, rest, _ = \
        split('^(?::(\S+)\s)?(\S+)((?:\s[^:]\S*)*)(?:\s:(.*))?$', message)
    params = params.split(' ')[1:]
    #print([prefix, command, params, rest])

    if command == 'PING':
      self.push('PONG :%s' % rest)

    elif command == 'PRIVMSG':
      self.on_privmsg(prefix, command, params, rest)

    elif command == '433':
      # ERR_NICKNAMEINUSE, retry with another name
      _, nickname, int, _ = split('^.*[^0-9]([0-9]+)$', self.nickname) \
          if search('[0-9]$', self.nickname) \
          else ['', self.nickname, 0, '']
      self.nickname = nickname + str(int + 1)
      self.handle_connect()

    # reset alarm
    alarm(self.alarm_timeout)

  def push(self, message):
    print('> %s' % message)
    asychat.push(self, message + self.get_terminator())

  def handle_connect(self):
    self.push('NICK %s' % self.nickname)
    self.push('USER %s %s %s :%s' %
        (self.username, self.hostname, self.server, self.realname))
    self.push('JOIN %s' % ','.join(self.targets))

  def on_privmsg(self, prefix, command, params, rest):
    def PRIVMSG(text):
      self.push('PRIVMSG %s :%s' % (','.join(params), text))

    def ME(text):
      PRIVMSG('ACTION ' + text + '')

    _from = prefix.split('!', 1)[0]

    try:
      _, _handle, _command, _argument, _ = split(
          '^(\w+|\*):\s*(\w+)(?:\s+(.*))?$', rest)
    except ValueError, error:
      if search(self.nickname, rest):
        PRIVMSG('I\'m so famous')
      return # ignore

    if _handle == self.nickname or _handle == '*':

      from os.path import realpath, dirname, join
      from subprocess import Popen as popen, PIPE

      Reaktor_dir = dirname(realpath(dirname(__file__)))
      public_commands = join(Reaktor_dir, 'public_commands')
      command = join(public_commands, _command)

      if is_executable(command):

        env = {}
        if _argument != None:
          env['argument'] = _argument

        try:
          p = popen([command], stdin=PIPE, stdout=PIPE, stderr=PIPE, env=env)
        except OSError, error:
          ME('is made of stupid')
          print('OSError@%s: %s' % (command, error))
          return

        stdout, stderr = [ x[:len(x)-1] for x in
            [ x.split('\n') for x in p.communicate()]]
        code = p.returncode
        pid = p.pid

        print('command: %s -> %s' % (command, code))
        [print('%s stdout: %s' % (pid, x)) for x in stdout]
        [print('%s stderr: %s' % (pid, x)) for x in stderr]

        if code == 0:
          [PRIVMSG(x) for x in stdout]
          [PRIVMSG(x) for x in stderr]
        else:
          ME('mimimi')

      else:
        if _handle != '*':
          PRIVMSG(_from + ': you are made of stupid')

# retrieve the value of a [singleton] variable from a tinc.conf(5)-like file
def getconf1(x, path):
  from re import findall
  pattern = '(?:^|\n)\s*' + x + '\s*=\s*(.*\w)\s*(?:\n|$)'
  y = findall(pattern, open(path, 'r').read())
  if len(y) < 1:
    raise AttributeError("len(getconf1('%s', '%s') < 1)" % (x, path))
  if len(y) > 1:
    y = ' '.join(y)
    raise AttributeError("len(getconf1('%s', '%s') > 1)\n  ====>  %s"
        % (x, path, y))
  return y[0]

if __name__ == "__main__":
  from os import environ as env
  name = getconf1('Name', '/etc/tinc/retiolum/tinc.conf')
  hostname = '%s.retiolum' % name
  nick = str(env.get('nick', name))
  host = str(env.get('host', 'supernode'))
  port = int(env.get('port', 6667))
  target = str(env.get('target', '#retiolum'))
  print('====> irc://%s@%s:%s/%s' % (nick, host, port, target))

  from getpass import getuser
  asybot(host, port, nick, [target], username=getuser(),
      ircname='//Reaktor running at %s' % hostname,
      hostname=hostname)