summaryrefslogtreecommitdiffstats
path: root/src/appl/popper/pop_enter.c
blob: 1206f6b4042620665eb48eb951fb52b68755e3a7 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186

/*
 * 
 */
 
#include <errno.h>
#include <stdio.h>
#include <sys/types.h>
#include <strings.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <mit-copyright.h>

#ifdef ZEPHYR
#include <zephyr/zephyr.h>

char *Zsignature = "Your friendly neighborhood post office.";
#endif ZEPHYR

void notify_recipient();

#define POP_MAILDIR "/usr/spool/pop"

char buffer[BUFSIZ];
char xtmpfile[512];           /* tmp file */
int  newmail;                /* fd for temp message */
int  maildrop;               /* file descriptor for drop */

extern int errno;
extern int sys_nerr;
extern char *sys_errlist[];


main (argc, argv)
     int argc;
     char **argv;     
{
  int status = 0;
  int  i = 1;                  /* argument counter */
      
  if(get_message() < 0)
    exit(1);

  while (--argc > 0) 
    {
      if(open_drop(argv[i]) < 0)
	{
	  lseek(newmail, 0, L_SET);
	  if(new_message(maildrop, newmail) < 0)
	    status = 1;
	  if(close(maildrop) < 0)
	    {
	      status = 1;
	      sprintf(buffer, "%s: error on close", argv[i]);
	      error(buffer, errno);
	    }
	  notify_recipient(argv[i]);
	}
      else
	status = 1;
      ++i;
    }
  
  close(newmail);
  unlink(xtmpfile);
  exit(status);
}



int
get_message()
{
  int  nchar;        

  sprintf(xtmpfile, "/tmp/tpop.%d", getpid());
  if((newmail = open(xtmpfile, O_RDWR|O_CREAT, 0600)) == -1) 
    {
      fprintf(stderr, "unable to open temporary file,  \"%s\".\n",  xtmpfile);
      return(-1);
    }
    
  while(nchar = read(0, buffer, sizeof(buffer)))
    write(newmail, buffer, nchar);  

  return(0);
}



int
open_drop(name)
     char *name;
{
  char dropfile[512];             

  sprintf(dropfile, "%s/%s", POP_MAILDIR, name);
  if ((maildrop = open(dropfile, O_RDWR|O_CREAT,0600)) == -1)  
    {
      fprintf(stderr, "unable to open \"%s\": %s.\n", 
	      dropfile, (errno < sys_nerr) ? sys_errlist[errno] : "");
      return(-1);
    }
  
  /*  Lock the user's real mail drop */
  
  if (flock(maildrop, LOCK_EX) == -1) 
    {
      fprintf(stderr, 
	   "unable to lock \"%s\" (service unavailable... the sequel): %s.\n", 
	      dropfile, (errno < sys_nerr) ? 
	      sys_errlist[errno] : "");
      return(-1);
    }

  lseek(maildrop, 0, L_XTND);
  return(0);
}



new_message(to, from)
     int to, from;
{
  int cc;
  
  cc = time(0);
  sprintf(buffer, "From popper %s\n", ctime(&cc)); 
  write(to, buffer, strlen(buffer) * sizeof(char));
  while(cc = read(from, buffer, sizeof(buffer)))
    write(to, buffer, cc);
  write(to, "\n", 1);
}
      


void
notify_recipient(name)
     char *name;
     
{
  char *message = "You have new mail";

#ifdef ZEPHYR
  static int init = 0;
  ZNotice_t notice;             /* Zephyr notice */
  int    ret;                   /* return value, length */
  
  if(init)
    if ((ret = ZInitialize()) == ZERR_NONE)
      init = 0;

  bzero(&notice, sizeof(notice));
  notice.z_kind = UNSAFE;
  notice.z_class          = "message";
  notice.z_class_inst     = "pop";
  notice.z_recipient      = name;

  zsend(&notice, &message, 1, 1);
#endif ZEPHYR
}



#ifdef ZEPHYR
zsend(notice, items, nitems, auth)
     ZNotice_t *notice;
     char **items;
     int nitems;
     int auth;
{
  int ret;

  if ((ret = ZSendList(notice, items, nitems, auth ? ZAUTH:ZNOAUTH)) !=
      ZERR_NONE)
    { /* syslog */
      if(auth) 
        return(zsend(notice, items, nitems, 0));
      else
        return(-1);
    }
}

#endif ZEPHYR