summaryrefslogtreecommitdiff
path: root/rschat.py
blob: b63fc4808ceb65365826a3cfb875c790230f4043 (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
#!/usr/bin/env python3

from __future__ import unicode_literals, with_statement

VERSION=(0, 1, 0)

from chat_iface import ChatInterface
from irc_iface import IRCInterface
try:
  from configparser import SafeConfigParser
except ImportError:
  from ConfigParser import SafeConfigParser
import logging
import os
import select
import sys
import time

logger = logging.getLogger('rschat')
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('<%(levelno)s>[%(asctime)s] %(name)s:%(funcName)s: %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)

logger.debug("Starting rschat version %d.%d.%d" % VERSION)

try:
  xdg_config_home = os.environ['XDG_CONFIG_HOME']
except KeyError:
  xdg_config_home = os.path.expanduser('~/.config')

try:
  xdg_config_dirs = os.environ['XDG_CONFIG_DIRS'].split(':')
except KeyError:
  xdg_config_dirs = ['/etc/xdg']

cfg = SafeConfigParser({
  'server': 'chat.freenode.net',
  'port': 6667,
  'nick': 'RSWBot',
  'channel': '#rswiki-cc',
  'chat': 'clan',
})

cfg.read([fldr + '/rschat.ini' for fldr in [xdg_config_home] + xdg_config_dirs + ['.']])

with IRCInterface(cfg.get('IRC', 'server'), cfg.get('IRC', 'port', raw=True), cfg.get('IRC', 'nick'), cfg.get('IRC', 'channel')) as irc, \
     ChatInterface(cfg.get('Chat', 'Chat'), cfg.get('Chat', 'user'), cfg.get('Chat', 'pass')) as chat:

  logger.debug('Primary initialization complete, processing IRC input')

  irc.poll()

  # poll for new messages
  logger.info('Initialization seems complete, starting main loop')

  def poll(recv, send):
    msgs = recv.poll()
    if msgs:
      list(map(send.send, ['%s: %s' % msg for msg in msgs]))

  while True:
    time.sleep(0.1)

    # raw IRC commands
    if select.select([sys.stdin], [], [], 0)[0]:
      irc.send_raw(sys.stdin.readline())

    poll(irc, chat)
    poll(chat, irc)