1 21 22 package org.armedbear.j.mail; 23 24 import java.awt.AWTEvent ; 25 import java.awt.event.MouseEvent ; 26 import java.util.List ; 27 import org.armedbear.j.Buffer; 28 import org.armedbear.j.BufferIterator; 29 import org.armedbear.j.Constants; 30 import org.armedbear.j.Debug; 31 import org.armedbear.j.Directories; 32 import org.armedbear.j.Editor; 33 import org.armedbear.j.File; 34 import org.armedbear.j.Frame; 35 import org.armedbear.j.FastStringBuffer; 36 import org.armedbear.j.History; 37 import org.armedbear.j.IdleThread; 38 import org.armedbear.j.InputDialog; 39 import org.armedbear.j.Line; 40 import org.armedbear.j.Log; 41 import org.armedbear.j.MessageDialog; 42 import org.armedbear.j.PasswordDialog; 43 import org.armedbear.j.Position; 44 import org.armedbear.j.Property; 45 import org.armedbear.j.Sidebar; 46 import org.armedbear.j.SimpleEdit; 47 48 public final class MailCommands implements Constants 49 { 50 public static void inbox() 51 { 52 if (!Editor.isMailEnabled()) 53 return; 54 final Editor editor = Editor.currentEditor(); 55 String inbox = Editor.preferences().getStringProperty(Property.INBOX); 56 if (inbox != null) 57 openMailbox(editor, inbox); 58 else 59 openMailbox(editor); } 61 62 public static void openMailbox() 63 { 64 if (!Editor.isMailEnabled()) 65 return; 66 final Editor editor = Editor.currentEditor(); 67 openMailbox(editor); 68 } 69 70 public static void openMailbox(String args) 71 { 72 openMailbox(Editor.currentEditor(), args); 73 } 74 75 private static void openMailbox(Editor editor) 76 { 77 InputDialog d = new InputDialog(editor, "Mailbox:", "Open Mailbox", null); 78 d.setHistory(new History("openMailbox")); 79 editor.centerDialog(d); 80 d.show(); 81 String s = d.getInput(); 82 if (s == null || s.length() == 0) 83 return; 84 editor.repaintNow(); 85 openMailbox(editor, s); 86 } 87 88 public static void openMailbox(Editor editor, String input) 89 { 90 final MailboxURL url = MailboxURL.parse(input); 91 if (url != null) 92 openMailbox(editor, url); 93 else 94 MessageDialog.showMessageDialog("Invalid mailbox name", 95 "Open Mailbox"); 96 } 97 98 public static void openMailbox(Editor editor, MailboxURL url) 99 { 100 String limitPattern = url.getLimitPattern(); 101 Log.debug("limitPattern = |" + limitPattern + "|"); 102 MailboxFilter filter = null; 103 boolean badLimitPattern = false; 104 if (limitPattern != null) { 105 filter = MailboxFilter.getMailboxFilter(limitPattern); 106 if (filter == null) { 107 MessageDialog.showMessageDialog("Bad limit pattern", 108 "Open Mailbox"); 109 limitPattern = null; 110 badLimitPattern = true; 111 } 112 } 113 if (url instanceof ImapURL || url instanceof PopURL) { 114 Mailbox mb = getMailbox(editor, url); 115 if (mb != null) { 116 if (mb.isLoaded()) { 117 if (filter != null && mb.getLimitFilter() == null) { 118 mb.limit(filter); 119 mb.setLimitPattern(limitPattern); 120 } else if (!badLimitPattern && 121 mb == Editor.currentEditor().getBuffer()) { 122 mb.limit(filter); 123 mb.setLimitPattern(limitPattern); 124 } 125 } else { 126 mb.setLimitFilter(filter); 127 mb.setLimitPattern(limitPattern); 128 } 129 editor.makeNext(mb); 130 editor.switchToBuffer(mb); 131 FolderTreeModel.getDefaultModel().maybeAddNodeForFolder(url); 132 IdleThread idleThread = IdleThread.getInstance(); 134 if (idleThread != null) { 135 idleThread.maybeAddTask(CheckMailTask.getInstance()); 136 if (mb instanceof PopMailbox) 137 idleThread.maybeAddTask( 138 RewriteMailboxesTask.getInstance()); 139 } 140 } 141 } else { 142 Debug.assertTrue(url instanceof LocalMailboxURL); 144 final File file = ((LocalMailboxURL)url).getFile(); 145 Mailbox mb = null; 146 for (BufferIterator it = new BufferIterator(); it.hasNext();) { 147 Buffer buf = it.nextBuffer(); 148 if (buf instanceof LocalMailbox) { 149 if (((LocalMailbox)buf).getMailboxFile().equals(file)) { 150 mb = (LocalMailbox) buf; 151 break; 152 } 153 } else if (buf instanceof Drafts) { 154 if (((Drafts)buf).getDirectory().equals(file)) { 155 mb = (Drafts) buf; 156 break; 157 } 158 } 159 } 160 if (mb == null) { 161 if (file.equals(Directories.getDraftsFolder())) 163 mb = new Drafts((LocalMailboxURL)url); 164 else 165 mb = new LocalMailbox((LocalMailboxURL)url); 166 } 167 mb.setLimitFilter(filter); 168 mb.setLimitPattern(limitPattern); 169 editor.makeNext(mb); 170 editor.switchToBuffer(mb); 171 } 172 } 173 174 public static Buffer getMailboxBuffer(Editor editor, MailboxURL url) 175 { 176 return getMailbox(editor, url); 177 } 178 179 public static Mailbox getMailbox(Editor editor, MailboxURL url) 180 { 181 if (url instanceof ImapURL) { 182 for (BufferIterator it = new BufferIterator(); it.hasNext();) { 184 Buffer buf = it.nextBuffer(); 185 if (buf instanceof ImapMailbox) { 186 ImapMailbox mb = (ImapMailbox) buf; 187 if (mb.getUrl().equals(url)) 188 return mb; 189 } 190 } 191 final ImapURL imapUrl = (ImapURL) url; 192 ImapSession session = ImapSession.getSession(imapUrl); 193 if (session == null) { 194 String user = imapUrl.getUser(); 195 if (user == null) { 196 user = InputDialog.showInputDialog(editor, "Login:", 197 "Login on " + imapUrl.getHost()); 198 if (user == null || user.length() == 0) 199 return null; 200 session = ImapSession.getSession(imapUrl, user); 201 } 202 if (session == null) { 203 String password = PasswordDialog.showPasswordDialog(editor, 204 "Password:", "Password"); 205 if (password == null || password.length() == 0) 206 return null; 207 session = ImapSession.getSession(imapUrl, user, password); 208 } 209 } 210 if (session != null) 211 return new ImapMailbox(imapUrl, session); 212 } else if (url instanceof PopURL) { 213 for (BufferIterator it = new BufferIterator(); it.hasNext();) { 214 Buffer buf = it.nextBuffer(); 215 if (buf instanceof PopMailbox) { 216 PopMailbox mb = (PopMailbox) buf; 217 if (mb.getUrl().equals(url)) 218 return mb; 219 } 220 } 221 final PopURL popUrl = (PopURL) url; 222 PopSession session = PopSession.getSession(popUrl); 223 if (session == null) { 224 String user = popUrl.getUser(); 225 if (user == null) { 226 user = InputDialog.showInputDialog(editor, "Login:", 227 "Login on " + popUrl.getHost()); 228 if (user == null || user.length() == 0) 229 return null; 230 session = PopSession.getSession(popUrl, user); 231 } 232 if (session == null) { 233 String password = PasswordDialog.showPasswordDialog(editor, 234 "Password:", "Password"); 235 if (password == null || password.length() == 0) 236 return null; 237 session = PopSession.getSession(popUrl, user, password); 238 } 239 } 240 if (session != null) 241 return new PopMailbox(popUrl, session); 242 } 243 return null; 244 } 245 246 public static void compose() 247 { 248 if (!Editor.isMailEnabled()) 249 return; 250 activateMailCompositionBuffer(Editor.currentEditor(), 251 new SendMail()); 252 } 253 254 public static void ccGroup() 255 { 256 final Buffer buffer = Editor.currentEditor().getBuffer(); 257 if (buffer instanceof SendMail) 258 ((SendMail)buffer).ccGroup(); 259 } 260 261 public static void attachFile() 262 { 263 final Buffer buffer = Editor.currentEditor().getBuffer(); 264 if (buffer instanceof SendMail) 265 ((SendMail)buffer).attachFile(); 266 } 267 268 public static void send() 269 { 270 final Buffer buffer = Editor.currentEditor().getBuffer(); 271 if (buffer instanceof SendMail) 272 ((SendMail)buffer).send(); 273 } 274 275 public static void sendMailElectricColon() 276 { 277 final Editor editor = Editor.currentEditor(); 278 final Line dotLine = editor.getDotLine(); 279 final int dotOffset = editor.getDotOffset(); 280 if (editor.getModeId() != SEND_MAIL_MODE || editor.getMark() != null || 281 dotOffset != dotLine.length()) { 282 editor.insertNormalChar(':'); 283 return; 284 } 285 final SendMail sm = (SendMail) editor.getBuffer(); 286 if (sm.isHeaderLine(dotLine)) { 287 int i; 291 for (i = 0; i < dotOffset; i++) { 292 char c = dotLine.charAt(i); 293 if (c != ' ' && c != '\t') 294 break; 295 } 296 if (i == dotOffset) { 297 editor.insertNormalChar(':'); 300 return; 301 } 302 final int begin = i; 303 for (++i; i < dotOffset; i++) { 304 char c = dotLine.charAt(i); 305 if (c == ' ' || c == '\t') { 306 editor.insertNormalChar(':'); 308 return; 309 } 310 } 311 try { 312 sm.lockWrite(); 313 } 314 catch (InterruptedException e) { 315 Log.error(e); 316 return; 317 } 318 try { 319 editor.addUndo(SimpleEdit.LINE_EDIT); 320 dotLine.setText(dotLine.substring(begin).concat(":")); 321 sm.modified(); 322 } 323 finally { 324 sm.unlockWrite(); 325 } 326 editor.getDot().setOffset(dotLine.length()); 327 editor.moveCaretToDotCol(); 328 Editor.updateInAllEditors(dotLine); 329 } else 330 editor.insertNormalChar(':'); 331 } 332 333 public static void sendMailTab() 334 { 335 final Editor editor = Editor.currentEditor(); 336 final Buffer buffer = editor.getBuffer(); 337 if (buffer instanceof SendMail) 338 ((SendMail)buffer).tab(editor); 339 } 340 341 public static void sendMailBackTab() 342 { 343 final Editor editor = Editor.currentEditor(); 344 final Buffer buffer = editor.getBuffer(); 345 if (buffer instanceof SendMail) 346 ((SendMail)buffer).backTab(editor); 347 } 348 349 public static void messageMoveToFolder() 350 { 351 final Buffer buffer = Editor.currentEditor().getBuffer(); 352 if (buffer instanceof MessageBuffer) 353 ((MessageBuffer)buffer).moveMessage(); 354 } 355 356 public static void messageDelete() 357 { 358 final Buffer buffer = Editor.currentEditor().getBuffer(); 359 if (buffer instanceof MessageBuffer) 360 ((MessageBuffer)buffer).deleteMessage(); 361 } 362 363 public static void messageFlag() 364 { 365 final Buffer buffer = Editor.currentEditor().getBuffer(); 366 if (buffer instanceof MessageBuffer) 367 ((MessageBuffer)buffer).flagMessage(); 368 } 369 370 public static void messageNext() 371 { 372 final Buffer buffer = Editor.currentEditor().getBuffer(); 373 if (buffer instanceof NewsGroupMessageBuffer) 374 ((NewsGroupMessageBuffer)buffer).nextArticle(); 375 else if (buffer instanceof MessageBuffer) 376 ((MessageBuffer)buffer).nextMessage(); 377 } 378 379 public static void messagePrevious() 380 { 381 final Buffer buffer = Editor.currentEditor().getBuffer(); 382 if (buffer instanceof NewsGroupMessageBuffer) 383 ((NewsGroupMessageBuffer)buffer).previousArticle(); 384 else if (buffer instanceof MessageBuffer) 385 ((MessageBuffer)buffer).previousMessage(); 386 } 387 388 public static void messageNextInThread() 389 { 390 final Buffer buffer = Editor.currentEditor().getBuffer(); 391 if (buffer instanceof MessageBuffer) 392 ((MessageBuffer)buffer).nextInThread(); 393 } 394 395 public static void messagePreviousInThread() 396 { 397 final Buffer buffer = Editor.currentEditor().getBuffer(); 398 if (buffer instanceof MessageBuffer) 399 ((MessageBuffer)buffer).previousInThread(); 400 } 401 402 public static void messageParent() 403 { 404 final Buffer buffer = Editor.currentEditor().getBuffer(); 405 if (buffer instanceof MessageBuffer) 406 ((MessageBuffer)buffer).parentMessage(); 407 } 408 409 public static void messageForward() 410 { 411 final Editor editor = Editor.currentEditor(); 412 final Buffer buffer = editor.getBuffer(); 413 if (buffer instanceof MessageBuffer) { 414 MessageBuffer messageBuffer = (MessageBuffer) buffer; 415 SendMail sm = new SendMail(messageBuffer); 416 activateMailCompositionBuffer(editor, sm); 417 } 418 } 419 420 public static void messageReplyToSender() 421 { 422 messageReply(false); 423 } 424 425 public static void messageReplyToGroup() 426 { 427 messageReply(true); 428 } 429 430 private static void messageReply(boolean replyToGroup) 431 { 432 Editor editor = Editor.currentEditor(); 433 final Buffer buffer = editor.getBuffer(); 434 if (buffer instanceof MessageBuffer) { 435 MessageBuffer messageBuffer = (MessageBuffer) buffer; 436 if (messageBuffer.getMailbox() != null) { 437 SendMail sm = new SendMail(messageBuffer, replyToGroup); 438 activateMailCompositionBuffer(editor, sm); 439 } 440 } 441 } 442 443 private static void activateMailCompositionBuffer(Editor editor, 444 SendMail sm) 445 { 446 editor.makeNext(sm); 447 Frame frame = editor.getFrame(); 448 editor.switchToBuffer(sm); 449 Editor ed = 451 frame.contains(editor) ? editor : frame.getCurrentEditor(); 452 if (ed.getBuffer() == sm) { 453 ed.setDot(sm.getInitialDotPos()); 454 ed.moveCaretToDotCol(); 455 ed.updateDisplay(); 456 } 457 } 458 459 public static final void messageIndex() 460 { 461 messageIndex(Editor.currentEditor()); 462 } 463 464 public static void messageIndex(Editor editor) 465 { 466 final Buffer buffer = editor.getBuffer(); 467 if (buffer instanceof MessageBuffer) { 468 MessageBuffer messageBuffer = (MessageBuffer) buffer; 469 Mailbox mailbox = messageBuffer.getMailbox(); 470 if (mailbox == null) 471 return; 472 for (BufferIterator it = new BufferIterator(); it.hasNext();) { 473 Buffer buf = it.nextBuffer(); 474 if (buf == mailbox) { 475 final Line line = 476 mailbox.getLineForEntry(messageBuffer.getMailboxEntry()); 477 if (editor == Editor.currentEditor()) { 478 Editor otherEditor = editor.getOtherEditor(); 479 if (otherEditor != null && messageBuffer.isTransient()) { 480 messageBuffer.saveWindowState(editor); 481 editor.otherWindow(); 482 editor.unsplitWindow(); 483 editor = Editor.currentEditor(); 484 } 485 } 486 editor.activate(mailbox); 487 messageBuffer.kill(); 488 Sidebar.refreshSidebarInAllFrames(); 489 if (line != null) { 490 editor.getDot().moveTo(line, 0); 491 editor.setUpdateFlag(REFRAME); 492 editor.setMark(null); 493 editor.moveCaretToDotCol(); 494 } 495 return; 496 } 497 } 498 } 499 } 500 501 public static void mailboxLastMessage() 502 { 503 final Editor editor = Editor.currentEditor(); 504 final Buffer buffer = editor.getBuffer(); 505 if (buffer instanceof Mailbox) { 506 if (editor.getDot() != null) { 507 Position end = buffer.getEnd(); 508 if (end != null) { 509 editor.moveDotTo(end.getLine(), 0); 510 editor.setUpdateFlag(REFRAME); 511 } 512 } 513 } 514 } 515 516 public static void mailboxGetNewMessages() 517 { 518 final Buffer buffer = Editor.currentEditor().getBuffer(); 519 if (buffer instanceof Mailbox) 520 ((Mailbox)buffer).getNewMessages(); 521 } 522 523 public static void mailboxLimit() 524 { 525 final Buffer buffer = Editor.currentEditor().getBuffer(); 526 if (buffer instanceof Mailbox) 527 ((Mailbox)buffer).limit(); 528 } 529 530 public static void mailboxUnlimit() 531 { 532 final Buffer buffer = Editor.currentEditor().getBuffer(); 533 if (buffer instanceof Mailbox) 534 ((Mailbox)buffer).unlimit(); 535 } 536 537 public static void mailboxReadMessage() 538 { 539 mailboxReadMessage(false); 540 } 541 542 public static void mailboxReadMessageOtherWindow() 543 { 544 mailboxReadMessage(true); 545 } 546 547 private static void mailboxReadMessage(boolean useOtherWindow) 548 { 549 final Editor editor = Editor.currentEditor(); 550 final Buffer buffer = editor.getBuffer(); 551 if (buffer instanceof Mailbox && editor.getDot() != null) { 552 AWTEvent e = editor.getDispatcher().getLastEvent(); 555 if (e instanceof MouseEvent ) 556 editor.mouseMoveDotToPoint((MouseEvent ) e); 557 if (useOtherWindow) 558 ((Mailbox)buffer).readMessageOtherWindow(editor.getDotLine()); 559 else 560 ((Mailbox)buffer).readMessage(editor.getDotLine()); 561 } 562 } 563 564 public static void mailboxCreateFolder() 565 { 566 final Buffer buffer = Editor.currentEditor().getBuffer(); 567 if (buffer instanceof Mailbox) 568 ((Mailbox)buffer).createFolder(); 569 } 570 571 public static void mailboxDeleteFolder() 572 { 573 final Buffer buffer = Editor.currentEditor().getBuffer(); 574 if (buffer instanceof Mailbox) 575 ((Mailbox)buffer).deleteFolder(); 576 } 577 578 public static void mailboxSaveToFolder() 579 { 580 final Buffer buffer = Editor.currentEditor().getBuffer(); 581 if (buffer instanceof Mailbox) 582 ((Mailbox)buffer).saveToFolder(); 583 } 584 585 public static void mailboxMoveToFolder() 586 { 587 final Buffer buffer = Editor.currentEditor().getBuffer(); 588 if (buffer instanceof Mailbox) 589 ((Mailbox)buffer).moveToFolder(); 590 } 591 592 public static void mailboxDelete() 593 { 594 final Buffer buffer = Editor.currentEditor().getBuffer(); 595 if (buffer instanceof Mailbox) 596 ((Mailbox)buffer).delete(); 597 } 598 599 public static void mailboxUndelete() 600 { 601 final Buffer buffer = Editor.currentEditor().getBuffer(); 602 if (buffer instanceof Mailbox) 603 ((Mailbox)buffer).undelete(); 604 } 605 606 public static void mailboxMarkRead() 607 { 608 final Buffer buffer = Editor.currentEditor().getBuffer(); 609 if (buffer instanceof Mailbox) 610 ((Mailbox)buffer).markRead(); 611 } 612 613 public static void mailboxMarkUnread() 614 { 615 final Buffer buffer = Editor.currentEditor().getBuffer(); 616 if (buffer instanceof Mailbox) 617 ((Mailbox)buffer).markUnread(); 618 } 619 620 public static void mailboxFlag() 621 { 622 final Buffer buffer = Editor.currentEditor().getBuffer(); 623 if (buffer instanceof Mailbox) 624 ((Mailbox)buffer).flag(); 625 } 626 627 public static void mailboxTag() 628 { 629 final Editor editor = Editor.currentEditor(); 630 final Buffer buffer = editor.getBuffer(); 631 if (buffer instanceof Mailbox) 632 ((Mailbox)buffer).tag(editor, editor.getDotLine()); 633 } 634 635 public static void mailboxTagPattern() 636 { 637 final Buffer buffer = Editor.currentEditor().getBuffer(); 638 if (buffer instanceof Mailbox) 639 ((Mailbox)buffer).tagPattern(); 640 } 641 642 public static void mailboxUntagAll() 643 { 644 final Buffer buffer = Editor.currentEditor().getBuffer(); 645 if (buffer instanceof Mailbox) 646 ((Mailbox)buffer).untagAll(); 647 } 648 649 public static void mailboxToggleRaw() 650 { 651 final Buffer buffer = Editor.currentEditor().getBuffer(); 652 if (buffer instanceof Mailbox) 653 ((Mailbox)buffer).toggleRaw(); 654 } 655 656 public static void mailboxExpunge() 657 { 658 final Buffer buffer = Editor.currentEditor().getBuffer(); 659 if (buffer instanceof Mailbox) 660 ((Mailbox)buffer).expunge(); 661 } 662 663 public static final void mailboxStop() 664 { 665 Editor.currentEditor().cancelBackgroundProcess(); 666 } 667 668 public static void messageToggleHeaders() 669 { 670 final Buffer buffer = Editor.currentEditor().getBuffer(); 671 if (buffer instanceof MessageBuffer) 672 ((MessageBuffer)buffer).toggleHeaders(); 673 } 674 675 public static void messageToggleRaw() 676 { 677 final Buffer buffer = Editor.currentEditor().getBuffer(); 678 if (buffer instanceof MessageBuffer) 679 ((MessageBuffer)buffer).toggleRaw(); 680 } 681 682 public static void messageToggleWrap() 683 { 684 final Buffer buffer = Editor.currentEditor().getBuffer(); 685 if (buffer instanceof MessageBuffer) 686 ((MessageBuffer)buffer).toggleWrap(); 687 } 688 689 public static void messageViewAttachment() 690 { 691 final Buffer buffer = Editor.currentEditor().getBuffer(); 692 if (buffer instanceof MessageBuffer) 693 ((MessageBuffer)buffer).viewAttachment(); 694 else if (buffer instanceof NewsGroupMessageBuffer) 695 ((NewsGroupMessageBuffer)buffer).viewInline(); 696 } 697 698 public static void messageSaveAttachment() 699 { 700 final Buffer buffer = Editor.currentEditor().getBuffer(); 701 if (buffer instanceof MessageBuffer) 702 ((MessageBuffer)buffer).saveAttachment(); 703 } 704 705 public static void bounce() 706 { 707 final Editor editor = Editor.currentEditor(); 708 final Buffer buffer = editor.getBuffer(); 709 if (buffer instanceof Mailbox) 710 ((Mailbox)buffer).bounce(); 711 else if (buffer instanceof MessageBuffer) 712 ((MessageBuffer)buffer).bounce(); 713 } 714 715 public static MailAddress[] bounceGetTo(Editor editor, int count) 716 { 717 FastStringBuffer sb = new FastStringBuffer("Bounce "); 718 sb.append(count); 719 sb.append(" message"); 720 if (count > 1) 721 sb.append('s'); 722 sb.append(" to:"); 723 InputDialog d = new InputDialog(editor, sb.toString(), "Bounce", null); 724 d.setHistory(new History("bounceMessage")); 725 editor.centerDialog(d); 726 d.show(); 727 String input = d.getInput(); 728 if (input == null) 729 return null; 730 MailAddress[] to = MailAddress.parseAddresses(input); 731 if (to == null || to.length == 0) 732 return null; 733 return to; 734 } 735 736 public static void toggleGroupByThread() 737 { 738 final Buffer buffer = Editor.currentEditor().getBuffer(); 739 if (buffer instanceof Mailbox) 740 ((Mailbox)buffer).toggleGroupByThread(); 741 } 742 743 public static void foldThread() 744 { 745 final Editor editor = Editor.currentEditor(); 746 final Buffer buffer = editor.getBuffer(); 747 if (buffer instanceof Mailbox) 748 ((Mailbox)buffer).foldThread(editor.getDotLine()); 749 } 750 751 public static void foldThreads() 752 { 753 final Editor editor = Editor.currentEditor(); 754 final Buffer buffer = editor.getBuffer(); 755 if (buffer instanceof Mailbox) 756 ((Mailbox)buffer).foldThreads(); 757 } 758 } 759 | Popular Tags |