home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / source / pcmail2.lzh / pcmail.6 < prev    next >
Encoding:
Text File  |  1990-01-31  |  62.8 KB  |  2,268 lines

  1.  
  2. #! /bin/sh
  3. # This is a shell archive.  Remove anything before this line, then unpack
  4. # it by saving it into a file and typing "sh file".  To overwrite existing
  5. # files, type "sh file -c".  You can also feed this as standard input via
  6. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  7. # will see the following message at the end:
  8. #        "End of archive 6 (of 11)."
  9. # Contents:  main/connect.c main/email.c main/gmail.c main/gphys.c
  10. #   main/gpres.c main/mbox.c main/nmail.c main/startup.c main/str.c
  11. # Wrapped by wswietse@tuewsa on Mon Jan 22 17:27:18 1990
  12. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  13. if test -f main/connect.c -a "${1}" != "-c" ; then 
  14.   echo shar: Will not over-write existing file \"main/connect.c\"
  15. else
  16. echo shar: Extracting \"main/connect.c\" \(6857 characters\)
  17. sed "s/^X//" >main/connect.c <<'END_OF_main/connect.c'
  18. X/*++
  19. X/* NAME
  20. X/*      connect 3
  21. X/* SUMMARY
  22. X/*      pre- and post protocol host access
  23. X/* PROJECT
  24. X/*      pc-mail
  25. X/* PACKAGE
  26. X/*      cico
  27. X/* SYNOPSIS
  28. X/*      int connect()
  29. X/*
  30. X/*      int disconnect()
  31. X/* DESCRIPTION
  32. X/*      connect() tries to make a connection to the remote host
  33. X/*      and to log on, using the dial-up script and login-name
  34. X/*    entries in the communications parameter file, and the password
  35. X/*    provided as command-line parameter to the cico program.
  36. X/*    A UUCP-like send/expect script facility is used. Thus a login
  37. X/*    sequence might look like:
  38. X/*
  39. X/*    send expect send expect ...
  40. X/*
  41. X/*    The program will send the first "send" string, then expect the
  42. X/*    first "expect" string, and so on.
  43. X/*
  44. X/*    Alternative expect/send sequences can be specified in the usual manner:
  45. X/*
  46. X/*    expect-send-expect-send-expect...
  47. X/*
  48. X/*    If the first expect string fails, the alternative send string is 
  49. X/*    transmitted and the alternative expect is tried, and so on, until
  50. X/*    an expect string succeeds, or until the list of alternatives is
  51. X/*    exhausted.
  52. X/*
  53. X/*    After the dial-up script has completed the program
  54. X/*    proceeds with the following build-in send/expect sequence:
  55. X/*
  56. X/*    ogin: your_login_name\\r ssword: your_password\\r
  57. X/*
  58. X/*      disconnect() tries to break a connection, using the disconnect
  59. X/*      entry in the communications parameter file. Unlike connect()
  60. X/*    this function is not driven by a send-expect script.
  61. X/*
  62. X/*    The following escape sequences are recognized in send or expect
  63. X/*    strings:
  64. X/*
  65. X/* .nf
  66. X/*    \\b    backspace
  67. X/*    \\r    carriage return
  68. X/*    \\n    newline
  69. X/*    \\t    tab
  70. X/*    \\s    space
  71. X/*    \\f    form feed
  72. X/*    \\nnn    octal character value
  73. X/*    \\\\    a real backslash
  74. X/* .fi
  75. X/*
  76. X/*    In addition, the following "send" strings are given special
  77. X/*    treatment:
  78. X/*
  79. X/* .nf
  80. X/*    BREAK    send a null character
  81. X/*    EOT    send Control-D
  82. X/* FUNCTIONS AND MACROS
  83. X/*      xwrite(), xgetc(), trap(), debug(4)(), log(), split()
  84. X/* FILES
  85. X/*      $MAILDIR/s00000        communications parameter file
  86. X/*    $MAILDIR/LOGFILE    system logfile
  87. X/* SEE ALSO
  88. X/*      params(5)       communications parameter file entries
  89. X/* DIAGNOSTICS
  90. X/*      connect() returns a status E_BADSETUP if the systems parameter
  91. X/*    file contains bad data, and E_NOLINE if the login script fails.
  92. X/* AUTHOR(S)
  93. X/*      W.Z. Venema
  94. X/*      Eindhoven University of Technology
  95. X/*      Department of Mathematics and Computer Science
  96. X/*      Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
  97. X/* CREATION DATE
  98. X/*      Fri Mar 27 17:11:12 GMT+1:00 1987
  99. X/* LAST MODIFICATION
  100. X/*    90/01/22 13:01:26
  101. X/* VERSION/RELEASE
  102. X/*    2.1
  103. X/*--*/
  104. X
  105. X#include <stdio.h>
  106. X#include <setjmp.h>
  107. X#include <ctype.h>
  108. X
  109. X#include "defs.h"
  110. X#include "params.h"
  111. X#include "status.h"
  112. X#include "comm.h"
  113. X#include "logs.h"
  114. X#include "sysdep.h"
  115. X
  116. Xhidden char *blnk = " \t";        /* send/expect separators */
  117. X
  118. X/* forward declarations */
  119. X
  120. Xhidden void conn_send();
  121. Xhidden void conn_xpct();
  122. Xhidden char *escape();
  123. X
  124. X/* connect - connect to remote system; simple script processing with retries */
  125. X
  126. Xpublic int connect()
  127. X{
  128. X    int    *savetrap = systrap;        /* save exception handler */
  129. X    jmp_buf mytrap;            /* our exception handler */
  130. X    int     retval;            /* completion code */
  131. X    char   *seq = DIAL_SEQUENCE;
  132. X    register char *cp;
  133. X
  134. X    /* set up exception handler */
  135. X
  136. X    if (retval = setjmp(systrap = mytrap)) {    /* get here if expect fails */
  137. X    systrap = savetrap;            /* it just happened */
  138. X    return (retval);
  139. X    }
  140. X    /* optional dial-up sequence */
  141. X
  142. X    for (cp = split(&seq, blnk); cp; cp = split(&seq, blnk)) {
  143. X    conn_send(escape(cp));
  144. X    if (cp = split(&seq, blnk))
  145. X        conn_xpct(escape(cp));
  146. X    }
  147. X
  148. X    /* mandatory login sequence; hack this for non-UNIX hosts */
  149. X
  150. X    conn_xpct("ogin:");
  151. X    conn_send(strcons("%s\r", LOGIN_NAME));
  152. X    conn_xpct("ssword:");
  153. X    conn_send(strcons("%s\r", password));
  154. X
  155. X    /* restore exception handler */
  156. X
  157. X    systrap = savetrap;                /* get here if expect wins */
  158. X    return (0);                    /* say no problems... */
  159. X}
  160. X
  161. X/* disconnect - disconnect line */
  162. X
  163. Xpublic int disconnect()
  164. X{
  165. X    conn_send(escape(DISC_SEQUENCE));        /* send disconnect sequence */
  166. X    return (0);                    /* always succeeds... */
  167. X}
  168. X
  169. X/* conn_send - send BREAK, EOT or string literal */
  170. X
  171. Xhidden void conn_send(s)
  172. Xregister char *s;
  173. X{
  174. X    static char null = '\0';
  175. X    static char eot = '\04';
  176. X
  177. X    sleep(1);
  178. X
  179. X    if (*s) {
  180. X    debug(4) ("Sending: %S\n", s);
  181. X    if (strcmp(s, "BREAK") == 0) {
  182. X        xwrite(ttfd, &null, 1);
  183. X    } else if (strcmp(s, "EOT") == 0) {
  184. X        xwrite(ttfd, &eot, 1);
  185. X    } else {
  186. X        while (*s) {
  187. X        delay();
  188. X        xwrite(ttfd, s++, 1);
  189. X        }
  190. X    }
  191. X    }
  192. X}
  193. X
  194. X/* conn_xpct - pattern matching without meta characters */
  195. X
  196. Xhidden void conn_xpct(s)
  197. Xchar   *s;
  198. X{
  199. X    int     c;
  200. X    int     i;
  201. X    int     n;
  202. X    char   *xp;
  203. X    char   *sp;
  204. X
  205. X    /*
  206. X     * Keep listening until we time out or until we receive the expected
  207. X     * string (thus, if the other end keeps sending garbage we will never
  208. X     * terminate). Make sure that we do not overrun our buffer. Parity bits
  209. X     * are ignored. If we do not succeed, try alternative sequences if they
  210. X     * are specified.
  211. X     */
  212. X
  213. X    for (xp = split(&s, "-"); xp; xp = split(&s, "-")) {
  214. X
  215. X    debug(4) ("Expecting: %S\nReceiving: ", xp);
  216. X
  217. X    if (((n = strlen(xp)) > MSGBUF))
  218. X        n = MSGBUF;
  219. X    for (i = 0; (c = xgetc()) != EOF; /* void */ ) {
  220. X        msgin[i++] = (c &= 0177);
  221. X        debug(4) ("%C", c);
  222. X        if (i >= n && strncmp(xp, &msgin[i - n], n) == 0) {
  223. X        debug(4) (" ok!\n");
  224. X        return;
  225. X        } else if (i >= MSGBUF) {
  226. X        strncpy(msgin, &msgin[i - (n - 1)], n - 1);
  227. X        i = n - 1;
  228. X        }
  229. X    }
  230. X    debug(4) (" failed!\n");
  231. X
  232. X    /* try alternative sequence, if specified, else fail */
  233. X
  234. X    if (sp = split(&s, "-")) {
  235. X        conn_send(sp);
  236. X    } else {
  237. X        trap(E_NOLINE, "LOGIN FAILED (at \"%S\")", xp);
  238. X    }
  239. X    }
  240. X}
  241. X
  242. X/* escape - interpret backslash sequences */
  243. X
  244. Xhidden char *escape(s)
  245. Xregister char *s;
  246. X{
  247. X    static char buf[BUFSIZ];
  248. X    register char *cp = buf;
  249. X    register char ch;
  250. X    int     c;
  251. X    int     i;
  252. X
  253. X    while (*s && cp < buf + sizeof(buf) - 1) {    /* don't overflow the buffer */
  254. X
  255. X    if (*s != '\\') {            /* ordinary character */
  256. X        *cp++ = *s++;
  257. X    } else if (isdigit(*++s) && *s < '8') {    /* \nnn octal code */
  258. X        sscanf(s, "%3o", &c);
  259. X        *cp++ = c;
  260. X        i = 1;
  261. X        s++;
  262. X        while (i++ < 3 && isdigit(*s) && *s < '8')
  263. X        s++;
  264. X    } else if ((ch = *s++) == 0) {        /* at string terminator */
  265. X        break;
  266. X    } else if (ch == 'b') {            /* \b becomes backspace */
  267. X        *cp++ = '\b';
  268. X    } else if (ch == 'f') {            /* \f becomes formfeed */
  269. X        *cp++ = '\f';
  270. X    } else if (ch == 'n') {            /* \n becomes newline */
  271. X        *cp++ = '\n';
  272. X    } else if (ch == 'r') {            /* \r becomes carriage ret */
  273. X        *cp++ = '\r';
  274. X    } else if (ch == 's') {            /* \s becomes blank */
  275. X        *cp++ = ' ';
  276. X    } else if (ch == 't') {            /* \t becomes tab */
  277. X        *cp++ = '\t';
  278. X    } else {                /* \any becomes any */
  279. X        *cp++ = ch;
  280. X    }
  281. X    }
  282. X    *cp = '\0';                    /* terminate the result */
  283. X    return (buf);
  284. X}
  285. END_OF_main/connect.c
  286. if test 6857 -ne `wc -c <main/connect.c`; then
  287.     echo shar: \"main/connect.c\" unpacked with wrong size!
  288. fi
  289. # end of overwriting check
  290. fi
  291. if test -f main/email.c -a "${1}" != "-c" ; then 
  292.   echo shar: Will not over-write existing file \"main/email.c\"
  293. else
  294. echo shar: Extracting \"main/email.c\" \(6160 characters\)
  295. sed "s/^X//" >main/email.c <<'END_OF_main/email.c'
  296. X/*++
  297. X/* NAME
  298. X/*    email 3
  299. X/* SUMMARY
  300. X/*    manipulate one message in preparation
  301. X/* PROJECT
  302. X/*    pc-mail
  303. X/* PACKAGE
  304. X/*    mail
  305. X/* SYNOPSIS
  306. X/*    #include "mail.h"
  307. X/*
  308. X/*    int work()
  309. X/*
  310. X/*    int work_disp(def_addr)
  311. X/*    char *def_addr;
  312. X/* DESCRIPTION
  313. X/*      The functions in this module are responsible for manipulations 
  314. X/*    on mail messages in preparation.
  315. X/*
  316. X/*    work() should be invoked when the user has selected an existing
  317. X/*    message in preparation. It does some initializations and invokes
  318. X/*    the work_disp() function.
  319. X/*
  320. X/*      work_disp() allows the user to specify the disposition of a
  321. X/*    mail message in preparation. It should be used after the user has
  322. X/*    created a message, or after the user has selected a message in 
  323. X/*    preparation.
  324. X/*
  325. X/*    The message file is displayed on the screen and user the 
  326. X/*    can choose to print, mail, edit or delete etc. the message.
  327. X/*
  328. X/*    The def_addr argument contains a default mail destination: for example,
  329. X/*    an address extracted from a message being replied to. It should be an
  330. X/*    e-mail address or an empty string.
  331. X/*
  332. X/*    The code in this module is a little tricky, to avoid "orphan" work
  333. X/*    files (message file without a metafile).
  334. X/* COMMANDS
  335. X/*    The program specified in the EDITOR environment variable,
  336. X/*    or a system-dependent default.
  337. X/* FILES
  338. X/*    mail.msg, file being edited in the current directory
  339. X/*      $MAILDIR/ennnnn, message file (body)
  340. X/*    $MAILDIR/cnnnnn, meta file (summary)
  341. X/*    $MAILDIR/header, template mail header file
  342. X/*    $MAILDIR/trailer, template signature file
  343. X/* SEE ALSO
  344. X/*      pager(3), pager(5), kbdinp(3), edit(3)
  345. X/* AUTHOR(S)
  346. X/*      W.Z. Venema
  347. X/*      Eindhoven University of Technology
  348. X/*      Department of Mathematics and Computer Science
  349. X/*      Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
  350. X/* CREATION DATE
  351. X/*    Tue May 12 15:35:20 GMT+1:00 1987
  352. X/* LAST MODIFICATION
  353. X/*    90/01/22 13:01:35
  354. X/* VERSION/RELEASE
  355. X/*    2.1
  356. X/*--*/
  357. X
  358. X#include <stdio.h>
  359. X#include <sys/types.h>
  360. X#include <sys/stat.h>
  361. X
  362. X#include <errno.h>
  363. X#include "defs.h"
  364. X#include "path.h"
  365. X#include "pager.h"
  366. X#include "screen.h"
  367. X#include "mail.h"
  368. X#include "status.h"
  369. X
  370. X/* forward declarations */
  371. X
  372. Xhidden void junk_work();
  373. Xhidden int edit_work();
  374. Xhidden int show_work();
  375. Xhidden int hold_work();
  376. Xhidden int send_work();
  377. Xhidden int queue_work();
  378. X
  379. Xpublic char address[MAXLINE];        /* default destination */
  380. Xhidden File *workfile = 0;        /* pager file */
  381. X
  382. X/* work - user selected message in preparation */
  383. X
  384. Xpublic int work()
  385. X{
  386. X    return (work_disp(""));
  387. X}
  388. X
  389. X/* work_disp - ask disposition of a message in preparation */
  390. X
  391. Xpublic int work_disp(def_addr)
  392. Xchar *def_addr;
  393. X{
  394. X    static Screen screen[] = {
  395. X    'C',    "Close",hold_work,"Send message later, return to message-selection menu",
  396. X#ifdef    ATTACH
  397. X    'A',    "Attach",attach", "Attach file to message",
  398. X#endif
  399. X    'D',    "Delete",delete,  delcurr,
  400. X    'E',    "Edit",    edit_work,"Edit this message",
  401. X    'M',    "Mail",    send_work,"Send this message to destination",
  402. X    'P',    "Print",print,      printcurr,
  403. X    PGUP,    PgUp,    pu_pager, pageup,
  404. X    PGDN,    PgDn,    pd_pager, pagedn,
  405. X    UP,    "Up",    up_pager, csrup,
  406. X    DOWN,    "Down",    dn_pager, csrdn,
  407. X    0,    0,    show_work,
  408. X    "(Reading a message in preparation)",
  409. X    };
  410. X    struct stat s;
  411. X
  412. X    strcpy(address, def_addr);            /* set up default address */
  413. X    kbdinp(screen);                /* ask disposition */
  414. X    junk_work();                /* destroy mail pager file */
  415. X    return (S_REDRAW);                /* say screen was changed */
  416. X}
  417. X
  418. X/* show_work - show message in preparation or error message in middle window */
  419. X
  420. Xhidden int show_work()
  421. X{
  422. X    if (workfile) {                /* check pager file exists */
  423. X    set_pager(workfile);            /* select existing display */
  424. X    } else if (rd_pager(workfile = open_pager(), message)) {
  425. X    mesg_pager(workfile, m_msgread);    /* cannot display message */
  426. X    }
  427. X    ds_pager();                    /* (re)draw display */
  428. X    return (0);                    /* screen is up-to-date */
  429. X}
  430. X
  431. X/* junk_work - destroy message in preparation display */
  432. X
  433. Xhidden void junk_work()
  434. X{
  435. X    if (workfile) {                /* no-op if no display */
  436. X    close_pager(workfile);            /* release memory */
  437. X    workfile = 0;                /* say it is gone */
  438. X    }
  439. X}
  440. X
  441. X/* edit_work - edit a message in preparation */
  442. X
  443. Xhidden int edit_work()
  444. X{
  445. X    register int stat;
  446. X
  447. X    if (stat = edit(message, MAILFILE))        /* try to edit the message */
  448. X    errdisp(stat);                /* edit() had a problem */
  449. X    junk_work();                /* force new message display */
  450. X    return (S_REDRAW);                /* say screen has changed */
  451. X}
  452. X
  453. Xhidden int label_work();
  454. X
  455. X/* hold_work - stop editing but do not yet mail a message in preparation */
  456. X
  457. Xhidden int hold_work()
  458. X{
  459. X    static Screen screen[] = {
  460. X    STRING,    0, label_work, int_error,
  461. X    0,    0, 0,
  462. X    getsummary,
  463. X    };
  464. X    struct stat s;
  465. X
  466. X    /*
  467. X     * The user does not yet want to send the message off. The purpose of the
  468. X     * following code is to ask for a one-line summary, but only if such a
  469. X     * comment does not yet exist. The summary is used to identify the
  470. X     * message in preparation in the message- selection display.
  471. X     */
  472. X
  473. X    if (stat(message, &s) || !stat(comment, &s)) {
  474. X    return (S_BREAK);            /* we are done here */
  475. X    } else {
  476. X    return (kbdinp(screen) | S_REDRAW);    /* ask for a summary */
  477. X    }
  478. X}
  479. X
  480. X/* label_work - save summary line to meta file */
  481. X
  482. Xhidden  label_work(string)
  483. Xchar   *string;
  484. X{
  485. X    register int stat;
  486. X
  487. X    if (stat = metafile(comment, string, (char *) 0)) {
  488. X    errdisp(stat);                /* oops, notify the user */
  489. X    return (S_REDRAW);            /* say screen has changed */
  490. X    } else {
  491. X    chmod(comment, 0444);            /* make comments read-only */
  492. X    junk_desk();                /* say mail box has changed */
  493. X    return (S_BREAK);            /* say no more work */
  494. X    }
  495. X}
  496. X
  497. X/* send_work - user wants to send message in preparation, ask for destination */
  498. X
  499. Xhidden int send_work()
  500. X{
  501. X    static Screen screen[] = {
  502. X    EDIT,    0,    queue_work,    address,
  503. X    0,    0,    when,
  504. X    "Press ESC to cancel. Send message to:",
  505. X    };
  506. X
  507. X    return (kbdinp(screen) | S_REDRAW);
  508. X}
  509. X
  510. X/* queue_work - spool mail, delete message in preparation and meta file */
  511. X
  512. Xhidden int queue_work(to)
  513. Xchar   *to;
  514. X{
  515. X    register int stat;
  516. X
  517. X    if (stat = submit(message, to)) {
  518. X    errdisp(stat);                /* cannot queue message */
  519. X    return (S_REDRAW);            /* say screen has changed */
  520. X    } else {
  521. X    return (unspool() | S_BREAK);        /* remove work and meta file */
  522. X    }
  523. X}
  524. END_OF_main/email.c
  525. if test 6160 -ne `wc -c <main/email.c`; then
  526.     echo shar: \"main/email.c\" unpacked with wrong size!
  527. fi
  528. # end of overwriting check
  529. fi
  530. if test -f main/gmail.c -a "${1}" != "-c" ; then 
  531.   echo shar: Will not over-write existing file \"main/gmail.c\"
  532. else
  533. echo shar: Extracting \"main/gmail.c\" \(6153 characters\)
  534. sed "s/^X//" >main/gmail.c <<'END_OF_main/gmail.c'
  535. X/*++
  536. X/* NAME
  537. X/*      gmail 1
  538. X/* SUMMARY
  539. X/*      deliver unsent mail via gnuucp
  540. X/* PROJECT
  541. X/*      pc-mail
  542. X/* PACKAGE
  543. X/*      gnu
  544. X/* SYNOPSIS
  545. X/*      gmail [-d debuglevel]
  546. X/* DESCRIPTION
  547. X/*    This program replaces the sending function of the pc-mail "cico"
  548. X/*    program, on systems that use GNUUCP for message transport.
  549. X/*
  550. X/*      gmail searches the pc-mail message data base for unsent mail
  551. X/*    (with the "Out" status) and queues it for transmission via GNUUCP.
  552. X/*    When a message has been queued it is renamed to reflect
  553. X/*    the "Sent" status.
  554. X/*
  555. X/*    This program is intended to be called via the MAILCMD environment
  556. X/*    variable, so that it is invoked upon exit from the mail user
  557. X/*    interface program.
  558. X/*
  559. X/*      In order to avoid corruption of the message data base, control-c
  560. X/*    interrupts are disabled while this program executes.
  561. X/* ENVIRONMENT
  562. X/*    MAILDIR, path to pc-mail message data base
  563. X/* COMMANDS
  564. X/*    rmail, the gnuucp mailer
  565. X/* FILES
  566. X/*      In the spool directory:
  567. X/*    d<seqno>    unsent mail, message body
  568. X/*    x<seqno>    unsent mail, destination and subject
  569. X/*    q<seqno>    sent mail, message body
  570. X/*    r<seqno>    sent mail, destination and subject
  571. X/* SEE ALSO
  572. X/*      path(5)         spool directory, file name formats
  573. X/* DIAGNOSTICS
  574. X/*      Problems are reported on the standard error output, and cause the
  575. X/*    program to terminate with a nonzero exit status.
  576. X/* BUGS
  577. X/*    It is left up to GNUUCP to determine what to do with undeliverable mail.
  578. X/* AUTHOR(S)
  579. X/*      W.Z. Venema
  580. X/*      Eindhoven University of Technology
  581. X/*      Department of Mathematics and Computer Science
  582. X/*      Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
  583. X/* CREATION DATE
  584. X/*    Wed Jan  3 22:16:28 MET 1990
  585. X/* LAST MODIFICATION
  586. X/*    90/01/22 13:01:40
  587. X/* VERSION/RELEASE
  588. X/*    2.1
  589. X/*--*/
  590. X
  591. X#include <stdio.h>
  592. X#include <signal.h>
  593. X#include <time.h>
  594. X#include <varargs.h>
  595. X
  596. X#include "defs.h"
  597. X#include "ndir.h"
  598. X#include "path.h"
  599. X
  600. X#ifndef    RMAIL
  601. X#define    RMAIL    "rmail"
  602. X#endif
  603. X
  604. X/* Forward declarations */
  605. X
  606. Xhidden char *get_dest();
  607. Xhidden int uuqueue();
  608. Xhidden void error();
  609. Xhidden void frename();
  610. Xhidden void parse_args();
  611. Xhidden void scanmail();
  612. Xhidden void usage();
  613. X
  614. X#define debug    if (dflag) (void) printf
  615. X
  616. Xhidden int dflag = 0;            /* debugging option */
  617. X
  618. Xpublic char *progname = "gmail";    /* for diagnostics */
  619. X
  620. X/* .. */
  621. X
  622. Xmain(argc, argv)
  623. Xint     argc;
  624. Xchar  **argv;
  625. X{
  626. X    (void) signal(SIGINT, SIG_IGN);        /* disable ctrl-c */
  627. X    if (pathinit())                /* get path info */
  628. X    error("no mail directory or MAILDIR environment variable not set");
  629. X    parse_args(argc, argv);            /* parse command args */
  630. X    scanmail();                    /* search for unsent mail */
  631. X    exit(0);
  632. X    /* NOTREACHED */
  633. X}
  634. X
  635. X/* parse_args - process command-line arguments */
  636. X
  637. Xhidden void parse_args(argc, argv)
  638. Xint     argc;
  639. Xchar  **argv;
  640. X{
  641. X    while (--argc && *++argv && **argv == '-') {/* process options */
  642. X    switch (*++*argv) {
  643. X    case 'd':                /* turn debugging on */
  644. X        dflag++;
  645. X        break;
  646. X    default:                /* unknown option */
  647. X        usage("invalid option: -%c", **argv);
  648. X        break;
  649. X    }
  650. X    }
  651. X
  652. X    /* check for extraneous arguments */
  653. X
  654. X    if (argc > 0)
  655. X    usage("unexpected argument: %s", *argv);
  656. X}
  657. X
  658. X/* scan for unsent mail */
  659. X
  660. Xhidden void scanmail()
  661. X{
  662. X    unsigned msgno;            /* message sequence number */
  663. X    register DIR *dp;
  664. X    struct direct *de;
  665. X    char   *dest;
  666. X
  667. X    debug("directory: \"%s\"\n", maildir);
  668. X
  669. X    /*
  670. X     * Scan the spool directory for unsent mail. After the message has been
  671. X     * piped through rmail, rename it to reflect the "Sent" status. Do not give
  672. X     * up if a file cannot be opened; just proceed with the next message.
  673. X     */
  674. X
  675. X    if ((dp = opendir(maildir)) == 0)
  676. X    error("cannot read the mail directory: %s", maildir);
  677. X
  678. X    while (de = readdir(dp)) {
  679. X    debug("file: \"%s\"\n", de->d_name);
  680. X    if (de->d_name[0] == OUT_META && (msgno = seqno(de->d_name))
  681. X    && (dest = get_dest(msgno)) && uuqueue(msgno, dest)) {
  682. X        frename(out_mesg(msgno), sent_mesg(msgno));
  683. X        frename(out_meta(msgno), sent_meta(msgno));
  684. X    }
  685. X    }
  686. X    closedir(dp);
  687. X}
  688. X
  689. X/* uuqueue - queue one message */
  690. X
  691. Xhidden int uuqueue(msgno, dest)
  692. Xunsigned msgno;
  693. Xchar   *dest;
  694. X{
  695. X    char    cmd[BUFSIZ];
  696. X    char   *path;
  697. X    char   *rcpt;
  698. X    static char sep[] = " \t\r\n";
  699. X
  700. X    if (access(path = out_mesg(msgno), 04)) {
  701. X    debug("%s: cannot read message file: %s\n", path, sys_errlist[errno]);
  702. X    return (0);
  703. X    } else {
  704. X
  705. X    /*
  706. X     * The GNUUCP rmail program has to invoked for each recipient. rmail
  707. X     * will have to deal with undeliverable mail anyway, so we ignore
  708. X     * that class of errors.
  709. X     */
  710. X
  711. X    for (rcpt = strtok(dest, sep); rcpt; rcpt = strtok((char *) 0, sep)) {
  712. X        (void) sprintf(cmd, "%s %s <%s", RMAIL, rcpt, path);
  713. X        debug("command: %s\n", cmd);
  714. X        if (system(cmd) == 127)
  715. X        error("could not invoke the shell");
  716. X    }
  717. X    return (1);
  718. X    }
  719. X}
  720. X
  721. X/* get_dest - extract recipients */
  722. X
  723. Xhidden char *get_dest(msgno)
  724. Xunsigned msgno;
  725. X{
  726. X    static char buf[MAXLINE];
  727. X    FILE   *fp;
  728. X    char   *ret;
  729. X    char   *path;
  730. X
  731. X    if ((fp = fopen(path = out_meta(msgno), "r")) == 0) {
  732. X    debug("%s: cannot open: %s\n", path, sys_errlist[errno]);
  733. X    return (0);
  734. X    } else {
  735. X    if ((ret = fgets(buf, sizeof(buf), fp)) == 0)
  736. X        debug("%s: no recipients found\n", path);
  737. X    (void) fclose(fp);
  738. X    return (ret);
  739. X    }
  740. X}
  741. X
  742. X/* frename - forcibly change the name of a file */
  743. X
  744. Xhidden void frename(from, to)
  745. Xchar   *from;
  746. Xchar   *to;
  747. X{
  748. X    debug("rename: %s -> %s\n", from, to);
  749. X
  750. X    if (chmod(to, 0600) == 0)
  751. X    (void) unlink(to);
  752. X    if (rename(from, to))
  753. X    error("cannot rename %s to %s: %s", from, to, sys_errlist[errno]);
  754. X}
  755. X
  756. X/* error - complain */
  757. X
  758. X/* VARARGS */
  759. X
  760. Xhidden void error(va_alist) 
  761. Xva_dcl
  762. X{
  763. X    va_list ap;
  764. X    char   *fmt;
  765. X
  766. X    (void) fprintf(stderr, "%s: ", progname);
  767. X    va_start(ap);
  768. X    fmt = va_arg(ap, char *);
  769. X    (void) vfprintf(stderr, fmt, ap);
  770. X    va_end(ap);
  771. X    (void) putc('\n', stderr);
  772. X    exit(2);
  773. X}
  774. X
  775. X/* usage - explain what is wrong */
  776. X
  777. X/* VARARGS */
  778. X
  779. Xhidden void usage(va_alist) 
  780. Xva_dcl
  781. X{
  782. X    va_list ap;
  783. X    char   *fmt;
  784. X
  785. X    (void) fprintf(stderr, "%s: ", progname);
  786. X    va_start(ap);
  787. X    fmt = va_arg(ap, char *);
  788. X    (void) vfprintf(stderr, fmt, ap);
  789. X    va_end(ap);
  790. X    (void) fprintf(stderr, "\nusage: gmail [-d]\n");
  791. X    exit(2);
  792. X}
  793. END_OF_main/gmail.c
  794. if test 6153 -ne `wc -c <main/gmail.c`; then
  795.     echo shar: \"main/gmail.c\" unpacked with wrong size!
  796. fi
  797. # end of overwriting check
  798. fi
  799. if test -f main/gphys.c -a "${1}" != "-c" ; then 
  800.   echo shar: Will not over-write existing file \"main/gphys.c\"
  801. else
  802. echo shar: Extracting \"main/gphys.c\" \(6844 characters\)
  803. sed "s/^X//" >main/gphys.c <<'END_OF_main/gphys.c'
  804. X/*++
  805. X/* NAME
  806. X/*    gphys 3
  807. X/* SUMMARY
  808. X/*    g protocol packet input/output
  809. X/* PROJECT
  810. X/*    pc-mail
  811. X/* PACKAGE
  812. X/*    cico
  813. X/* SYNOPSIS
  814. X/*    #include "gp.h"
  815. X/*
  816. X/*    void gsctrl(fd,c)
  817. X/*    int fd,c;
  818. X/*
  819. X/*    void gsdata(fd,pk,c)
  820. X/*    int fd,c;
  821. X/*    Packet *pk;
  822. X/*
  823. X/*    int grpack(fd,pk)
  824. X/*    int fd;
  825. X/*    Packet *pk;
  826. X/* DESCRIPTION
  827. X/*    The functions in this module send and receive packets. Interfacing
  828. X/*    is based on Packet structures. Messages are interpreted elsewhere.
  829. X/*
  830. X/*    gsctrl() sends a control packet to the remote receiver (no data
  831. X/*    segment).
  832. X/*
  833. X/*    gsdata() sends a data packet to the remote receiver. 
  834. X/*    The Packet structure is completed with a 16-bit checksum.
  835. X/*    This function expects read/write sequence information in 
  836. X/*    the c parameter.
  837. X/*
  838. X/*    grpack() receives a packet from the remote transmitter and checks
  839. X/*    its integrity. It fills in the k, c, len and check fields of the 
  840. X/*    Packet structure and returns the type of message (DATA, SHORT, 
  841. X/*    CLOSE, RJ, RR, etcetera).
  842. X/* DIAGNOSTICS
  843. X/*    grpack() returns FAIL if a corrupted packet was received, and
  844. X/*    TIME if no packet was received within the time-out interval.
  845. X/* BUGS
  846. X/*    No data re-reading in case of transmission errors.
  847. X/*    Some parts of the code rely on 8-bit bytes, 16-bit short integers.
  848. X/* AUTHOR(S)
  849. X/*    W.Z. Venema
  850. X/*    Eindhoven University of Technology
  851. X/*    Department of Mathematics and Computer Science
  852. X/*    Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
  853. X/* CREATION DATE
  854. X/*    Sun Apr 19 11:39:27 GMT+1:00 1987
  855. X/* LAST MODIFICATION
  856. X/*    90/01/22 13:01:42
  857. X/* VERSION/RELEASE
  858. X/*    2.1
  859. X/*--*/
  860. X
  861. X#include <signal.h>
  862. X#include <setjmp.h>
  863. X#include "gp.h"
  864. X
  865. X/* local and forward declarations */
  866. X
  867. Xstatic jmp_buf timebuf;
  868. Xstatic int chksum(),readhead(),readdata(),clkint();
  869. X
  870. X#define    READ(fd,cp,n)    { if (read(fd,cp,n) != n) clkint(); }
  871. X
  872. X/*
  873. X* "A six byte framing envelope is constructed using the control"
  874. X* "byte C of a packet and five other bytes as depicted below."
  875. X*        <DLE><k><c0><c1><C><x>
  876. X* "The <DLE> symbol denotes the ASCII ctrl/P character. If the"
  877. X* "envelope is to be followed by a data segment, <k> has the"
  878. X* "value log2(size)-4; i.e. 1 <= k < 8. If k is 9, then the"
  879. X* "envelope represents a control packet. The <c0> and <c1>"
  880. X* "bytes are the low-order and high-order bytes respectively of"
  881. X* "0xAAA minus a 16-bit checksum. For control packets, this"
  882. X* "16-bit checksum is the same as the control byte C. For data"
  883. X* "packets, the checksum is calculated by the program below."
  884. X* "The <x> byte is the exclusive-or of <k><c0><c1><C>. Error"
  885. X* "control is accomplished by checking a received framing"
  886. X* "envelope for compliance with the definition, and comparing a"
  887. X* "checksum function of the data segment with <c0><c1>."
  888. X*/
  889. X
  890. X/* gsctrl - send control packet (no data segment) */
  891. X
  892. Xvoid gsctrl(fd,c)
  893. Xint fd,c;
  894. X{
  895. X    char header[6];
  896. X    register char chkhdr;
  897. X    register char *cp = header;
  898. X    int cksm = MAGIC-c;                /* do checksum */
  899. X
  900. X    *cp++ = CTRL('P');                /* start of header */
  901. X    chkhdr  = *cp++ = KCTRL;            /* k byte (control) */
  902. X    chkhdr ^= *cp++ = cksm;            /* c0 byte (checksum lsb) */
  903. X    chkhdr ^= *cp++ = cksm>>8;            /* c1 byte (checksum msb) */
  904. X    chkhdr ^= *cp++ = c;            /* message|sequence info */
  905. X    *cp = chkhdr;                /* header checksum */
  906. X
  907. X    write(fd,header,sizeof(header));        /* send header */
  908. X    DEBUG(9,"xmt: %o\n",c&0377);        /* show header */
  909. X}
  910. X
  911. X/* gsdata - send data packet */
  912. X
  913. Xvoid gsdata(fd,pk,c)
  914. Xint fd,c;
  915. Xregister Packet *pk;
  916. X{
  917. X    char header[6];
  918. X    register char chkhdr;
  919. X    register char *cp = header;
  920. X    int cval = pk->c|(c&077);            /* fold in sequence info */
  921. X
  922. X    pk->chk = MAGIC-(chksum(pk->data,pk->len)^(0377&cval));
  923. X
  924. X    *cp++ = CTRL('P');                /* start of header */
  925. X    chkhdr  = *cp++ = pk->k;            /* k byte (message length) */
  926. X    chkhdr ^= *cp++ = pk->chk;            /* c0 byte (checksum lsb) */
  927. X    chkhdr ^= *cp++ = pk->chk>>8;        /* c1 byte (checksum msb) */
  928. X    chkhdr ^= *cp++ = cval;            /* data|sequence info */
  929. X    *cp = chkhdr;                /* header checksum */
  930. X
  931. X    write(fd,header,sizeof(header));        /* send header */
  932. X    DEBUG(9,"xmt: %o\n",cval&0377);        /* show header */
  933. X
  934. X    write(fd,pk->data,pk->len);            /* send data segment */
  935. X    DEBUG(9,"xmt: data %d bytes\n",pk->segl);    /* show data */
  936. X}
  937. X
  938. X/* grpack - receive one data or control packet; return packet type info */
  939. X
  940. Xint grpack(fd,pk)
  941. Xint fd;
  942. Xregister Packet *pk;
  943. X{
  944. X    if (setjmp(timebuf))            /* in case we time out */
  945. X    return(TIME);                /* it just happened */
  946. X    signal(SIGALRM,clkint);            /* alarm clock response */
  947. X    alarm(ALARM);                /* set alarm clock */
  948. X
  949. X    if (readhead(fd,pk)) {            /* read packet header */
  950. X    DEBUG(7,"rcv: bad header\n","");
  951. X    alarm(0);                /* turn timer off */
  952. X    return(FAIL);                /* header checksum error */
  953. X    } else if (pk->k == KCTRL) {
  954. X    alarm(0);                /* turn timer off */
  955. X    return(MESG(pk->c));            /* CLOSE | RJ | RR etcetera */
  956. X    } else if (readdata(fd,pk)) {
  957. X    DEBUG(7,"rcv: bad data\n","");
  958. X    alarm(0);                /* turn timer off */
  959. X    return(FAIL);                /* data checksum error */
  960. X    } else {
  961. X    alarm(0);                /* turn timer off */
  962. X    return(TYPE(pk->c));            /* DATA | SHORT */
  963. X    }
  964. X}
  965. X
  966. X/* readhead - read header and check header checksum */
  967. X
  968. Xstatic int readhead(fd,pk)
  969. Xint fd;
  970. Xregister Packet *pk;
  971. X{
  972. X    char header[5];
  973. X    int ok;
  974. X    register char chkhdr;
  975. X    register char *cp = header;
  976. X
  977. X    do {                    /* start reading */
  978. X    READ(fd,cp,1);                /* skip all garbage */
  979. X    } while (*cp != CTRL('P'));            /* up to packet header */
  980. X
  981. X    READ(fd,header,sizeof(header));        /* read packet header */
  982. X
  983. X    chkhdr  = pk->k = *cp++;            /* data length or control */
  984. X    chkhdr ^= pk->chk = *cp++&0377;        /* data checksum lsb */
  985. X    chkhdr ^= *cp;
  986. X    pk->chk |= (*cp++&0377)<<8;    /* data checksum msb */
  987. X    chkhdr ^= pk->c = *cp++;            /* control packet or data */
  988. X    if (ok = (chkhdr == *cp))
  989. X    DEBUG(9,"rcv: %o\n",pk->c&0377);
  990. X    return(!ok);                /* check the checksum */
  991. X}
  992. X
  993. X/* readdata - read data segment and check data checksum */
  994. X
  995. Xstatic int readdata(fd,pk)
  996. Xint fd;
  997. Xregister Packet *pk;
  998. X{
  999. X    if (seglen[pk->k] > pk->len) {
  1000. X    DEBUG(7,"rcv: data %d bytes too big\n",seglen[pk->k]);
  1001. X    return(1);
  1002. X    } else {
  1003. X    register int i;
  1004. X    DEBUG(9,"rcv: data %d bytes\n",pk->len = seglen[pk->k]);
  1005. X    for (i = 0; i < pk->len; i++) {
  1006. X        READ(fd,&pk->data[i],1);
  1007. X    }
  1008. X    return(pk->chk+(chksum(pk->data,pk->len)^(pk->c&0377)) != MAGIC);
  1009. X    }
  1010. X}
  1011. X
  1012. X/* clkint - tiny time-out routine */
  1013. X
  1014. Xstatic int clkint()
  1015. X{
  1016. X    DEBUG(9,"rcv: timed out\n","");
  1017. X    longjmp(timebuf,1);
  1018. X    /* NOTREACHED */
  1019. X}
  1020. X
  1021. X/* chksum - unix packet driver checksum algorithm */
  1022. X
  1023. Xstatic int chksum(s,n)
  1024. Xregister char *s;
  1025. Xregister n;
  1026. X{
  1027. X    register short sum;
  1028. X    register unsigned short t;
  1029. X    register short x;
  1030. X
  1031. X    sum = -1;
  1032. X    x = 0;
  1033. X    do {
  1034. X    if (sum < 0) {
  1035. X        sum <<= 1;
  1036. X        sum++;
  1037. X    } else
  1038. X        sum <<= 1;
  1039. X    t = sum;
  1040. X    sum += (unsigned)*s++ & 0377;
  1041. X    x += sum ^ n;
  1042. X    if ((unsigned short)sum <= t) {
  1043. X        sum ^= x;
  1044. X    }
  1045. X    } while (--n > 0);
  1046. X
  1047. X    return(sum);
  1048. X}
  1049. END_OF_main/gphys.c
  1050. if test 6844 -ne `wc -c <main/gphys.c`; then
  1051.     echo shar: \"main/gphys.c\" unpacked with wrong size!
  1052. fi
  1053. # end of overwriting check
  1054. fi
  1055. if test -f main/gpres.c -a "${1}" != "-c" ; then 
  1056.   echo shar: Will not over-write existing file \"main/gpres.c\"
  1057. else
  1058. echo shar: Extracting \"main/gpres.c\" \(6380 characters\)
  1059. sed "s/^X//" >main/gpres.c <<'END_OF_main/gpres.c'
  1060. X/*++
  1061. X/* NAME
  1062. X/*    gpres.c 3
  1063. X/* SUMMARY
  1064. X/*    g-protocol general interface
  1065. X/* PROJECT
  1066. X/*    pc-mail
  1067. X/* PACKAGE
  1068. X/*    cico
  1069. X/* SYNOPSIS
  1070. X/*    int gopen(fd);
  1071. X/*    int fd;
  1072. X/*
  1073. X/*    int gwrite(fd,buf,len)
  1074. X/*    int fd,len;
  1075. X/*    char *buf;
  1076. X/*
  1077. X/*    int gread(fd,buf,len)
  1078. X/*    int fd,len;
  1079. X/*    char *buf;
  1080. X/*
  1081. X/*    int gclose(fd)
  1082. X/*    int fd;
  1083. X/* DESCRIPTION
  1084. X/*    The functions in this module present an interface that closely
  1085. X/*    resembles the unix kernel i/o interface.
  1086. X/*
  1087. X/*    gopen() handles the initial message exchange. fd should be
  1088. X/*    connected to a tty line. gopen() normally returns a zero value.
  1089. X/*
  1090. X/*    gwrite() returns the number of bytes `written' to the remote system.
  1091. X/*    It should be considered an error if this is not equal to the number
  1092. X/*    of bytes requested.
  1093. X/*    A zero-length write should be used to indicate EOF during file transfer.
  1094. X/*
  1095. X/*    gread() returns the requested number of bytes or the number of
  1096. X/*    bytes sent by the remote system, whichever is smaller.
  1097. X/*    A zero-length read indicates EOF during file transfer.
  1098. X/*
  1099. X/*    gclose() shuts the protocol down, but does not otherwise change
  1100. X/*    communications line parameters. It normally returns a zero value.
  1101. X/* FUNCTIONS AND MACROS
  1102. X/*    galloc(), gfree(), gsproto(), grproto()
  1103. X/* DIAGNOSTICS
  1104. X/*    All functions return -1 in case of unrecoverable problems.
  1105. X/* BUGS
  1106. X/*    All g protocol routines assume that the XON/XOFF flow control
  1107. X/*    has been turned off.
  1108. X/*    Some parts of the code rely on 8-bit bytes, 16-bit short integers.
  1109. X/* AUTHOR(S)
  1110. X/*    W.Z. Venema
  1111. X/*    Eindhoven University of Technology
  1112. X/*    Department of Mathematics and Computer Science
  1113. X/*    Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
  1114. X/* CREATION DATE
  1115. X/*    Sun Apr 19 12:41:37 GMT+1:00 1987
  1116. X/* LAST MODIFICATION
  1117. X/*    90/01/22 13:01:43
  1118. X/* VERSION/RELEASE
  1119. X/*    2.1
  1120. X/*--*/
  1121. X
  1122. X#include <setjmp.h>
  1123. X#include "gp.h"
  1124. X
  1125. X/* local and forward declarations */
  1126. X
  1127. Xstatic jmp_buf failbuf;
  1128. Xstatic void gpeek(),gpoke(),memcpy();
  1129. X
  1130. X/* gfail - exception handling */
  1131. X
  1132. Xvoid gfail()
  1133. X{
  1134. X    longjmp(failbuf,1);
  1135. X}
  1136. X
  1137. X/* gopen - not quite an analogon of unix open(2) */
  1138. X
  1139. Xint gopen(fd)
  1140. Xint fd;
  1141. X{
  1142. X    return(ginit(fd));        /* do packet stuff elsewhere */
  1143. X}
  1144. X
  1145. X/* gwrite - g-protocol analogon of unix write(2) */
  1146. X
  1147. Xgwrite(fd,data,len)
  1148. Xint fd,len;
  1149. Xchar *data;
  1150. X{
  1151. X    /* set up exception handling */
  1152. X
  1153. X    if (setjmp(failbuf))            /* in case gsproto fails */
  1154. X    return(FAIL);                /* it just did */
  1155. X
  1156. X    /* handle special case of zero-length writes separately */
  1157. X
  1158. X    if (len <= 0) {                /* end-of-file message */
  1159. X    register Packet *pk = galloc();        /* allocate output packet */
  1160. X    gpoke(pk,data,len);            /* make null-data packet */
  1161. X    gsproto(fd,pk);                /* send to other side */
  1162. X    } else {                    /* true data message */
  1163. X    register int shot;            /* quantum size */
  1164. X    register int rest;            /* amount left to do */
  1165. X    for (rest = len; rest > 0; rest -= shot,data += shot) {
  1166. X        register Packet *pk = galloc();    /* allocate output packet */
  1167. X        gpoke(pk,data,shot = MIN(pk->len,rest));/* fill the packet */
  1168. X        gsproto(fd,pk);
  1169. X    }
  1170. X    }
  1171. X    return(len);                /* no problems detected */
  1172. X}
  1173. X
  1174. X/* gread - g-protocol analogon of unix read(2) */
  1175. X
  1176. Xgread(fd,data,len)
  1177. Xint fd,len;
  1178. Xchar *data;
  1179. X{
  1180. X    static Packet *pk;                /* our byte stock */
  1181. X    register int igot;                /* our return value */
  1182. X
  1183. X    /* set up exception handling */
  1184. X
  1185. X    if (setjmp(failbuf))            /* in case grproto fails */
  1186. X    return(FAIL);                /* it just did */
  1187. X
  1188. X    /* if no bytes in stock, get some fresh ones and see how much we got */
  1189. X
  1190. X    if (pk == 0 || pk->segl <= 0)        /* we are out of data */
  1191. X    gpeek(pk = grproto(fd));        /* get fresh packet */
  1192. X
  1193. X    /* return as many bytes as asked, or as in stock, whichever is less */
  1194. X
  1195. X    if ((igot = MIN(len,pk->segl)) > 0) {
  1196. X    memcpy(data,pk->segp,igot);        /* copy to caller's buffer */
  1197. X    pk->segp += igot;            /* update stock pointer */
  1198. X    pk->segl -= igot;            /* update stock count */
  1199. X    }
  1200. X    if (pk->segl <= 0)                /* if we exhausted the stock */
  1201. X    gfree(pk);                /* release packet */
  1202. X    return(igot);                /* no problems detected */
  1203. X}
  1204. X
  1205. X/* gclose - turn g protocol off */
  1206. X
  1207. Xgclose(fd)
  1208. Xint fd;
  1209. X{
  1210. X    return(gfinit(fd));                /* not here! */
  1211. X}
  1212. X
  1213. X/* 
  1214. X* "Each transmitter is constrained to observe the maximum data segment"
  1215. X* "size established during initial synchronization by the receiver that"
  1216. X* "it sends to. (...) `short' packets have zero or more data bytes but less"
  1217. X* "than the maximum. The first one or two bytes of the data segment of"
  1218. X* "a short packet are `count' bytes that indicate the difference between"
  1219. X* "the maximum size and the number of bytes in the short segment. If the"
  1220. X* "difference is less than 127, one count byte is used. If the difference"
  1221. X* "exceeds 127, then the low-order seven bits of the difference are put"
  1222. X* "in the first data byte and the remaining high-order bit is set as an"
  1223. X* "indication that the remaining bits of the difference are in the second"
  1224. X* "byte.
  1225. X*/
  1226. X
  1227. X/* gpoke - prepare packet for transmission */
  1228. X
  1229. Xstatic void gpoke(pk,data,len)
  1230. Xregister Packet *pk;
  1231. Xint len;
  1232. Xchar *data;
  1233. X{
  1234. X    register int diff = pk->len-len;        /* packet/data size mismatch */
  1235. X
  1236. X    pk->segp = pk->data;            /* set up write pointer */
  1237. X    pk->segl = len;                /* actual segment length */
  1238. X    if (diff < 0 || len < 0) {
  1239. X    DEBUG(7,"gpoke: trouble\n","");        /* something very wrong */
  1240. X    gfail();
  1241. X    /* NOTREACHED */
  1242. X    } else if (diff == 0) {
  1243. X    pk->c = DATA;                /* long data segment */
  1244. X    } else if (diff <= 127) {
  1245. X    pk->c = SHORT;                /* short data segment */
  1246. X    *pk->segp++ = diff;            /* one difference byte */
  1247. X    } else if (diff > 127) {
  1248. X    pk->c = SHORT;                /* tiny data segment */
  1249. X    *pk->segp++ = diff|0200;        /* two difference bytes */
  1250. X    *pk->segp++ = diff>>7;
  1251. X    }
  1252. X    memcpy(pk->segp,data,pk->segl);        /* copy data into packet */
  1253. X}
  1254. X
  1255. X/* gpeek - prepare newly packet for reading */
  1256. X
  1257. Xstatic void gpeek(pk)
  1258. Xregister Packet *pk;
  1259. X{
  1260. X    register int diff;
  1261. X
  1262. X    pk->segp = pk->data;            /* set up read pointer */
  1263. X    if (TYPE(pk->c) == DATA) {
  1264. X    diff = 0;                /* long data segment */
  1265. X    } else if (TYPE(pk->c) != SHORT) {
  1266. X    DEBUG(7,"gread: trouble\n","");        /* something funny */
  1267. X    gfail();
  1268. X    /* NOTREACHED */
  1269. X    } else if ((diff = *pk->segp++&0377)&0200) {/* short data segment */
  1270. X    diff = (diff&0177)|((*pk->segp++&0377)<<7);
  1271. X    }
  1272. X    pk->segl = pk->len-diff;            /* actual segment size */
  1273. X    DEBUG(9,"rcv: data %d bytes\n",pk->segl);
  1274. X}
  1275. X
  1276. X/* memcpy - not-so-efficient implementation */
  1277. X
  1278. Xstatic void memcpy(dst,src,len)
  1279. Xregister char *dst,*src;
  1280. Xregister int len;
  1281. X{
  1282. X    while (len-- > 0)
  1283. X    *dst++ = *src++;
  1284. X}
  1285. END_OF_main/gpres.c
  1286. if test 6380 -ne `wc -c <main/gpres.c`; then
  1287.     echo shar: \"main/gpres.c\" unpacked with wrong size!
  1288. fi
  1289. # end of overwriting check
  1290. fi
  1291. if test -f main/mbox.c -a "${1}" != "-c" ; then 
  1292.   echo shar: Will not over-write existing file \"main/mbox.c\"
  1293. else
  1294. echo shar: Extracting \"main/mbox.c\" \(6112 characters\)
  1295. sed "s/^X//" >main/mbox.c <<'END_OF_main/mbox.c'
  1296. X/*++
  1297. X/* NAME
  1298. X/*      mbox 3
  1299. X/* SUMMARY
  1300. X/*      display and manipulate one non-work message
  1301. X/* PROJECT
  1302. X/*      pc-mail
  1303. X/* PACKAGE
  1304. X/*      mail
  1305. X/* SYNOPSIS
  1306. X/*      int mbox(meta,msgid)
  1307. X/*    int meta;
  1308. X/*    unsigned msgid;
  1309. X/* DESCRIPTION
  1310. X/*      mbox() is invoked when the user has selected a non-work mail message.
  1311. X/*    It instructs the pager to display the selected mail message. 
  1312. X/*      The user has the usual options for manipulating the message
  1313. X/*    being displayed.
  1314. X/*
  1315. X/*    The meta parameter indicates the message type,
  1316. X/*    and msgid is the numerical message id. If the message file is being
  1317. X/*    read for the first time (meta == NEW_META), it will be marked as read
  1318. X/*    by renaming the meta file.
  1319. X/*
  1320. X/*    Message header lines can be suppressed selectively (see setup).
  1321. X/* FILES
  1322. X/*      $MAILDIR/?nnnnn, message and meta files
  1323. X/* SEE ALSO
  1324. X/*      pager(3), pager(5), kbdinp(3)
  1325. X/* DIAGNOSTICS
  1326. X/*      If a selected mail message could not be found an error message
  1327. X/*      is displayed instead.
  1328. X/* AUTHOR(S)
  1329. X/*      W.Z. Venema
  1330. X/*      Eindhoven University of Technology
  1331. X/*      Department of Mathematics and Computer Science
  1332. X/*      Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
  1333. X/* CREATION DATE
  1334. X/*      Sun Apr  5 13:01:12 GMT+1:00 1987
  1335. X/* LAST MODIFICATION
  1336. X/*    90/01/22 13:02:11
  1337. X/* VERSION/RELEASE
  1338. X/*    2.1
  1339. X/*--*/
  1340. X
  1341. X#include <stdio.h>
  1342. X#include <ctype.h>
  1343. X
  1344. X#include "defs.h"
  1345. X#include "path.h"
  1346. X#include "pager.h"
  1347. X#include "screen.h"
  1348. X#include "mail.h"
  1349. X#include "ascf.h"
  1350. X#include "params.h"
  1351. X#include "ms_parse.h"
  1352. X
  1353. X /*
  1354. X  * Storage for header lines to be ignored. Both the strings with header
  1355. X  * names, and the pointers to these strings, are kept together.
  1356. X  */
  1357. X
  1358. Xstruct ignore {
  1359. X    char    strs[BUFSIZ];        /* null-terminated strings */
  1360. X    char   *ptrs[BUFSIZ / 2];        /* null-terminated list of pointers */
  1361. X};
  1362. X
  1363. Xhidden void ign_init();            /* forward declarations */
  1364. Xhidden int ign_header();
  1365. Xhidden int mbox_filter();
  1366. X
  1367. Xhidden File *letter = 0;        /* pager file */
  1368. X
  1369. X/* show_letter - display selected mail message */
  1370. X
  1371. Xhidden int show_letter()
  1372. X{
  1373. X    set_pager(letter);                /* select message display */
  1374. X    ds_pager();                    /* put it on the screen */
  1375. X    return (0);                    /* say screen is ok */
  1376. X}
  1377. X
  1378. X/* mbox - user has selected a message file */
  1379. X
  1380. Xpublic int mbox(meta, id)
  1381. Xint     meta;
  1382. Xunsigned id;
  1383. X{
  1384. X    static Screen screen[] = {
  1385. X    'C',    "Close",0,        initscreen,
  1386. X    'D',    "Delete",delete,    delcurr,
  1387. X    'M',    "Mail",    mailfile,    "Mail a copy of this message",
  1388. X    'P',    "Print",print,        printcurr,
  1389. X    'R',    "Reply",reply,        "Create reply to sender",
  1390. X    'S',    "Save",    save,        "Save this message to ordinary file",
  1391. X    'W',    "Work",    makework,    "Save this message to work file",
  1392. X    '|',    "|",    filter,        "Filter this message through command",
  1393. X    PGUP,    PgUp,    pu_pager,    pageup,
  1394. X    PGDN,    PgDn,    pd_pager,    pagedn,
  1395. X    UP,    "Up",    up_pager,    csrup,
  1396. X    DOWN,    "Down",    dn_pager,    csrdn,
  1397. X    0,    0,    show_letter,    "(Reading a mail message)",
  1398. X    };
  1399. X    char   *seen;
  1400. X
  1401. X    /* 
  1402. X     * Mail being read for the first time is renamed to reflect the
  1403. X     * status change.
  1404. X     */
  1405. X
  1406. X    if (mbox_filter(letter = open_pager(), message)) {
  1407. X    mesg_pager(letter, m_msgread);        /* no file or read error */
  1408. X    } else if (meta != NEW_META) {        /* unread message? */
  1409. X     /* void */ ;                /* no */
  1410. X    } else if (rename(comment, seen = old_meta(id)) == 0) {
  1411. X    strcpy(comment, seen);            /* mark message as read */
  1412. X    junk_desk();                /* say desk-top outdated */
  1413. X    }
  1414. X    kbdinp(screen);                /* look at the screen */
  1415. X    close_pager(letter), letter = 0;        /* destroy the display */
  1416. X    return (S_REDRAW);                /* force screen redrawing */
  1417. X}
  1418. X
  1419. X/* mbox_filter - suppress some message-header lines */
  1420. X
  1421. Xhidden int mbox_filter(pp, path)
  1422. XFile   *pp;
  1423. Xchar   *path;
  1424. X{
  1425. X#if (defined(lint) && defined(iAPX286))
  1426. X    static
  1427. X#endif
  1428. X    struct ignore ignore;
  1429. X    FILE   *fp;
  1430. X
  1431. X    if ((fp = ascopen(path, "r")) == 0) {
  1432. X    return (1);
  1433. X    } else {
  1434. X    char    buf[BUFSIZ];
  1435. X    int     ig_flag = 0;
  1436. X    int     ret;
  1437. X    int     context = MS_UUCP;
  1438. X
  1439. X    ign_init(&ignore);            /* initialize filter */
  1440. X
  1441. X    /*
  1442. X     * The header-line suppression algorithm is effective for RFC822-like
  1443. X     * headers lines only. Its main use is to get rid of the "Received:"
  1444. X     * lines that frequently show up in non-local mail.
  1445. X     */
  1446. X
  1447. X    while (ascgets(buf, sizeof(buf), fp)) {
  1448. X        switch (context = ms_parse(context, buf)) {
  1449. X        case MS_UUCP:
  1450. X        app_pager(pp, buf);
  1451. X        break;
  1452. X        case MS_HEADER:
  1453. X        if ((ig_flag = (ign_header(buf, &ignore))) == 0)
  1454. X            app_pager(pp, buf);
  1455. X        break;
  1456. X        case MS_CONT:
  1457. X        if (ig_flag == 0)
  1458. X            app_pager(pp, buf);
  1459. X        break;
  1460. X        case MS_BODY:
  1461. X        app_pager(pp, buf);
  1462. X        break;
  1463. X        }
  1464. X    }
  1465. X    ret = ferror(fp);
  1466. X    ascclose(fp);
  1467. X    return (ret);
  1468. X    }
  1469. X}
  1470. X
  1471. X/* ign_init - setup header lines to be ignored */
  1472. X
  1473. Xhidden void ign_init(ig)
  1474. Xstruct ignore *ig;
  1475. X{
  1476. X    Info   *ip = getparams() + P_IGNORE;/* what to ignore */
  1477. X    char   *sp = ":, ";            /* string separators */
  1478. X    char  **lp = ig->ptrs;        /* separated strings */
  1479. X
  1480. X    /*
  1481. X     * This function takes, from the pc-mail setup file, a (blank or
  1482. X     * comma)-separated list with names of mail headers to be ignored when a
  1483. X     * message is displayed. The list of names is broken up into separate
  1484. X     * strings. The result, a null-terminated list of string pointers, is
  1485. X     * stored in the ig argument. We use strtok() for string splitting. Since
  1486. X     * that function destroys its input, and since the user may change the
  1487. X     * setup at any time, we keep in the ig argument a copy of the relevant
  1488. X     * setup information.
  1489. X     */
  1490. X
  1491. X    if (ip->strval == 0) {            /* nothing to ignore */
  1492. X    *lp = 0;
  1493. X    } else {                    /* copy, then split */
  1494. X    (void) strncpy(ig->strs, ip->strval, sizeof(ig->strs));
  1495. X    for (*lp = strtok(ig->strs, sp); *lp; *lp = strtok((char *) 0, sp))
  1496. X        lp++;
  1497. X    }
  1498. X}
  1499. X
  1500. X/* ign_header - do we ignore this header line */
  1501. X
  1502. Xhidden int ign_header(buf, ig)
  1503. Xregister char *buf;
  1504. Xstruct ignore *ig;
  1505. X{
  1506. X    register int l;            /* header name length */
  1507. X    register char **list;        /* ptr to ignored header names */
  1508. X
  1509. X    /* Make sure that the header name is followed by a colon */
  1510. X
  1511. X    for (list = ig->ptrs; *list; list++) {
  1512. X    if (buf[l = strlen(*list)] == ':' && istrncmp(buf, *list, l) == 0)
  1513. X        return (1);
  1514. X    }
  1515. X    return (0);
  1516. X}
  1517. END_OF_main/mbox.c
  1518. if test 6112 -ne `wc -c <main/mbox.c`; then
  1519.     echo shar: \"main/mbox.c\" unpacked with wrong size!
  1520. fi
  1521. # end of overwriting check
  1522. fi
  1523. if test -f main/nmail.c -a "${1}" != "-c" ; then 
  1524.   echo shar: Will not over-write existing file \"main/nmail.c\"
  1525. else
  1526. echo shar: Extracting \"main/nmail.c\" \(6579 characters\)
  1527. sed "s/^X//" >main/nmail.c <<'END_OF_main/nmail.c'
  1528. X/*++
  1529. X/* NAME
  1530. X/*      nmail
  1531. X/* SUMMARY
  1532. X/*      extract originator and subject from new mail received by cico
  1533. X/* PROJECT
  1534. X/*      pc-mail
  1535. X/* PACKAGE
  1536. X/*      nmail
  1537. X/* SYNOPSIS
  1538. X/*      nmail [-d debuglevel]
  1539. X/* DESCRIPTION
  1540. X/*      nmail searches for new mail files received by cico and extracts
  1541. X/*    the originator's name, and message subject for later use by the 
  1542. X/*    mail visual shell.
  1543. X/*
  1544. X/*     Return address formats we understand (in order of preference):
  1545. X/* .nf
  1546. X/*
  1547. X/*    From: address (full_name)    (take full_name)
  1548. X/*    From: full_name <address>    (take full_name)
  1549. X/*    From: address            (take address)
  1550. X/*    >From address            (take address)
  1551. X/*    From address            (take address)
  1552. X/*
  1553. X/* .fi
  1554. X/*      To avoid tampering, new files will have read-only permission.
  1555. X/*
  1556. X/*      In order to avoid corruption, control-c interrupts are disabled
  1557. X/*    while this program executes.
  1558. X/* FILES
  1559. X/*      In the spool directory:
  1560. X/*    n<seqno>    received mail message
  1561. X/*    h<seqno>    extracted originator name, subject
  1562. X/* SEE ALSO
  1563. X/*      path(5)         spool directory, file names
  1564. X/*      cico(1)         network process
  1565. X/*      mail(1)       visual mail shell
  1566. X/* DIAGNOSTICS
  1567. X/*      Exit status zero when no errors were detected, nonzero in case of file
  1568. X/*      access errors. See status(5) for error codes.
  1569. X/* AUTHOR(S)
  1570. X/*      W.Z. Venema
  1571. X/*      Eindhoven University of Technology
  1572. X/*      Department of Mathematics and Computer Science
  1573. X/*      Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
  1574. X/* CREATION DATE
  1575. X/*      Tue Mar 31 20:14:11 GMT+1:00 1987
  1576. X/* LAST MODIFICATION
  1577. X/*    90/01/22 13:02:20
  1578. X/* VERSION/RELEASE
  1579. X/*    2.1
  1580. X/*--*/
  1581. X
  1582. X#include <stdio.h>
  1583. X#include <signal.h>
  1584. X#include <time.h>
  1585. X#include <ctype.h>
  1586. X#include <varargs.h>
  1587. X
  1588. X#include "defs.h"
  1589. X#include "ndir.h"
  1590. X#include "path.h"
  1591. X#include "status.h"
  1592. X#include "ms_parse.h"
  1593. X
  1594. Xextern struct tm *localtime();        /* system functions */
  1595. X
  1596. Xhidden void parse_args();        /* forward declarations */
  1597. Xhidden void newmail();
  1598. Xhidden void extract();
  1599. Xhidden void usage();
  1600. X
  1601. Xhidden int dflag = 0;            /* debugging option */
  1602. X
  1603. X#define debug    if (dflag) printf
  1604. X
  1605. Xpublic char *progname = "nmail";
  1606. X
  1607. Xmain(argc, argv)
  1608. Xint     argc;
  1609. Xchar  **argv;
  1610. X{
  1611. X    signal(SIGINT, SIG_IGN);            /* disable ctrl-c */
  1612. X    parse_args(argc, argv);            /* parse command args */
  1613. X    if (pathinit())                /* get path info */
  1614. X    exit(E_NOSPOOL);            /* bad MAILDIR variable */
  1615. X    umask(0222);                /* make files read-only */
  1616. X    newmail();                    /* get headers from new mail */
  1617. X    exit(0);
  1618. X    /* NOTREACHED */
  1619. X}
  1620. X
  1621. X/* parse_args - process command-line arguments */
  1622. X
  1623. Xhidden void parse_args(argc, argv)
  1624. Xint     argc;
  1625. Xchar  **argv;
  1626. X{
  1627. X    while (--argc && *++argv && **argv == '-') {/* process options */
  1628. X    switch (*++*argv) {
  1629. X    case 'd':                /* turn debugging on */
  1630. X        if (--argc == 0)
  1631. X        usage("missing debugging level argument");
  1632. X        if ((dflag = atoi(*++argv)) < 0 || dflag > 9)
  1633. X        dflag = 0;
  1634. X        break;
  1635. X    default:                /* unknown option */
  1636. X        usage("invalid option: -%c", **argv);
  1637. X        break;
  1638. X    }
  1639. X    }
  1640. X
  1641. X    /* check for extraneous arguments */
  1642. X
  1643. X    if (argc > 0)
  1644. X    usage("unexpected argument: %s", *argv);
  1645. X}
  1646. X
  1647. X/* scan for new mail that hasn't gotten yet a metafile */
  1648. X
  1649. Xhidden void newmail()
  1650. X{
  1651. X    register DIR *dp;
  1652. X    struct direct *de;
  1653. X    unsigned msgno;
  1654. X
  1655. X    debug("directory: \"%s\"\n", maildir);
  1656. X
  1657. X    /*
  1658. X     * Scan the spool directory for newly-arrived mail.
  1659. X     * 
  1660. X     * Incoming mail message files have a name of "n<seqno>". The originator
  1661. X     * name is normally present in files with names "h<seqno>" or "o<seqno>".
  1662. X     * The presence of an "o" file implies that the file "n<seqno>" has been
  1663. X     * read by the user. An "h" file means that the user has not yet read the
  1664. X     * message file.
  1665. X     * 
  1666. X     * If a message file has no corresponding "h" or "o" file we assume it is a
  1667. X     * new mail message and create an "h" file with the name of the
  1668. X     * originator and the subject of the message.
  1669. X     */
  1670. X
  1671. X    if ((dp = opendir(maildir)) == 0)
  1672. X    exit(E_NOSPOOL);
  1673. X
  1674. X    while (de = readdir(dp)) {
  1675. X    debug("nmail: file \"%s\"\n", de->d_name);
  1676. X    if (de->d_name[0] == NEW_MESG
  1677. X        && (msgno = seqno(de->d_name))) {
  1678. X        if (access(old_meta(msgno), 4) == 0) {
  1679. X         /* already marked as read */ ;
  1680. X        } else if (access(new_meta(msgno), 4) == 0) {
  1681. X         /* already marked as unread */ ;
  1682. X        } else {                /* create meta file */
  1683. X        extract(new_mesg(msgno), new_meta(msgno));
  1684. X        }
  1685. X    }
  1686. X    }
  1687. X    closedir(dp);
  1688. X}
  1689. X
  1690. X/* extract - extract originator and subject info from mail file to meta file */
  1691. X
  1692. Xhidden void extract(mail, meta)
  1693. Xchar   *mail;
  1694. Xchar   *meta;
  1695. X{
  1696. X    FILE   *mesgfp,
  1697. X           *metafp;
  1698. X    char    line[MAXLINE];
  1699. X    char    from[MAXLINE];            /* name of sender */
  1700. X    char    subj[MAXLINE];            /* message subject */
  1701. X    int     context = MS_UUCP;
  1702. X
  1703. X    debug("-- \"%s\" -> \"%s\"\n", mail, meta);
  1704. X
  1705. X    if ((mesgfp = fopen(mail, "r")) == NULL)    /* cannot open existing file */
  1706. X    exit(E_SYSFAIL);
  1707. X
  1708. X    strcpy(from, "Somewhere");            /* default originator */
  1709. X    subj[0] = '\0';                /* initialize subject */
  1710. X
  1711. X    /*
  1712. X     * Some mailers generate real headers, separated from the message body by
  1713. X     * an empty line. So we stop when we find an empty line. Other mailers
  1714. X     * have no headers, so we stop when we see no header line. The following
  1715. X     * algorithm tries to extract the real user name if possible, otherwise
  1716. X     * it takes whatever it can get.
  1717. X     */
  1718. X
  1719. X    while ((context != MS_BODY) && fgets(line, sizeof(line), mesgfp)) {
  1720. X    switch (context = ms_parse(context, line)) {
  1721. X    case MS_UUCP:
  1722. X        if (sscanf(line, "%*[>] From %s", from) != 1)
  1723. X        (void) sscanf(line, "From %s", from);
  1724. X        break;
  1725. X    case MS_HEADER:
  1726. X        if (hscanf(line, "Subject:", " %[^\n]", subj) == 0
  1727. X        && hscanf(line, "From:", " %*s ( %[^)] )", from) == 0)
  1728. X        (void) hscanf(line, "From:", " %[^<] <", from);
  1729. X        break;
  1730. X    }
  1731. X    }
  1732. X
  1733. X    /* carefully check all went well */
  1734. X
  1735. X    if (ferror(mesgfp))                /* sorry, read problem */
  1736. X    exit(E_READERR);
  1737. X    if ((metafp = fopen(meta, "w")) == NULL)    /* cannot create metafile */
  1738. X    exit(E_WRITERR);
  1739. X    fprintf(metafp, "%s\n%s\n", from, subj);    /* write originator, subject */
  1740. X    if (ferror(metafp)) {
  1741. X    (void) fclose(metafp);            /* ms-dog needs this! */
  1742. X    (void) chmod(meta, 0666);        /* sorry, write problem */
  1743. X    (void) unlink(meta);            /* delete metafile */
  1744. X    exit(E_WRITERR);
  1745. X    }
  1746. X    (void) fclose(mesgfp);
  1747. X    (void) fclose(metafp);
  1748. X}
  1749. X
  1750. X/* usage - explain what is wrong */
  1751. X
  1752. X/* VARARGS */
  1753. X
  1754. Xhidden void usage(va_alist) 
  1755. Xva_dcl
  1756. X{
  1757. X    va_list ap;
  1758. X    char   *fmt;
  1759. X
  1760. X    va_start(ap);
  1761. X    fmt = va_arg(ap, char *);
  1762. X    vfprintf(stderr, fmt, ap);
  1763. X    va_end(ap);
  1764. X    fprintf(stderr, "\nusage: nmail [-d debugging_level]\n");
  1765. X    exit(2);
  1766. X}
  1767. END_OF_main/nmail.c
  1768. if test 6579 -ne `wc -c <main/nmail.c`; then
  1769.     echo shar: \"main/nmail.c\" unpacked with wrong size!
  1770. fi
  1771. # end of overwriting check
  1772. fi
  1773. if test -f main/startup.c -a "${1}" != "-c" ; then 
  1774.   echo shar: Will not over-write existing file \"main/startup.c\"
  1775. else
  1776. echo shar: Extracting \"main/startup.c\" \(6115 characters\)
  1777. sed "s/^X//" >main/startup.c <<'END_OF_main/startup.c'
  1778. X/*++
  1779. X/* NAME
  1780. X/*      startup 3
  1781. X/* SUMMARY
  1782. X/*      startup/terminate network protocol
  1783. X/* PROJECT
  1784. X/*      pc-mail
  1785. X/* PACKAGE
  1786. X/*      cico
  1787. X/* SYNOPSIS
  1788. X/*      startproto()
  1789. X/*
  1790. X/*      endproto()
  1791. X/* DESCRIPTION
  1792. X/*      startproto() should be called after a successfull login on a remote
  1793. X/*      host. It performs the primary handshake with the other system
  1794. X/*      (call accepted/locked) and negotiates a communications protocol.
  1795. X/*      It then sets the function pointers Close/Read/Write to the
  1796. X/*      appropriate values. Until endproto() is called, all i/o to the
  1797. X/*      remote host should proceed through the functions pointed to by
  1798. X/*      Read/Write.
  1799. X/*
  1800. X/*      endproto() turns the protocol off, and sends the silly "OO" message
  1801. X/*      to the remote system. It does not disconnect, nor does it change
  1802. X/*      the state of the communications port.
  1803. X/* FUNCTIONS AND MACROS
  1804. X/*      xgetc(), xwrite(), trap()
  1805. X/* DIAGNOSTICS
  1806. X/*      The process of negotiation is shown when debugging is enabled.
  1807. X/*      startproto() and endproto() return 0 in case of success, E_LOST
  1808. X/*      if no response arrived and E_REJECT if the response differed
  1809. X/*      from the expected response.
  1810. X/* BUGS
  1811. X/*      startproto() assumes that the local system is the calling system.
  1812. X/* AUTHOR(S)
  1813. X/*      W.Z. Venema
  1814. X/*      Eindhoven University of Technology
  1815. X/*      Department of Mathematics and Computer Science
  1816. X/*      Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
  1817. X/* CREATION DATE
  1818. X/*      Fri Mar 27 13:43:00 GMT+1:00 1987
  1819. X/* LAST MODIFICATION
  1820. X/*    90/01/22 13:02:41
  1821. X/* VERSION/RELEASE
  1822. X/*    2.1
  1823. X/*--*/
  1824. X
  1825. X#include <stdio.h>
  1826. X#include <setjmp.h>
  1827. X
  1828. X#include "defs.h"
  1829. X#include "params.h"
  1830. X#include "comm.h"
  1831. X#include "logs.h"
  1832. X#include "status.h"
  1833. X#include "sysdep.h"
  1834. X
  1835. X/* forward declarations */
  1836. X
  1837. Xhidden char *xpct();            /* expect a string */
  1838. Xhidden char *send();            /* send a string */
  1839. X
  1840. X/* the functions that inplement the various protocols */
  1841. X
  1842. Xextern  kopen(), kclose(), kread(), kwrite();    /* k protocol */
  1843. Xextern  gopen(), gclose(), gread(), gwrite();    /* g protocol */
  1844. X
  1845. Xtypedef struct proto {
  1846. X    char    name;            /* name of the protocol */
  1847. X    int     (*open) ();            /* the open function */
  1848. X    int     (*close) ();        /* the close function */
  1849. X    int     (*read) ();            /* the read function */
  1850. X    int     (*write) ();        /* the write function */
  1851. X};
  1852. X
  1853. X/* the order of protocols is significant! */
  1854. X
  1855. Xhidden struct proto ptbl[] = {
  1856. X    'k', kopen, kclose, kread, kwrite,    /* try this first */
  1857. X    'g', gopen, gclose, gread, gwrite,    /* then this one */
  1858. X    /* add your protocols at the appropriate place */
  1859. X    0,                    /* terminator! */
  1860. X};
  1861. X
  1862. X/* startproto - do primary handshake, establish protocol and turn it on */
  1863. X
  1864. Xpublic  startproto()
  1865. X{
  1866. X    int    *savetrap = systrap;
  1867. X    jmp_buf mytrap;
  1868. X    register struct proto *pp;
  1869. X    register char *cp;
  1870. X    int     status;
  1871. X
  1872. X    if (status = setjmp(systrap = mytrap)) {    /* get here if expect fails */
  1873. X    systrap = savetrap;
  1874. X    return (status);
  1875. X    }
  1876. X    /* the primary handshake: who are we and is it ok we call right now */
  1877. X
  1878. X    sscanf(xpct("Shere"), "Shere=%s", rmthost);    /* try to get host name */
  1879. X    log("SUCCEEDED (call to %s)", rmthost);
  1880. X
  1881. X    /* some uucico implementations seem to have problems with debug level 0 */
  1882. X
  1883. X    send(strcons("S%s -x%d", LOGIN_NAME, MAX(dflag, 1)));
  1884. X    xpct("ROK");                /* we're accepted or rejected */
  1885. X
  1886. X    /* choose a protocol from the list offered by the other side */
  1887. X
  1888. X    for (cp = xpct("P") + 1, pp = ptbl; pp->name && !index(cp, pp->name); pp++)
  1889. X     /* void */ ;
  1890. X    if (pp->name == 0) {            /* no common protocol */
  1891. X    send("UN");
  1892. X    trap(E_REJECT, "FAILED (no common protocol in \"%s\")", cp);
  1893. X    /* NOTREACHED */
  1894. X    }
  1895. X    send(strcons("U%c", pp->name));        /* my choice of protocol */
  1896. X
  1897. X    /* install protocol */
  1898. X
  1899. X    Close = pp->close;                /* for endproto() */
  1900. X    Read = pp->read;
  1901. X    Write = pp->write;
  1902. X    if (pp->open && CALL(pp->open) (ttfd))    /* start up a protocol */
  1903. X    trap(E_LOST, "FAILED (startup)");
  1904. X
  1905. X    /* finish up */
  1906. X
  1907. X    log("OK (startup)");
  1908. X    systrap = savetrap;                /* get here if expect wins */
  1909. X    return (0);
  1910. X}
  1911. X
  1912. X/* endproto - terminate protocol */
  1913. X
  1914. Xpublic  endproto()
  1915. X{
  1916. X    int    *savetrap = systrap;            /* save exception handler */
  1917. X    jmp_buf mytrap;
  1918. X    int     status;
  1919. X
  1920. X    if (Close)                    /* check there is one */
  1921. X    CALL(Close) (ttfd);            /* turn protocol off */
  1922. X    send("OOOOOO");                /* byebye */
  1923. X
  1924. X    /* Don\'t wait for the other side\'s OOOOOO, just sleep and quit. */
  1925. X
  1926. X    (void) sleep(1);
  1927. X    log("OK (conversation complete)");
  1928. X    return (0);
  1929. X}
  1930. X
  1931. X/* send - write message to remote host and return pointer to message */
  1932. X
  1933. Xhidden char *send(str)
  1934. Xchar   *str;
  1935. X{
  1936. X    xwrite(ttfd, "\020", 1);            /* message header */
  1937. X    xwrite(ttfd, str, strlen(str) + 1);        /* include trailing null */
  1938. X    debug(4) ("send: %S\n", str);
  1939. X    return (str);                /* return the message */
  1940. X}
  1941. X
  1942. X/* xpct - read message from host in "^Pmessage[\0\n]" format; trap on errors */
  1943. X
  1944. Xhidden char *xpct(pat)
  1945. Xchar   *pat;
  1946. X{
  1947. X    register int c;
  1948. X    register char *p = msgin;
  1949. X    register int inmsg = 0;
  1950. X
  1951. X    /*
  1952. X     * Keep reading until we time out, or until a complete message has been
  1953. X     * received. Consider the link as lost in case of time out. Assume we are
  1954. X     * rejected if the received message differs from what was expected.
  1955. X     */
  1956. X
  1957. X    debug(4) ("xpct: %S\n", pat);
  1958. X
  1959. X    for (;;) {
  1960. X    if ((c = xgetc()) == EOF) {
  1961. X        trap(E_LOST, "FAILED (protocol handshake)");
  1962. X        /* NOTREACHED */
  1963. X    } else if ((c &= 0177) == '\020') {
  1964. X        debug(4) (" got sync\n");        /* got start of message */
  1965. X        p = msgin;                /* reset */
  1966. X        inmsg = 1;
  1967. X    } else if (inmsg == 0) {    
  1968. X        debug(4) ("%C", c);            /* don\'t store, just debug */
  1969. X    } else if (*p++ = ((c == '\n') ? '\0' : c)) {
  1970. X        debug(4) ("%C", c);            /* store and debug */
  1971. X        if (p >= msgin + MSGBUF) {        /* spurious Ctrl-P seen? */
  1972. X        p = msgin;            /* reset */
  1973. X        inmsg = 0;            /* reset */
  1974. X        }
  1975. X    } else if ((debug(4) ("\n")), strncmp(pat, msgin, strlen(pat)) == 0) {
  1976. X        return (msgin);            /* expect succeeded */
  1977. X    } else {
  1978. X        msgin[30] = '\0';            /* truncate to 30 */
  1979. X        trap(E_REJECT, "FAILED (%S)", msgin);
  1980. X        /* NOTREACHED */
  1981. X    }
  1982. X    }
  1983. X}
  1984. END_OF_main/startup.c
  1985. if test 6115 -ne `wc -c <main/startup.c`; then
  1986.     echo shar: \"main/startup.c\" unpacked with wrong size!
  1987. fi
  1988. # end of overwriting check
  1989. fi
  1990. if test -f main/str.c -a "${1}" != "-c" ; then 
  1991.   echo shar: Will not over-write existing file \"main/str.c\"
  1992. else
  1993. echo shar: Extracting \"main/str.c\" \(6415 characters\)
  1994. sed "s/^X//" >main/str.c <<'END_OF_main/str.c'
  1995. X/*++
  1996. X/* NAME
  1997. X/*    strcons,istrcmp,strvec,vecstr,split 3
  1998. X/* SUMMARY
  1999. X/*    string utility routines
  2000. X/* PROJECT
  2001. X/*    pc-mail
  2002. X/* PACKAGE
  2003. X/*    general stuff
  2004. X/* SYNOPSIS
  2005. X/*    #include "defs.h"
  2006. X/*
  2007. X/*    char *strcons(format,args)
  2008. X/*    char *format;
  2009. X/*
  2010. X/*    char *split(cpp, sep)
  2011. X/*    char **cpp;
  2012. X/*    char *sep;
  2013. X/*
  2014. X/*    int istrncmp(s1,s2,n)
  2015. X/*    char *s1,s2;
  2016. X/*    int n;
  2017. X/*
  2018. X/*    int istrcmp(s1,s2)
  2019. X/*    char *s1,s2;
  2020. X/*
  2021. X/*    char **strvec(string,separ)
  2022. X/*    char *string;
  2023. X/*    char *separ;
  2024. X/*
  2025. X/*    freevec(vec)
  2026. X/*    char **vec;
  2027. X/*
  2028. X/*    char *vecstr(vector,separ)
  2029. X/*    char **vector;
  2030. X/*    char *separ;
  2031. X/* DESCRIPTION
  2032. X/*    strcons() produces a formatted string, using printf()-like
  2033. X/*    arguments. Basically it is an sprintf() that returns a
  2034. X/*    pointer to the result. memory for the result is taken from
  2035. X/*    a small memory pool that is recycled upon successive calls.
  2036. X/*
  2037. X/*    split() searches the string pointed to by cpp for the occurrance
  2038. X/*    of the text token (a string not containing any of the characters
  2039. X/*    given in the "sep" argument). *cpp is updated if a token is
  2040. X/*    found; a null pointer is returned otherwise. This function
  2041. X/*    is an attempt to improve upon the strtok() function, which
  2042. X/*    can parse only one string at a time. It still modifies its
  2043. X/*    arguments, however.
  2044. X/*
  2045. X/*    istrcmp() is a case-insensitive version of the strcmp() function.
  2046. X/*
  2047. X/*    istrncmp() is a case-insensitive version of the strncmp() function.
  2048. X/*
  2049. X/*    strvec() breaks a null-terminated string using the separators given
  2050. X/*    in separ, and returns a null-terminated vector of pointers to the
  2051. X/*    resulting substrings. Memory for the vector and substrings are
  2052. X/*    allocated in dynamic memory. The original string is not modified.
  2053. X/*
  2054. X/*    freevec() frees storage allocated by strvec().
  2055. X/*
  2056. X/*    vecstr() takes a null-terminated vector of string pointers
  2057. X/*    and builds a string from the strings pointed to by the vector
  2058. X/*    argument, separated by the string in the separ argument.
  2059. X/*    Memory for the result is allocated in dynamic memory.
  2060. X/* FUNCTIONS AND MACROS
  2061. X/*    strtok(), malloc(), memcpy(), sprintf()
  2062. X/* DIAGNOSTICS
  2063. X/*    strvec(), vecstr() return a null pointer if there was not enough memory
  2064. X/*    avaliable to hold the result.
  2065. X/* BUGS
  2066. X/*    strcons() does not do smart garbage collection; it just uses
  2067. X/*    a circular buffer. The present implementation is not portable
  2068. X/*    to machines that pass arguments via registers.
  2069. X/*
  2070. X/*    strvec() cannot handle strings with more than BUFSIZ words.
  2071. X/*    strvec() uses strtok(), which may have side effects.
  2072. X/* AUTHOR(S)
  2073. X/*    W.Z. Venema
  2074. X/*    Eindhoven University of Technology
  2075. X/*    Department of Mathematics and Computer Science
  2076. X/*    Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
  2077. X/* CREATION DATE
  2078. X/*    Tue Apr  5 20:59:29 MET 1988
  2079. X/* LAST MODIFICATION
  2080. X/*    90/01/22 13:02:43
  2081. X/* VERSION/RELEASE
  2082. X/*    2.1
  2083. X/*--*/
  2084. X
  2085. X#include <stdio.h>
  2086. X#include <ctype.h>
  2087. X#include <varargs.h>
  2088. X
  2089. X#include "defs.h"
  2090. X
  2091. X#define    NBUF    4
  2092. X
  2093. X/* strcons - quick-and-dirty string constructor */
  2094. X
  2095. X/* VARARGS */
  2096. X
  2097. Xchar   *strcons(va_alist) 
  2098. Xva_dcl
  2099. X{
  2100. X    va_list ap;
  2101. X    static char strbuf[NBUF][BUFSIZ];
  2102. X    static int where = 0;
  2103. X    register char *cp;
  2104. X    char   *fmt;
  2105. X
  2106. X    va_start(ap);
  2107. X    fmt = va_arg(ap, char *);
  2108. X    (void) vsprintf(cp = strbuf[where = (where + 1) % NBUF], fmt, ap);
  2109. X    va_end(ap);
  2110. X    return (cp);
  2111. X}
  2112. X
  2113. X/* istrcmp - case-insensitive string comparison */
  2114. X
  2115. X#define    LOW(c)    (isascii(c)&&isupper(c)?tolower(c):(c))
  2116. X
  2117. Xint     istrcmp(s1, s2)
  2118. Xregister char *s1;
  2119. Xregister char *s2;
  2120. X{
  2121. X    while (*s1 && (LOW(*s1) == LOW(*s2)))
  2122. X    s1++, s2++;
  2123. X    return (LOW(*s1) - LOW(*s2));
  2124. X}
  2125. X
  2126. X/* istrncmp - case-insensitive string comparison */
  2127. X
  2128. X#define    LOW(c)    (isascii(c)&&isupper(c)?tolower(c):(c))
  2129. X
  2130. Xint     istrncmp(s1, s2, n)
  2131. Xregister char *s1;
  2132. Xregister char *s2;
  2133. Xregister int n;
  2134. X{
  2135. X    while (n > 0 && *s1 && (LOW(*s1) == LOW(*s2)))
  2136. X    n--, s1++, s2++;
  2137. X    return (n > 0 ? LOW(*s1) - LOW(*s2) : 0);
  2138. X}
  2139. X
  2140. X/* strvec - make vector of substring pointers */
  2141. X
  2142. Xchar  **strvec(str, sep)
  2143. Xchar   *str;
  2144. Xchar   *sep;
  2145. X{
  2146. X#ifdef lint
  2147. X    static
  2148. X#endif
  2149. X    char   *tmp[BUFSIZ];        /* scratch substring pointer storage */
  2150. X    register char **cpp = tmp;
  2151. X    char   *sp;                /* ptr to private copy of original */
  2152. X    register int bytec;
  2153. X
  2154. X    /* make a copy of the original string */
  2155. X
  2156. X    if ((sp = malloc(strlen(str) + 1)) == 0)
  2157. X    return (0);
  2158. X    (void) strcpy(sp, str);
  2159. X
  2160. X    /* chop our copy at sequences of one or more separators */
  2161. X
  2162. X    for (*cpp = strtok(sp, sep); *cpp; *++cpp = strtok((char *) 0, sep))
  2163. X     /* void */ ;
  2164. X
  2165. X    /* now construct the vector of pointers to the substrings */
  2166. X
  2167. X    if ((cpp = (char **) malloc(bytec = (cpp - tmp + 1) * sizeof(*cpp))) == 0)
  2168. X    return (0);
  2169. X    return ((char **) memcpy((char *) cpp, (char *) tmp, bytec));
  2170. X}
  2171. X
  2172. X/* freevec - release storage allocated by strvec() */
  2173. X
  2174. Xfreevec(vec)
  2175. Xchar  **vec;
  2176. X{
  2177. X    free(vec[0]);
  2178. X    free((char *) vec);
  2179. X}
  2180. X
  2181. X/* vecstr - from null-terminated vector of string pointers to one flat string */
  2182. X
  2183. Xpublic char *vecstr(vec, sep)
  2184. Xchar  **vec;
  2185. Xchar   *sep;
  2186. X{
  2187. X    register char **cpp;
  2188. X    register int len = 0;        /* length of final string */
  2189. X    register char *cp;
  2190. X    register int flen = strlen(sep);    /* filler between substrings */
  2191. X
  2192. X    /* find out how big the resulting string will be */
  2193. X
  2194. X    for (cpp = vec; *cpp; cpp++)
  2195. X    len += strlen(*cpp) + flen;
  2196. X
  2197. X    /* allocate and initialize the result string */
  2198. X
  2199. X    if ((cp = malloc(len + 1)) == 0)
  2200. X    return (0);
  2201. X    *cp = '\0';
  2202. X
  2203. X    /* fill the resulting string */
  2204. X
  2205. X    for (cpp = vec; *cpp; cpp++) {
  2206. X    (void) strcat(cp, *cpp);
  2207. X    (void) strcat(cp, sep);
  2208. X    }
  2209. X    return (cp);
  2210. X}
  2211. X
  2212. X/* split - return next token in *cpp, update cpp */
  2213. X
  2214. Xpublic char *split(cpp, sep)
  2215. Xregister char **cpp;
  2216. Xregister char *sep;
  2217. X{
  2218. X    register char *start;
  2219. X    char   *end;
  2220. X    char   *strpbrk();
  2221. X
  2222. X    /*
  2223. X     * Find the beginning of the first token. If none is found, just return a
  2224. X     * null value. Otherwise, if there is a separator that follows the token,
  2225. X     * nullify it and advance *cpp to the first character after the nullified
  2226. X     * separator. If the token is not followed by a separator advance *cpp to
  2227. X     * the null byte that follows the token.
  2228. X     */
  2229. X
  2230. X    start = *cpp + strspn(*cpp, sep);
  2231. X
  2232. X    if (start[0] == 0) {
  2233. X    return (0);                /* no token */
  2234. X    } else if (end = strpbrk(start, sep)) {    /* look for separator */
  2235. X    *end = '\0';                /* nullify */
  2236. X    *cpp = end + 1;                /* advance beyond end */
  2237. X    return (start);                /* return token */
  2238. X    } else {
  2239. X    *cpp = start + strlen(start);        /* advance to terminator */
  2240. X    return (start);                /* return token */
  2241. X    }
  2242. X}
  2243. END_OF_main/str.c
  2244. if test 6415 -ne `wc -c <main/str.c`; then
  2245.     echo shar: \"main/str.c\" unpacked with wrong size!
  2246. fi
  2247. # end of overwriting check
  2248. fi
  2249. echo shar: End of archive 6 \(of 11\).
  2250. cp /dev/null ark6isdone
  2251. MISSING=""
  2252. for I in 1 2 3 4 5 6 7 8 9 10 11 ; do
  2253.     if test ! -f ark${I}isdone ; then
  2254.     MISSING="${MISSING} ${I}"
  2255.     fi
  2256. done
  2257. if test "${MISSING}" = "" ; then
  2258.     echo You have unpacked all 11 archives.
  2259.     rm -f ark[1-9]isdone ark[1-9][0-9]isdone
  2260. else
  2261.     echo You still need to unpack the following archives:
  2262.     echo "        " ${MISSING}
  2263. fi
  2264. ##  End of shell archive.
  2265. exit 0
  2266.  
  2267.  
  2268.