Index: gw/bb_alog.c
===================================================================
RCS file: /home/cvs/gateway/gw/bb_alog.c,v
retrieving revision 1.15
diff -a -u -r1.15 bb_alog.c
--- gw/bb_alog.c	9 Jan 2008 20:06:57 -0000	1.15
+++ gw/bb_alog.c	28 Nov 2008 02:43:02 -0000
@@ -100,6 +100,7 @@
  *   %t - the time of the message, formatted as "YYYY-MM-DD HH:MM:SS"
  *   %T - the time of the message, in UNIX epoch timestamp format
  *   %I - the internal message ID
+ *   %w - the smsc-provided message ID
  *
  * Most escape codes should be compatible with escape codes used in
  * sms-service groups.
@@ -299,6 +300,11 @@
             }
             break;
 
+	case 'w': /* the smsc-provided message ID */
+	    if (msg->sms.foreign_id != NULL)
+	        octstr_append(result, msg->sms.foreign_id);
+	    break;
+
         /* XXX add more here if needed */
 
 	case '%':
Index: gw/dlr.c
===================================================================
RCS file: /home/cvs/gateway/gw/dlr.c,v
retrieving revision 1.56
diff -a -u -r1.56 dlr.c
--- gw/dlr.c	19 Feb 2008 11:12:30 -0000	1.56
+++ gw/dlr.c	28 Nov 2008 02:43:11 -0000
@@ -392,6 +392,10 @@
         O_SET(msg->sms.sender, dlr->source);
         /* if dlr_url was present, recode it here again */
         O_SET(msg->sms.dlr_url, dlr->url);
+        /* add the foreign_id */
+        Octstr *tmp = octstr_duplicate(ts);
+        O_SET(msg->sms.foreign_id, tmp);
+        octstr_destroy(tmp);
         /* 
          * insert original message to the data segment 
          * later in the smsc module 
@@ -468,6 +472,7 @@
     dlrmsg->sms.dlr_url = octstr_duplicate(msg->sms.dlr_url);
     dlrmsg->sms.msgdata = octstr_duplicate(reply);
     dlrmsg->sms.boxc_id = octstr_duplicate(msg->sms.boxc_id);
+    dlrmsg->sms.foreign_id = octstr_duplicate(msg->sms.foreign_id);
     time(&dlrmsg->sms.time);
 
     debug("dlr.dlr", 0,"SMSC[%s]: DLR = %s",
Index: gw/msg-decl.h
===================================================================
RCS file: /home/cvs/gateway/gw/msg-decl.h,v
retrieving revision 1.34
diff -a -u -r1.34 msg-decl.h
--- gw/msg-decl.h	9 Jan 2008 20:06:57 -0000	1.34
+++ gw/msg-decl.h	28 Nov 2008 02:43:12 -0000
@@ -86,6 +86,7 @@
 		INTEGER(time);
 		OCTSTR(smsc_id);
 		OCTSTR(smsc_number);
+		OCTSTR(foreign_id);
 		OCTSTR(service);
 		OCTSTR(account);
 		UUID(id);
@@ -107,8 +108,8 @@
 		INTEGER(msg_left);
 		VOID(split_parts);
 		INTEGER(priority);
-                INTEGER(resend_try);
-                INTEGER(resend_time);
+		INTEGER(resend_try);
+		INTEGER(resend_time);
 	})
 
 MSG(ack,
Index: gw/urltrans.c
===================================================================
RCS file: /home/cvs/gateway/gw/urltrans.c,v
retrieving revision 1.108
diff -a -u -r1.108 urltrans.c
--- gw/urltrans.c	24 Jun 2008 11:28:41 -0000	1.108
+++ gw/urltrans.c	28 Nov 2008 02:43:37 -0000
@@ -617,6 +617,14 @@
      *   * validity, deferred, rpi - we don't receive these from smsc
      *   * username, password, dlr-url, account - nonsense to send
      */
+    case 'w':
+        if (request->sms.foreign_id == NULL)
+        	break;
+        enc = octstr_duplicate(request->sms.foreign_id);
+        octstr_url_encode(enc);
+        octstr_append(result, enc);
+        octstr_destroy(enc);
+        break;
 
     case '%':
         octstr_format_append(result, "%%");
Index: gw/smsc/smsc_smpp.c
===================================================================
RCS file: /home/cvs/gateway/gw/smsc/smsc_smpp.c,v
retrieving revision 1.107
diff -a -u -r1.107 smsc_smpp.c
--- gw/smsc/smsc_smpp.c	21 Oct 2008 12:43:07 -0000	1.107
+++ gw/smsc/smsc_smpp.c	28 Nov 2008 02:43:49 -0000
@@ -1513,6 +1513,7 @@
                     }
                 }
 
+                msg->sms.foreign_id = octstr_duplicate( tmp );
                 /* SMSC ACK.. now we have the message id. */
                 if (DLR_IS_ENABLED_DEVICE(msg->sms.dlr_mask))
                     dlr_add(smpp->conn->id, tmp, msg);
