File: notify.c

package info (click to toggle)
jitterbug 1.6.2-4
  • links: PTS
  • area: main
  • in suites: potato
  • size: 572 kB
  • ctags: 403
  • sloc: ansic: 4,965; sh: 1,376; perl: 122; makefile: 49; awk: 38
file content (191 lines) | stat: -rw-r--r-- 4,905 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* 
   The Jitterbug report tracking system
   routines to handle notification of message changes

   Copyright (C) Andrew Tridgell 1997

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

/* NOTE: This module contains routines that are called from both within
   the chrooted environment and outside it. Be careful! */

#include "jitterbug.h"

/* clean a notify list - removing the current user if listed. This
   prevents users getting email notification of their own actions */
static char *clean_notify(char *list)
{
	extern char *user;
	char *list2, *tok;

	if (!list || !*list || !user || !*user) return list;

	list2 = strdup(list);
	*list2 = 0;

	for (tok=strtok(list," ,\t"); tok; tok=strtok(NULL," ,\t")) {
		if (strcasecmp(user, tok) == 0) continue;
		if (*list2) strcat(list2,",");
		strcat(list2, tok);
	}	

	return list2;
}

/* return the current .notify contents for a directory or "" if none 
   called from within the cgi program
*/
char *dir_notification(char *directory)
{
	char fname[1000];
	char *ret;

	check_overflow(strlen(root_directory()) + strlen(directory) + 20, sizeof(fname));
	sprintf(fname, "%s/%s/.notify", root_directory(), directory);
	ret = load_file(fname, NULL, 0);
	if (!ret) ret = "";
	return ret;
}


/* return the current .notify contents for a message or "" if none 
   must be called with the current working directory set to the directory
   of the message
*/
char *msg_notification(char *id)
{
	char fname[1000];
	char *ret;

	check_overflow(strlen(id) + 10, sizeof(fname));
	sprintf(fname, "%s.notify", id);
	ret = load_file(fname, NULL, 0);
	if (!ret) ret = "";
	return ret;
}


/* give a summary of the message */
static void notify_summary(int fd, char *id)
{
	struct message_info info;
	char *notes;

	memset(&info, 0, sizeof(info));

	if (!get_info(id, &info, 8192)) return;

	smtp_write(fd, "\nMessage summary for %s%s\n", lp_pr_identifier(), id);
	if (info.from)
		smtp_write(fd,"\tFrom: %s\n", unquote(info.from));
	if (info.subject)
		smtp_write(fd,"\tSubject: %s\n", unquote(info.subject));
	if (info.date)
		smtp_write(fd,"\tDate: %s\n", unquote(info.date));
	smtp_write(fd,"\t%d replies \t%d followups\n", 
		   count_replies(id,NULL),
		   count_followups(id,NULL));

	if (lp_base_url()) {
		smtp_write(fd,"\tURL: %s?findid=%s\n", lp_base_url(), id);
	}

	notes = load_ext(id, "notes");
	if (notes)
		smtp_write(fd,"\tNotes: %s\n\n", unquote(notes));

	smtp_write(fd,"\n====> ORIGINAL MESSAGE FOLLOWS <====\n\n");

	smtp_write_data(fd, info.raw);

	if (strlen(info.raw) < info.size) {
		smtp_write(fd,"\n====> MESSAGE TRUNCATED AT %d <====\n\n",
			   strlen(info.raw));
	}
	
}

/* notify the notification list about changes to a directory */
void notify_dir(char *directory,char *id, char *fmt, ...)
{
	va_list ap;
	char buf[1000];
	int fd;
	char *notification = dir_notification(directory);

	notification = clean_notify(notification);

	if (! *notification) {
		/* no notification list */
		return;
	}

	check_overflow(strlen(directory)+strlen(id)+30, sizeof(buf));
	sprintf(buf,"Notification: %s/%s", directory, id);

	fd = smtp_start_mail(lp_from_address(), notification, NULL, NULL, 
			     buf, 5000);
	if (fd == -1) return;

	smtp_write(fd,"\n%s notification\n\n", lp_title());
	va_start(ap, fmt);
	vsprintf(buf,fmt,ap);
	va_end(ap);
	smtp_write_data(fd, buf);

	notify_summary(fd, id);

	smtp_end_mail(fd);
}

/* notify the notification list about changes to a message 
   This must be called with the current working directory
   set to the directory containing the message
*/
void notify_msg(char *id,char *fmt, ...)
{
	va_list ap;
	char buf[1000];
	int fd;
	char *notification = msg_notification(id);

	notification = clean_notify(notification);

	if (! *notification) {
		/* no notification list */
		return;
	}

	check_overflow(strlen(id) + 30, sizeof(buf));
	sprintf(buf,"Notification: %s%s", lp_pr_identifier(), id);

	fd = smtp_start_mail(lp_from_address(), notification, NULL, NULL, 
			     buf, 5000);
	if (fd == -1) return;

	smtp_write(fd,"X-Notification: %s\n", lp_from_address());

	smtp_write(fd,"\n%s notification\n\n", lp_title());
	va_start(ap, fmt);
	vsprintf(buf,fmt,ap);
	va_end(ap);
	smtp_write_data(fd, buf);

	notify_summary(fd, id);

	smtp_end_mail(fd);
}