|
|
1.1 ! root 1: #include <stdio.h> ! 2: #include <signal.h> ! 3: #include "mail.h" ! 4: #include "string.h" ! 5: #include "message.h" ! 6: ! 7: /* imported */ ! 8: char *getenv(); ! 9: ! 10: /* ! 11: * auxiliary routines used by commands ! 12: */ ! 13: ! 14: /* drop the newline off a string */ ! 15: static char * ! 16: dropleadingwhite(cmd) ! 17: char *cmd; ! 18: { ! 19: while(*cmd==' ' || *cmd=='\t') ! 20: cmd++; ! 21: return cmd; ! 22: } ! 23: ! 24: /* drop the newline off a string */ ! 25: static void ! 26: dropnewline(cmd) ! 27: char *cmd; ! 28: { ! 29: cmd = cmd+strlen(cmd)-1; ! 30: if (*cmd=='\n') ! 31: *cmd = '\0'; ! 32: } ! 33: ! 34: /* make sure an argument exists */ ! 35: static int ! 36: needargument(cmd) ! 37: char *cmd; ! 38: { ! 39: if (*cmd=='\0') { ! 40: fprintf(stderr, "?argument missing\n"); ! 41: return -1; ! 42: } ! 43: return 0; ! 44: } ! 45: ! 46: /* make the header line for a message */ ! 47: char * ! 48: header(mp) ! 49: message *mp; ! 50: { ! 51: static char hd[256]; ! 52: ! 53: sprintf(hd, "%3d %c %4d %s %.30s", mp->pos, mp->status&DELETED?'d':' ', ! 54: mp->size, s_to_c(mp->sender), s_to_c(mp->date)); ! 55: return hd; ! 56: } ! 57: ! 58: /* ! 59: * command routines ! 60: */ ! 61: ! 62: /* current message */ ! 63: whereis(mp) ! 64: message *mp; ! 65: { ! 66: printf("%d\n", mp->pos); ! 67: return 0; ! 68: } ! 69: ! 70: /* print header */ ! 71: prheader(mp) ! 72: message *mp; ! 73: { ! 74: printf("%s\n", header(mp)); ! 75: return 0; ! 76: } ! 77: ! 78: /* change message status */ ! 79: delete(mp) ! 80: message *mp; ! 81: { ! 82: mp->status |= DELETED; ! 83: return 0; ! 84: } ! 85: undelete(mp) ! 86: message *mp; ! 87: { ! 88: mp->status &= ~DELETED; ! 89: return 0; ! 90: } ! 91: ! 92: /* store a message into a file */ ! 93: store(mp, cmd, header) ! 94: message *mp; ! 95: char *cmd; ! 96: { ! 97: static string *mbox=NULL; ! 98: FILE *fp; ! 99: char *home; ! 100: ! 101: dropnewline(cmd); ! 102: cmd = dropleadingwhite(cmd); ! 103: if (*cmd=='\0') { ! 104: if (mbox==NULL){ ! 105: mbox=s_new(); ! 106: if ((home=getenv("HOME"))!=NULL) { ! 107: s_append(mbox, home); ! 108: s_append(mbox, "/"); ! 109: } ! 110: s_append(mbox, "mbox"); ! 111: } ! 112: cmd = s_to_c(mbox); ! 113: } ! 114: fp = fopen(cmd, "a"); ! 115: if (fp == NULL) { ! 116: fprintf(stderr, "?can't open %s\n", cmd); ! 117: return -1; ! 118: } ! 119: m_print(mp, fp, header, header); ! 120: fclose(fp); ! 121: return 0; ! 122: } ! 123: ! 124: static interrupt = 0; ! 125: ! 126: SIGRETURN ! 127: remember_int(s) ! 128: int s; ! 129: { ! 130: signal(SIGINT, remember_int); ! 131: interrupt = 1; ! 132: } ! 133: ! 134: /* pipe a message (and possible tty input) to a command */ ! 135: static int ! 136: pipecmd(mp, cmd, fromtty, mailinput) ! 137: message *mp; ! 138: char *cmd; ! 139: int fromtty; ! 140: int mailinput; ! 141: { ! 142: FILE *fp; ! 143: char buf[128]; ! 144: int status, rv=0; ! 145: SIG_TYP istat, qstat; ! 146: int onatty; ! 147: ! 148: onatty = isatty(fileno(stdin)); ! 149: if (fromtty){ ! 150: printf("!%s\n", cmd); ! 151: fflush(stdout); ! 152: } ! 153: fp = popen(cmd, "w"); ! 154: if (fp==NULL) { ! 155: fprintf(stderr, "?can't exec %s\n", cmd); ! 156: return -1; ! 157: } ! 158: interrupt = 0; ! 159: istat = signal(SIGINT, remember_int); ! 160: qstat = signal(SIGQUIT, remember_int); ! 161: if (fromtty) { ! 162: while (fgets(buf, sizeof(buf), stdin)!=NULL) { ! 163: if (interrupt!=0) ! 164: break; ! 165: if (onatty && strcmp(buf, ".\n")==0) ! 166: break; ! 167: fputs(buf, fp); ! 168: fflush(fp); ! 169: } ! 170: clearerr(stdin); ! 171: } ! 172: if (!interrupt && mailinput) ! 173: m_print(mp, fp, 0, 1); ! 174: if (status=pclose(fp)) ! 175: rv = -1; ! 176: signal(SIGINT, istat); ! 177: signal(SIGQUIT, qstat); ! 178: if (fromtty) ! 179: printf("!\n"); ! 180: return rv; ! 181: } ! 182: ! 183: /* pass a message to someone else */ ! 184: remail(mp, cmd, ttyinput) ! 185: message *mp; ! 186: char *cmd; ! 187: int ttyinput; ! 188: { ! 189: static string *cmdstring=NULL; ! 190: ! 191: dropnewline(cmd); ! 192: if(cmdstring==NULL) ! 193: cmdstring = s_new(); ! 194: s_append(s_restart(cmdstring), "/bin/mail "); ! 195: s_append(cmdstring, cmd); ! 196: if (pipecmd(mp, s_to_c(cmdstring), ttyinput, 1)<0) ! 197: return -1; ! 198: return 0; ! 199: } ! 200: ! 201: /* output a message */ ! 202: print(mp) ! 203: message *mp; ! 204: { ! 205: m_print(mp, stdout, 0, 1); ! 206: return 0; ! 207: } ! 208: ! 209: /* pipe mail into a command */ ! 210: pipemail(mp, cmd, ttyinput) ! 211: message *mp; ! 212: char *cmd; ! 213: { ! 214: dropnewline(cmd); ! 215: if (pipecmd(mp, cmd, ttyinput, 1)<0) ! 216: return -1; ! 217: return 0; ! 218: } ! 219: ! 220: /* escape to the shell */ ! 221: escape(cmd) ! 222: char *cmd; ! 223: { ! 224: char *cp; ! 225: ! 226: cp = cmd+strlen(cmd)-1; ! 227: if (*cp=='\n') ! 228: *cp = '\0'; ! 229: system(cmd); ! 230: printf("!\n"); ! 231: return 0; ! 232: } ! 233: ! 234: /* reply to a message */ ! 235: reply(mp, mailinput) ! 236: message *mp; ! 237: int mailinput; ! 238: { ! 239: static string *cmdstring=NULL; ! 240: ! 241: if (cmdstring==NULL) ! 242: cmdstring = s_new(); ! 243: s_append(s_restart(cmdstring), "/bin/mail "); ! 244: s_append(cmdstring, s_to_c(mp->sender)); ! 245: if (pipecmd(mp, s_to_c(cmdstring), 1, mailinput)<0) ! 246: return -1; ! 247: return 0; ! 248: } ! 249: ! 250: /* print out the command list */ ! 251: help() ! 252: { ! 253: printf("Commands are of the form [range] command [argument].\n"); ! 254: printf("The commmands are:\n"); ! 255: printf("b\tprint the next ten headers\n"); ! 256: printf("d\tmark for deletion\n"); ! 257: printf("h\tprint message header (,h to print all headers)\n"); ! 258: printf("m addr\tremail message to addr\n"); ! 259: printf("M addr\tremail message to addr preceded by user input\n"); ! 260: printf("p\tprint the message\n"); ! 261: printf("q\texit from mail, and save messages not marked for deletion\n"); ! 262: printf("r\treply to sender\n"); ! 263: printf("R\treply to sender with original message appended\n"); ! 264: printf("s file\tappend message to file\n"); ! 265: printf("u\tunmark message for deletion\n"); ! 266: printf("x\texit without changing mail box\n"); ! 267: printf("| cmd\tpipe mail to command\n"); ! 268: printf("! cmd\tescape to commmand\n"); ! 269: printf("?\tprint this message\n"); ! 270: return 0; ! 271: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.