<?php
// $Id: file_mailsave.module,v 1.1 2009/09/24 18:43:27 miglius Exp $

/**
 * @file
 * Creates file attachments from email attachments
 */

//////////////////////////////////////////////////////////////////////////////
// Core API hooks

/**
 * Implementation of hook_perm().
 */
function file_mailsave_perm() {
  return array('attach files from email');
}

/**
 * Implementation of hook_mail().
 */
function file_mailsave_mail($key, &$message, $params) {
  $language = $message['language'];
  $account = isset($params['account']) ? $params['account'] : (object)array();
  $variables = array_merge(user_mail_tokens($account, $language), $params['variables'] ? $params['variables'] : array());
  $message['subject'] .= _file_mailsave_mail_text($key .'_subject', $language, $variables);
  $message['body'][] = _file_mailsave_mail_text($key .'_body', $language, $variables);
}

//////////////////////////////////////////////////////////////////////////////
// Mailsave API hooks

/**
 * Implementation of hook_mailsave().
 */
function file_mailsave_mailsave($node, $result, $i, $header, $mailbox) {
  if (!user_access('attach files from email'))
    return;

  $node->files = array();
  $i = 0;
  foreach ($node->mailsave_attachments as $file) {
    $file = (object)$file;

    // Call the validation functions.
    $errors = array();
    foreach (file_get_validators() as $function => $args) {
      array_unshift($args, $file);
      $errors = array_merge($errors, call_user_func_array($function, $args));
    }

    if (!empty($errors)) {
      $account = user_load($node->uid);
      $variables = array('!filename' => $file->filename, '!subject' => $header->subject, '!errors' => strip_tags(implode("\n", $errors)));
      $params = array('account' => $account, 'variables' => $variables);
      $message = drupal_mail('file_mailsave', 'file', $account->mail, user_preferred_language($account), $params);
    }
    else {
      $file_node = (object)array('nosave' => TRUE);
      if (!file_node_save($file_node, $file)) {
        watchdog('file_mailsave', 'Error saving file %file. Please, contact site administrator.', array('%file' => $file->filename), WATCHDOG_ERROR);
      }
      else {
        $file_node->file->list = 1;
        $file_node->file->weight = 0;
        $file_node->file->sid = 's_'. $i;
        $node->files['s_'. $i] = (array)$file_node->file;
      }
    }
    // mailsave creates temporal files with and without '_0' suffix.
    file_delete($file->filepath);
    file_delete(drupal_substr($file->filepath, 0, drupal_strlen($file->filepath)-2));
    $i++;
  }
  unset($node->mailsave_attachments);
  return $node;
}

//////////////////////////////////////////////////////////////////////////////
// Mail strings

/**
 * Returns a mail string for a variable name.
 *
 * Used by file_mailsave_mail() and the settings forms to retrieve strings.
 */
function _file_mailsave_mail_text($key, $language = NULL, $variables = array()) {
  $langcode = isset($language) ? $language->language : NULL;

  if ($admin_setting = variable_get('file_mailsave_mail_'. $key, FALSE)) {
    // An admin setting overrides the default string.
    return t($admin_setting, $variables, $langcode);
  }
  else {
    // No override, return default string.
    switch ($key) {
      case 'file_subject':
        return t('Re: !subject', $variables, $langcode);
      case 'file_body':
        return t("Saving file !filename at !site failed with the following error:\n\n!errors", $variables, $langcode);
    }
  }
}

