? modules/sms_legacy
Index: sms.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/smsframework/sms.module,v
retrieving revision 1.8.2.9.2.1
diff -u -p -r1.8.2.9.2.1 sms.module
--- sms.module	10 Mar 2009 19:13:28 -0000	1.8.2.9.2.1
+++ sms.module	5 May 2009 22:09:35 -0000
@@ -10,48 +10,141 @@
 /**
  * Sends a message using the active gateway.
  * 
- * @param $number
- *   The destination number.
  * 
  * @param $message
- *   The text of the messsage to send.
- * 
- * @param $options
- *   An array of dditional properties as defined by gateway modules.
- */
-function sms_send($number, $message, $options = array()) {
-  $gateway = sms_default_gateway();
+ *   a sms message object, containing at least $message->type
+ *   - for legacy this can also be the former variable $number, an is recognized so, if $a1 is set to string
+ * @param $a1
+ *   a optional variable to provide legacy behaviour, this will be recognized as $message->message, so message text
+ *   do NOT! set in none-legacy cases
+ * @param $a2
+ *   for legacy reasons: an array of additional properties as defined by gateway modules.
+ */
+function sms_send(&$message, $a1 = NULL, $a2 = NULL) {
+  $legacy = FALSE;
+  if (isset($a1)) {
+  	$number = $message;
+    $message = new stdClass();
+    $message->recipient = $number;
+    $message->text = $a2;
+    $message->gateway = array('options' => $a2);
+    $message->type = 'sms_legacy';
+    $legacy = TRUE;
+  }
+  //ensure $message->direction is set to 'out' for sending messages
+  $message->direction = 'out';
+  
+  //add the default gateway
+  //TODO: maybe gateway handling shall be rewritten too, and default gateway implement during 'pre send' if none present?
+  $message->gateway = (object)sms_default_gateway();
   
+  //PRE SEND
+  _sms_invokebyref('pre send',$message,'sms');
+  
+  //SEND
+  /** MOVED to sms_legacy.module
   foreach (module_implements('sms_send') as $module) {
     $function = $module .'_sms_send';
     $function($number, $message, $options, $gateway);
   }
+  */
+  _sms_invokebyref('send',$message,'sms');
+  
+  //finally SEND through gateway
+  //provide legacy send method for marked $gateways
+  if (isset($message->gateway->legacy) && $message->gateway->legacy) {
+  	$gateway = (array)$message->gateway;
+    $options = $gateway->options;
+    $number = $message->recipient;
+    $message = $message->text;
+    if (function_exists($gateway['send'])) {
+      $response = $gateway['send']($number, $message, $options);
+      $message->response = $response;//response has to fetched in legacy
+    }
+  }
+  else {
+  	if (isset($message->gateway->send) && $message->gateway->send) {
+  		$function = $message->gateway->send;
+      $function($message);//response will be set by non-legacy within the function
+  	}
+  }
+  //POST SEND
+  _sms_invokebyref('post send',$message,'sms');
+  
+  //sms_handle_result moved to sms_sms('post send'):
+  //legacy messages would expect a boolean value
+  // else none
+  if ($legacy) {
+    return ($message->response['status'] == TRUE);  
+  }
+  else {
+  	//TODO: necessary??
+  }
+}
 
-  if (function_exists($gateway['send'])) {
-    $response = $gateway['send']($number, $message, $options);
+/**
+ * Implementation of hook_sms()
+ */
+function sms_sms($op,&$message) {
+	if ($op = 'post send') {
+    $response = $message->respone;
+    $number = $message->recipient;
+		return sms_handle_result($response, $number, $message);
+	}
+}
+
+/**
+ * Helper function to invoke $message object by ref through all ops
+ * 
+ * @param $op
+ *   the operator for hook_sms
+ * @param
+ *   the message object
+ */
+function _sms_invokebyref($op,&$message,$hook = 'sms') {
+  foreach (module_implements($hook) as $module) {
+    $function = $module."_$hook";
+    $function($op,$message);	
   }
-  return sms_handle_result($response, $number, $message);
 }
 
+
 /**
  * Callback for incoming messages. Allows gateways modules to pass messages in
  * a standard format for processing.
  * 
- * @param $number
- *   The sender's mobile number.
- * 
  * @param $message
- *   The content of the text message.
- */
-function sms_incoming($number, $message, $options = array()) {
+ *   the message object
+ *   - for legacy cases this could be the sender's mobile number
+ * @param $a1
+ *   only use in legacy case: The content of the text message
+ * @param $a2
+ *   only used in legacy case: an $options array
+ */
+function sms_incoming($message, $a1 = NULL, $a2 = array()) {
+  
+  if (isset($a1)) {
+    $number = $message;
+    $message = new stdClass();
+    if (is_array($a2)) $message = (object)$a2;
+    $message->sender = $number;
+    $message->text = $a1;
+  }
+  
+  //TODO: rethink rules implementation with additional ops
   if (module_exists('rules')) {
-    rules_invoke_event('sms_incoming', $number, $message);
+    rules_invoke_event('sms_incoming', $message->sender, $message->text);
   }
 
   // Execute three phases
+  /** moved to sms_legacy.module
   module_invoke_all('sms_incoming', 'pre process', $number, $message, $options);
   module_invoke_all('sms_incoming', 'process', $number, $message, $options);
   module_invoke_all('sms_incoming', 'post process', $number, $message, $options);
+  */
+  _sms_invokebyref('pre receive',$message,'sms');
+  _sms_invokebyref('receive',$message,'sms');
+  _sms_invokebyref('post receive',$message,'sms');
 }
 
 /**
