joonis Logo

Mailto script for Opera supporting attachments on Linux

opera_mailto.py [4.11 KB]
  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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  opera_mailto.py
#
#  The MIT License
#
#  Copyright (c) 2012 joonis new media
#  Author: Thimo Kraemer <thimo.kraemer@joonis.de>
#
#  Permission is hereby granted, free of charge, to any person obtaining a copy
#  of this software and associated documentation files (the "Software"), to deal
#  in the Software without restriction, including without limitation the rights
#  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#  copies of the Software, and to permit persons to whom the Software is
#  furnished to do so, subject to the following conditions:
#
#  The above copyright notice and this permission notice shall be included in
#  all copies or substantial portions of the Software.
#
#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#  THE SOFTWARE.
#

import sys, os
import subprocess
import time
import urlparse, cgi
import wnck, gtk
import tempfile
import shutil

mail_path = os.path.expanduser('~/.opera/mail')
drafts_path = mail_path + '/store/drafts'

def get_opera():
    screen = wnck.screen_get_default()
    screen.force_update()
    for wnd in screen.get_windows():
        name = wnd.get_name()
        if name.endswith('- Opera'):
            #app = wnd.get_application()
            return wnd

def needs_attention(wnd):
    while gtk.events_pending():
        gtk.main_iteration()
    return wnd.or_transient_needs_attention()

def is_active(wnd):
    while gtk.events_pending():
        gtk.main_iteration()
    return wnd.is_active()

def activate(wnd, transient=False):
    if transient:
        wnd.activate_transient(int(time.time()))
    else:
        wnd.activate(int(time.time()))
    while not is_active(wnd):
        time.sleep(0.2)


# parse arguments
if len(sys.argv) >= 2:
    url = sys.argv[1]
    url = url.replace('mailto:///', 'mailto:')
    url = url.replace('+', '%20')
else:
    url = 'mailto:'

# parse query string
qs = url.split('?', 1)
if len(qs) == 1:
    qs = ''
else:
    qs = qs[1]
params = cgi.parse_qs(qs)

# Current number of files in draft folder
drafts = os.listdir(drafts_path)

# open new compose window
subprocess.Popen(['opera', url])


if params.has_key('attach'):
    # get opera window
    opera = None
    while not opera:
        opera = get_opera()
        time.sleep(0.5)
    activate(opera)

    # Save draft
    subprocess.call(['xte',
        'keydown Control_L',
        'key s',
        'keyup Control_L',
        ])
    while not is_active(opera):
        time.sleep(0.2)

    # Get new Opera message id
    started = time.time()
    while True:
        names = os.listdir(drafts_path)
        if len(names) > len(drafts):
            for name in names:
                if name not in drafts:
                    msg_id = name.split('.')[0]
                    break
            else:
                raise Exception, 'Could not detect new draft'
            break
        if time.time() - started > 5:
            raise Exception, 'Timeout saving draft'
        time.sleep(0.1)

    # Close compose window
    subprocess.call(['xte',
        'keydown Control_L',
        'key w',
        'keyup Control_L',
        ])
    while not is_active(opera):
        time.sleep(0.2)

    msg_path = '%s/%s' % (drafts_path, msg_id)
    os.mkdir(msg_path)

    # Copy attachments
    for i in range(len(params['attach'])):
        attach = params['attach'][i].replace('file://', '')
        if os.path.isfile(attach):
            name = os.path.split(attach)[1]
            path = '%s/opr%i-%s' % (msg_path, i, name)
            shutil.copyfile(attach, path)

    # Reopen compose window
    subprocess.call(['xte',
        'keydown Control_L',
        'keydown Shift_L',
        'key t',
        'keyup Control_L',
        'keyup Shift_L',
        ])

Kann ich
  Ihnen helfen?


Schreiben Sie mir
doch einfach unter
giraffe@joonis.de