home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 7 / FreshFishVol7.bin / bbs / comm / cyberpager-1.5.lha / CyberPager / source / dialer / doService.c < prev    next >
C/C++ Source or Header  |  1994-02-07  |  7KB  |  300 lines

  1. #include "dialer.h"
  2.  
  3.   typedef struct {
  4.       ULONG med_Entries;
  5.       struct ExAllData med_Data[100];
  6.   } MyExAllBuf_t;
  7.  
  8. MyExAllBuf_t ead1, ead2;
  9.  
  10. static UBYTE fileNameBuffer[FILENAME_BUF_SIZE];
  11.  
  12. static const UBYTE EndOfTrans[] =
  13. {
  14.     HSHAKE_EOT, '\r'
  15. };
  16.  
  17. static int ProcessExAllBuffer(PagerService_t * svc, STRPTR spoolDir, MyExAllBuf_t * edata, ULONG *sentCount)
  18. {
  19.     struct ExAllData *ead;
  20.     int result;
  21.  
  22.     result = 0;
  23.  
  24.     if (edata->med_Entries) {
  25.         if (strlen(spoolDir) + 16 > sizeof(fileNameBuffer)) {
  26.             ErrorMsg("spool filename too long.");
  27.             ULog(ph, -1, "spool filename too long.");
  28.             return 5;
  29.         }
  30.  
  31.         ead = edata->med_Data;
  32.  
  33.         while (ead && !result) {
  34.             strcpy(fileNameBuffer, spoolDir);
  35.             AddPart(fileNameBuffer, ead->ed_Name, sizeof(fileNameBuffer));
  36.  
  37.             result = SendSpoolFile(svc, fileNameBuffer);
  38.  
  39.             if (!result)
  40.                 *sentCount += 1;
  41.  
  42.             ead = ead->ed_Next;
  43.         }
  44.     }
  45.  
  46.     return result;
  47. }
  48.  
  49. static const UBYTE WildCards[] = "#[0-9A-Z]";
  50. static UBYTE Pattern[sizeof(WildCards) * 2 + 2];
  51.  
  52. int DoOneService(STRPTR serviceName, BOOL oneShot)
  53. {
  54.     STRPTR spoolDir;
  55.     BPTR spoolLock;
  56.     struct ExAllControl *eac;
  57.     MyExAllBuf_t *eadata, *ead;
  58.     PagerService_t *svc;
  59.     int more;
  60.     int result;
  61.     ULONG sentCount, totalSentCount;
  62.  
  63.     totalSentCount = 0;
  64.     result = 5;
  65.  
  66.     if (svc = FindPagerService(ph, serviceName)) {
  67.         SetLogService(ph, svc->svc_Name);
  68.  
  69.         /*
  70.          * we lock on the service's name to insure that concurrent
  71.          * dialers don't try to call the same service at the same
  72.          * time.
  73.          */
  74.  
  75.         if (LockFile(ph, svc->svc_Name)) {
  76.             if (spoolDir = NameInSpool(ph, svc->svc_Name)) {
  77.                 if (spoolLock = Lock(spoolDir, ACCESS_READ)) {
  78.                     if (ParsePatternNoCase((STRPTR)WildCards, Pattern, sizeof(Pattern)) >= 0) {
  79.                         if (eac = (struct ExAllControl *)AllocDosObject(DOS_EXALLCONTROL, TAG_DONE)) {
  80.  
  81.                             sentCount = 1;    /* so that we enter the
  82.                                      * loop the first time */
  83.  
  84.                             result = 0;
  85.  
  86.                             /*
  87.                              * we loop looking
  88.                              * for files until
  89.                              * there are none
  90.                              * more left to send.
  91.                              * this way, if more
  92.                              * files are spooled
  93.                              * for a service
  94.                              * while we are
  95.                              * on-line talking to
  96.                              * it, we'll pick
  97.                              * them up this call
  98.                              * rather than having
  99.                              * to dial in a
  100.                              * second time.
  101.                              */
  102.  
  103.                             while (sentCount && !result) {
  104.                                 sentCount = 0;
  105.  
  106.                                 eac->eac_LastKey = 0;
  107.                                 eac->eac_Entries = 0;
  108.                                 eac->eac_MatchString = Pattern;
  109.                                 eac->eac_MatchFunc = NULL;
  110.  
  111.                                 ead1.med_Entries = 0;
  112.                                 ead2.med_Entries = 0;
  113.  
  114.                                 ead = &ead2;    /* next buffer to be
  115.                                          * filled */
  116.                                 eadata = &ead1;    /* buffer to be
  117.                                          * processed */
  118.  
  119.                                 do {
  120.                                     more = ExAll(spoolLock, ead->med_Data, sizeof(ead->med_Data) - 200, ED_TYPE, eac);
  121.                                     if (!more) {
  122.                                         if (IoErr() != ERROR_NO_MORE_ENTRIES) {
  123.                                             ErrorMsg("ExAll failed %ld.", IoErr());
  124.                                             ULog(ph, -1, "ExAll failed %ld.", IoErr());
  125.                                             result = 10;
  126.                                             break;
  127.                                         }
  128.                                     }
  129.  
  130.                                     ead->med_Entries = eac->eac_Entries;
  131.  
  132.                                     if (result = ProcessExAllBuffer(svc, spoolDir, eadata, &sentCount)) {
  133.                                         if (more) {
  134.                                             ExAllEnd(spoolLock, ead->med_Data, sizeof(ead->med_Data) - 200, ED_TYPE, eac);
  135.                                             more = 0;
  136.                                         }
  137.                                     }
  138.                                     else {
  139.                                         if (eadata == &ead1) {
  140.                                             eadata = &ead2;
  141.                                             ead = &ead1;
  142.                                         }
  143.                                         else {
  144.                                             eadata = &ead1;
  145.                                             ead = &ead2;
  146.                                         }
  147.                                     }
  148.                                 } while (more);
  149.  
  150.                                 if (!result)
  151.                                     result = ProcessExAllBuffer(svc, spoolDir, eadata, &sentCount);
  152.  
  153.                                 totalSentCount += sentCount;
  154.                             }
  155.  
  156.                             FreeDosObject(DOS_EXALLCONTROL, eac);
  157.                         }
  158.                         else
  159.                             ErrorMsg("out of memory!");
  160.                     }
  161.                     else {
  162.                         ErrorMsg("ParsePatternNoCase() failed.");
  163.                         ULog(ph, -1, "ParsePatternNoCase() failed.");
  164.                     }
  165.  
  166.                     UnLock(spoolLock);
  167.                 }
  168.                 else {
  169.                     ErrorMsg("couldn't lock spool directory.");
  170.                     ULog(ph, -1, "couldn't lock spool directory.");
  171.                 }
  172.  
  173.                 FreeNameInSpool(spoolDir);
  174.             }
  175.             else
  176.                 ErrorMsg("out of memory!");
  177.  
  178.             UnLockFile(ph, svc->svc_Name);
  179.         }
  180.         else {
  181.             ErrorMsg("service lock failed.");
  182.             ULog(ph, -1, "service lock failed.");
  183.         }
  184.  
  185.         FreePagerService(svc);
  186.     }
  187.     else if (oneShot) {
  188.         ErrorMsg("couldn't find \"%s\" service.", serviceName);
  189.         ULog(ph, -1, "couldn't find \"%s\" service.", serviceName);
  190.     }
  191.  
  192.     /*
  193.      * make sure that we always hang up after processing a service.  if
  194.      * we can't reopen but are a oneShot call then we don't care about
  195.      * the error since it doesn't affect us and the cleanup code will
  196.      * know how to deal with it.  if we're not oneShot, though, a failure
  197.      * to reopen the device means subsequence services would fail when
  198.      * trying to connect so we return an error in this case.
  199.      */
  200.  
  201.     if (online) {
  202.         SerWrite((STRPTR)EndOfTrans, sizeof(EndOfTrans));
  203.  
  204.         if (!HangUp() && !oneShot) {
  205.             ErrorMsg("couldn't reopen modem %ld after hangup.", openedModem);
  206.             ULog(ph, -1, "couldn't reopen modem %ld after hangup.", openedModem);
  207.             result = 5;
  208.         }
  209.  
  210.         ULog(ph, -1, "connection terminated");
  211.     }
  212.  
  213.     if (totalSentCount)
  214.         ULog(ph, -1, "%ld message%s delivered this call", totalSentCount, totalSentCount == 1 ? "" : "s");
  215.  
  216.     SetLogService(ph, NULL);
  217.  
  218.     return result;
  219. }
  220.  
  221. struct ExAllData eaBuf[100];
  222.  
  223. int DoAllServices(void)
  224. {
  225.     STRPTR spoolDir;
  226.     BPTR spoolLock;
  227.     struct ExAllControl *eac;
  228.     struct ExAllData *exdata, fakeExdata;
  229.     int more;
  230.     PagerService_t *svc;
  231.     int result;
  232.  
  233.     result = 10;
  234.  
  235.     fakeExdata.ed_Next = NULL;
  236.  
  237.     if (spoolDir = NameInSpool(ph, NULL)) {
  238.         if (spoolLock = Lock(spoolDir, ACCESS_READ)) {
  239.             if (eac = (struct ExAllControl *)AllocDosObject(DOS_EXALLCONTROL, TAG_DONE)) {
  240.                 eac->eac_LastKey = 0;
  241.                 eac->eac_Entries = 0;
  242.                 eac->eac_MatchString = NULL;
  243.                 eac->eac_MatchFunc = NULL;
  244.  
  245.                 result = 0;
  246.  
  247.                 do {
  248.                     more = ExAll(spoolLock, eaBuf, sizeof(eaBuf) - 200, ED_TYPE, eac);
  249.                     if (!more) {
  250.                         if (IoErr() != ERROR_NO_MORE_ENTRIES) {
  251.                             ErrorMsg("ExAll failed %ld.", IoErr());
  252.                             ULog(ph, -1, "ExAll failed %ld.", IoErr());
  253.                             result = 10;
  254.                             break;
  255.                         }
  256.                     }
  257.  
  258.                     if (eac->eac_Entries) {
  259.                         exdata = eaBuf;
  260.  
  261.                         while (exdata) {
  262.                             if (exdata->ed_Type > 0)
  263.                                 if (svc = FindPagerService(ph, exdata->ed_Name)) {
  264.                                     if (result = DoOneService(svc->svc_Name, FALSE)) {
  265.                                         if (more) {
  266.                                             ExAllEnd(spoolLock, eaBuf, sizeof(eaBuf) - 200, ED_TYPE, eac);
  267.                                             more = 0;
  268.                                         }
  269.  
  270.                                         exdata = &fakeExdata;
  271.                                     }
  272.  
  273.                                     FreePagerService(svc);
  274.                                 }
  275.  
  276.                             exdata = exdata->ed_Next;
  277.                         }
  278.                     }
  279.                 } while (more);
  280.  
  281.                 FreeDosObject(DOS_EXALLCONTROL, eac);
  282.             }
  283.             else
  284.                 ErrorMsg("out of memory!");
  285.  
  286.             UnLock(spoolLock);
  287.         }
  288.         else {
  289.             ErrorMsg("couldn't lock spool directory.");
  290.             ULog(ph, -1, "couldn't lock spool directory.");
  291.         }
  292.  
  293.         FreeNameInSpool(spoolDir);
  294.     }
  295.     else
  296.         ErrorMsg("out of memory!");
  297.  
  298.     return result;
  299. }
  300.