Index: twitter.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/twitter/twitter.module,v
retrieving revision 1.1
diff -u -p -r1.1 twitter.module
--- twitter.module	23 Jan 2007 17:05:50 -0000	1.1
+++ twitter.module	20 Nov 2007 06:21:10 -0000
@@ -25,6 +25,13 @@ function twitter_user($op, &$edit, &$acc
         '#title' => t('Text format'),
         '#default_value' => $edit['twitter_text'],
         '#description' => t('Format of the text to submit. Use !title and !url for the post title and url respectively.'));
+    $form['twitter']['twitter_throttle'] = array(
+                                                  '#title' => t('Throttle'),
+                                                  '#description' => t('Limit number of posts to X per hour. Leave blank for no throttle'),
+                                                  '#type' => 'textfield',
+                                                  '#size' => 4,
+                                                  '#default_value' => $edit['twitter_throttle'],
+                                                );
       return $form;
     case 'insert':
     case 'update':
@@ -53,6 +60,29 @@ function twitter_post($node, $account) {
   if (empty($account->twitter_encrypted)) {
     return false;
   }
+  
+  $max_per_hour = $account->twitter_throttle;
+  
+  //If they want to thottle the max twitter posts per hour
+  if (!empty($max_per_hour)) {
+    $last_post = variable_get('twitter_hour_of_last_post', 0);
+    $this_hour = intval(date('Yzg'));
+    $num_this_hour = variable_get('twitter_num_post_this_hour', 0);
+
+    //if the last post was in the same hour
+    //and we've reach the limit
+    if ($last_post == $this_hour && $num_this_hour >= $max_per_hour) {
+      drupal_set_message(t('Already posted to twitter %max times this hour. Not posting again', array('%max'=> $max_per_hour)));
+      return;
+    }
+
+    //if it's a new hour
+    if ($last_post != $this_hour) {
+      //It's a new hour.
+      variable_set('twitter_num_post_this_hour', 0);
+      $num_this_hour = 0;
+    }
+  }
 
   $text = ($account->twitter_text) ? $account->twitter_text : 'New post: !title (!url)';
   $text = t($text, array('!title' => $node->title,
@@ -62,6 +92,21 @@ function twitter_post($node, $account) {
                    'Content-type' => 'application/x-www-form-urlencoded');
   $data = 'status='. urlencode($text);
 
+
   $result = drupal_http_request(TWITTER_URL, $headers, 'POST', $data);
-  drupal_set_message(t('Posted to twitter.com'));
+
+  if ($result->code == '200') {
+    drupal_set_message(t('Posted to twitter.com'));
+    //Mark the info we need to calculate throttle
+    if (!empty($max_per_hour)) {
+      //mark the hour of this post
+      variable_set('twitter_hour_of_last_post', $this_hour);
+      //increment the number this hour
+      variable_set('twitter_num_post_this_hour', intval($num_this_hour) + 1);
+    }
+  }
+  else {
+    drupal_set_message(t('Post to twitter.com failed'), 'error');
+  }
+
 }
\ No newline at end of file
