summaryrefslogtreecommitdiff
path: root/scripts/mail.pl
blob: 23f99c01e0f1c6fd27843fcb6b726d6baf1a6894 (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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
use strict;
use vars qw($VERSION %IRSSI);
$VERSION = "2.92";
%IRSSI = (
    authors     => "Timo Sirainen, Matti Hiljanen, Joost Vunderink, Bart Matthaei",
    contact     => "tss\@iki.fi, matti\@hiljanen.com, joost\@carnique.nl, bart\@dreamflow.nl",
    name        => "mail",
    description => "Fully customizable mail counter statusbar item with multiple mailbox and multiple Maildir support",
    sbitems     => "mail",
    license     => "Public Domain",
    url         => "http://irssi.org, http://scripts.irssi.de",
);

# Mail counter statusbar item
# for irssi 0.8.1 by Timo Sirainen
#
# Maildir support added by Matti Hiljanen
# Multiple Maildir/mbox and customization support added by Joost Vunderink
# OLD mailtreatment switch added by Bart Matthaei.
# Improved some regexps in maildirmode by Bart Matthaei.
# Maildirmode regexps (hopefully) fixed for good by Matti Hiljanen. 
#
# You can add any number of mailboxes or Maildirs to watch for new mail in.
# Give them any name and <name>:<count> will appear in your mail
# statusbar item, where <count> is the number of unread messages.
# If only 1 mailbox/Maildir is defined, the statusbar item will have the 
# familiar form [Mail: <count>].
# If you set mail_show_message to ON, irssi will print a message in the
# active window whenever new mail arrives.
#
# Check /mailbox help for help.

use Irssi::TextUI;
use Irssi;

my $maildirmode = 0; # maildir=1, file(spools)=0
my $old_is_not_new = 0; 
my $extprog;
my ($last_refresh_time, $refresh_tag);

# for mbox caching
my ($last_size, $last_mtime, $last_mailcount, $last_mode);

# list of mailboxes
my %mailboxes = (); 
my %new_mails_in_box = ();
my $nummailboxes = 0; 

# the string to be stored in Irssi's mail_mailboxes setting
my $mailboxsetting = "";

sub cmd_print_help {
  Irssi::print(
  "MAILBOX ADD <num> <file|dir>\n".
  "MAILBOX DEL <num>\n".
  "MAILBOX SHOW\n\n".
  "Statusbar item to keep track of how many (new) emails there are in ".
  "each of your mailboxes/Maildirs.\n\n".
  "/MAILBOX ADD <name> <file|dir>\n".
  "    - Adds a mailbox or a Maildir to the list.\n".
  "/MAILBOX DEL <name>\n".
  "    - Removes mailbox or Maildir named <name> from the list.\n".
  "/MAILBOX SHOW\n".
  "    - Shows a list of the defined mailboxes.\n\n".
  "Use the following commands to change the behaviour:\n\n".
  "/SET MAILDIRMODE on|off\n".
  "    - If maildirmode is on, the mailboxes in the list are assumed to be ".
  "directories. Otherwise they are assumed to be spool files.\n".
  "      Default: off.\n".
  "/SET MAIL_OLDNOTNEW on|off\n".
  "    - If switched on, mail marked als \"OLD\" will not be treated as new.\n".
  "      Default: off.\n".
  "/SET MAIL_EXT_PROGRAM <prog>\n".
  "    - <prog> will be used to check for mail.\n".
  "/SET MAIL_REFRESH_TIME <num>\n".
  "    - Sets the time between checks to <num> seconds.\n      Default: 60.\n".
  "/SET MAIL_SHOW_MESSAGE on|off\n".
  "    - If this is on, a message will be printed in the active window ".
  "whenever new email is received.\n      Default: off.\n".
  "/SET MAIL_SHOW_ONLY_UNREAD on|off\n".
  "    - If you don't want to see a mailbox if it does not contain any new ".
  "mail, set this to on.\n      Default: on.\n" .
  "/SET MAIL_SEPARATOR <char>\n".
  "    - Sets the character to be printed between each mailbox.\n".
  "      The default is a comma.\n".
  "/SET MAIL_FORMAT <format>\n".
  "    - Sets the format of each mailbox.\n".
  "      Allowed variables:\n".
  "      %%n = mailbox name\n".
  "      %%u = number of unread mail\n".
  "      %%r = number of read mail\n".
  "      %%t = total amount of mail\n".
  "      The default format is %%n:%%u/%%t.\n".
  "\nSee also: STATUSBAR"
  ,MSGLEVEL_CRAP);
}

sub mbox_count {
  my $mailfile = shift;
  my $unread = 0;
  my $read = 0;
  my $maildirmode=Irssi::settings_get_bool('maildir_mode');
  my $old_is_not_new=Irssi::settings_get_bool('mail_oldnotnew');

  if ($extprog ne "") {
     my $total = `$extprog`;
     chomp $total;
     ($read, $unread) = split ' ', $total, 2;
  } else {
    if (!$maildirmode) {
      if (-f $mailfile) {
        my @stat = stat($mailfile);
	my $size = $stat[7];
	my $mtime = $stat[9];

	# if the file hasn't changed, get the count from cache
	return $last_mailcount if ($last_size == $size && $last_mtime == $mtime);
	$last_size = $size;
	$last_mtime = $mtime;

	return 0 if (!open(my $f, "<", $mailfile));

	# count new mails only
	my $internal_removed = 0;
	while (<$f>) {
	  $unread++ if (/^From /);

	  if(!$old_is_not_new) {
	  	$unread-- if (/^Status: R/);
	  } else {
	  	$unread-- if (/^Status: [OR]/);
	  }

	  $read++ if (/^From /);

	  # Remove folder internal data, but only once
	  if (/^Subject: .*FOLDER INTERNAL DATA/) {
	    if ($internal_removed == 0) {
	      $internal_removed = 1;
	      $read--;
	      $unread--;
	    }
	  }
	}
	close($f);
      }
    } else {
      opendir(DIR, "$mailfile/cur") or return 0;
      while (defined(my $file = readdir(DIR))) {
        next if $file =~ /^(.|..)$/;
        # Maildir flags: http://cr.yp.to/proto/maildir.html
        # My old regexps were useless if the MUA added any 
        # non-default flags -qvr
        # 
        # deleted mail
        next if $file =~ /\:.*?T.*?$/;
	    if($old_is_not_new) {
           # when mail gets moved from new to cur it's name _always_
           # changes from uniq to uniq:info, even when it's still not
           # read. I assume "old mail" means mail which hasn't been read
           # yet but it has been "acknowledged" by the user. (it's been
           # moved to cur) -qvr
           if ($file =~ /\:.*?$/) {
              $read++;
      		  next;
           }
        } else {
           if ($file =~ /\:.*?S.*?$/) {
              $read++;
      		  next;
           }
        }
        $unread++;
      }
      closedir(DIR);

      opendir(DIR, "$mailfile/new") or return 0;
      while (defined(my $file = readdir(DIR))) {
        next if $file =~ /^(.|..)$/;
        $unread++;
      }
      closedir(DIR);
    }
  }

  if ($unread eq "" || $unread < 0) {
    $unread = 0;
  }
  if ($read eq "" || $read < 0) {
    $read = 0;
  }

  $last_mailcount = $unread;

  return ($unread, $read);
}

# Checks for mail and sets the statusbar item to the right string.
# Also shows a message in the active window if that setting is set.
sub mail {
  my ($item, $get_size_only) = @_;

  my $result;
  my $format = Irssi::settings_get_str('mail_format');
  my $unread = 0;
  my $read = 0;
  my $total = 0;

  # check all mailboxes for new email
  foreach my $name (keys(%mailboxes)) {
    my $box = $mailboxes{$name};
    # replace "~/" at the beginning by the user's home dir
    $box =~ s/^~\//$ENV{'HOME'}\//;

    ($unread, $read) = mbox_count($box);
    $unread = "0" if ($unread eq "");
    $read = "0" if ($read eq "");
    $total = $unread + $read;
    $total = "0" if ($total eq "");

    next if (Irssi::settings_get_bool('mail_show_only_unread') && $unread == 0);

    if ($total eq "") { $total = 0; }
    if (length($result) > 0) {
      $result .= Irssi::settings_get_str('mail_separator');
    }
    my $string = $format;
    $string =~ s/%n/$name/;
    $string =~ s/%u/$unread/;
    $string =~ s/%r/$read/;
    $string =~ s/%t/$total/;
    $result .= $string;
    
    # Show -!- You have <num> new messages in <name>.
    # Show this only if there are any new, unread messages.
    if (Irssi::settings_get_bool('mail_show_message') &&
        $unread > $new_mails_in_box{$name}) {
      my $new_mails = $unread - $new_mails_in_box{$name};
      if ($nummailboxes == 1) {
        Irssi::print("You have $new_mails new message" . ($new_mails != 1 ? "s." : "."), MSGLEVEL_CRAP);
      } else {
        Irssi::print("You have $new_mails new message" . ($new_mails != 1 ? "s " : " ") . "in $name.", MSGLEVEL_CRAP);
      }
    }

    $new_mails_in_box{$name} = $unread;
  }
  
  if (length($result) == 0) {
    # no mail - don't print the [Mail: ] at all
    if ($get_size_only) {
      $item->{min_size} = $item->{max_size} = 0;
    }
  } else {
    $item->default_handler($get_size_only, undef, $result, 1);
  }
}

sub refresh_mail {
  Irssi::statusbar_items_redraw('mail');
}

# Adds the mailboxes from a string. Only to be used during startup.
sub add_mailboxes {
  my $boxstring = $_[0];
  my @boxes = split(/,/, $boxstring);

  foreach my $dbox(@boxes) {
    my $name = substr($dbox, 0, index($dbox, '='));
    my $box = substr($dbox, index($dbox, '=') + 1, length($dbox));
    addmailbox($name, $box);
  }
}

sub addmailbox {
  my ($name, $box) = @_;

  if (exists($mailboxes{$name})) {
    if ($box eq $mailboxes{$name}) {
      Irssi::print("Mailbox $name already set to $box", MSGLEVEL_CRAP);
    } else {
      Irssi::print("Mailbox $name changed to $box", MSGLEVEL_CRAP);
      $new_mails_in_box{$name} = 0;
    }
  } else {
    Irssi::print("Mailbox $name added: " . $box, MSGLEVEL_CRAP);
    $new_mails_in_box{$name} = 0;
    $nummailboxes++;
  }
  $mailboxes{$name} = $box;
}

sub delmailbox {
  my $name = $_[0];

  if (exists($mailboxes{$name})) {
    Irssi::print("Mailbox $name removed", MSGLEVEL_CRAP);
    delete($mailboxes{$name});
    delete($new_mails_in_box{$name});
    $nummailboxes--;
  } else {
    Irssi::print("No such mailbox $name. Use /mailbox show to see a list.", MSGLEVEL_CRAP);
  }
}

sub update_settings_string {
  my $setting;

  foreach my $name (keys(%mailboxes)) {
    $setting .= $name . "=" . $mailboxes{$name} . ",";
  }

  Irssi::settings_set_str("mail_mailboxes", $setting);
}

sub cmd_addmailbox {
  my ($name, $box) = split(/ +/, $_[0]);

  if ($name eq "" || $box eq "") {
    Irssi::print("Use /mailbox add <name> <mailbox> to add a mailbox.", MSGLEVEL_CRAP);
    return;
  }

  addmailbox($name, $box);
  update_settings_string();
  refresh_mail();
}

sub cmd_delmailbox {
  my $name = $_[0];

  if ($name eq "") {
    Irssi::print("Use /mailbox del <name> to delete a mailbox.", MSGLEVEL_CRAP);
    return;
  }

  delmailbox($name);
  update_settings_string();
  refresh_mail();
}

sub cmd_showmailboxes {
  if ($nummailboxes == 0) {
    Irssi::print("No mailboxes defined.", MSGLEVEL_CRAP);
    return;
  }
  Irssi::print("Mailboxes:", MSGLEVEL_CRAP);
  foreach my $box (keys(%mailboxes)) {
    Irssi::print("$box: " . $mailboxes{$box}, MSGLEVEL_CRAP);
  }
}

sub cmd_mailboxes {
  my ($data, $server, $item) = @_;
  if ($data =~ m/^[(show)|(add)|(del)]/i ) {
    Irssi::command_runsub ('mailbox', $data, $server, $item);
  }
  else {
    Irssi::print("Use /mailbox (show|add|del).")
  }
}

sub init_mailboxes {
  # Add the mailboxes at startup of the script
  my $boxes = Irssi::settings_get_str('mail_mailboxes');
  if (length($boxes) > 0) {
    add_mailboxes($boxes);
  }
}

sub read_settings {
  $extprog = Irssi::settings_get_str('mail_ext_program');
  my $time = Irssi::settings_get_int('mail_refresh_time');
  my $mode = Irssi::settings_get_bool('maildir_mode');
  unless ($time == $last_refresh_time) {
     $last_refresh_time = $time;
     Irssi::timeout_remove($refresh_tag) if ($refresh_tag);
     $refresh_tag = Irssi::timeout_add($time*1000, 'refresh_mail', undef);
  }
  return if ($mode == $last_mode);
  $last_mode = $mode;
  refresh_mail;
}


if (!$maildirmode) {
  my $default = "1=" . $ENV{'MAIL'} . ",";
  Irssi::settings_add_str('misc', 'mail_mailboxes', $default);
} else {
  my $default = "1=~/Maildir/,";
  Irssi::settings_add_str('misc', 'mail_mailboxes', $default);
}

Irssi::command_bind('mailbox show', 'cmd_showmailboxes');
Irssi::command_bind('mailbox add', 'cmd_addmailbox');
Irssi::command_bind('mailbox del', 'cmd_delmailbox');
Irssi::command_bind('mailbox help', 'cmd_print_help');
Irssi::command_bind('mailbox', 'cmd_mailboxes');

Irssi::settings_add_str('misc', 'mail_ext_program', '');
Irssi::settings_add_int('misc', 'mail_refresh_time', 60);
Irssi::settings_add_bool('misc', 'maildir_mode', "$maildirmode");
Irssi::settings_add_bool('misc', 'mail_oldnotnew', "$old_is_not_new");
Irssi::settings_add_str('misc', 'mail_separator', ",");
Irssi::settings_add_bool('misc', 'mail_show_message', "0");
Irssi::settings_add_str('misc', 'mail_format', '%n:%u/%t');
Irssi::settings_add_bool('misc', 'mail_show_only_unread', "1");

Irssi::statusbar_item_register('mail', '{sb Mail: $0-}', 'mail');

read_settings();
init_mailboxes();
Irssi::signal_add('setup changed', 'read_settings');
refresh_mail();

# EOF