Page 1 of 1

MobileProxy: 2-Way Mobile Device Proxy

Posted: 2007-12-27 05:50
by redrummy
MobileProxy:
Link hMS mailboxes to phones with SMS e-mail addresses for 2-way message proxy - Basically a very limited function hMS client on the phone. It's almost like having a BlackBerry. Well, not really, but it can run on hMS with any phone that has an SMS e-mail address. It's a matter of principle. ;)
- Receive SMS-friendly copies of new hMS mailbox messages on phone
- Send e-mail through hMS from phone (sent "from" hMS mailbox)
- Optional confirmation of sent message intended recipient back to phone
- Optional debug logging to hMS event log
- Requires hMailServer v4.4.x? (developed on 4.4.2-B275)

Background:
MobileProxy is an evolution of "Forward SMS-compatible message" (http://www.hmailserver.com/forum/viewtopic.php?t=11183). Thanks to cmurphy54 for the code that got me started. I'd still be scratching my head without it.

Disclaimer:
Warning! Use of this script is solely at your option and risk. No warranties or claims of fitness of any kind are provided. I don't know VBS so it's amazing that I got this to work. I'm sure that a skilled coder would find a more elegant way to do it and would likely see errors in logic, practice, and general sanity with what it written, but like I said (see previous sentence).

Feedback for for education and/or improvement is welcome. This script is theoretically written for multiple pairings but is not tested it for more than one. If you think you would find this useful but have problems with it let me know and I'll try to help. No promises. ;)

Without further adieu, MobileProxy:

Code: Select all

' MobileProxy function for hMailserver v2007123001 (redrummy) 
'   requires hMailServer v4.4.x? (developed on 4.4.2-B275) 
'   for discussion see http://www.hmailserver.com/forum/viewtopic.php?t=11209 

' Function: 
' Proxy messages for e-mail enabled mobile device (pseudo-BlackBerry functionality) 
'   using paired addresses for hMS mailbox and mobile device (see Usage) 

'  Setup: 
' Mandatory - Edit oProxyPairs and oNotifyPairs as indicated under InitializeVariables 
' Optional - Edit Const strings as desired or necessary to suit your needs 
' Call with - Inbox Rule / Action / Run function / Script function: MobileProxy 
'   (Recommended - Call after rules that filter undesirable or low priority mail)

' Usage: 
' Proxy mode - Send message from mobile device to paired mailbox with intended 
'   recipient address as 1st word of message - MobileProxy will send message from 
'   hMS mailbox to intended recipient with all message text following 1st word 
' Notify mode - Messages received in mailbox NOT from paired mobile device are 
'   copied to mobile device address with SMS-friendly version of message 
'   from original sender (if mobile carrier relay server permits, see Notify strings) 

Option Explicit 

'*************************************************************** 
' User defined strings 
'*************************************************************** 

' Notify strings: 
' Auto-replaced strings are: 
'   [mailbox] (hMS paired mailbox address)
'   [sender] (Internet sender from address)
'   [subject] (Subject of received message)
'   [body] (Text-only body of received message)
'   [n] (newline/CRLF) 
' xxxChars - Number of characters to include from original message subject/body text 
' Note: Oubound message size will be SubjChars + BodyChars + additional literal or [replaced] strings 
Const conNotifyFrom = "[sender]" 
Const conNotifySubject = "[subject]" 
Const conNotifySubjChars = 20 
Const conNotifyBody = "([mailbox])[n][body]" 
Const conNotifyBodyChars = 80 

' Proxy strings: 
' Auto-replaced strings are: 
'   [sender] (hMS paired mailbox address)
'   [recipient] (intended recipient address)
' Confirm - Send confirmation to mobile of intended proxy recipient = 1 for yes, 0 for no 
Const conProxySubject = "Mobile Message from [sender]" 
Const conProxyConfirm = 1 
Const conProxyConfSubj = "Confirmation ([recipient])" 
Const conProxyConfBody = "Message submitted for delivery to [recipient]"

' write to hmailserver_events.log for debugging = 1 for yes, 0 for no 
Const conDebug = 1

'*************************************************************** 
' Initialize variables, define notify and proxy pairs 
'*************************************************************** 

Dim oProxyPairs, oNotifyPairs  

Sub InitializeVariables 
  ' Create dictionary objects for our notification and proxy lookups 
  Set oProxyPairs = CreateObject("Scripting.Dictionary") 
  Set oNotifyPairs = CreateObject("Scripting.Dictionary") 

  ' Add proxy pairs in the format: oProxyPairs.Add "1234567890@mobile.domain", "user@hmailserver.domain" 
  ' oProxyPairs.Add "1234567890@mobile.domain", "user@hmailserver.domain" 
  oProxyPairs.Add "1234567890@mobile.domain", "user@hmailserver.domain" 

  ' Add notify pairs in the format: oNotifyPairs.Add "user@hmailserver.domain", "1234567890@mobile.domain" 
  ' oNotifyPairs.Add "user@hmailserver.domain", "1234567890@mobile.domain" 
  oNotifyPairs.Add "user@hmailserver.domain", "1234567890@mobile.domain" 

End Sub 

'*************************************************************** 
' MobileProxy main function 
'*************************************************************** 

Function MobileProxy(oMessage) 
  DebugLog("--- MobileProxy Function Start ---") 
  InitializeVariables 
  NotifyMobile(oMessage) 
  DisposeVariables 
  DebugLog("--- MobileProxy Function End ---") 
End Function 

'*************************************************************** 
' Debug log sub 
'*************************************************************** 

Sub DebugLog(strDebugText)
  If conDebug = 1 Then EventLog.Write(strDebugText) End If 
End Sub

'*************************************************************** 
' Notify to mobile sub
'*************************************************************** 

Sub NotifyMobile(oMessage) 
  Dim i, strNotifySender, strNotifyMailbox, strNotifyFrom, strNotifyRcpt, strNotifySubj, strNotifySubjShort, strNotifyBody, strNotifyBodyShort 
  DebugLog("INBOX subj: " & oMessage.Subject) 
  DebugLog("INBOX from: " & oMessage.FromAddress) 
  ' Check message recipients for notify pairings 
  For i = 0 To oMessage.Recipients.Count - 1 
    strNotifyMailbox = oMessage.Recipients(i).Address 
    If oNotifyPairs.Exists(strNotifyMailbox) Then 
      DebugLog("INBOX rcpt: " & strNotifyMailbox & " (paired, process)") 
      ' Check if sender is mobile device 
      If oProxyPairs.Exists(oMessage.FromAddress) Then 

        ' JUMP TO PROXY SUB 
        DebugLog("ACTION: Proxy") 
        ProxyMobile(oMessage) 

        Else 
        ' CONTINUE NOTIFY SUB 
        DebugLog("ACTION: Notify") 
        strNotifySender = oMessage.FromAddress 
        ' Get proxy sender address pair key 
        strNotifyRcpt = oNotifyPairs.Item(strNotifyMailbox) 
        DebugLog("NOTIFY pairing: " & strNotifyMailbox & " > " & strNotifyRcpt) 
        ' Trim subject and message body text to specified lengths, tag if trimmed 
        strNotifySubjShort = Left(oMessage.Subject, conNotifySubjChars) 
        If not strNotifySubjShort = oMessage.Subject Then 
          strNotifySubjShort = strNotifySubjShort & "(...)" 
        End If 
        strNotifyBodyShort = Left(oMessage.Body, conNotifyBodyChars) 
        If not strNotifyBodyShort = oMessage.Body Then 
          strNotifyBodyShort = strNotifyBodyShort & "(...)" 
        End If 
        ' Remove coded returns & spaces 
        strNotifyBodyShort = Replace(strNotifyBodyShort, " =" & vbCRLF, "") 
        strNotifyBodyShort = Replace(strNotifyBodyShort, "=0D", "") 
        strNotifyBodyShort = Replace(strNotifyBodyShort, "=20", "") 
        ' Auto-replace supported strings 
        strNotifyFrom = conNotifyFrom 
        strNotifyFrom = Replace(strNotifyFrom, "[mailbox]", strNotifyMailbox) 
        strNotifyFrom = Replace(strNotifyFrom, "[sender]", strNotifySender) 
        DebugLog("NOTIFY from: " & strNotifyFrom) 
        strNotifySubj = conNotifySubject 
        strNotifySubj = Replace(strNotifySubj, "[mailbox]", strNotifyMailbox) 
        strNotifySubj = Replace(strNotifySubj, "[sender]", strNotifySender) 
        strNotifySubj = Replace(strNotifySubj, "[subject]", strNotifySubjShort) 
        DebugLog("NOTIFY subj: " & strNotifySubj) 
        strNotifyBody = conNotifyBody 
        strNotifyBody = Replace(strNotifyBody, "[mailbox]", strNotifyMailbox) 
        strNotifyBody = Replace(strNotifyBody, "[sender]", strNotifySender) 
        strNotifyBody = Replace(strNotifyBody, "[subject]", strNotifySubjShort) 
        strNotifyBody = Replace(strNotifyBody, "[n]", vbCRLF) 
        strNotifyBody = Replace(strNotifyBody, "[body]", strNotifyBodyShort) 
        ' Send message 
        SendMessage strNotifyFrom, strNotifyRcpt, strNotifySubj, strNotifyBody, strNotifyMailbox 
      End If 
      ' If no pairing for recipient then skip it  
     Else DebugLog("INBOX rcpt: " & strNotifyMailbox & " (not paired, ignore)") 
    End If 
  Next 
End Sub 

'*************************************************************** 
' Proxy for mobile sub
'*************************************************************** 

Sub ProxyMobile(oMessage) 
  Dim arrProxyMsg, strProxyMsg, strProxyLocal, strProxyMobile, strProxySubject, strProxyTo, strProxyConfSubj, strProxyConfBody 
  'Get proxy sender address pair key 
  strProxyMobile = oMessage.FromAddress 
  strProxyLocal = oProxyPairs.Item(strProxyMobile) 
  DebugLog("PROXY pairing: " & strProxyMobile & " > " & strProxyLocal) 
  strProxyMsg = Replace(oMessage.Body, vbCRLF, " ") 
  DebugLog("PROXY message: " & strProxyMsg) 
  ' Get the 1st word of the message (delimited by space) 
  arrProxyMsg = Split(strProxyMsg) 
  strProxyTo = arrProxyMsg(0) 
  ' Check if 1st word contains "@" (therefore assumed to be an e-mail address) 
  If InStr(strProxyTo, "@") > 1 Then 
    DebugLog("PROXY from: " & strProxyLocal) 
    DebugLog("PROXY rcpt: " & strProxyTo) 
    ' Remove recipient address and coded returns & spaces from body text
    strProxyMsg = Replace(strProxyMsg, strProxyTo & " ", "") 
	strProxyMsg = Replace(strProxyMsg, " =" & vbCRLF, "")
    strProxyMsg = Replace(strProxyMsg, "=0D", "")
    strProxyMsg = Replace(strProxyMsg, "=20", "")
    ' Auto-replace supported strings 
    strProxySubject = conProxySubject 
    strProxySubject = Replace(strProxySubject, "[sender]", strProxyLocal) 
    DebugLog("PROXY subj: " & strProxySubject) 
    ' Send message 
    SendMessage strProxyLocal, strProxyTo, strProxySubject, strProxyMsg, strProxyMobile 
    ' Optionally send confirmation of intended recipient back to mobile 
    If conProxyConfirm = 1 Then 
      DebugLog("PROXY conf: Yes") 
      ' Auto-replace supported strings 
      strProxyConfSubj = conProxyConfSubj 
      strProxyConfSubj = Replace(strProxyConfSubj, "[sender]", strProxyMobile) 
      strProxyConfSubj = Replace(strProxyConfSubj, "[recipient]", strProxyTo) 
      strProxyConfBody = conProxyConfBody 
      strProxyConfBody = Replace(strProxyConfBody, "[sender]", strProxyMobile) 
      strProxyConfBody = Replace(strProxyConfBody, "[recipient]", strProxyTo) 
      ' Send message 
      SendMessage strProxyLocal, strProxyMobile, strProxyConfSubj, conProxyConfBody, strProxyMobile 
     Else DebugLog("PROXY conf: No") 
    End If
   ' If message from mobile is not for relay then skip it 
   Else DebugLog("PROXY rcpt: (none, don't proxy)") 
  End If 
End Sub 

'*************************************************************** 
' Send message sub 
'*************************************************************** 

Sub SendMessage(strSendFrom, strSendRecipient, strSendSubject, strSendBody, strSendSource) 
  Dim oNewMessage 
  DebugLog("SEND from: " & strSendFrom) 
  DebugLog("SEND subj: " & strSendSubject) 
  DebugLog("SEND rcpt: " & strSendRecipient) 
  Set oNewMessage = CreateObject("hMailServer.Message") 
  With oNewMessage 
    .HeaderValue("X-hMailServer-MobileProxy-Source") = strSendSource 
    .HeaderValue("From") = "<" & strSendFrom & ">"
    .FromAddress = strSendFrom
	.AddRecipient "", strSendRecipient 
    .Subject = strSendSubject 
    .Body = strSendBody 
    .Save 
  End With 
  Set oNewMessage = Nothing 
  DebugLog("SEND stat: Submitted") 
End Sub 

'*************************************************************** 
' Dispose variables sub 
'*************************************************************** 

Sub DisposeVariables 
  Set oNotifyPairs = Nothing 
End Sub

Posted: 2007-12-28 05:38
by redrummy
EDIT: So, I discovered that I had to manually insert

Code: Select all

oMessage.HeaderValue("From") = "<" & strSendFrom & ">"
when creating a message in the send message sub because

Code: Select all

oMessage.FromAddress = strSendFrom
doesn't seem to perform this and the message arrives without a sender (or is blocked) at the other end. Not sure if this is intended behavior or not but things seem to be working now. The updated script is posted above.

Posted: 2007-12-29 02:13
by redrummy
UPDATE v2007122802:
- Improved debug log readability
- Added header "X-hMailServer-MobileProxy-Source" on sent message

UPDATE v2007123001:
- Create sub for debug log (duh, I guess there's a reason others do this)

The script in the original post is updated.

ToDo:
- Unify Notify & Proxy pairings, add display names (probably w/ array)

Re: MobileProxy: 2-Way Mobile Device Proxy

Posted: 2017-12-07 04:03
by palinka
This is great. It's pretty old now but still a great concept and very useful. I set it up and did some testing on HMS 5.6.6 and its sending the message to my cell phone, but when I reply from mobile, it gets sent to the original sender but with my sms email (123456789@sms.mobilecarrier.com) as the sender - not the email address it's paired with in the script. My sms is replying to the original sender directly. It's not sending the message through HMS. Any ideas about how to fix this?