summaryrefslogtreecommitdiffstats
path: root/qbiff.c
blob: c2d474fb8e4e46ce766fa1002581d2c3953abab3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <sys/types.h>
#include <sys/stat.h>
#include <utmpx.h>
#include "stralloc.h"
#include "substdio.h"
#include "subfd.h"
#include "open.h"
#include "byte.h"
#include "str.h"
#include "headerbody.h"
#include "hfield.h"
#include "env.h"
#include "exit.h"

substdio sstty;
char buftty[1024];
int fdtty;

struct utmpx *ut;
char line[sizeof(ut->ut_line) + 1];
stralloc woof = {0};
stralloc tofrom = {0};
stralloc text = {0};

void doit(s,n) char *s; int n;
{
 if (!stralloc_catb(&text,s,n)) _exit(0);
 if (text.len > 78) text.len = 78;
}
void dobody(h) stralloc *h; { doit(h->s,h->len); }
void doheader(h) stralloc *h;
{
 int i;
 if (hfield_known(h->s,h->len) == H_SUBJECT)
  {
   i = hfield_skipname(h->s,h->len);
   doit(h->s + i,h->len - i);
  }
}
void finishheader() { ; }

int main()
{
 char *user;
 char *sender;
 char *userext;
 struct stat st;
 int i;

 if (chdir("/dev") == -1) _exit(0);

 if (!(user = env_get("USER"))) _exit(0);
 if (!(sender = env_get("SENDER"))) _exit(0);
 if (!(userext = env_get("LOCAL"))) _exit(0);
 if (str_len(user) > sizeof(ut->ut_user)) _exit(0);

 if (!stralloc_copys(&tofrom,"*** TO <")) _exit(0);
 if (!stralloc_cats(&tofrom,userext)) _exit(0);
 if (!stralloc_cats(&tofrom,"> FROM <")) _exit(0);
 if (!stralloc_cats(&tofrom,sender)) _exit(0);
 if (!stralloc_cats(&tofrom,">")) _exit(0);

 for (i = 0;i < tofrom.len;++i)
   if ((tofrom.s[i] < 32) || (tofrom.s[i] > 126))
     tofrom.s[i] = '_';

 if (!stralloc_copys(&text,"    ")) _exit(0);
 if (headerbody(subfdin,doheader,finishheader,dobody) == -1) _exit(0);

 for (i = 0;i < text.len;++i)
   if ((text.s[i] < 32) || (text.s[i] > 126))
     text.s[i] = '/';

 if (!stralloc_copys(&woof,"\015\n\007")) _exit(0);
 if (!stralloc_cat(&woof,&tofrom)) _exit(0);
 if (!stralloc_cats(&woof,"\015\n")) _exit(0);
 if (!stralloc_cat(&woof,&text)) _exit(0);
 if (!stralloc_cats(&woof,"\015\n")) _exit(0);

 while ((ut = getutxent()) != NULL)
   if (ut->ut_type == USER_PROCESS && !str_diffn(ut->ut_user,user,sizeof(ut->ut_user)))
    {
     byte_copy(line,sizeof(ut->ut_line),ut->ut_line);
     line[sizeof(ut->ut_line)] = 0;
     if (line[0] == '/') continue;
     if (!line[0]) continue;
     if (line[str_chr(line,'.')]) continue;
     fdtty = open_append(line);
     if (fdtty == -1) continue;
     if (fstat(fdtty,&st) == -1) { close(fdtty); continue; }
     if (!(st.st_mode & 0100)) { close(fdtty); continue; }
     if (st.st_uid != getuid()) { close(fdtty); continue; }
     substdio_fdbuf(&sstty,write,fdtty,buftty,sizeof(buftty));
     substdio_putflush(&sstty,woof.s,woof.len);
     close(fdtty);
    }
 _exit(0);
}