--- /dev/null	2010-09-30 00:27:40.488288380 -0400
+++ feeds.rules.inc	2010-09-30 03:26:42.287768355 -0400
@@ -0,0 +1,206 @@
+<?php
+// $Id$
+
+/**
+ * Implementation of hook_rules_action_info().
+ * @ingroup rules
+ */
+function feeds_rules_action_info() {
+  return array(
+    'feeds_rules_action_import' => array(
+      'label' => t('Import a feed'),
+      'arguments' => array(
+        'feed_source' => array(
+          'type' => 'feed_source',
+          'label' => t('Feed source'),
+        ),
+      ),
+      'module' => 'Feeds',
+    ),
+
+    'feeds_rules_action_set_url' => array(
+      'label' => t('Set the url of a feed source'),
+      'arguments' => array(
+        'feed_source' => array('type' => 'feed_source', 'label' => t('Feed source')),
+        'feed_url' => array('type' => 'string', 'label' => t('Url')),
+      ),
+      'module' => 'Feeds',
+    ),
+
+    'feeds_rules_action_set_path' => array(
+      'label' => t('Set the filepath of a feed source'),
+      'arguments' => array(
+        'feed_source' => array('type' => 'feed_source', 'label' => t('Feed source')),
+        'feed_path' => array('type' => 'string', 'label' => t('Path')),
+      ),
+      'module' => 'Feeds',
+    ),
+
+    'feeds_rules_action_load' => array(
+      'label' => t('Load a feed source by importer and optionally node id'),
+      'arguments' => array(
+        'feed_importer' => array(
+          'type' => 'string',
+          'label' => t('Importer name')
+        ),
+        'feed_nid' => array(
+          'type' => 'number',
+          'label' => t('Node id of source if importer is attached to a node'),
+          'required' => FALSE,
+        ),
+      ),
+      'new variables' => array(
+        'feed_source' => array(
+          'type' => 'feed_source',
+          'label' => t('Loaded feed source'),
+        ),
+      ),
+      'module' => 'Feeds',
+    ),
+
+    'feeds_rules_action_clear' => array(
+      'label' => t('Clear a feed source'),
+      'arguments' => array(
+        'feed_source' => array('type' => 'feed_source', 'label' => t('Feed source')),
+      ),
+      'module' => 'Feeds',
+    ),
+
+    'feeds_rules_action_load_from_node' => array(
+      'label' => t('Load a feed source from a node'),
+      'arguments' => array(
+        'node' => array('type' => 'node', 'label' => t('Node to load feed source from')),
+      ),
+      'new variables' => array(
+        'feed_source_from_node' => array(
+          'type' => 'feed_source',
+          'label' => t('Feed source loaded from node'),
+        ),
+      ),
+      'module' => 'Feeds',
+    ),
+  );
+}
+
+/**
+ * Callabck for the action feeds_rules_action_import.
+ *
+ * TODO: Add option to use batch?  Combine this function with the next, similar
+ * to _feeds_rules_action_set_path.
+ */
+function feeds_rules_action_import($source) {
+  _feeds_rules_action_helper($source, 'import');
+}
+
+/**
+ * Callabck for the action feeds_rules_action_clear.
+ *
+ * TODO: Add option to use batch?
+ */
+function feeds_rules_action_clear($source) {
+  _feeds_rules_action_helper($source, 'clear');
+}
+
+/**
+ * Helper function for feeds_rules_action_clear and feeds_rules_action_import.
+ */
+function _feeds_rules_action_helper($source, $action, $action) {
+  try {
+    while ($state != $source->$action());
+  }
+  catch (Exception $e) {
+    /**
+     * TODO: Determine if $souce is attached to a node and use node title.
+     */
+    $importer_config = $source->importer->getConfig();
+    drupal_set_message(t("There was an eror {$action}ing: %importer",
+      array('%importer' => $importer_config['name']))
+    );
+    return FALSE;
+  }
+}
+
+/**
+ * Loads a feed.
+ */
+function feeds_rules_action_load($importer, $nid) {
+  return array('feed_source' => feeds_source($importer, $nid));
+}
+
+/**
+ * Set the url of a feed source that uses the FeedsHTTPFetcher.
+ */
+function feeds_rules_action_set_url($source, $url) {
+  _feeds_rules_action_set_path($source, $url, 'FeedsHTTPFetcher');
+}
+
+/**
+ * Set the path of a feed source that uses the FeedsFileFetcher.
+ */
+function feeds_rules_action_set_path($source, $path) {
+  _feeds_rules_action_set_path($source, $path, 'FeedsFileFetcher');
+}
+
+/**
+ * Helper function for the previous two functions.
+ *
+ * TODO: Validation of path.
+ */
+function _feeds_rules_action_set_path($source, $path, $type) {
+  $config = $source->getConfig();
+  if (isset($config[$type])) {
+    $config[$type]['source'] = $url;
+    $source->setConfig($config);
+  }
+  return array('feed_source' => $source);
+}
+
+/**
+ * Loads a feed source based on the node provided.
+ * Currently will only find the first importer bound to the node type.
+ */
+function feeds_rules_action_load_from_node($node) {
+  foreach (feeds_enabled_importers() as $feed_id) {
+    $feed = feeds_importer($feed_id);
+    if ($feed->config['content_type'] == $node->type) {
+      return array('feed_source_from_node' => feeds_source($feed_id, $node->nid));
+    }
+  }
+}
+
+/**
+ * Implementation of hook_rules_data_type_info().
+ */
+function feeds_rules_data_type_info() {
+  return array(
+    'feed_source' => array(
+      'label' => t('Feed Source'),
+      'class' => 'feed_source_rules_data_type',
+      'savable' => TRUE,
+      'identifiable' => TRUE,
+      'uses_input_form' => FALSE,
+      'module' => 'Feeds',
+    ),
+  );
+}
+
+/**
+ * Provides a data type of feed_source to rules. Allows for intelligent saving.
+ */
+class feed_source_rules_data_type extends rules_data_type {
+  function save() {
+    $source = &$this->get();
+    $source->save();
+    return TRUE;
+  }
+
+  function load($source_id) {
+    return feeds_source($source_id['importer'], $source_id['nid']);
+  }
+
+  function get_identifier() {
+    $source = &$this->get();
+    $importer_config = $source->importer->getConfig();
+    return array('importer' => $importer_config['name'], 'nid' => $source->feed_nid);
+  }
+}
