-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathBBBikeMail.pm
258 lines (234 loc) · 6.94 KB
/
BBBikeMail.pm
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# -*- perl -*-
#
# Author: Slaven Rezic
#
# Copyright (C) 1998,2000,2003,2013 Slaven Rezic. All rights reserved.
# This package is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
#
# Mail: [email protected]
# WWW: http://bbbike.de
#
package BBBikeMail;
use strict;
use vars qw($top @popup_style
$can_send_mail $can_send_mail_via_Mail_Mailer
$cannot_send_mail_via_Mail_Mailer
$cannot_send_mail_reason);
*top = \$main::top;
*redisplay_top = \&main::redisplay_top;
*status_message = \&main::status_message;
sub enter_send_mail {
enter_send_anything('mail', @_);
}
sub enter_send_anything {
my($type, $subject, %args) = @_;
my $data = $args{-data};
my $to = $args{-to};
my $typename = ($type eq 'mail' ? 'Mail' : die "unhandled type $type");
capabilities();
if ($type eq 'mail' && !$can_send_mail) {
my $reason = $cannot_send_mail_reason;
$top->messageBox
(-icon => "error",
-message => "Kann keine Mails versenden" .
(defined $reason && $reason ne '' ?
". Grund: $reason" : ""),
);
return;
}
if ($type eq 'mail' && $args{-to}) {
# Note that send_mail_via_browser does not always work very
# well if -to is missing.
if ($^O eq 'MSWin32' || !$can_send_mail_via_Mail_Mailer) {
send_mail_via_browser($args{-to}, $subject, $args{-data});
return;
}
}
my $t = redisplay_top($top, $type, -title => $typename);
return if !defined $t;
my $row = 0;
$t->Label(-text => "$typename an:")->grid(-row => $row,
-column => 0,
-sticky => "e");
my $e;
if ($type eq 'mail') {
my $mail_alias;
eval {
require Mail::Alias;
require Tk::BrowseEntry;
$mail_alias = new Mail::Alias::Ucbmail;
$mail_alias->read("$ENV{HOME}/.mailrc");
};
if (!$@ && $mail_alias) {
$e = $t->BrowseEntry(-textvariable => \$to);
my @alias;
while(my($k,$v) = each %$mail_alias) {
foreach (@$v) {
push @alias, @{ $mail_alias->expand($_) }
}
}
$e->insert("end", sort { lc($a) cmp lc($b) } @alias);
}
}
if (!$e) {
$e = $t->Entry(-textvariable => \$to);
}
$e->grid(-row => $row, -column => 1, -sticky => "w");
$e->tabFocus;
$row++;
my $comment_txt;
if ($type eq 'mail') {
$t->Label(-text => "Subject")->grid(-row => $row,
-column => 0,
-sticky => "e");
$t->Entry(-textvariable => \$subject)->grid(-row => $row,
-column => 1,
-sticky => "w");
$row++;
$t->Label(-text => "zusätzlicher Text:")->grid(-row => $row,
-column => 0,
-sticky => "ne");
$comment_txt = $t->Scrolled('Text', -scrollbars => "osoe",
-width => 40,
-height => 5,
)->grid(-row => $row,
-column => 1);
}
my $close_window = sub { $t->destroy;};
my $apply_window = sub {
if ($type eq 'mail') {
if (defined $to && $to ne '' &&
defined $subject && $subject ne '') {
if ($comment_txt) {
$data = $data . "\n" . $comment_txt->get("1.0", "end");
}
send_mail($to, $subject, $data);
return 1; # usually cannot check send_mail success
} else {
main::status_message("Bitte Empfänger und Subject angeben!", "err");
return 0;
}
} else {
die "Handling type $type NYI";
}
};
my $ok_window = sub {
my $success = &$apply_window;
&$close_window if $success;
};
$row++;
my $bf = $t->Frame->grid(-row => $row, -column => 0,
-columnspan => 2);
my $okb = $bf->Button
(Name => 'ok',
-command => $ok_window)->grid(-row => 0, -column => 0,
-sticky => 'ew');
if ($Tk::VERSION >= 800) {
$okb->configure(-default => "disabled");
}
my $cb = $bf->Button
(Name => 'cancel',
-command => $close_window)->grid(-row => 0, -column => 1,
-sticky => 'ew');
## No! Otherwise it's not possible to use <Return> in the
## text field!
#$t->bind('<Return>' => sub { $okb->invoke });
$t->bind('<Escape>' => sub { $cb->invoke });
$t->Popup(@popup_style);
}
sub send_mail {
my(@args) = @_;
capabilities();
if ($^O eq 'MSWin32' || !$can_send_mail_via_Mail_Mailer) {
send_mail_via_browser(@args);
} else {
send_mail_via_Mail_Mailer(@args);
}
}
sub send_mail_via_Mail_Mailer {
my($to, $subject, $data, %args) = @_;
my $cc = delete $args{CC};
warn "Extra arguments: " . join(" ", %args) if %args;
eval {
require Mail::Send;
require Mail::Mailer;
Mail::Mailer->VERSION(1.53);
my $msg = new Mail::Send Subject => $subject, To => $to;
$msg->add("MIME-Version", "1.0");
$msg->add("Content-Type", "text/plain; charset=ISO-8859-1");
$msg->add("Content-Transfer-Enconding", "8bit");
$msg->add("CC", $cc) if $cc;
$msg->add("Comments", "generated by BBBikeMail.pm BBBike/$BBBike::VERSION"
. " (OS=$^O)"
);
my $fh = $msg->open;
print $fh $data;
$fh->close;
};
if ($@) {
$top->bell;
status_message("Fehler: $@\nMöglicherweise ist kein Mailprogramm vorhanden.\nFür das Versenden von Mails ist das Modul Mail::Send erforderlich.\n", 'error');
}
}
sub send_mail_via_browser {
my($to, $subject, $data, %args) = @_;
my $url = create_mailto_url($to, $subject, $data, %args);
require WWWBrowser;
$main::devel_host = $main::devel_host if 0; # cease -w
if ($main::devel_host) {
warn "Sende URL <$url> zum Browser...\n";
}
WWWBrowser::start_browser($url);
}
sub create_mailto_url {
# Tested with linux-mozilla 1.7 and FreeBSD's seamonkey 1.0.5
# On modern systems data should probably be utf8-encoded
# (checked with Mozilla Thunderbird on Windows)
my($to, $subject, $data, %args) = @_;
for ($to, $subject, $data, $args{CC}) {
if ($_) {
require Encode;
$_ = Encode::encode("utf-8", $_);
}
}
require CGI;
CGI->import('-oldstyle_urls');
my $url = "mailto:";
$url .= $to if defined $to && $to !~ m{^\s*$};
$url .= "?" . CGI->new({subject=>$subject,
body=>$data,
($args{CC} ? (cc=>$args{CC}) : ()),
})->query_string;
$url;
}
sub capabilities {
if ($^O eq 'MSWin32') {
# XXX Sending mail is currently disabled completely on
# windows. Reason: the method creating a mailto URL and using
# it with WWWBrowser::start_browser only works for short
# content (less than 2000 bytes). This is usually too less for
# route texts. An alternative is not yet found.
$can_send_mail = 0;
# Shortcut early, don't try Mail::Send or Mail::Mailer.
# Usually this does not work out-of-the-box, as the smtp host
# needs to be configured somewhere.
return;
} else {
$can_send_mail = 1; # via browser
}
eval {
die "Specified to not use Mail::Mailer via variable \$cannot_send_mail_via_Mail_Mailer"
if $cannot_send_mail_via_Mail_Mailer;
require Mail::Send;
require Mail::Mailer;
Mail::Mailer->VERSION(1.53); # previous versions were unreliable
$can_send_mail_via_Mail_Mailer = 1;
};
if (!$can_send_mail) {
$cannot_send_mail_reason = $@;
}
}
# peacify -w
$main::top = $main::top if 0;
1;