A
download mailroot.cpp
Language: C++
Copyright: Copyright 1987-96, Bit Bucket Software Co.
LOC: 1205
Project Info
BinkleyTerm XE(btxe)
Server: SourceForge
Type: cvs
...e\b\btxe\btxe\btnt\sources\
   Addr.cpp
   Addr.h
   aglcrc.cpp
   aglcrc.h
   asyn_lnx.cpp
   asyn_os2.cpp
   asyn_w32.cpp
   async.h
   asyos2v1.h
   b_banner.cpp
   b_bbs.cpp
   b_faxrcv.cpp
   b_flofil.cpp
   b_frproc.cpp
   b_help.cpp
   b_initva.cpp
   b_logs.cpp
   b_passwo.cpp
   b_proto.cpp
   b_rspfil.cpp
   b_sbinit.cpp
   b_script.cpp
   b_search.cpp
   b_sessio.cpp
   b_spawn.cpp
   b_term.cpp
   b_termov.cpp
   b_today.cpp
   b_wazoo.cpp
   b_whack.cpp
   b_wzsend.cpp
   banner.cpp
   banner.h
   BasicString.h
   bbs_ctra.cpp
   bbs_data.cpp
   bbs_incl.h
   bbs_io.cpp
   bink.h
   bink_asm.cpp
   binkdlg.cpp
   binkpipe.h
   box.h
   brec.cpp
   bsend.cpp
   bt.cpp
   btconfig.cpp
   btutil.cpp
   buffer.h
   cache.cpp
   cache.h
   callback.cpp
   callerid.cpp
   callwin.cpp
   callwin.h
   capi.cpp
   cfosline.cpp
   chat.cpp
   com.h
   com_dos.h
   com_lnx.h
   com_os2.h
   com_w32.h
   common.cpp
   cpuload.cpp
   css.cpp
   css.h
   data.cpp
   debug.cpp
   debug.h
   defines.h
   dosfuncs.cpp
   emsi.cpp
   emxmxcom.cpp
   evtparse.cpp
   evtsetup.cpp
   exceptio.cpp
   exceptio.h
   Exceptions.cpp
   Exceptions.h
   externs.h
   faxproto.h
   fcfgread.cpp
   fidomail.h
   file_all.cpp
   file_dos.cpp
   file_io.h
   file_lnx.cpp
   file_os2.cpp
   file_w32.cpp
   foffsets.h
   fossil.cpp
   freepoll.cpp
   ftsc.cpp
   gethcomm.h
   heap.cpp
   history.cpp
   history.h
   hydra.cpp
   hydra.h
   includes.cpp
   includes.h
   janus.cpp
   janus.h
   keybd.h
   keyfncs.h
   keymap.cpp
   langload.cpp
   language.h
   lngmagic.h
   m7rec.cpp
   m7send.cpp
   mailer.cpp
   mailovly.cpp
   mailroot.cpp
   maxcomm.h
   maxmcp.cpp
   maxmcp.h
   maxprm.cpp
   mdm_proc.cpp
   misc.cpp
   msgs.h
   Node.cpp
   Node.h
   NodeList.cpp
   NodeList.h
   nodeproc.cpp
   os2_pm.cpp
   outbound.cpp
   pipe.cpp
   pktmsgs.cpp
   pktmsgs.h
   protcomm.cpp
   prototyp.h
   recvbark.cpp
   recvsync.cpp
   sbuf.cpp
   sbuf.h
   sched.cpp
   sched.h
   script2.cpp
   SelfTest.cpp
   SelfTest.h
   sendbark.cpp
   sendsync.cpp
   squish.cpp
   squish.h
   srif.cpp
   statetbl.cpp
   stats.cpp
   stdafx.cpp
   stdafx.h
   timer.cpp
   timer.h
   times.cpp
   types.h
   v7debug.cpp
   version.cpp
   version7.cpp
   version7.h
   vfos_dos.cpp
   vfos_lnx.cpp
   vfos_os2.cpp
   vfos_qnx.cpp
   vfos_w32.cpp
   vfossil.h
   video.h
   vt100.cpp
   vt100.h
   xfer.h
   xmrec.cpp
   xmsend.cpp
   xstring.h
   yoohoo.cpp
   zmodem.cpp
   zmodem.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
/*---------------------------------------------------------------------------*
 *
 *                               BinkleyTerm
 *
 *              (C) Copyright 1987-96, Bit Bucket Software Co.
 *     For license and contact information see /doc/orig_260/license.260.
 *
 *           This version was modified by the BinkleyTerm XE Team.
 *        For contact information see /doc/team.lst and /doc/join.us.
 *  For a complete list of changes see /doc/xe_user.doc and /doc/xe_hist.doc.
 *
 * Filename    : $Source: /cvsroot/btxe/btnt/sources/mailroot.cpp,v $
 * Revision    : $Revision: 1.2 $
 * Tagname     : $Name:  $
 * Last updated: $Date: 2005/01/03 03:27:00 $
 * State       : $State: Exp $
 * Orig. Author: Bob Hartman
 *
 * Description : BinkleyTerm Mail Control Routines
 *
 *---------------------------------------------------------------------------*/

/* Include this file before any other includes or defines! */

#include "includes.h"
#include "fidomail.h"

char *estring (int, int);
static int scan_netmail (void);


/* MMP 960419 A call to movelogs() has been added for every return statement
 * in do_mail(). */

int
do_mail (ADDR* baddr, int type) /* 1 = manual, 0 = normal, -1 = CM */
{
    int i;
    int ret = 0;                  /* TJW 960526 return value */
    char junk[128];
    short CM;
    CALLHIST CallInfo;
    long callstart = 0;
    long callend;

    /* VRP 990826 start */

    int sess_result = mission_ok;
    HIDDEN *hidden;
    HIDDEN_LINE *hidden_line;
    int wday, hours, mins, secs, ths;

    /* VRP 990826 end */

    memset (&CallInfo, 0, sizeof (CallInfo));
    set_prior (PRIO_MODEM);       /* TJW 960526 */

    b_init ();
    caller = 1;

    /* MR 970803 already done in b_init:
     * got_packet = got_arcmail = got_mail = got_fax = sent_mail = 0; */

    no_WaZOO_Session = QueryNodeFlag (no_WaZOO, NOWAZOO, baddr);  /* r. hoerner */
    no_EMSI_Session = QueryNodeFlag (no_EMSI, NOEMSI, baddr);  /* r. hoerner */

    sprintf (junk, "%s", Full_Addr_Str (baddr));

    called_addr = remote_addr = *baddr;  /* structure assignment */

    if (!net_params)
    {
        status_line (MSG_TXT (M_INSUFFICIENT_DATA));
        set_xy ("");
        goto end_movelogs;
    }

    if (!nodeproc (junk))         /* sets "newnodedes" struct */
        goto end_movelogs;

    /* VRP hidden line support start */

    /*
     * Here we must deside to use main line or one of hidden one
     *
     */

    for (hidden = hidden1; hidden; hidden = hidden->next)
    {
        if (SameAddress ((ADDR *) hidden, baddr))
        {
            if (hidden->current == NULL)
            {
                /*
                 * Use main (nodelist) line - no changes needed if is online now
                 * In other case - try to find appropriate hidden line
                 *
                 */

                /* if any "Override" statement: */
                GetAltPhone ((char *) &newnodedes.PhoneNumber, baddr);
                GetAltModemflag ((word *) & newnodedes.ModemType, baddr);
                GetAltFidoFlag ((word *) & newnodedes.NodeFlags, baddr);
                if (GetAltOnLineTime (&newnodedes.online_start, &newnodedes.online_end, baddr))
                {
                    newnodedes.NodeFlags |= B_CM;
                }

                /*
                 * The main line isn't online now - try to find hidden one
                 *
                 */

                if (!(newnodedes.NodeFlags & B_CM) &&
                        !IsOnLine (newnodedes.online_start, newnodedes.online_end))
                {
                    hidden->current = hidden->hidden_line1;
                }
            }

            if (hidden->current != NULL)
            {
                /*
                 * Use hidden line - we must find appropriate line which 
                 * is now online
                 *
                 */

                dostime (&wday, &hours, &mins, &secs, &ths);
                wday = (short) weekdays[wday % 7];
                hidden_line = hidden->current;

                while (hidden_line)
                {
                    status_line (">%d:%d/%d.%d - days %d",
                                 hidden->Zone, hidden->Net, hidden->Node,
                                 hidden->Point, hidden_line->days);

                    if ((hidden_line->days & wday) &&
                            (IsOnLine (hidden_line->start, hidden_line->end) ||
                             (hidden_line->fido & B_CM)))
                    {
                        hidden->current = hidden_line;

                        strcpy (newnodedes.PhoneNumber, hidden_line->num);
                        newnodedes.ModemType = (byte) hidden_line->modem;
                        newnodedes.NodeFlags = hidden_line->fido;
                        newnodedes.online_start = hidden_line->start;
                        newnodedes.online_end = hidden_line->end;

                        break;
                    }
                    hidden_line = hidden_line->next;
                }

                if (hidden_line == NULL)
                {
                    for (hidden_line = hidden->hidden_line1;
                            hidden_line != hidden->current;
                            hidden_line = hidden_line->next)
                    {
                        if ((hidden_line->days & wday) &&
                                (IsOnLine (hidden_line->start, hidden_line->end) ||
                                 (hidden_line->fido & B_CM)))
                        {
                            hidden->current = hidden_line;

                            strcpy (newnodedes.PhoneNumber, hidden_line->num);
                            newnodedes.ModemType = (byte) hidden_line->modem;
                            newnodedes.NodeFlags = hidden_line->fido;
                            newnodedes.online_start = hidden_line->start;
                            newnodedes.online_end = hidden_line->end;

                            break;
                        }
                    }
                }
            }

            break;
        }
    }

    if (hidden == NULL)
    {
        /*
         * No hidden line found - use the main one
         *
         */

        GetAltPhone ((char *) &newnodedes.PhoneNumber, baddr);
        GetAltModemflag ((word *) & newnodedes.ModemType, baddr);
        GetAltFidoFlag ((word *) & newnodedes.NodeFlags, baddr);
        if (GetAltOnLineTime (&newnodedes.online_start, &newnodedes.online_end, baddr))
        {
            newnodedes.NodeFlags |= B_CM;
        }
    }

    /* VRP hidden line support end */

    if (!do_dial_strings (type))  /* r. hoerner: moved, "bittype" */
    {
        status_line (">don't know how to dial to %s", Full_Addr_Str (baddr));
        ret = 0;
        goto end_movelogs;
    }

    /* else: either no matching "ModemTrans", then we could use "PreDial", *
     * or "ModemTrans" matched and we found a predial string there         */

    // status_line (MSG_TXT (M_PROCESSING_NODE),
    // Full_Addr_Str (baddr), newnodedes.SystemName);

    if (type > 0)                 /* manual dial */
    {
        if (flag_file (TEST_AND_SET, &called_addr, 1))
        {
            if (ComIsOnline(hcModem))
                modem_hangup ();
            goto end_movelogs;
        }

        if (ComIsOnline(hcModem) ||              /* called manually maybe? */
                (ExtMailMask && (newnodedes.ModemType == (byte) ExtMailMask)))
            goto process_the_damned_mail;  /* yup, just do some mail */

        try_2_connect ((char *) newnodedes.PhoneNumber, &called_addr);
    }
    else
    {
        if ((type == 0) && (cur_event >= 0))  /* normal dial */
        {
            /* ------------------------------------------
             * If this event is supposed to be only local
             * ------------------------------------------ */

            /* TJW 970304: this is OLD behaviour and not appropriate for XE !!!!!!!!
             * so I commented this out to see what happens ...
             *
             *  if (e_ptrs[cur_event].behavior & MAT_LOCAL)
             *  {
             *    if (e_ptrs[cur_event].node_cost < 0)
             *    {
             *      if ((int) newnodedes.RealCost < -e_ptrs[cur_event].node_cost)
             *        goto dial_aborted;
             *    }
             *    else
             *    {
             *      if ((int) newnodedes.RealCost > e_ptrs[cur_event].node_cost)
             *        goto dial_aborted;
             *    }
             *  }
             */

            /* --------------------------------------------
             * If this is a "receive only" event then abort,
             * if not a "CRASH" event, too.
             * -------------------------------------------- */

            if ((e_ptrs[cur_event].behavior & MAT_NOOUT)  /* r.hoerner 1/12/97 */
                    && !(e_ptrs[cur_event].behavior & MAT_HIPRICM))
                goto dial_aborted;

            if (newnodelist)
            {
                CM = newnodedes.NodeFlags & B_CM;

                /* --------------------------------------------
                 * If event is no mail-to-all event and Non-CM
                 * -------------------------------------------- */

                if (!(e_ptrs[cur_event].behavior & MAT_NOMAIL24) && !CM
                        && !IsOnLine (newnodedes.online_start, newnodedes.online_end))  /* VRP 990820 */
                    goto dial_aborted;

                /* ------------------------------------------------
                 * If this event is supposed to send to Non-CM only
                 * ------------------------------------------------ */

                if ((e_ptrs[cur_event].behavior & MAT_NOCM) && CM)
                    goto dial_aborted;
            }
        }

        /* ------------------------------------------------
         * If we cannot set our BSY flag
         * ------------------------------------------------ */

        if (flag_file (TEST_AND_SET, &called_addr, 1))
        {
            if (ComIsOnline(hcModem))
                modem_hangup ();
            goto end_movelogs;
        }

        if (un_attended)
        {
            sb_move (holdwin, 1, 0);
            sb_wa (holdwin, colors.calling, HOLDWIN_SIZE - 2);  /* TJW 960503 */
        }

        if (!blank_on_key)
            screen_blank = 0;

        if (ExtMailMask && (newnodedes.ModemType == (byte) ExtMailMask))
            goto process_the_damned_mail;

        i = try_1_connect ((char *) newnodedes.PhoneNumber, &called_addr);

        /* ------------------------------------------------
         * If we could not get a connection, 'i' means
         *  1 = IGNORE
         *  0 = FAILURE
         * <0 = ERROR (WAS_BUSY == -2)
         * ------------------------------------------------ */

        /* VRP hidden line support start */

        /*
         * Here we must switch to next hidden line if any
         *
         */

        if (i == FAILURE || i == WAS_BUSY || i == USER_BREAK)
        {
            for (hidden = hidden1; hidden; hidden = hidden->next)
            {
                if (SameAddress ((ADDR *) hidden, &called_addr))
                {
                    if (hidden->current == NULL)
                    {
                        hidden->current = hidden->hidden_line1;
                    }
                    else
                    {
                        hidden->current = hidden->current->next;
                    }
                }
            }
        }

        /* VRP hidden line support end */

        if (i <= 0)                 /* MR 970523 was "i < 0"                            */
        {
            if (chg_call)
            {
                change_call_to_normal (&called_addr);
                chg_call = 0;
            }

            if (un_attended)
            {
                sb_move (holdwin, 1, 0);
                sb_wa (holdwin, colors.hold, HOLDWIN_SIZE - 2);  /* TJW 960503 */
            }

            modem_init ();

            status_line (MSG_TXT (M_END_OF_ATTEMPT));
            flag_file (CLEAR_FLAG, &called_addr, 1);

            ret = i;
            goto end_movelogs;
        }
    }

process_the_damned_mail:

    if (ComIsOnline(hcModem) ||                /* if we did,        */
            (ExtMailMask && (newnodedes.ModemType == (byte) ExtMailMask)))
    {
        /* CFS 961225 - we don't reset the counter here if UseRingingCost
           is used. The counter is running since either the dial or the
           last ringing. Exception is if the counter isn't started for
           some reason */
        if (!UseRingingCost || callstart == 0)
        {
            callstart = unix_time (NULL);
            // status_line (" setting1 callstart to %08lx", callstart);
        }
        else
            callstart = cfscallstart;
#if defined(OS_2) && !defined(__EMX__)
        UpdateCID = 0;
#endif
        sess_result = b_session (1, &CallInfo);  /* do a mail session  *//* VRP 990826 */

        /* VRP 990913 start */

        if (CallInfo.calltype == CT_MAILER || CallInfo.calltype == CT_EXTMAIL)
            CallInfo.h.m.success = sess_result;

        /* VRP 990913 end */

        callend = unix_time (NULL);
        // status_line (" setting1 callend to %08lx", callend);

        b_session_cleanup (sess_result);  /* VRP 990826 */

        hist.last_Elapsed = callend - callstart;
        hist.callcost += cost_of_call (1, callstart, callend, &CallInfo);
        ++hist.connects;

        if (CallInfo.calltype == CT_MAILER)
            SaveCallInfo (CallInfo);

        if ((SaveExtSession & SAVE_EXT_OUT) && CallInfo.calltype == CT_EXTMAIL)  /* VRP 990913 */
            SaveCallInfo (CallInfo);

        write_stats ();

        if (un_attended && (got_arcmail || got_packet || got_mail))
        {
            bad_call (baddr, -1);
            receive_exit ();
        }

        if (un_attended)
        {
            sb_move (holdwin, 1, 0);
            sb_wa (holdwin, colors.hold, HOLDWIN_SIZE - 2);  /* TJW960503 */
        }

        ret = 1;
        goto end_movelogs;
    }
    else
    {
        if (chg_call)
        {
            change_call_to_normal (&called_addr);
            chg_call = 0;
        }

        status_line (MSG_TXT (M_END_OF_ATTEMPT));
        flag_file (CLEAR_FLAG, &called_addr, 1);
        modem_init ();              /* MR 970425 */
    }

    if (un_attended)
    {
        sb_move (holdwin, 1, 0);
        sb_wa (holdwin, colors.hold, HOLDWIN_SIZE - 2);  /* TJW960503 */
    }

    write_stats ();
    ret = 2;
    goto end_movelogs;

dial_aborted:

    /* r.hoerner: i like to test, whether this really can happen.
       I guess: no! */
    status_line ("!Emergency brake: No dialout in this event");

end_movelogs:

#if defined(OS_2) && !defined(__EMX__)
    UpdateCID = 1;
#endif
    movelogs ();
    do_today (1);
    set_prior (PRIO_REGULAR);     /* TJW 960526 */
    return ret;
}


int
handle_inbound_mail (int answer_now)
{
    CALLHIST CallInfo;
    long lEndTime;                /* master timeout           */
    long callstart;
    long callend;
    int mr;                       /* Modem response           */
    int iRingCount = 0;           /* for counting 'em         */
    int answered = 0;             /* already answered phone ? */
    int i;
    int sess_result = mission_ok; /* VRP 990915 */

    /* MR 970523 removed interval timer, master timeout is enough */

    memset (&CallInfo, 0, sizeof (CallInfo));
    no_WaZOO_Session = no_WaZOO;
    no_EMSI_Session = no_EMSI;
    caller = 0;

    memset ((char *) &remote_addr, 0, sizeof (remote_addr));
    remote_addr.Domain = NULL;    /* only Microsoft can explain this  */

    lEndTime = timerset (2 * PER_MINUTE);  /* Master timeout of 2 minutes      */

    /* MR 970311 removed those ugly gotos.... */

    do
    {
        /* Do we have carrier, RING, or NO DIAL TONE? */
        if (timeup (lEndTime) || ((answer_now <= 0) && (iRingCount == 0) &&
                                  !(server_mode && ComIsOnline(hcModem)) && !CHAR_AVAIL ()))
        {
            time_release ();
#if defined(OS_2) && !defined(__EMX__)
            UpdateCID = 1;
#endif
            return (0);               /* No, nothing to do                */
        }

        mail_only = 1;

        if (cur_event >= 0)
            if (e_ptrs[cur_event].behavior & MAT_BBS)
                mail_only = 0;

        if (answer_now <= 0)
            while (!timeup (lEndTime) && !CHAR_AVAIL () && !KEYPRESS ())
            {
                if (answered)           /* if we answered the phone, count down seconds */
                {
                    int tmp = (lEndTime - timerset (0)) / PER_SECOND;

                    do_status (M_READY_ANSWCOUNT, &tmp);  /* MR 972412 */
                }

                time_release ();        /* wait for another result          */
            }

        // MR 970523 not needed, modem_response checks for ESC
        //
        //    if (KEYPRESS ())                  /* If aborted by user,              */
        //    {
        // #ifdef OS_2 /*  TS 970328, CFS please have a look if this is intentional */
        //      UpdateCID=1;
        // #endif
        //      return (1);                     /* get out                          */
        //    }

        if (server_mode && ComIsOnline(hcModem))
        {
            mr = CONNECTED;
#if defined(OS_2) && !defined(__EMX__)
            UpdateCID = 0;
#endif
        }
        else
        {
            if (answer_now > 0)
            {
                mr = RINGING;           /* say it rang                      */
                iRingCount = ring_wait; /* we want to answer right now      */
                answer_now = -1;
            }
            else
                mr = modem_response (5 * PER_SECOND);

            if ((mr == RINGING) && (modem.answer != NULL))  /* saw RING        */
            {
                ++iRingCount;           /* no need to check an timer, we've only got 10secs */

#if defined(OS_2) && !defined(__EMX__)
                UpdateCID = 0;
#endif
                /* Unblank on the first ring */
                if ((answer_now < 0) || (iRingCount == 1))
                {
                    if (!blank_on_key)
                        screen_blank = 0;

                    if (un_attended)
                        sb_show ();
                }

                /* Answer if we've reached the correct ring number */
                if (iRingCount >= ring_wait)
                {
                    char *response = NULL;
                    int userbreak = 0;
                    long t = timerset (PER_SECOND);

                    free (CurrentCID);
                    CurrentCID = NULL;

                    do
                    {
                        if (fpnumber[0] != '\0' && CurrentCID == NULL)
                        {
                            CurrentCID = Get_CallerID (fpnumber);

                            if (rejected_call (CurrentCID))
                            {
                                time_release ();
                                return (0);
                            }
                        }

                        if (CHAR_AVAIL ())
                            response = get_response (t, 1, &userbreak);
                        else
                            response = NULL;
                    }
                    while (response && !(timeup (t)));

                    set_banner ();
                    CLEAR_INBOUND ();

                    // SMS 980823 begin
                    for (i = 0; i < num_ext_mail; i++)
                    {
                        if (stricmp ("RING", ext_mail_string[i]) == 0)
                        {
                            if (CurrentCID != NULL)
                                strcpy (mdm_reliable, CurrentCID);
                            else
                                mdm_reliable[0] = '\0';
                            UUCPexit (lev_ext_mail[i], 0);
                            i = num_ext_mail + 1;
                        }
                    }

                    if (i == num_ext_mail)
                    {
                        // no "ExtrnMail RING" => answer modem
                        modem_answer ();
                    }
                    // SMS 980823 end

                    answered = 1;

                    /* MR 970523 restart master timeout */
                    lEndTime = timerset (60 * PER_SECOND);
                }
                else
                {
                    /* Otherwise wait up to 10 seconds for something to happen */
                    /* MR 971223 answer count down */
                    int tmp = ring_wait - iRingCount;  /* int, using printf %d */

                    do_status (M_READY_ANSWCOUNT, &tmp);

                    /* MR 970523 restart master timeout */
                    lEndTime = timerset (5 * PER_SECOND);
                }
            }
            else if (mr == FAILURE)   /* If we got "No Carrier"           */
            {
                modem_hangup ();        /* MR 970523 hangup                 */
                modem_init ();          /*           and reinit modem       */
                time_release ();
#if defined(OS_2) && !defined(__EMX__)
                UpdateCID = 1;
#endif
                return (0);             /* Nothing happened...              */
            }

        }
    }                             /* then proceed along               */
    while ((mr != CONNECTED) && !(mr & FAX));

    /* MR 970311 end of rewrite... more to come ;) */

    newnodedes.RealCost = 0;      /* TJW 961008 init costs for BBS/FAX callers */
    start_hist = hist;
    callstart = unix_time (NULL);

    // status_line (" setting2 callstart to %08lx", callstart);

    set_prior (PRIO_MODEM);       /* TJW 960526 */

    if (!blank_on_key)
        screen_blank = 0;

    if (un_attended)
        sb_show ();

    /* TJW 960625 will be Inc-Fail if this was all */
    last_type (6, (ADDR *) addrMyAddr);

    if (mr & FAX)
    {
        int gotfax = 1;
        int i;

        if (!fax_in)
        {
            /* We have a FAX result code but are not configured to
             * receive any. See if the user has set up a fax exit
             * using the external mail strings */

            for (i = 0; i < num_ext_mail; i++)
            {
                if (stricmp (saved_response, ext_mail_string[i]) == 0)
                {
                    last_type (5, (ADDR *) addrMyAddr);
                    UUCPexit (lev_ext_mail[i], 0);
                    goto end_fax;
                }
            }

            status_line ("!FAX: no 'FaxInDir'. Cannot receive");
        }
        else
        {
            last_type (5, (ADDR *) addrMyAddr);
            gotfax = faxreceive (FAX, &CallInfo);  /* Try to get a FAX                 */
        }

end_fax:
        modem_hangup ();            /* Then hang up                     */

        callend = unix_time (NULL);
        // status_line (" setting2a callend to %08lx", callend);

        hist.last_Elapsed = callend - callstart;
        hist.callcost += cost_of_call (0, callstart, callend, &CallInfo);

        if (gotfax)                 /* If we got one,                   */
        {
            hist.fax_calls++;         /* TJW 960903 update faxcnt in hist */

            /* AW 970531 save FAX callinfo   */
            if (fax_in && CallInfo.calltype == CT_FAX)
                SaveCallInfo (CallInfo);

            got_fax = 1;              /* Say we got mail                  */
            receive_exit ();          /* And try to exit                  */
        }

        modem_init ();              /* MR 970425 and reinitialize it    */
    }
    else
    {
        long t = timerset (1000);   /* TE 131097: some modems are quite */

        while ((!ComIsOnline(hcModem)) && (!timeup (t)))  /* slow turning DCD on, e.g. VMO  */
            time_release ();

        if (ComIsOnline(hcModem))                /* if we have a carrier,            */
        {
            sess_result = b_session (0, &CallInfo);  /* do a mail session *//* VRP 990913 */

            /* VRP 990913 start */

            if (CallInfo.calltype == CT_MAILER || CallInfo.calltype == CT_EXTMAIL)
                CallInfo.h.m.success = sess_result;

            /* VRP 990913 end */

            callend = unix_time (NULL);
            // status_line (" setting2b callend to %08lx", callend);

            b_session_cleanup (mission_ok);  /* VRP 990824 */

            hist.last_Elapsed = callend - callstart;
            hist.callcost += cost_of_call (0, callstart, callend, &CallInfo);

            if (CallInfo.calltype == CT_MAILER)
                SaveCallInfo (CallInfo);

            if ((SaveExtSession & SAVE_EXT_IN) && CallInfo.calltype == CT_EXTMAIL)  /* VRP 990913 */
                SaveCallInfo (CallInfo);

            /* We got inbound mail */
            if (got_arcmail || got_packet || got_mail)
            {                         /* r. hoerner:                    */
                if (DelBadCall)         /* if "undialable" site called us */
                {
                    bad_call (&remote_addr, -1);  /* then delete badcall status */
                    for (i = 0; i < num_rakas; i++)  /* do this to all of his AKAs too */
                        bad_call (&remote_akas[i], -1);  /* AG 990120 */
                }

                receive_exit ();
            }
        }
        else
        {
            modem_hangup ();          /* Try to reset modem               */

            callend = unix_time (NULL);
            // status_line (" setting2c callend to %08lx", callend);

            modem_init ();            /* MR 970424 and reinitialize it    */

            hist.last_Elapsed = callend - callstart;
            hist.callcost += cost_of_call (0, callstart, callend, &CallInfo);
        }
    }

    mailer_banner ();
    do_today (1);
    set_prior (PRIO_REGULAR);     /* TJW 960526 */

    return (1);
}

void
receive_exit ()
{
    char junk1[150];
    int i;
    BINK_EVENT evt;
    int gotany;                   /* MMP 960419 */

    if (cur_event >= 0)
        evt = e_ptrs[cur_event];

    /* MMP 960419/960507 Create BTMAIL.IN in flags directory */

    gotany = got_fax || got_mail || got_packet || got_arcmail || got_tic;  /* VRP 990918 */

    if (gotany && MailFlag)
    {
        FILE *fp;

        if ((MailFlag[0] == '%') && (MailFlag[1] == 's'))
            sprintf (junk1, MailFlag, flag_dir ? flag_dir : "");
        else
            strcpy (junk1, MailFlag);

        fp = fopen (junk1, write_binary);
        if (fp != NULL)
            fclose (fp);
    }

    /* VRP 990920 start */

    if (got_packet && NetMailFlag)
    {
        FILE *fp;

        if ((NetMailFlag[0] == '%') && (NetMailFlag[1] == 's'))
            sprintf (junk1, NetMailFlag, flag_dir ? flag_dir : "");
        else
            strcpy (junk1, NetMailFlag);

        fp = fopen (junk1, write_binary);
        if (fp != NULL)
            fclose (fp);
    }

    if (got_arcmail && ArcMailFlag)
    {
        FILE *fp;

        if ((ArcMailFlag[0] == '%') && (ArcMailFlag[1] == 's'))
            sprintf (junk1, ArcMailFlag, flag_dir ? flag_dir : "");
        else
            strcpy (junk1, ArcMailFlag);

        fp = fopen (junk1, write_binary);
        if (fp != NULL)
            fclose (fp);
    }

    if (got_tic && TicFlag)
    {
        FILE *fp;

        if ((TicFlag[0] == '%') && (TicFlag[1] == 's'))
            sprintf (junk1, TicFlag, flag_dir ? flag_dir : "");
        else
            strcpy (junk1, TicFlag);

        fp = fopen (junk1, write_binary);
        if (fp != NULL)
            fclose (fp);
    }

    /* VRP 990920 end */

    if ((aftermail != NULL) && gotany)
    {
        strcpy (junk1, aftermail);
        if (cur_event >= 0)
            strcat (junk1, evt.cmd);

        // HJK - 98/02/25 - Some changes to 'fast' aftermail
        if (aftermail[0] == '#')
        {
            memmove (&junk1[0], &junk1[1], strlen (junk1));
            i = ExecSession ((LPSTR ) junk1);
            if (i == 0)               /* this is: with OS_2 and _WIN32 */
            {
                status_line (MSG_TXT (M_LAUNCH_AFTER_MAIL));
#ifdef BINKLEY_SOUNDS
                Make_Sound (fnMailSound);
#endif
                goto aftermail_done;
            }

            aftermail[0] = ' ';
        }

        status_line (MSG_TXT (M_AFTERMAIL));
#ifdef BINKLEY_SOUNDS
        Make_Sound (fnMailSound);
#endif

        if (!quickaftermail)
            modem_busy ();

        screen_clear ();
        vfossil_cursor (1);
        strcpy (junk1, aftermail);

        if (cur_event >= 0)
            strcat (junk1, evt.cmd);

        close_up (0);
        b_spawn (junk1);
        come_back ();
        status_line (MSG_TXT (M_OK_AFTERMAIL));

        if (!quickaftermail)
        {
            modem_init ();            /* MR 970310 */
            xmit_reset (0, 0);
        }

        IPC_SetStatus (MSG_TXT (M_MCP_WAITING));
        waitfor_line = timerset (PER_MINUTE);

aftermail_done:;
    }

    if (cur_event >= 0)
    {
        int errlvl = 0;
        int exitlvl = 0;

        if (got_arcmail && (evt.errlevel[2]))
        {
            exitlvl = evt.errlevel[2];
            if (errlvlshell[exitlvl - 1] == NULL)
            {
                errlvl = exitlvl;
                status_line (MSG_TXT (M_EXIT_COMPRESSED), errlvl);
            }
            else
            {
                status_line (MSG_TXT (M_SHELL_COMPRESSED), exitlvl);
                errl_shell (exitlvl);
                got_arcmail = got_mail = got_packet = 0;
            }
        }

        for (i = 0; i < 6; i++)
        {
            if (user_exits[i])
            {
                user_exits[i] = 0;
                exitlvl = evt.errlevel[i + 3];

                if (errlvlshell[exitlvl - 1] == NULL)
                {
                    if (errlvl == 0)
                    {
                        errlvl = exitlvl;
                        status_line (MSG_TXT (M_EXIT_AFTER_EXTENT),
                                     &(evt.err_extent[i][0]), errlvl);
                    }
                }
                else
                {
                    status_line (MSG_TXT (M_SHELL_AFTER_EXTENT),
                                 &(evt.err_extent[i][0]), exitlvl);
                    errl_shell (exitlvl);
                }
            }
        }

#ifdef BINKLEY_SOUNDS
        /* If we got an exit level, it's still set even if we shelled.
           So use its presence to determine whether to make noise */

        if (exitlvl)
            Make_Sound (fnFileSound);
#endif

        /*  See if we still want to exit */
        if (errlvl)
            errl_exit (errlvl);
    }

    if (got_fax && cur_event >= 0 && (evt.faxerrlevel))
    {
#ifdef BINKLEY_SOUNDS
        Make_Sound (fnFAXSound);
#endif
        /* MR 961123 */
        got_fax = 0;
        errl_exit_shell (evt.faxerrlevel, MSG_TXT (M_EXIT_FAX), MSG_TXT (M_SHELL_FAX));
    }

    if ((got_fax || got_mail || got_packet) &&
            (cur_event >= 0) && (evt.errlevel[1]))
    {
#ifdef BINKLEY_SOUNDS
        Make_Sound (fnMailSound);
#endif
        /* MR 961123 */
        got_fax = got_mail = got_packet = got_tic = 0;  /* VRP 990918 */
        errl_exit_shell (evt.errlevel[1], MSG_TXT (M_EXIT_AFTER_MAIL),
                         MSG_TXT (M_SHELL_AFTER_MAIL));
    }

    got_arcmail = 0;
    got_packet = 0;
    got_mail = 0;
    got_fax = 0;
    got_tic = 0;                  /* VRP 990918 */
}

/* MR 961123 a little new function... */

void
errl_exit_shell (int errlvl, char *txt_exit, char *txt_shell)
{
    if (errlvlshell[errlvl - 1] == NULL)
    {
        status_line (txt_exit, errlvl);
        errl_exit (errlvl);
    }
    else
    {
        status_line (txt_shell, errlvl);
        errl_shell (errlvl);
    }
}

void
errl_exit (int n)
{
    write_sched ();
    status_line (MSG_TXT (M_BINK_END), ANNOUNCE);
    modem_busy ();
    b_sbexit ();
    gotoxy (0, SB_ROWS - 1);      /* TJW 960517 */
    cputs ("\n");                 /* TJW 960517 */

    if (vfossil_installed)
        vfossil_close ();

    if (!share)
        MDM_DISABLE ();

    closelogs ();                 /* TJW 960527 */
    no_longer_alive ();           /* MR  961123 */
    set_prior (PRIO_NORMAL);      /* SET PRIORITY BACK TO NORMAL (r. hoerner) */
    exit (n);
}

void
errl_shell (int n)
{
#ifdef  OS_2                    /*  TS 970206   */
    HEV hev = 0;
    APIRET ulrc = 0;

#endif
#ifdef _WIN32
    HANDLE hsem;
    DWORD rc;

#endif

    write_sched ();

    if (!strnicmp ("start ", errlvlshell[n - 1], 6))
    {
        status_line (MSG_TXT (M_START_ERRLVL_SESSION), n);
        close_up (0);
        b_spawn (errlvlshell[n - 1]);  /*  TS 970404, was `system ()'  */
        come_back ();
        IPC_SetStatus (MSG_TXT (M_MCP_WAITING));
        return;
    }
#ifdef OS_2
    else if (!strnicmp ("post ", errlvlshell[n - 1], 5))
    {
        PCHAR sem_name;

        status_line (MSG_TXT (M_POSTING_SEMAPHORE), n);
        sem_name = strlwr (strdup (errlvlshell[n - 1]));
        ulrc = DosCreateEventSem ((PSZ) strstr (sem_name, "\\sem32\\"), &hev, 0, 0);

        if (ulrc == ERROR_DUPLICATE_NAME)
            if (!DosOpenEventSem ((PSZ) strstr (sem_name, "\\sem32\\"), &hev))
            {
                DosPostEventSem (hev);
                DosCloseEventSem (hev);
            }

        if (ulrc == NO_ERROR)
        {
            status_line (MSG_TXT (M_NO_SEMAPHORE), strstr (sem_name, "\\sem32\\"));
            DosCloseEventSem (hev);
        }

        free (sem_name);
        return;
    }
#endif
#ifdef _WIN32                   /* AS 970316 - very late :-), sem handling for Win32 */
    else if (!strnicmp ("post ", errlvlshell[n - 1], 5))
    {
        status_line (MSG_TXT (M_POSTING_SEMAPHORE), n);

        /* instead of a semaphore, we use an Win32 Event. */
        hsem = CreateEvent (NULL, TRUE, TRUE, errlvlshell[n - 1] + 5);
        rc = GetLastError ();

        if (hsem == NULL)
            status_line (":Error creating semaphore %s", errlvlshell[n - 1] + 5);
        else
        {
            if (rc == ERROR_ALREADY_EXISTS)
            {
                status_line (">Semaphore %s already exists, flagging it", errlvlshell[n - 1] + 5);
                SetEvent (hsem);
            }
        }

        return;
    }
#endif
    else if (!strnicmp ("poll ", errlvlshell[n - 1], 5))  /* JNK 970224 */
    {
        ADDR addr;
        char *c, *s, name[31], *HoldName, junk[255];
        int i, fail;
        FILE *f;

        c = s = errlvlshell[n - 1];
        for (;;)                    /* we go forever */
        {
            i = 0;
            fail = 1;

            while (!isdigit (*c))     /* go to address */
                c++;

            while (((*c) != ' ') && ((c - s) < (char) strlen (s)))  /* copy address */
                name[i++] = *(c++);
            name[i] = '\0';

            if (find_address (name, &addr))  /* try to cath address */
                if (nodefind (&addr, 0))  /* is this node listed? */
                    if (!flag_file (TEST_AND_SET, &addr, 0))  /* is this node busy? */
                    {
                        HoldName = HoldAreaNameMunge (&addr);
                        sprintf (junk, "%s%s.clo", HoldName, Hex_Addr_Str (&addr));
                        f = fopen (junk, append_binary);
                        if (f != NULL)      /* try to create/open poll_file */
                        {
                            fclose (f);
                            fail = 0;
                        }

                        flag_file (CLEAR_FLAG, &addr, 0);
                    }

            if (fail == 0)
                status_line (MSG_TXT (M_ERRLVL_POLL), name);  /* all okidoki */
            else
                status_line (MSG_TXT (M_ERRLVL_POLL_FAIL), name);  /* tough luck */

            if ((c - s) == (char) strlen (s))  /* is the end already there? */
                break;
        }

        global_rescan ();           /* force mailer-rescan */
    }
    else if (strnicmp ("none", errlvlshell[n - 1], 4))  /* MR 961123 */
    {
        modem_busy ();
        screen_clear ();
        vfossil_cursor (1);
        close_up (0);
        b_spawn (errlvlshell[n - 1]);
        come_back ();
        status_line (MSG_TXT (M_RETURN_ERRLVL_SHELL));
        modem_init ();              /* MR 970310 */
        xmit_reset (0, 0);
        waitfor_line = timerset (PER_MINUTE);
    }
}

long
random_time (int x)
{
    int i;

    if (x == 0)
        return (0L);

    /* Number of seconds to delay is random based on x +/- 50% */
    i = (rand () % (x + 1)) + (x / 2);

    return (timerset ((unsigned int) (i * PER_SECOND)));  /*  TS970519    */
}

char *
HoldAreaNameMunge (ADDR* maddr)
{
    static char munged[PATHLEN];  /* TJW 960526 */
    register char *p, *q;
    int i;

    if (maddr == NULL)            /* if invalid address pointer   */
        return (BINKpath);          /* return at least a valid path */

    if ((maddr->Domain != my_addr.Domain) && (maddr->Domain != NULL))
    {
        /* CEH 19990514: this is no longer needed */
        /* *domain_loc = '\0'; */

        strcpy (munged, domain_area);
        q = &(munged[strlen (munged)]);
        for (i = 0; domain_name[i] != NULL; i++)
        {
            if (domain_name[i] == maddr->Domain)
            {
                if (domain_abbrev[i] != NULL)
                {
                    p = domain_abbrev[i];
                    while (*p)
                        *q++ = *p++;
                    sprintf (q, ".%03x" DIR_SEPS, maddr->Zone);

                    return (munged);
                }
            }
        }
        /* if we are here, then no matching domain name was found. */
        /* let's take our Hold directory and return a name, that   */
        /* will allow to store the coming files parallel to that   */
        /* directory. In other words:                              */
        /* If our Hold-directory is  c:\binkley\outbound           */
        /* then return an name as    c:\binkley\outbound.000       */
        /*                                             (r.hoerner) */
    }

    strcpy (munged, hold_area);
    q = &(munged[strlen (munged)]);
    if (maddr->Zone != addrMyAddr->Zone)
    {
        --q;
        sprintf (q, ".%03x" DIR_SEPS, maddr->Zone);
    }

    return (munged);
}

void
mailer_banner ()
{
    if (un_attended)
    {
        char junk[256];

        vfossil_cursor (0);
        // clear_filetransfer ();

        show_cs_port (cur_baud.rate_value, FALSE);  /*  TS 961122   */
        sprintf (junk, "%-2d", cur_event + 1);
        sb_move_puts (settingswin, SET_EVNT_ROW, SET_COL, junk);
    }

    set_baud (max_baud.rate_value, 0);
}

void
clear_filetransfer ()
{
    if (un_attended)
        sb_fillc (filewin, ' ');
}

char *
estring (int e, int how_big)
{
    char j[30];
    char *p, *q;
    static char ebuf[50];

    *(p = &ebuf[0]) = '\0';
    if (e >= 0)
    {
        if (e_ptrs[e].behavior & MAT_BBS)
        {
            *p++ = 'B';
            if (how_big)
                *p++ = ' ';
        }

        if (e_ptrs[e].behavior & MAT_CM)
        {
            *p++ = 'C';
            if (how_big)
                *p++ = ' ';
        }

        if (e_ptrs[e].behavior & MAT_DYNAM)
        {
            *p++ = 'D';
            if (how_big)
                *p++ = ' ';
        }

        if (how_big && (e_ptrs[e].behavior & MAT_FORCED))
        {
            *p++ = 'F';
            *p++ = ' ';
        }

        if (e_ptrs[e].behavior & MAT_HIPRICM)
        {
            *p++ = 'H';
            if (how_big)
                *p++ = ' ';
        }

        if (e_ptrs[e].behavior & MAT_NOCM)
        {
            *p++ = 'K';
            if (how_big)
                *p++ = ' ';
        }

        if (e_ptrs[e].behavior & MAT_LOCAL)
        {
            *p++ = 'L';
            if (how_big)
            {
                switch (e_ptrs[e].extramask & EXTR_LBHVMASK)
                {
                case EXTR_LINDEXCOST:
                    *p++ = 'I';
                    break;

                case EXTR_LMINUTECOST:
                    *p++ = 'C';
                    break;

                case EXTR_LTOTALCOST:
                    *p++ = 'T';
                    break;

                default:
                    *p++ = '?';
                    break;
                }

                switch (e_ptrs[e].extramask & EXTR_LRELMASK)
                {
                case EXTR_LEQUALS:
                    sprintf (j, "=%d ", e_ptrs[e].node_cost);
                    break;

                case EXTR_LLESS:
                    sprintf (j, "<%d ", e_ptrs[e].node_cost);
                    break;

                case EXTR_LGREATER:
                    sprintf (j, ">%d ", e_ptrs[e].node_cost);
                    break;

                default:
                    sprintf (j, "??? ");
                    break;
                }

                q = j;
                while (*q)
                    *p++ = *q++;
            }
        }

        if (how_big && (e_ptrs[e].behavior & MAT_NOMAIL24))
        {
            *p++ = 'M';
            *p++ = ' ';
        }

        if (e_ptrs[e].behavior & MAT_NOREQ)
        {
            *p++ = 'N';
            if (how_big)
                *p++ = ' ';
        }

        if (e_ptrs[e].behavior & MAT_OUTONLY)
        {
            *p++ = 'S';
            if (how_big)
                *p++ = ' ';
        }

        if (e_ptrs[e].behavior & MAT_NOOUT)
        {
            *p++ = 'R';
            if (how_big)
                *p++ = ' ';
        }

        if (how_big && (e_ptrs[e].behavior & MAT_NOOUTREQ))
            *p++ = 'X';

        if (how_big && (e_ptrs[e].behavior & MAT_NOPICKUP))
            *p++ = 'P';

        if (how_big && (e_ptrs[e].extramask & EXTR_NOSOUND))
            *p++ = 'Y';
    }

    *p = '\0';
    return (ebuf);
}

void
do_status (int mnum, int *addData)
{
    char junk[256];
    char junk2[256];
    char *str = MSG_TXT (mnum);
    char *mcp_str = NULL;

    switch (mnum)
    {
    case M_FAX_RECV:
        mcp_str = MSG_TXT (M_MCP_FAX_RECV);
        break;

    case M_READY_INIT:
        mcp_str = MSG_TXT (M_MCP_INIT);
        break;

    case M_READY_WAITING:
        mcp_str = MSG_TXT (M_MCP_WAITING);
        break;

    case M_READY_HANGUP:
        mcp_str = MSG_TXT (M_MCP_HANGUP);
        break;

    case M_READY_ANSWCOUNT:
        mcp_str = MSG_TXT (M_MCP_ANSWERING);
        break;

    case M_READY_ANSWERING:
        mcp_str = MSG_TXT (M_MCP_ANSWERING);
        break;

    case M_READY_CONNECT:
        mcp_str = MSG_TXT (M_MCP_CONNECT);
        break;

    case M_READY_RESCAN:
        mcp_str = MSG_TXT (M_MCP_RESCAN);
        break;

    case M_READY_READING:
        mcp_str = MSG_TXT (M_MCP_READING);
        break;
    }

    if (mcp_str != NULL)
        IPC_SetStatus (mcp_str);

    sprintf (junk, "%-2d/%-6.6s", cur_event + 1, estring (cur_event, 0));
    sb_move_puts (settingswin, SET_EVNT_ROW, SET_COL, junk);

    if (addData != NULL)          /* MR 971223 */
    {
        sprintf (junk2, str, *addData);
        str = junk2;
    }

    sprintf (junk, "%-10s", str); /* TJW 960514 */
    sb_move_puts (settingswin, SET_STAT_ROW, SET_COL, junk);
    show_os_memfree ();
    sb_show ();
}

void
do_session (int mnum)
{
    char junk[16];

    sprintf (junk, "%-10s", MSG_TXT (mnum));
    sb_move_puts (settingswin, SET_SESSION_ROW, SET_COL, junk);
    sb_show ();
}

void
do_protocol (int mnum)
{
    char junk[16];

    sprintf (junk, "%-10s", MSG_TXT (mnum));
    sb_move_puts (settingswin, SET_PROTOCOL_ROW, SET_COL, junk);
    show_os_memfree ();
    sb_show ();
}

void
list_next_event ()
{
    int i, count;
    char *p;
    char j1[100], j2[100];


    if (cur_event >= 0 || next_event >= 0)
        clear_filetransfer ();

    if (cur_event >= 0)
    {
        p = estring (cur_event, 1);
        sprintf (j1, "%s%s%s", e_ptrs[cur_event].ename,
                 strlen (e_ptrs[cur_event].ename) > 0 ? " / " : "", p);
        sprintf (j2, MSG_TXT (M_CUR_EVENT), cur_event + 1, j1);
        sb_move_puts (filewin, 0, 0, j2);
    }

    if (next_event >= 0)
    {
        i = time_to_next (0);       /* MR 970803 must be first, sets up next_event */
        p = estring (next_event, 1);
        sprintf (j1, "%s%s%s", e_ptrs[next_event].ename,
                 strlen (e_ptrs[next_event].ename) > 0 ? " / " : "", p);
        sprintf (j2, MSG_TXT (M_NEXT_EVENT), next_event + 1, j1, i / 60, i % 60);
        sb_move_puts (filewin, 1, 0, j2);
    }

    count = scan_netmail ();
    if (count == 0)
        sprintf (j1, "%-5s", "-");
    else
        sprintf (j1, "%-5d", count);
    sb_move_puts (settingswin, SET_MAIL_ROW, SET_COL, j1);

    sb_show ();
}

static int
scan_netmail (void)             /* returns number of unread netmails */
{
    int count = 0;

    if (netmail == NULL)
        return count;

    if (*netmail == '$')
    {
        count = SquishScan (netmail + 1);

        if (count == -1)
        {
            free (netmail);
            netmail = NULL;
        }
    }
    else
    {
        int i, k;
        short iReadT[2];
        FILE *fpt;
        struct FILEINFO dta;
        char j[PATHLEN];

        strcpy (j, netmail);
        strcat (j, "lastread");     /* Full path to LASTREAD  */

        fpt = share_fopen (j, read_binary, DENY_WRITE);  /* Open the file          */
        if (fpt != NULL)
        {
            /*
             * Try to read two records. If there are two, the first record is the
             * current pointer and the second one is the last-read. If there is
             * one record, it is the last-read.
             */

            i = fread (iReadT, sizeof (short), 2, fpt);  /* Get lastread ptr   */

            fclose (fpt);             /* Then close the file */
            if (i)
            {
                int f = 0;              /* "findfirst"        */

                k = iReadT[0];          /* Last msg read      */
                strcpy (j, netmail);
                strcat (j, "*.msg");    /* Wildcard for .MSG  */

                while (!dfind (&dta, j, f))  /* If there are any,  */
                {
                    f = 1;
                    if (atoi (dta.name) > k)  /* See if one's more  */
                    {
                        FILE *fn;
                        FIDOMSG fidomsg;
                        char fname[128];

                        sprintf (fname, "%s%s", netmail, dta.name);

                        fn = share_fopen (fname, read_binary, DENY_WRITE);
                        if (fn)
                        {
                            i = fread ((void *) &fidomsg, 1, sizeof (fidomsg), fn);
                            fclose (fn);

                            if (i == sizeof (fidomsg))
                            {
                                /* not interested in self written mails */
                                if ((fidomsg.attribute & FM_LOCAL_MSG)
                                        || (fidomsg.attribute & FM_MSG_SENT))
                                    continue;

                                /* not interested in mails for 3rd parties */
                                if (fidomsg.attribute & FM_INTRANSIT)
                                    continue;

                                /* not interested mails already read       */
                                if (!(fidomsg.attribute & FM_MSG_RECVED))
                                    count++;
                            }                 /* if      */
                        }                   /* if (fn) */
                        else
                            count++;
                    }                     /* if (atoi) */
                }                       /* while (dfind) */

                if (f)
                    dfind (&dta, NULL, 2);
            }                         /* If any records read from LASTREAD */
        }                           /* If we were able to open LASTREAD  */
    }                             /* If the user specified NetMail     */

    return count;
}


/* AW 980521                                                             */
/* created this function, because with maximum optimisation switches the */
/* Watcom compiler traps on the old code sequence:                       */
/*
 *  if (exact_mdm_type)
 *    match = (m->mdm == newnodedes.ModemType) ? 1 : 0;
 *  else
 *    match = (m->mdm  & newnodedes.ModemType) ? 1 : 0;
 *
 * It seems to be nice anyway to have such function, because this sequence is
 * duplicated all over the sources. */

int
ModemType_matches (byte mdm, byte b)
{
    if (exact_mdm_type)           /* binkley default */
        return (mdm == b) ? 1 : 0;

    return (mdm & b) ? 1 : 0;
}

MDM_TRNS *
find_mdm_trans (byte modembyte)
{
    MDM_TRNS *m = mm_head;

    while (m)
    {
        if (ModemType_matches (m->mdm, modembyte))
            return m;

        m = m->next;
    }

    return NULL;
}

char *
get_modem_flags (char *s)
{
    MDM_TRNS *m = mm_head;
    int flag = 0;

    s[0] = '\0';
    while (m)
    {
        if (ModemType_matches (m->mdm, newnodedes.ModemType) && m->ascii[0])
        {
            if (flag)
                strcat (s, ",");
            else
                flag = 1;
            strcat (s, m->ascii);
        }

        m = m->next;
    }

    return s;
}

/* $Id: mailroot.cpp,v 1.2 2005/01/03 03:27:00 vildanov Exp $ */

About Koders | Resources | Downloads | Support | Black Duck | Terms of Service | DMCA | Privacy Policy | Contact Us