Repository: OCA/server-tools

Issue:
Errors produced by fetchmail_attach_from_folder
I'm getting this log error every time fetchmail is run in crontab or manually

```
2015-09-02 06:09:04,798 9318 INFO robosavvy openerp.addons.fetchmail_attach_from_folder.model.fetchmail_server: start checking for emails in CRM-Leads server Nuno
2015-09-02 06:09:05,024 9318 INFO robosavvy openerp.addons.fetchmail_attach_from_folder.model.fetchmail_server: finished checking for emails in CRM-Leads server Nuno
2015-09-02 06:09:05,024 9318 INFO robosavvy openerp.addons.fetchmail_attach_from_folder.model.fetchmail_server: start checking for emails in CRM-Customers server Nuno
2015-09-02 06:09:05,272 9318 INFO robosavvy openerp.addons.fetchmail_attach_from_folder.model.fetchmail_server: finished checking for emails in CRM-Customers server Nuno
2015-09-02 06:09:05,708 9318 ERROR robosavvy openerp.addons.base.ir.ir_cron: Call of self.pool.get('fetchmail.server')._fetch_mails(cr, uid, *()) failed in Job 6
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_cron.py", line 138, in _callback
    getattr(model, method_name)(cr, uid, *args)
  File "/usr/lib/python2.7/dist-packages/openerp/api.py", line 241, in wrapper
    return old_api(self, *args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/openerp/addons/fetchmail/fetchmail.py", line 187, in _fetch_mails
    return self.fetch_mail(cr, uid, ids, context=context)
  File "/usr/lib/python2.7/dist-packages/openerp/api.py", line 241, in wrapper
    return old_api(self, *args, **kwargs)
  File "/var/lib/odoo/.local/share/Odoo/addons/8.0/fetchmail_attach_from_folder/model/fetchmail_server.py", line 73, in fetch_mail
    connection.close()
  File "/usr/lib/python2.7/imaplib.py", line 394, in close
    typ, dat = self._simple_command('CLOSE')
  File "/usr/lib/python2.7/imaplib.py", line 1087, in _simple_command
    return self._command_complete(name, self._command(name, *args))
  File "/usr/lib/python2.7/imaplib.py", line 837, in _command
    ', '.join(Commands[name])))
error: command CLOSE illegal in state AUTH, only allowed in states SELECTED
```

There are two folder rules for IMAP account "Nuno":

This rule creates leads from emails showing up in folder CRM-Leads - it works as expected (although unfortunately it only catches incoming emails, not outgoing emails) 
![crm-leads](https://cloud.githubusercontent.com/assets/10811472/9659663/87aa2de6-5249-11e5-9ca7-3ec43818241e.png)

This rule is supposed to create message threads associated with customers - this doesnt seem to do anything. no mails are read :(
![crm-customers](https://cloud.githubusercontent.com/assets/10811472/9659718/daa90cec-5249-11e5-925d-ab1c243c2633.png)



Discussion:
@limor1 say `connection.debug = 4` after https://github.com/OCA/server-tools/blob/8.0/fetchmail_attach_from_folder/model/fetchmail_server.py#L70 and post the output. This logs to stderr, so you'll have to run your server in the foreground

Hi, i got the same problem with following configuration:
1. some incoming email accounts without using folder feature
2. one incoming email account with two folder lines on INBOX

The second part works well. The first part not, because of this error above.

My very fast but ugly solution in fetchmail_server(skip connection if no folders):

```
#FROM
connection = this.connect()
for folder in this.folder_ids.filtered('active'):
    this.handle_folder(connection, folder)
connection.close()

#TO
if this.folder_ids.filtered('active'):
    connection = this.connect()
    for folder in this.folder_ids.filtered('active'):
        this.handle_folder(connection, folder)
    connection.close()
```


Resolving patch:
diff --git a/fetchmail_attach_from_folder/model/fetchmail_server.py b/fetchmail_attach_from_folder/model/fetchmail_server.py
--- a/fetchmail_attach_from_folder/model/fetchmail_server.py
+++ b/fetchmail_attach_from_folder/model/fetchmail_server.py
@@ -60,6 +60,8 @@ def fetch_mail(self, cr, uid, ids, context=None):
         for this in self.browse(cr, uid, ids, context):
             if this.object_id:
                 check_original.append(this.id)
+            if not this.folder_ids.filtered('active'):
+                continue
 
             context.update(
                 {


Test patch: