~ubuntu-branches/ubuntu/karmic/spambayes/karmic

« back to all changes in this revision

Viewing changes to Outlook2000/sandbox/set_read_flag.py

  • Committer: Bazaar Package Importer
  • Author(s): Jorge Bernal
  • Date: 2005-04-07 14:02:02 UTC
  • Revision ID: james.westby@ubuntu.com-20050407140202-mgyh6t7gn2dlrrw5
Tags: upstream-1.0.1
ImportĀ upstreamĀ versionĀ 1.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from __future__ import generators
 
2
# Set items to read/unread
 
3
 
 
4
import pythoncom
 
5
import os, sys
 
6
 
 
7
from win32com.mapi import mapi, mapiutil
 
8
from win32com.mapi.mapitags import *
 
9
 
 
10
MSGFLAG_READ = 0x1
 
11
 
 
12
CLEAR_READ_FLAG = 0x00000004
 
13
CLEAR_RN_PENDING = 0x00000020
 
14
CLEAR_NRN_PENDING = 0x00000040
 
15
SUPPRESS_RECEIPT = 0x1
 
16
 
 
17
import mapi_driver
 
18
 
 
19
def SetReadState(driver, mapi_folder, subject, unread):
 
20
    hr, data = mapi_folder.GetProps( (PR_DISPLAY_NAME_A,), 0)
 
21
    name = data[0][1]
 
22
    num = 0
 
23
    for item in driver.GetItemsWithValue(mapi_folder, PR_SUBJECT_A, subject):
 
24
        flags_base = mapi.MAPI_DEFERRED_ERRORS | SUPPRESS_RECEIPT
 
25
        if unread:
 
26
            item.SetReadFlag(mapi.MAPI_DEFERRED_ERRORS|CLEAR_READ_FLAG)
 
27
        else:
 
28
            item.SetReadFlag(flags_base)
 
29
        num += 1
 
30
        # Check the set worked.
 
31
        hr, props = item.GetProps((PR_MESSAGE_FLAGS,), 0)
 
32
        ((tag, val), ) = props
 
33
        if val & MSGFLAG_READ == unread:
 
34
            print "MAPI SetReadState appears to have failed to change the message state"
 
35
            print "Requested set to unread=%s but the MAPI field after was %r" % \
 
36
                    (unread, val)
 
37
    print "Processed", num, "items"
 
38
 
 
39
def usage(driver):
 
40
    folder_doc = driver.GetFolderNameDoc()
 
41
    msg = """\
 
42
Usage: %s [-u] subject of the message
 
43
-f - Search for the message in the specified folder (default = Inbox)
 
44
-u - Mark as unread
 
45
 
 
46
Marks as read (or unread) all messages that match the subject.  Subject
 
47
matching is substring and ignore-case.
 
48
 
 
49
%s
 
50
Use the -n option to see all top-level folder names from all stores.""" \
 
51
    % (os.path.basename(sys.argv[0]),folder_doc)
 
52
    print msg
 
53
 
 
54
def main():
 
55
    driver = mapi_driver.MAPIDriver()
 
56
 
 
57
    import getopt
 
58
    try:
 
59
        opts, args = getopt.getopt(sys.argv[1:], "u")
 
60
    except getopt.error, e:
 
61
        print e
 
62
        print
 
63
        usage(driver)
 
64
        sys.exit(1)
 
65
    folder_name = ""
 
66
 
 
67
    unread = False
 
68
    for opt, opt_val in opts:
 
69
        if opt == "-u":
 
70
            unread = True
 
71
        else:
 
72
            print "Invalid arg"
 
73
            return
 
74
 
 
75
    if not folder_name:
 
76
        folder_name = "Inbox" # Assume this exists!
 
77
 
 
78
    subject = " ".join(args)
 
79
    if not subject:
 
80
        print "You must specify a subject"
 
81
        print
 
82
        usage(driver)
 
83
        sys.exit(1)
 
84
 
 
85
    try:
 
86
        folder = driver.FindFolder(folder_name)
 
87
    except ValueError, details:
 
88
        print details
 
89
        sys.exit(1)
 
90
 
 
91
    SetReadState(driver, folder, subject, unread)
 
92
 
 
93
if __name__=='__main__':
 
94
    main()