1: # The LearningOnline Network with CAPA
2: #
3: # Routines for messaging
4: #
5: # (Routines to control the menu
6: #
7: # (TeX Conversion Module
8: #
9: # 05/29/00,05/30 Gerd Kortemeyer)
10: #
11: # 10/05 Gerd Kortemeyer)
12: #
13: # 10/19,10/20,10/30,
14: # 02/06/01 Gerd Kortemeyer
15: # 07/27 Guy Albertelli
16: # 07/27,07/28,07/30,08/03 Gerd Kortemeyer
17:
18: package Apache::lonmsg;
19:
20: use strict;
21: use Apache::lonnet();
22: use vars qw($msgcount);
23: use HTML::TokeParser;
24: use Apache::Constants qw(:common);
25:
26: # ===================================================================== Package
27:
28: sub packagemsg {
29: my ($subject,$message,$citation)=@_;
30: $message=~s/\</\<\;/g;
31: $message=~s/\>/\>\;/g;
32: $citation=~s/\</\<\;/g;
33: $citation=~s/\>/\>\;/g;
34: $subject=~s/\</\<\;/g;
35: $subject=~s/\>/\>\;/g;
36: my $now=time;
37: $msgcount++;
38: my $partsubj=$subject;
39: $partsubj=&Apache::lonnet::escape($partsubj);
40: $partsubj=substr($partsubj,0,50);
41: my $msgid=&Apache::lonnet::escape(
42: $now.':'.$partsubj.':'.$ENV{'user.name'}.':'.
43: $ENV{'user.domain'}.':'.$msgcount.':'.$$);
44: return $msgid,
45: '<sendername>'.$ENV{'user.name'}.'</sendername>'.
46: '<senderdomain>'.$ENV{'user.domain'}.'</senderdomain>'.
47: '<subject>'.$subject.'</subject>'.
48: '<time>'.localtime($now).'</time>'.
49: '<servername>'.$ENV{'SERVER_NAME'}.'</servername>'.
50: '<host>'.$ENV{'HTTP_HOST'}.'</host>'.
51: '<client>'.$ENV{'REMOTE_ADDR'}.'</client>'.
52: '<browsertype>'.$ENV{'browser.type'}.'</browsertype>'.
53: '<browseros>'.$ENV{'browser.os'}.'</browseros>'.
54: '<browserversion>'.$ENV{'browser.version'}.'</browserversion>'.
55: '<browsermathml>'.$ENV{'browser.mathml'}.'</browsermathml>'.
56: '<browserraw>'.$ENV{'HTTP_USER_AGENT'}.'</browserraw>'.
57: '<courseid>'.$ENV{'request.course.id'}.'</courseid>'.
58: '<role>'.$ENV{'request.role'}.'</role>'.
59: '<resource>'.$ENV{'request.filename'}.'</resource>'.
60: '<msgid>'.$msgid.'</msgid>'.
61: '<message>'.$message.'</message>'.
62: '<citation>'.$citation.'</citation>';
63: }
64:
65: # ================================================== Unpack message into a hash
66:
67: sub unpackagemsg {
68: my $message=shift;
69: my %content=();
70: my $parser=HTML::TokeParser->new(\$message);
71: my $token;
72: while ($token=$parser->get_token) {
73: if ($token->[0] eq 'S') {
74: my $entry=$token->[1];
75: my $value=$parser->get_text('/'.$entry);
76: $content{$entry}=$value;
77: }
78: }
79: return %content;
80: }
81:
82: # ======================================================= Get info out of msgid
83:
84: sub unpackmsgid {
85: my $msgid=&Apache::lonnet::unescape(shift);
86: my ($sendtime,$shortsubj,$fromname,$fromdomain)=split(/\:/,
87: &Apache::lonnet::unescape($msgid));
88: my %status=&Apache::lonnet::get('email_status',[$msgid]);
89: if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; }
90: unless ($status{$msgid}) { $status{$msgid}='new'; }
91: return ($sendtime,$shortsubj,$fromname,$fromdomain,$status{$msgid});
92: }
93:
94: # =============================== Automated message to the author of a resource
95:
96: sub author_res_msg {
97: my ($filename,$message)=@_;
98: unless ($message) { return 'empty'; }
99: $filename=&Apache::lonnet::declutter($filename);
100: my ($domain,$author,@dummy)=split(/\//,$filename);
101: my $homeserver=&Apache::lonnet::homeserver($author,$domain);
102: if ($homeserver ne 'no_host') {
103: my $id=unpack("%32C*",$message);
104: my $msgid;
105: ($msgid,$message)=&packagemsg($filename,$message);
106: return &Apache::lonnet::reply('put:'.$domain.':'.$author.
107: ':nohist_res_msgs:'.
108: &Apache::lonnet::escape($filename.'_'.$id).'='.
109: &Apache::lonnet::escape($message),$homeserver);
110: }
111: return 'no_host';
112: }
113:
114: # ================================================== Critical message to a user
115:
116: sub user_crit_msg {
117: my ($user,$domain,$subject,$message)=@_;
118: # Check if allowed missing
119: my $status='';
120: my $msgid='undefined';
121: unless (($message)&&($user)&&($domain)) { $status='empty'; };
122: my $homeserver=&Apache::lonnet::homeserver($user,$domain);
123: if ($homeserver ne 'no_host') {
124: my $msgid;
125: ($msgid,$message)=&packagemsg($subject,$message);
126: $status=&Apache::lonnet::critical(
127: 'put:'.$domain.':'.$user.':critical:'.
128: &Apache::lonnet::escape($msgid).'='.
129: &Apache::lonnet::escape($message),$homeserver);
130: } else {
131: $status='no_host';
132: }
133: &Apache::lonnet::logthis(
134: 'Sending critical email '.$msgid.
135: ', log status: '.
136: &Apache::lonnet::log($ENV{'user.domain'},$ENV{'user.name'},
137: $ENV{'user.home'},
138: 'Sending critical '.$msgid.' to '.$user.' at '.$domain.' with status: '
139: .$status));
140: return $status;
141: }
142:
143: # =================================================== Critical message received
144:
145: sub user_crit_received {
146: my $msgid=shift;
147: my %message=&Apache::lonnet::get('critical',[$msgid]);
148: my %contents=&unpackagemsg($message{$msgid});
149: my $status='rec: '.
150: &user_normal_msg($contents{'sendername'},$contents{'senderdomain'},
151: 'Receipt: '.$ENV{'user.name'}.' at '.$ENV{'user.domain'},
152: 'User '.$ENV{'user.name'}.' at '.$ENV{'user.domain'}.
153: ' acknowledged receipt of message "'.
154: $contents{'subject'}.'" dated '.$contents{'time'}.".\n\n"
155: .'Message ID: '.$contents{'msgid'});
156: $status.=' trans: '.
157: &Apache::lonnet::put(
158: 'nohist_email',{$contents{'msgid'} => $message{$msgid}});
159: $status.=' del: '.
160: &Apache::lonnet::del('critical',[$contents{'msgid'}]);
161: &Apache::lonnet::log($ENV{'user.domain'},$ENV{'user.name'},
162: $ENV{'user.home'},'Received critical message '.
163: $contents{'msgid'}.
164: ', '.$status);
165: return $status;
166: }
167:
168: # ======================================================== Normal communication
169:
170: sub user_normal_msg {
171: my ($user,$domain,$subject,$message,$citation)=@_;
172: # Check if allowed missing
173: my $status='';
174: my $msgid='undefined';
175: unless (($message)&&($user)&&($domain)) { $status='empty'; };
176: my $homeserver=&Apache::lonnet::homeserver($user,$domain);
177: if ($homeserver ne 'no_host') {
178: my $msgid;
179: ($msgid,$message)=&packagemsg($subject,$message,$citation);
180: $status=&Apache::lonnet::critical(
181: 'put:'.$domain.':'.$user.':nohist_email:'.
182: &Apache::lonnet::escape($msgid).'='.
183: &Apache::lonnet::escape($message),$homeserver);
184: } else {
185: $status='no_host';
186: }
187: &Apache::lonnet::log($ENV{'user.domain'},$ENV{'user.name'},
188: $ENV{'user.home'},
189: 'Sending '.$msgid.' to '.$user.' at '.$domain.' with status: '.$status);
190: return $status;
191: }
192:
193: # =============================================================== Status Change
194:
195: sub statuschange {
196: my ($msgid,$newstatus)=@_;
197: my %status=&Apache::lonnet::get('email_status',[$msgid]);
198: if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; }
199: unless ($status{$msgid}) { $status{$msgid}='new'; }
200: unless (($status{$msgid} eq 'replied') ||
201: ($status{$msgid} eq 'forwarded')) {
202: &Apache::lonnet::put('email_status',{$msgid => $newstatus});
203: }
204: }
205: # ===================================================================== Handler
206:
207: sub discrit {
208: my $r=shift;
209: $r->print('<h1><font color=red>Critical Messages</font></h1>'.
210: '<form action=/adm/email method=post>'.
211: '<input type=hidden name=confirm value=true>');
212: my %what=&Apache::lonnet::dump('critical');
213: map {
214: my %content=&unpackagemsg($what{$_});
215: $content{'message'}=~s/\n/\<br\>/g;
216: $r->print('<hr>From: <b>'.$content{'sendername'}.'@'.
217: $content{'senderdomain'}.'</b> ('.$content{'time'}.
218: ')<br><blockquote>'.$content{'message'}.'</blockquote>'.
219: '<input type=submit name="rec_'.$_.'" value="Confirm Receipt">');
220: } sort keys %what;
221: $r->print('</form>');
222: }
223:
224: sub handler {
225: my $r=shift;
226:
227: # ----------------------------------------------------------- Set document type
228:
229: $r->content_type('text/html');
230: $r->send_http_header;
231:
232: return OK if $r->header_only;
233:
234: # --------------------------- Get query string for limited number of parameters
235:
236: map {
237: my ($name, $value) = split(/=/,$_);
238: $value =~ tr/+/ /;
239: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
240: if (($name eq 'display') || ($name eq 'replyto') ||
241: ($name eq 'forward') || ($name eq 'mark') ||
242: ($name eq 'sendreply') || ($name eq 'compose') ||
243: ($name eq 'sendmail') || ($name eq 'critical')) {
244: unless ($ENV{'form.'.$name}) {
245: $ENV{'form.'.$name}=$value;
246: }
247: }
248: } (split(/&/,$ENV{'QUERY_STRING'}));
249:
250: # --------------------------------------------------------------- Render Output
251:
252: $r->print('<html><head><title>EMail and Messaging</title></head>');
253: $r->print(
254: '<body bgcolor="#FFFFFF"><img align=right src=/adm/lonIcons/lonlogos.gif>');
255: $r->print('<h1>EMail</h1>');
256: if ($ENV{'form.display'}) {
257: my $msgid=$ENV{'form.display'};
258: &statuschange($msgid,'read');
259: my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
260: my %content=&unpackagemsg($message{$msgid});
261: $r->print('<b>Subject:</b> '.$content{'subject'}.
262: '<br><b>From:</b> '.$content{'sendername'}.' at '.
263: $content{'senderdomain'}.
264: '<br><b>Time:</b> '.$content{'time'}.'<hr>Functions: '.
265: '<a href="/adm/email?replyto='.&Apache::lonnet::escape($msgid).
266: '"><b>Reply</b></a><hr><pre>'.
267: $content{'message'}.'</pre><hr>'.$content{'citation'});
268: } elsif ($ENV{'form.replyto'}) {
269: my $msgid=$ENV{'form.replyto'};
270: my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
271: my %content=&unpackagemsg($message{$msgid});
272: my $quotemsg='> '.$content{'message'};
273: $quotemsg=~s/\r/\n/g;
274: $quotemsg=~s/\f/\n/g;
275: $quotemsg=~s/\n+/\n\> /g;
276: my $subject='Re: '.$content{'subject'};
277: my $dispcrit='';
278: if (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
279: $dispcrit=
280: '<input type=checkbox name=critmsg> Send as critical message<p>';
281: }
282: $r->print(<<"ENDREPLY");
283: <form action="/adm/email" method=post>
284: <input type=hidden name=sendreply value="$msgid">
285: Subject: <input type=text size=50 name=subject value="$subject"><p>
286: <textarea name=message cols=60 rows=10>
287: $quotemsg
288: </textarea><p>
289: $dispcrit
290: <input type=submit value="Send Reply">
291: </form>
292: ENDREPLY
293: } elsif ($ENV{'form.sendreply'}) {
294: my $msgid=$ENV{'form.sendreply'};
295: my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
296: my %content=&unpackagemsg($message{$msgid});
297: &statuschange($msgid,'replied');
298: if (($ENV{'form.critmsg'}) &&
299: (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'}))) {
300: $r->print('Sending critical: '.
301: &user_crit_msg($content{'sendername'},
302: $content{'senderdomain'},
303: $ENV{'form.subject'},
304: $ENV{'form.message'}));
305: } else {
306: $r->print('Sending: '.&user_normal_msg($content{'sendername'},
307: $content{'senderdomain'},
308: $ENV{'form.subject'},
309: $ENV{'form.message'}));
310: }
311: } elsif ($ENV{'form.confirm'}) {
312: map {
313: if ($_=~/^form\.rec\_(.*)$/) {
314: $r->print('<b>Confirming Receipt:</b> '.
315: &user_crit_received($1).'<br>');
316: }
317: } keys %ENV;
318: &discrit($r);
319: } elsif ($ENV{'form.critical'}) {
320: &discrit($r);
321: } elsif ($ENV{'form.forward'}) {
322: } elsif ($ENV{'form.mark'}) {
323: } elsif ($ENV{'form.compose'}) {
324: my $dispcrit='';
325: if (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
326: $dispcrit=
327: '<input type=checkbox name=critmsg> Send as critical message<p>';
328: }
329: $r->print(<<"ENDCOMP");
330: <form action="/adm/email" method=post>
331: <input type=hidden name=sendmail value=on>
332: Subject: <input type=text size=50 name=subject value=""><p>
333: <textarea name=message cols=60 rows=10>
334: </textarea><p>
335: $dispcrit
336: <input type=submit value="Send Mail">
337: </form>
338: ENDCOMP
339: } elsif ($ENV{'form.sendmail'}) {
340: } else {
341: $r->print('<table border=2><tr><th> </th><th>Date</th>'.
342: '<th>Username</th><th>Domain</th><th>Subject</th><th>Status</th></tr>');
343: map {
344: my ($sendtime,$shortsubj,$fromname,$fromdomain,$status)=
345: &Apache::lonmsg::unpackmsgid($_);
346: if ($status eq 'new') {
347: $r->print('<tr bgcolor="#FFBB77">');
348: } elsif ($status eq 'read') {
349: $r->print('<tr bgcolor="#BBBB77">');
350: } elsif ($status eq 'replied') {
351: $r->print('<tr bgcolor="#AAAA88">');
352: } else {
353: $r->print('<tr bgcolor="#99BBBB">');
354: }
355: $r->print('<td><a href="/adm/email?display='.$_.
356: '">Open</a></td><td>'.localtime($sendtime).'</td><td>'.
357: $fromname.'</td><td>'.$fromdomain.'</td><td>'.
358: &Apache::lonnet::unescape($shortsubj).'</td><td>'.
359: $status.'</td></tr>');
360: } sort split(/\&/,&Apache::lonnet::reply('keys:'.
361: $ENV{'user.domain'}.':'.
362: $ENV{'user.name'}.':nohist_email',
363: $ENV{'user.home'}));
364: $r->print('</table></body></html>');
365:
366: }
367: $r->print('</body></html>');
368: return OK;
369:
370: }
371: # ================================================= Main program, reset counter
372:
373: sub BEGIN {
374: $msgcount=0;
375: }
376:
377: 1;
378: __END__
379:
380:
381:
382:
383:
384:
385:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>