How to use ResponseListener

A plugin which implements Swift_Events_ResponseListener will be invoked every time Swift receives a new response from the connection. This can come in useful if used in combination with a CommandListener because if you know what response codes to expect, you can “correct” problems by issuing the RSET command or whatever other action may be appropriate. It probably helps to know a little about the SMTP protocol before attempting such things however.

The plugin receives an instance of Swift_Events_ResponseEvent, which incidentally is the object which gets returned from command() inside Swift. Therefore it would also be technically possible to “trick” Swift into believing that a different response was issued to what was actually issued. Would that be useful? To be honest, I don’t know, I’ve never had cause to do it ;)

Here’s the interface:

/**
 * Contains the list of methods a plugin requiring the use of a ResponseEvent must implement
 * @package Swift_Events
 * @author Chris Corbyn <chris@w3style.co.uk>
 */
interface Swift_Events_ResponseListener extends Swift_Events_Listener
{
	/**
	 * Executes when Swift receives a response
	 * @param Swift_Events_ResponseEvent Information about the response
	 */
	public function responseReceived(Swift_Events_ResponseEvent $e);
}

And here’s an example of how you might kid Swift that it always receives the response codes it expects. Note that we need to implement two interfaces here.

class NeverArguesBackPlugin
    implements Swift_Events_BeforeCommandListener, Swift_Events_ResponseListener
{
    protected $code;
    
    public function beforeCommandSent(Swift_Events_CommandEvent $e)
    {
        $this->code = $e->getCode();
    }
 
    public function responseReceived(Swift_Events_ResponseEvent $e)
    {
        if ($this->code)
        {
            $e->setCode($this->code);
        }
    }
}

Nasty indeed! :)

 
v3/plugindev/responseevent.txt · Last modified: 2007/03/22 16:33 by d11wtq
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki