home *** CD-ROM | disk | FTP | other *** search
/ .net 1999 December / netCD65.iso / pc / Software / VirtualA / 16bit / vaper16.exe / %MYDIR% / Hmimail.ebs < prev    next >
Encoding:
Text File  |  1999-10-06  |  35.4 KB  |  909 lines

  1. 'T:HMIMAIL.EBS for CompuServe
  2. ' VA 4.01 release
  3.  
  4. Declare Function ParseString(args As String, delim As String) As String
  5. Declare Function FileName$(fullpath As String)
  6. Declare Sub AddToMsgFile(SourceFileName as String)
  7. Declare Function QueueFile(service As String, sfilename As String, queueflags As Long) As Boolean
  8. Declare Function MyDate$
  9. Declare Sub ReportSuccess(msg As String)
  10. Declare Function ForumName(ByVal forum As String) As String
  11. Declare Function FullErrorMessage() As String
  12. Declare Function MakeDateString(s As HMITIMESTAMP) As String
  13. Declare Sub CaptureLine(textline As String)
  14. Declare Sub AddToFile(TargetFileName As String, SourceFileName as String)
  15. Declare Function FileUrl(fn As String) As String
  16.  
  17. Const BlockSize = 2048
  18.  
  19. 'T:iFlags for CompuServe (constant)
  20. Const IM_DELETE = 2048        ' Delete scratchpad file afterwards?
  21.  
  22. Const fRECEIPT = 1         'CIS mail receipt
  23. Const fPRIVATE = 2         'CIS private forum message
  24. Const fNOFORMAT = 4         'CIS unformatted message
  25. Const fBINARY = 8         'CIS binary file
  26. Const fDONTCOPY = 16     'don't copy the message back to you
  27.  
  28. Public DownloadDir As String
  29. Public WorkingPath As String
  30. Public WhereAmI As String
  31. Public currentCAP As Integer
  32. Public EMPconfig As EMPCONFIG
  33. Public MailName As String
  34. Public PersonalAddress As String
  35. Dim MailSubject As String, MailFilename As String
  36. Dim MailAddresses As String
  37. Dim MailCount As Integer, MailFlags As Integer
  38. Dim OldMail As Boolean, MailSent As Boolean
  39.  
  40. Const HMI_STATE_FIRST% = 0
  41. Const HMI_STATE_NEXT% = HMI_STATE_FIRST + 1
  42. Const HMI_STATE_ABORT% = HMI_STATE_NEXT + 1
  43.  
  44. ' enum EMPCONTENTTYPES:
  45. Const EMP_TEXT% = 0                                  ' Ascii Text Object
  46. Const EMP_BINARY% = EMP_TEXT + 1                  ' 8-bit data
  47. Const EMP_GIF% = EMP_BINARY + 1                      ' Graphic Interchange Format
  48. Const EMP_BINARY_FILE% = EMP_GIF + 1              ' Contents_Name contains filename
  49. Const EMP_TEXT_FILE% = EMP_BINARY_FILE + 1          ' Text file
  50. Const EMP_TEXT_OBJECT% = EMP_TEXT_FILE + 1        ' TextObject message
  51. Const EMP_JPEG% = EMP_TEXT_OBJECT + 1
  52.  
  53. ' enum EMPIMPORTANCETYPES:
  54. Const EMP_LOW_IMPORTANCE% = 0
  55. Const EMP_NORMAL_IMPORTANCE% = EMP_LOW_IMPORTANCE + 1
  56. Const EMP_HIGH_IMPORTANCE% = EMP_NORMAL_IMPORTANCE + 1
  57.  
  58. ' enum EMPSENSITIVITYTYPES:
  59. Const EMP_STANDARD% = 0
  60.  
  61. ' enum EMPSEARCHTYPES:
  62. Const EMP_SUBJECT% = 0
  63. Const EMP_ORIGINATOR% = EMP_SUBJECT + 1
  64. Const EMP_IMPORTANCE% = EMP_ORIGINATOR + 1
  65. Const EMP_SENSITIVITY% = EMP_IMPORTANCE + 1
  66. Const EMP_READ% = EMP_SENSITIVITY + 1
  67. Const EMP_UNREAD% = EMP_READ + 1
  68. Const EMP_DATE% = EMP_UNREAD + 1
  69. Const EMP_BEFOREDATE% = EMP_DATE
  70. Const EMP_AFTERDATE% = EMP_DATE
  71. Const EMP_MESSAGEID% = 8
  72.  
  73. ' Options:
  74. Const EMP_CAN_RECEIVE_MULTIPLE% = &H1
  75. Const EMP_AUTO_DELETE% = &H1
  76.  
  77. ' Restrictions:
  78. Const EMP_CANT_SEND% = &H1
  79. Const EMP_ONLY_ONE_CONTENT% = &H2
  80. Const EMP_CANT_DELAY_DELETE% = &H4
  81. Const EMP_NO_FORWARD_MSG% = &H8
  82. Const EMP_NO_SURCHARGES_ON_SEND% = &H10
  83.  
  84. ' Components
  85. Const EMP_SRC_TOTAL_SIZE% = &H4
  86.  
  87. ' Components:
  88. Const EMP_MHC_MSG_ID% = &H1
  89. Const EMP_MHC_DATE% = &H2
  90. Const EMP_MHC_ORIGINATOR_ADDR% = &H4
  91. Const EMP_MHC_RECIPIENT_OPTIONS% = &H8
  92. Const EMP_MHC_PRIMARY_RECIPIENTS% = &H10
  93. Const EMP_MHC_COPY_RECIPIENTS% = &H20
  94. Const EMP_MHC_IN_REPLY_TO% = &H40
  95. Const EMP_MHC_TABLE_OF_CONTENTS% = &H80
  96. Const EMP_MHC_EXTENDED_ENVELOPES% = &H100
  97. Const EMP_MHC_EXPIRATION_TIME% = &H200
  98.  
  99. ' Type of Recipient
  100. ' enum EMPCOPYTYPES:
  101. Const EMP_PRIMARY_RECIPIENT% = 0
  102. Const EMP_COPY_RECIPIENT% = EMP_PRIMARY_RECIPIENT + 1
  103. Const EMP_BLIND_COPY% = EMP_COPY_RECIPIENT + 1
  104.  
  105. ' Msg Release options
  106. Const EMP_MRO_DELAY_RELEASE% = &H1
  107. Const EMP_MRO_SET_EXPIRE% = &H2
  108. Const EMP_MRO_REPLY_TO% = &H4
  109. Const EMP_MRO_APPEND_DISTRIBUTION% = &H8
  110. Const EMP_MRO_EXTENDED_ENVELOPES% = &H10
  111. Const EMP_MRO_FORWARD_MSG% = &H20
  112.  
  113. ' enum EMPRETENTIONTYPES:
  114. Const EMP_USE_DEFAULT_RETENTION% = 0
  115. Const EMP_USE_MAXIMUM_RETENTION% = EMP_USE_DEFAULT_RETENTION + 1
  116. Const EMP_RETAIN_UNTIL_DATE% = EMP_USE_MAXIMUM_RETENTION + 1
  117. Const EMP_DELETE_NORMAL% = EMP_RETAIN_UNTIL_DATE + 1
  118. Const EMP_DELETE_REGARDLESS% = EMP_DELETE_NORMAL + 1
  119. Const EMP_USE_MAXIMUM_RETENTION_REGARDLESS% = EMP_DELETE_REGARDLESS + 1
  120. Const EMP_RETAIN_UNTIL_DATE_REGARDLESS% = EMP_USE_MAXIMUM_RETENTION_REGARDLESS + 1
  121. Const EMP_DELETE_NORMAL_REGARDLESS% = EMP_RETAIN_UNTIL_DATE_REGARDLESS + 1
  122.  
  123. Function HMIGotoMail()
  124.     Dim i As Integer
  125.  
  126.     If WhereAmI="cis:mail" Then
  127.         HMIGotoMail = True
  128.         Exit Function
  129.     End If
  130.  
  131.     SetHighMessageNumber
  132.  
  133.     On Error Goto GoMail_Error
  134.     If DoHMIGotoPage("cis:mail", -1, CAP_EMAIL) Then
  135.         EMPGetConfig EMPconfig, "", "", 0
  136.         WhereAmI = "cis:mail"
  137.         HMIGotoMail = True
  138.         If EMPconfig.wRestrictions And EMP_ONLY_ONE_CONTENT% Then OldMail = True
  139.         Exit Function
  140.     End If
  141. GoMail_Error:
  142.     WhereAmI = ""
  143.     HMIGotoMail = False
  144. End Function
  145.  
  146. Function GetMailType(i As Integer) As String
  147.     Select Case i
  148.         Case EMP_TEXT%
  149.             GetMailType = "Ascii"
  150.         Case EMP_BINARY%
  151.             GetMailType = "Binary"
  152.         Case EMP_GIF%
  153.             GetMailType = "GIF"
  154.         Case EMP_BINARY_FILE%
  155.             GetMailType = "Binary file"
  156.         Case EMP_TEXT_FILE%
  157.             GetMailType = "Text file"
  158.         Case EMP_TEXT_OBJECT%
  159.             GetMailType = "Text object"
  160.         Case EMP_JPEG%
  161.             GetMailType = "JPEG"
  162.         Case Else
  163.             GetMailType = "Unknown type - " & Str(i)
  164.     End Select
  165. End Function
  166.  
  167. 'T:MailTo (subroutine) (CompuServe)
  168. Sub MailTo(id As String, user As String, flags As Integer, subject As String, filename As String)
  169.     Dim inpline As String
  170.  
  171.     If Not HMIGotoMail() Then
  172.         LogResult id + " : Failed : Mail is unavailable : " + FullErrorMessage
  173.         Exit Sub
  174.     End If
  175.  
  176.     If EMPconfig.wRestrictions And EMP_CANT_SEND% Then
  177.         LogResult id + " : Failed : You are not allowed to send messages"
  178.         Exit Sub
  179.     End If
  180.     MailSubject = Left$(subject, 40)
  181.     MailFilename = filename
  182.     MailCount = 0
  183.     MailAddresses = ""
  184.     MailFlags = flags
  185.  
  186.     ' The primary recipient
  187.     MailCC user
  188. End Sub
  189.  
  190. 'T:MailCC (subroutine) (CompuServe)
  191. Sub MailCC(cc As String)
  192.     Dim realname As String, email As String, ipname As String
  193.     Dim RemoveRecipient As Boolean
  194.  
  195.     ' Get the email address
  196.     AnalyseName cc, realname, email
  197.     If email="" Then email = realname
  198.  
  199.     ' Check to see if we're sending an IP msg to ourselves
  200.     ipname = "internet:"+Session.LoginName+"@compuserve.com"
  201.     ReplaceAllInString ipname, ",", "."
  202.     If StrComp(email, ipname, 1)=0 Then RemoveRecipient = True
  203.     If StrComp(email, Mid$(ipname, 10), 1)=0 Then RemoveRecipient = True
  204.     If StrComp(email, PersonalAddress+"@compuserve.com", 1)=0 Then RemoveRecipient = True
  205.  
  206.     ' Check to see if we're sending an internal msg to ourselves
  207.     If StrComp(email, PersonalAddress, 1)=0 Then RemoveRecipient = True
  208.     If StrComp(email, Session.LoginName, 1)=0 Then RemoveRecipient = True
  209.  
  210.     If RemoveRecipient=False Then
  211.         MailCount = MailCount + 1
  212.         If MailAddresses="" Then
  213.             MailAddresses = cc
  214.         Else
  215.             MailAddresses = MailAddresses + Chr$(10) + cc
  216.         End If
  217.     End If
  218. End Sub
  219.  
  220. 'T:SetEnvelope (subroutine) (CompuServe)
  221. Sub SetEnvelope(envelope As EMPENVELOPE, user As String, UrgentMail As Boolean)
  222.     AnalyseName user, envelope.lpRecipientName, envelope.lpRecipientAddr
  223.     If envelope.lpRecipientName = "" Then
  224.         envelope.lpRecipientName = envelope.lpRecipientAddr
  225.     End If
  226.     If envelope.lpRecipientAddr = "" Then
  227.         envelope.lpRecipientAddr = envelope.lpRecipientName
  228.     End If
  229.  
  230.     If UrgentMail Then
  231.         envelope.moOptions.cImportance = EMP_HIGH_IMPORTANCE%
  232.     Else
  233.         envelope.moOptions.cImportance = EMP_NORMAL_IMPORTANCE%
  234.     End If
  235.     envelope.moOptions.cSensitivity = EMP_STANDARD%
  236.     envelope.moOptions.cReceiptNotification = 0
  237.     envelope.nType = EMP_COPY_RECIPIENT
  238. End Sub
  239.  
  240. 'T:PreProcessMail (subroutine) (CompuServe)
  241. Function PreProcessMail(InFileName As String, OutFileName As String, _
  242.                    ByRef InReplyTo As String, ByRef RealName As String, _
  243.                    ByRef UrgentMail As Boolean) As Boolean
  244.     Dim OutFileNum as Integer, InFileNum as Integer
  245.     Dim TheData as String
  246.     Dim count As Long
  247.  
  248.     count = 0
  249.     On Error Goto PreProcessMail_error
  250.  
  251.     InFileNum = FreeFile()
  252.     Open InFileName For Input Access Read Shared As #InFileNum
  253.     
  254.     OutFileNum = FreeFile()
  255.     Open OutFileName for Output Access Write Shared as #OutFileNum
  256.  
  257.     ' Send as shown
  258.     Print #OutFileNum, "@l";
  259.     
  260.     ' Operate on blocks of BlockSize at a time
  261.     Do While Not Eof(InFileNum)
  262.         Line Input #InFileNum, TheData
  263.  
  264.         ' Is this from somebody else ?
  265.         If count<3 And Mid$(TheData, 1, 6)="From: " Then
  266.             RealName = Mid$(TheData, 7)
  267.         ElseIf count<3 And Mid$(TheData, 1, 13)="In-Reply-To: " Then
  268.             InReplyTo = Mid$(TheData, 15)
  269.             ReplaceAllInString InReplyTo, ">", ""
  270.         ElseIf count<3 And Mid$(TheData, 1, 14)="Priority: High" Then
  271.             UrgentMail = True
  272.         Else
  273.             ReplaceAllInString TheData, "@", "@@"
  274.             Print #OutFileNum, TheData + Chr$(10);
  275.         End If
  276.         count = count + 1
  277.     Loop
  278.     Print #OutFileNum, " ";
  279.  
  280.     Close #InFileNum
  281.     Close #OutFileNum
  282.     PreProcessMail = True
  283.     Exit Function
  284.  
  285. PreProcessMail_error:
  286.     LogResult "Error in PreProcessMail : " + Str$(Err) + " in line " + Str$(Erl) + ":" + Error$
  287.     Close #InFileNum
  288.     Close #OutFileNum
  289.     PreProcessMail = False
  290. End Function
  291.  
  292. 'T:SendMail (subroutine) (CompuServe)
  293. Sub SendMail(id As String)
  294.     Dim filename As String, header As String, user As String
  295.     Dim sendresponse As EMPSENDRESPONSE
  296.     Dim envelopes() As EMPENVELOPE
  297.     Dim badenvelopes() As EMPBADENVELOPE
  298.     Dim contents(0 To 0) As EMPCONTENTSDESC
  299.     Dim forward As String, InReplyTo As String, RealName As String
  300.     Dim notime As HMITIMESTAMP
  301.     Dim i As Integer, j As Integer
  302.     Dim addresses As String, badaddresses As String, badcount As Integer
  303.     Dim path As String, sname As String, nState As Integer
  304.     Dim SourceFileNum as Integer, SourceFileAt as Long
  305.     Dim SourceFileLen as Long, TheData as String, BytesToRead as Long
  306.     Dim confirm As EMPCONFIRMATION
  307.     Dim mroOptions As Integer, UrgentMail As Boolean
  308.     Dim tmpFilename As String, tmp As String
  309.  
  310.     On Error Goto SendMail_error
  311.     MailSent = False
  312.     MailCount = MailCount - 1
  313.     If MailCount < 0 Then
  314.         LogResult id+" : SendMail Failed : No valid destination addresses"
  315.         Terminal.TransferStatus
  316.         Exit Sub
  317.     End If
  318.  
  319.     Terminal.Print "Sending mail " + MailSubject + Basic.Eoln$
  320.  
  321.     InReplyTo = ""
  322.     RealName = MailName
  323.     UrgentMail = False
  324.     If (MailFlags And fBINARY)=fBINARY Then
  325.         ' Binary mail
  326.         SplitPath MailFilename, path, sname
  327.         contents(0).nType = EMP_BINARY_FILE%
  328.         tmpFilename = MailFilename
  329.     Else
  330.         ' ASCII mail
  331.         sname = "Mail Message"
  332.         contents(0).nType = EMP_TEXT_OBJECT%
  333.  
  334.         ' Process header lines and add formatting codes
  335.         tmpFilename = WorkingPath + "sendmail.txt"
  336.         If Not PreProcessMail(MailFilename, tmpFilename, InReplyTo, RealName, UrgentMail) Then
  337.             Exit Sub
  338.         End If
  339.     End If
  340.     contents(0).dwSize = FileLen(tmpFilename)
  341.     contents(0).dwSurcharge = 0
  342.  
  343. SendMail_retry:
  344.     ReDim envelopes(0 To MailCount)
  345.     addresses = MailAddresses
  346.     user = ParseString(MailAddresses, Chr$(10))
  347.     SetEnvelope envelopes(0), user, UrgentMail
  348.  
  349.     If MailCount >= 1 Then
  350.         For i = 1 To MailCount
  351.             user = ParseString(MailAddresses, Chr$(10))
  352.             SetEnvelope envelopes(i), user, UrgentMail
  353.         Next
  354.     End If
  355.  
  356.     ' Make sure it's a legal In-Reply-To otherwise newmail dies
  357.     tmp = Left$(InReplyTo, 2)
  358.     If tmp<>"MC" And IsNumeric(tmp)=False Then InReplyTo = ""
  359.     If Len(InReplyTo) > 35 Then InReplyTo = ""
  360.  
  361.     envelopes(0).moOptions.cReceiptNotification = ((MailFlags And fRECEIPT) <> 0)
  362.     envelopes(0).nType = EMP_PRIMARY_RECIPIENT%
  363.     mroOptions = EMP_MRO_APPEND_DISTRIBUTION%
  364.     If InReplyTo<>"" Then mroOptions = mroOptions Or EMP_MRO_REPLY_TO%
  365.     EMPSendMsgHeader sendresponse, badenvelopes(), mroOptions, _
  366.             MailSubject, RealName, notime, notime, InReplyTo, envelopes(), _
  367.             contents(), MailCount + 1, "", forward
  368.     If sendresponse.wCount > 0 Then
  369.         For i = 0 To Ubound(badenvelopes)
  370.             j = badenvelopes(i).wNumber - 1
  371.             envelopes(j).nType = badenvelopes(i).cReason
  372.             envelopes(j).moOptions.cImportance = 255
  373.         Next
  374.         MailCount = -1
  375.         MailAddresses = ""
  376.         For i = 0 To Ubound(envelopes)
  377.             user = ParseString(addresses, Chr$(10))
  378.             If envelopes(i).moOptions.cImportance = 255 Then
  379.                 LogResult id + " : Invalid address (" & _
  380.                     envelopes(i).nType & "): " + user
  381.                 badaddresses = badaddresses + user + Chr$(10)
  382.                 badcount = badcount + 1
  383.             Else
  384.                 MailAddresses = MailAddresses + user + Chr$(10)
  385.                 MailCount = MailCount + 1
  386.             End If
  387.         Next
  388.         If MailCount >= 0 Then Goto SendMail_retry
  389.         LogResult id+" : SendMail Failed : No valid destination addresses"
  390.         Exit Sub
  391.     End If
  392.  
  393.     ' Find out length of the message/file
  394.     SourceFileLen = FileLen(tmpFilename)
  395.     If SourceFileLen <= 0 Then
  396.         LogResult id+" : SendMail Failed : Can't upload file"
  397.         Exit Sub
  398.     End If
  399.  
  400.     ' Send the file
  401.     BytesToRead = SourceFileLen
  402.     SourceFileNum = FreeFile()
  403.     Open tmpFilename for Binary Access Read Shared as #SourceFileNum
  404.     SourceFileAt = 1        'read point
  405.  
  406.     TheData = String$(BlockSize, " ")
  407.     nState = HMI_STATE_FIRST%
  408.     Terminal.TransferStatus True, sname, 0, SourceFileLen
  409.  
  410.     ' Operate on blocks of BlockSize at a time
  411.     Do While BytesToRead > BlockSize
  412.         Get #SourceFileNum, SourceFileAt, TheData
  413.         i = EMPSendMsgContents(nState, 1, MailSubject, sname, contents(0).nType, TheData)
  414.         BytesToRead = BytesToRead - BlockSize
  415.         SourceFileAt = SourceFileAt + BlockSize
  416.         nState = HMI_STATE_NEXT%
  417.         Terminal.TransferStatus True, sname, SourceFileAt-1, SourceFileLen
  418.     Loop
  419.  
  420.     ' Now do the last block of less than BlockSize
  421.     If BytesToRead > 0 then
  422.         TheData = String$(BytesToRead, " ")
  423.         Get #SourceFileNum, SourceFileAt, TheData
  424.         i = EMPSendMsgContents(nState, 1, MailSubject, sname, contents(0).nType, TheData)
  425.     End If
  426.     Terminal.TransferStatus True, sname, SourceFileLen, SourceFileLen
  427.     i = EMPSendMsgContents(HMI_STATE_NEXT%, 1, MailSubject, sname, contents(0).nType, "")
  428.     Close #SourceFileNum
  429.     Terminal.TransferStatus
  430.  
  431.     ' Check it succeeded
  432.     EMPGetConfirmation confirm
  433.     If confirm.lpMsgID <> "" Then
  434.         ReportSuccess id & " : Posted Mail " & MailSubject
  435.         MailSent = True
  436.     End If
  437.  
  438.     If (MailFlags And fDONTCOPY)=0 Then
  439.         ' Write message back to yourself
  440.         header = "Date: " + MyDate$ + Chr$(10)
  441.         If PersonalAddress="" Then
  442.             header = header + "From: " + RealName + " [" + Session.LoginName + "]" + Chr$(10)
  443.         Else
  444.             header = header + "From: " + RealName + " [" + PersonalAddress + "]" + Chr$(10)
  445.         End If
  446.         header = header + "Subject: " + MailSubject + Chr$(10)
  447.         header = header + "Message-Id: <" & confirm.lpMsgID & ">" + Chr$(10)
  448.         If InReplyTo<>"" Then
  449.             header = header + "In-Reply-To: <" & InReplyTo & ">" + Chr$(10)
  450.         End If
  451.         header = header + "Content-Type: " & GetMailType(contents(0).nType) & Chr$(10)
  452.  
  453.         user = ParseString(addresses, Chr$(10))
  454.         If Mid$(user, 1, 9)="internet:" Then user = Mid$(user, 10)
  455.         header = header + "To: " + user + Chr$(10)
  456.         For i = 1 To MailCount
  457.             user = ParseString(addresses, Chr$(10))
  458.             If Mid$(user, 1, 9)="internet:" Then user = Mid$(user, 10)
  459.             If i=1 Then header = header + "Cc:"
  460.             header = header + " " + user + Chr$(10)
  461.         Next
  462.         For i = 1 To badcount
  463.             user = ParseString(badaddresses, Chr$(10))
  464.             If Mid$(user, 1, 9)="internet:" Then user = Mid$(user, 10)
  465.             header = header + "X-BadAddress: " + user + Chr$(10)
  466.         Next
  467.  
  468.         ' Create temp file, and put message in it
  469.         filename = UniqueFilename$()
  470.         On Error Goto MailTo_error
  471.         Open filename For Output Access Write Shared As #1
  472.         Print #1, "#pragma ciscontrol=yes"
  473.         Print #1, "Memo #1 (" & Len(header) + SourceFileLen & ")"
  474.         Print #1, header
  475. MailTo_error:
  476.         Close #1
  477.         AddToFile filename, tmpFilename
  478.         result = QueueFile(Session.Service, filename, IM_DELETE)
  479.     End If
  480.     If (MailFlags And fBINARY)=0 Then DeleteFile tmpFilename
  481.     Exit Sub
  482.  
  483. SendMail_error:
  484.     ' Report error to user
  485.     LogResult id+" : SendMail Failed : Error :" & FullErrorMessage
  486.     Terminal.TransferStatus
  487. End Sub
  488.  
  489. Function ListUsers(recipient() As EMPNAMEANDADDR, erecipient() As EMPNAMEANDADDR) As String
  490.     Dim u As String, n As String, i As Integer
  491.  
  492.     On Error Resume Next
  493.     For i = 0 To Ubound(recipient)
  494.         If recipient(i).lpAddr <> "" Then
  495.             ReplaceAllInString recipient(i).lpName, ";", " "
  496.             If UCase$(Mid$(recipient(i).lpAddr, 1, 9))="INTERNET:" Then
  497.                 u = Mid$(recipient(i).lpAddr, 10)
  498.             Else
  499.                 If Mid$(recipient(i).lpAddr, 1, 1)="[" Then
  500.                     u = recipient(i).lpName+" "+recipient(i).lpAddr
  501.                 Else
  502.                     u = recipient(i).lpName+" ["+recipient(i).lpAddr+"]"
  503.                 End If
  504.             End If
  505.             If n="" Then
  506.                 n = u
  507.             Else
  508.                 n = n + Chr$(10) + " " + u
  509.             End If
  510.         End If
  511.     Next
  512.     For i = 0 To Ubound(erecipient)
  513.         If erecipient(i).lpAddr <> "" Then
  514.             ReplaceAllInString erecipient(i).lpName, ";", " "
  515.             If UCase$(Mid$(erecipient(i).lpAddr, 1, 9))="INTERNET:" Then
  516.                 u = Mid$(erecipient(i).lpAddr, 10)
  517.             Else
  518.                 If Mid$(erecipient(i).lpAddr, 1, 1)="[" Then
  519.                     u = erecipient(i).lpName+" "+erecipient(i).lpAddr
  520.                 Else
  521.                     u = erecipient(i).lpName+" ["+erecipient(i).lpAddr+"]"
  522.                 End If
  523.             End If
  524.             If n="" Then
  525.                 n = u
  526.             Else
  527.                 n = n + Chr$(10) + " " + u
  528.             End If
  529.         End If
  530.     Next
  531.     On Error Goto 0
  532.     n = Trim$(n)
  533.     If n <> "" And Right$(n, 1)=Chr$(10) Then
  534.         ListUsers = Mid$(n, 1, Len(n)-1)
  535.     Else
  536.         ListUsers = Trim$(n)
  537.     End If
  538. End Function
  539.  
  540. Function ExtractX400Name(xaddr As String) As String
  541.     Dim i As Integer, realname As String
  542.  
  543.     i = InStr(xaddr, "g=", 1)
  544.     If i<>0 Then realname = Mid$(xaddr, i+2)
  545.     i = InStr(realname, ")")
  546.     If i<>0 Then realname = Mid$(realname, 1, i-1)
  547.     i = InStr(xaddr, "s=", 1)
  548.     If i<>0 Then realname = realname + " " + Mid$(xaddr, i+2)
  549.     i = InStr(realname, ";")
  550.     If i<>0 Then realname = Mid$(realname, 1, i-1)
  551.     If realname="" Then realname = "X400 Gateway"
  552.  
  553.     ExtractX400Name = realname
  554. End Function
  555.  
  556. 'T:FetchMail (subroutine) (CompuServe)
  557. Sub FetchMail(id As String)
  558.     Dim hdr As EMPHEADER, precipient() As EMPNAMEANDADDR, b As Boolean
  559.     Dim crecipient() As EMPNAMEANDADDR, content() As EMPCONTENTS
  560.     Dim msgno As Integer, i As Integer, messagefilename As String
  561.     Dim msgtext As String, count As Long, binfname As String
  562.     Dim l As Long, subject As String, obinfname As String, badchars As String
  563.     Dim msgheader As String, searchterm(0 to 0) As EMPSEARCHTERM
  564.     Dim msg() As EMPMSGSUMMARY, userid As String, szStatus() As EMPMSGSTATUS
  565.     Dim lpMsgs() As EMPMSGDISPOSE, disp As EMPDISPOSERESPONSE
  566.     Dim crs As Integer, tmp As String, s As String, realname As String
  567.     Dim contentno As Integer, mailnum As Integer, from As String
  568.     Dim reciplist As EMPRECIPIENTSLIST, eprecipient() As EMPNAMEANDADDR
  569.     Dim ecrecipient() As EMPNAMEANDADDR, deletemail As Boolean
  570.     Dim firstblock As Boolean
  571.  
  572.     deletemail = ReadIni$("Service "+Session.Service, "Delete Mail", Session.IniFilename)="YES"
  573.  
  574.     ' The Content() doesn't seem to be working
  575.     Terminal.Print "Checking for new mail..." + Basic.Eoln$
  576.  
  577.     ' Go MAIL
  578.     If Not HMIGotoMail() Then
  579.         LogResult "Collect Mail : Failed : Mail is unavailable : " + FullErrorMessage
  580.         Exit Sub
  581.     End If
  582.     If EMPconfig.wUnreadMsgs=1 Then
  583.         Terminal.Print "There is 1 unread mail message." + Basic.Eoln$
  584.     Else
  585.         Terminal.Print "There are " & LTrim(Str(EMPconfig.wUnreadMsgs)) & " unread mail messages." + Basic.Eoln$
  586.     End If
  587.  
  588.     ' Search for any mail
  589.     searchterm(0).nType = EMP_AFTERDATE%
  590.     searchterm(0).lpPattern = "800101:"
  591.     msgno = 0
  592.  
  593.     ' Start downloading
  594.     On Error Goto ReadMail_error
  595.     messagefilename = UniqueFileName$()
  596.     Capture CAPTURE_ON, messagefilename
  597.  
  598.     Do
  599.         EMPSearchMsg msg(), 10, msgno, EMP_SRC_TOTAL_SIZE%, searchterm()
  600.  
  601.         ReDim lpMsgs(0 To UBound(msg))
  602.         For mailnum = 0 To UBound(msg)
  603.             msgno = msg(mailnum).wMsgNo
  604.             If msgno>0 Then
  605.                 count = count + 1
  606.                 If count=1 Then Terminal.CaptureStatus CAPTURE_ON
  607.                 Terminal.CaptureStatus count
  608.  
  609.                 EMPGetMsgHeader hdr, precipient(), crecipient(), content(), msgno, &HFF
  610.  
  611.                 ' Extended recipient list
  612.                 If (hdr.wComponents And EMP_MHC_EXTENDED_ENVELOPES%)<>0 Then
  613.                     EMPReadRecipients reciplist, eprecipient(), ecrecipient(), msgno, 1, 50, 1, 50
  614.                 End If
  615.                 For contentno = 0 To UBound(content)
  616.                     ' Check for empty fields
  617.                     subject = msg(mailnum).lpSubject
  618.                     If subject="" Then subject = "==== No Subject ===="
  619.                     If Mid(hdr.lpOriginatorAddr, 1, 1)="[" Then
  620.                         userid = hdr.lpOriginatorAddr
  621.                     Else
  622.                         userid = "["+hdr.lpOriginatorAddr+"]"
  623.                     End If
  624.  
  625.                     ' Get realname out of X400 mess
  626.                     If Left$(msg(mailnum).lpOriginatorName, 5)="X400:" Then
  627.                         realname = ExtractX400Name(msg(mailnum).lpOriginatorName)
  628.                     Else
  629.                         realname = msg(mailnum).lpOriginatorName
  630.                     End If
  631.                     ReplaceAllInString realname, ";", " "
  632.  
  633.                     ' Tell the user what's happening
  634.                     Terminal.Print realname & " " & _
  635.                                    MakeDateString(hdr.tsDate) & " " & subject
  636.                     If UBound(content)>0 Then Terminal.Print ", Part" & Str(contentno+1) & " of" & Str(UBound(content)+1) & "."
  637.                     Terminal.Print Basic.Eoln$
  638.  
  639.                     If content(contentno).nType=EMP_TEXT% Or _
  640.                        content(contentno).nType=EMP_TEXT_FILE% Or _
  641.                        content(contentno).nType=EMP_TEXT_OBJECT% Then
  642.                         ' Generate the message header
  643.                         msgheader = "Date: " & MakeDateString(hdr.tsDate) & Chr$(10) & _
  644.                                     "From: " & realname & " " & userid & Chr$(10) & _
  645.                                     "Subject: " & subject & Chr$(10) & _
  646.                                     "Message-Id: <" & hdr.lpMsgID & ">" & Chr$(10) & _
  647.                                     "Content-Type: " & GetMailType(content(contentno).nType) & Chr$(10)
  648.                         If hdr.lpInReplyTo>"" Then msgheader = msgheader + "In-Reply-To: <" + hdr.lpInReplyTo + ">" + Chr$(10)
  649.                         msgheader = msgheader + "To: " + ListUsers(precipient(), eprecipient()) + Chr$(10)
  650.                         tmp = ListUsers(crecipient(), ecrecipient())
  651.                         If tmp<>"" Then msgheader = msgheader + "Cc: " + tmp + Chr$(10)
  652.                         If hdr.moOptions.cImportance = 0 Then msgheader = msgheader + "Priority: Low" + Chr$(10)
  653.                         If hdr.moOptions.cImportance = 1 Then msgheader = msgheader + "Priority: Normal" + Chr$(10)
  654.                         If hdr.moOptions.cImportance = 2 Then msgheader = msgheader + "Priority: Urgent" + Chr$(10)
  655.  
  656.                         ' Get first block so we can check it's not CIS's
  657.                         ' dreadful automatic MIME decode junk
  658.                         l = 1
  659.                         firstblock = True
  660.                         Terminal.TransferStatus False, "Mail Message", 0, content(contentno).dwSize
  661.                         msgtext = EMPReadTextContents(msgno, content(contentno).cNumber, l, BlockSize)
  662.  
  663.                         'Terminal.Print "Orig=" + hdr.lpOriginatorAddr + basic.eoln$
  664.  
  665.                         ' Do we need to use our own header
  666.                         from = LCase$(hdr.lpOriginatorAddr)
  667.                         If Mid$(from, 1, 9)="internet:" And _
  668.                            from<>"internet:postmaster@compuserve.com" And _
  669.                            Left$(msgtext, 9)<>"Contents:" Then
  670.                             msgheader = ""
  671.                         End If
  672.  
  673.                         ' Translate control characters?
  674.                         If Mid$(from, 1, 9)="internet:" Then
  675.                             CaptureText Chr$(10) & "#pragma ciscontrol=no"
  676.                         Else
  677.                             CaptureText Chr$(10) & "#pragma ciscontrol=yes"
  678.                         End If
  679.  
  680.                         ' Write the header + size
  681.                         l = content(contentno).dwSize + Len(msgheader) + 1
  682.                         CaptureText Chr$(10) & "Memo #" & LTrim(Str(msgno)) & " (" & l & ")" & Chr$(10)
  683.                         If msgheader<>"" Then CaptureText msgheader & Chr$(10)
  684.  
  685.                         ' Get the body text and write it out
  686.                         l = 1
  687.                         crs = 0
  688.                         Do
  689.                             If Not firstblock Then
  690.                                 msgtext = EMPReadTextContents(msgno, content(contentno).cNumber, l, BlockSize)
  691.                             End If
  692.                             firstblock = False
  693.                             l = l + Len(msgtext)
  694.                             Terminal.TransferStatus False, "Mail Message", l-1, content(contentno).dwSize
  695.                             crs = crs + ItemCount(msgtext, Chr$(13))
  696.                             CaptureText msgtext
  697.                         Loop Until msgtext=""
  698.                         If l < content(contentno).dwSize Then
  699.                             CaptureText String$(content(contentno).dwSize - l, " ")
  700.                         End If
  701.                         CaptureLine String$(crs, " ")
  702.                         Terminal.TransferStatus
  703.                     Else
  704.                         ' Try to figure out the filename
  705.                         If content(contentno).lpName="" Then
  706.                             binfname = subject
  707.                         Else
  708.                             binfname = Filename$(content(contentno).lpName)
  709.                         End If
  710.                         obinfname = binfname
  711.                         LogResult "Downloading binary mail:" & binfname
  712.  
  713.                         ' Support long file names in 32-bit version
  714.                         If Basic.OS = ebWin32 Then
  715.                             badchars = "[]:;'""{}<>,+=*"+Chr$(13)
  716.                         Else
  717.                             badchars = " []:;'""{}<>,+=*"+Chr$(13)
  718.                         End If
  719.  
  720.                         ' Figure out mail filename - truncate at invalid character.
  721.                         binfname = Trim$(binfname)
  722.                         For i=1 to Len(binfname)
  723.                             If InStr(badchars, Mid$(binfname, i, 1)) Then
  724.                                 binfname = Mid$(binfname, 1, i-1)
  725.                                 Exit For
  726.                             End If
  727.                         Next
  728.  
  729.                         ' Make sure that whatever I have is a dos legal filename
  730.                         If InStr(binfname, ".")<>0 Then
  731.                             If Basic.OS <> ebWin32 Then
  732.                                 binfname = Left$(Left$(binfname,InStr(binfname, ".")-1),8) & "." & Mid$(binfname, InStr(binfname, ".")+1, 3)
  733.                             End If
  734.                             i=InStr(".", binfname)+1
  735.                             i=InStr(".", binfname)
  736.                             If i<>0 Then
  737.                                 binfname = Mid$(binfname, 1, i-1)
  738.                             End If
  739.                         End If
  740.                         If UCase$(binfname)="RE" Then binfname = ""
  741.  
  742.                         If binfname <> obinfname Then LogResult "Name truncated to:" & binfname
  743.                         obinfname = binfname
  744.  
  745.                         ' Was it a filename?
  746.                         If FileExists(DownloadDir+binfname) Or binfname="" Then
  747.                             ' Generate a filename
  748.                             i = 1
  749.                             Do
  750.                                 binfname = "mail"+Ltrim(Str(i))+".bin"
  751.                                 i = i+1
  752.                             Loop While FileExists(DownloadDir+binfname)
  753.                         End If
  754.                         If binfname <> obinfname Then LogResult "File exists - renamed to:" & binfname
  755.  
  756.                         Open DownloadDir+binfname For Binary Access Write Shared As #1
  757.                         l = 1
  758.                         Terminal.TransferStatus False, binfname, 0, content(contentno).dwSize
  759.                         Do
  760.                             msgtext = ""  ' needed to stop out of string space error
  761.                             crs = EMPReadBinaryContents(msgno, _
  762.                                        content(contentno).cNumber, l, msgtext, _
  763.                                        BlockSize) 
  764.                             If crs <= 0 Then Exit Do
  765.                             'Work round bug in Terminal 182 which always 
  766.                             'sets string length to BlockSize
  767.                             msgtext = Left$(msgtext, crs)
  768.                             Put #1, , msgtext
  769.                             l = l + Len(msgtext)
  770.                             Terminal.TransferStatus False, binfname, l-1, content(contentno).dwSize
  771.                         Loop
  772.                         Terminal.TransferStatus
  773.  
  774.                         ' Write a message pointing to the file
  775.                         msgheader = "Date: " & MakeDateString(hdr.tsDate) & Chr$(10) & _
  776.                                     "From: " & realname & " " & userid & Chr$(10) & _
  777.                                     "Subject: " & subject & Chr$(10) & _
  778.                                     "Message-Id: <" & hdr.lpMsgID & ">" & Chr$(10) & _
  779.                                     "Content-Type: " & GetMailType(content(contentno).nType) & Chr$(10)
  780.                         If hdr.lpInReplyTo>"" Then msgheader = msgheader + "In-Reply-To: <" + hdr.lpInReplyTo + ">" + Chr$(10)
  781.                         msgheader = msgheader + "To: " + ListUsers(precipient(), eprecipient()) + Chr$(10)
  782.                         tmp = ListUsers(crecipient(), ecrecipient())
  783.                         If tmp<>"" Then msgheader = msgheader + "Cc: " + tmp + Chr$(10)
  784.                         If hdr.moOptions.cImportance = 0 Then msgheader = msgheader + "Priority: Low" + Chr$(10)
  785.                         If hdr.moOptions.cImportance = 1 Then msgheader = msgheader + "Priority: Normal" + Chr$(10)
  786.                         If hdr.moOptions.cImportance = 2 Then msgheader = msgheader + "Priority: Urgent" + Chr$(10)
  787.                         msgheader = msgheader + Chr$(10) + "*** File Message ***" + Chr$(10) + Chr$(10)
  788.                         msgheader = msgheader + "The size of the file is" & Str(content(contentno).dwSize) & " bytes." + Chr$(10)
  789.                         msgheader = msgheader + "The file will be stored in " & FileUrl(DownloadDir + binfname) + Chr$(10) + Chr$(10)
  790.                         If content(contentno).lpSubject<>"" Then
  791.                             msgheader = msgheader + "Additional Information:" + Chr$(10)
  792.                             msgheader = msgheader + content(contentno).lpSubject + Chr$(10)
  793.                         End If
  794.  
  795.                         ' Write the pseudo msg
  796.                         CaptureText Chr$(10) & "#pragma ciscontrol=yes"
  797.                         CaptureText Chr$(10) & "Memo #" & LTrim(Str(msgno)) & " (" & Len(msgheader) & ")" & Chr$(10)
  798.                         CaptureText msgheader
  799.  
  800.                         Close #1
  801.                     End If
  802.                 Next
  803.  
  804.                 ' Delete the mail we just got
  805.                 If deletemail Then
  806.                     lpMsgs(mailnum).nDisposition = EMP_DELETE_REGARDLESS%
  807.                     lpMsgs(mailnum).nMsgNo = msgno
  808.                 End If
  809.             End If
  810.         Next
  811.         If deletemail Then EMPSendMsgDisposition disp, szStatus(), lpMsgs()
  812.     Loop Until msgno=0
  813.  
  814. ReadMail_error:
  815.     If Err<>9 Then
  816.         LogResult "Error in FetchMail : " + FullErrorMessage()
  817.     End If
  818.     On Error Goto 0
  819.     Capture CAPTURE_OFF
  820.     Terminal.CaptureStatus CAPTURE_OFF
  821.  
  822.     ' Import the mail
  823.     If count > 0 Then
  824.         b = QueueFile(Session.Service, messagefilename, IM_DELETE)
  825.         ReportSuccess id & " :" & Str$(count) & " mail messages have been collected"
  826.     Else
  827.         DeleteFile messagefilename
  828.         ReportSuccess id & " : There was no mail to collect"
  829.     End If
  830. End Sub
  831.  
  832. 'T:ExternalMailAddress (subroutine) (CompuServe)
  833. Function ExternalMailAddress(s As String)
  834.     Dim p As Integer, q As Integer
  835.  
  836.     p = InStr(s, ":")
  837.     q = InStr(s, " ")
  838.     If p>0 And p<q Then
  839.         ExternalMailAddress = True
  840.     ElseIf OldMail And InStr(s, "@") Then
  841.         ExternalMailAddress = True
  842.     Else
  843.         ExternalMailAddress = False
  844.     End If
  845. End Function
  846.  
  847. 'T:AttachFile (subroutine) (CompuServe)
  848. Sub AttachFile(id As String, filename As String, person As String, _
  849.                eraseafter As String, codetype As String)
  850.     Dim user As String, externalusers As String, localusers As String
  851.     Dim cisfilename As String, fname As String, sent As String
  852.     Dim i As Integer, maxlen As Integer, inBracket As Boolean
  853.     Dim ch As String
  854.  
  855.     sent = person
  856.     cisfilename = FileName$(filename)
  857.     maxlen = Len(person)
  858.     For i=1 To maxlen
  859.         ch = Mid$(person, i, 1)
  860.         If InBracket And ch=")" Then
  861.             InBracket = False
  862.             user = user + ch
  863.         ElseIf InBracket=False And ch="(" Then
  864.             InBracket = True
  865.             user = user + ch
  866.         ElseIf InBracket=False And ch=";" Then
  867.             If ExternalMailAddress(user) Then
  868.                 externalusers = externalusers + Chr$(10) + user
  869.             Else
  870.                 localusers = localusers + Chr$(10) + user
  871.             End If
  872.             user = ""
  873.         Else
  874.             user = user + ch
  875.         End If
  876.     Next
  877.     If user<>"" Then
  878.         If ExternalMailAddress(user) Then
  879.             externalusers = externalusers + Chr$(10) + user
  880.         Else
  881.             localusers = localusers + Chr$(10) + user
  882.         End If
  883.     End If
  884.  
  885.     If localusers > "" Then
  886.         localusers = Mid$(localusers, 2)    'Take off leading '\n'
  887.  
  888.         MailTo "Attachment", ParseString(localusers, Chr$(10)), fBINARY+fDONTCOPY, cisfilename, filename
  889.         While localusers<>""
  890.             MailCC ParseString(localusers, Chr$(10))
  891.         Wend
  892.         SendMail "Attachment"
  893.     End If
  894.     If externalusers > "" Then
  895.         externalusers = Mid$(externalusers, 2)  'Take off leading '\n'
  896.         fname = Session.ServicePath + "cis.bin"
  897.         FileEncode filename, fname, CODE_UUENCODE
  898.  
  899.         MailTo "Attachment", ParseString(externalusers, Chr$(10)), fDONTCOPY, cisfilename, fname
  900.         While externalusers<>""
  901.             MailCC ParseString(externalusers, Chr$(10))
  902.         Wend
  903.         SendMail "Attachment"
  904.         DeleteFile fname
  905.     End If
  906.     If MailSent Then ReportSuccess id & " : " & filename & " was sent to " & sent
  907. End Sub
  908.  
  909.