# -*- coding: utf-8 -*-
# vim:ts=4:sw=4:expandtab 2
# Copyright 2016, 2017 juga (juga at riseup dot net), MIT license.
"""Netowrk utils for the DHCP client implementation of the Anonymity Profile
([:rfc:`7844`])."""
import logging
import subprocess

from pyroute2 import IPRoute
from pyroute2.netlink import NetlinkError

from .constants import RESOLVCONF, RESOLVCONF_ADMIN

logger = logging.getLogger(__name__)


def set_net(lease):
    ipr = IPRoute()
    try:
        index = ipr.link_lookup(ifname=lease.interface)[0]
    except IndexError as e:
        logger.error('Interface %s not found, can not set IP.',
                     lease.interface)
        exit(1)
    try:
        ipr.addr('add', index, address=lease.address,
                 mask=int(lease.subnet_mask_cidr))
    except NetlinkError as e:
        if ipr.get_addr(index=index)[0].\
                get_attrs('IFA_ADDRESS')[0] == lease.address:
            logger.debug('Interface %s is already set to IP %s' %
                         (lease.interface, lease.address))
        else:
            logger.error(e)
    else:
        logger.debug('Interface %s set to IP %s' %
                     (lease.interface, lease.address))
    try:
        ipr.route('add', dst='0.0.0.0', gateway=lease.router, oif=index)
    except NetlinkError as e:
        if ipr.get_routes(table=254)[0].\
                get_attrs('RTA_GATEWAY')[0] == lease.router:
            logger.debug('Default gateway is already set to %s' %
                         (lease.router))
        else:
            logger.error(e)
    else:
        logger.debug('Default gateway set to %s', lease.router)
    ipr.close()
    set_dns()


def set_dns(lease):
    if os.path.exists(RESOLVCONF_ADMIN):
        cmd = [RESOLVCONF_ADMIN, 'add' lease.interface, lease.name_server]
        proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE)
        try:
            (stdout, stderr) = proc.communicate()
        except TypeError as e:
            logger.error(e)
        return
    # TODO: check systemd-resolved
    if os.path.exists(RESOLVCONF):
        cmd = [RESOLVCONF, '-a', lease.interface]
        proc = subprocess.Popen(cmd, stdin=subprocess.PIPE,
                                tdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdin = '\n'.join(['nameserver ' + nm for nm in
                           lease.name_server.split()])
        stdin = str.encode(stdin)
        try:
            (stdout, stderr) = proc.communicate(stdin)
        except TypeError as e:
            logger.error(e)
        logger.debug('result %s, stdout %s, stderr %s', proc.returncode,
                     stdout, stderr)


def systemd_resolved_status():
    # NOTE: not used currently
    from dbus import SystemBus, SessionBus
    bus = SystemBus()
    systemd = bus.get_object('org.freedesktop.systemd1',
                             '/org/freedesktop/systemd1')
    manager = Interface(systemd,
                        dbus_interface='org.freedesktop.systemd1.Manager')
    unit = manager.LoadUnit('sytemd-resolved.service')
    proxy = bus.get_object('org.freedesktop.systemd1', str(unit))
    resolved = Interface(proxy, dbus_interface='org.freedesktop.systemd1.Unit')
    r = proxy.Get('org.freedesktop.systemd1.Unit',
                  'ActiveState',
                  dbus_interface='org.freedesktop.DBus.Properties')


def systemd_resolved_status():
    # NOTE: not used currently
    from pydbus import SystemBus
    bus = SystemBus()
    systemd = bus.get('org.freedesktop.systemd1')
    unit = systemd.LoadUnit('sytemd-resolved.service')
    resolved = bus.get('.systemd1', unit[0])
    resolved.Get('org.freedesktop.systemd1.Unit', 'ActiveState')


def systemd_resolved_start():
    # NOTE: not used currently
    from pydbus import SystemBus
    bus = SystemBus()
    systemd = bus.get(".systemd1")
    try:
        systemd.StartUnit("systemd-resolved.service", "fail")
    except:
        # g-io-error-quark: GDBus.Error:org.freedesktop.systemd1.NoSuchUnit:
        # Unit foo.service not found. (36)
        logger.error("Could not start systemd-resolved")


def systemd_resolved_get_dns():
    # busctl introspect org.freedesktop.resolve1
    # /org/freedesktop/resolve1/link/_35 |grep DNS
    pass


def systemd_resolved_set_dns():
    # ip l
    # busctl call org.freedesktop.resolve1
    # /org/freedesktop/resolve1 org.freedesktop.resolve1.Manager
    # SetLinkDNS 'ia(iay)' 5 1 2 4 8 8 8 8
    pass

@@ -224,16 +224,31 @@
 logging%0A
+import os.path%0A
 import s
@@ -1673,16 +1673,21 @@
 set_dns(
+lease
 )%0A%0A%0Adef 
@@ -1781,16 +1781,17 @@
 N, 'add'
+,
  lease.i
@@ -2814,18 +2814,17 @@
 us, 
-SessionBus
+Interface
 %0A   
@@ -3185,24 +3185,26 @@
 r(unit))%0A   
+ #
  resolved = 
@@ -3219,16 +3219,43 @@
 e(proxy,
+%0A    #                     
  dbus_in
@@ -3447,22 +3447,92 @@
 rties')%0A
-%0A%0Adef 
+    if str(r) == 'active':%0A        return True%0A    return True%0A%0A%0Adef pydbus_
 systemd_
@@ -3709,32 +3709,33 @@
 emd.LoadUnit('sy
+s
 temd-resolved.se

Fix linter, syntax