Theory of imperfect waveguides: the effect of wall impedance