summaryrefslogtreecommitdiffstats
path: root/actions/services/__init__.py.in
blob: 5f19e260903d5f8fceacda259d7cec01fbc8db52 (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
#!@PYTHON@
# -*- coding: UTF-8 -*-
# -*- python -*-
# Copyright (C) 2005 by Sebastien Estienne
#
# This file may be distributed and/or modified under the terms of
# the GNU General Public License version 2 as published by
# the Free Software Foundation.
# This file is distributed without any warranty; without even the implied
# warranty of merchantability or fitness for a particular purpose.
# See "COPYING" in the source distribution for more information.
#
# $id$
#

# BIG WARNING:
# this file is for the moment a big ugly quick hack
# I'm working on a clean plugin system to handle actions for service types
# it will hopefully appear in 0.3
# this file is just an interim solution to give the same functionnaly as 0.1

try:
    import gettext
    gettext.bindtextdomain("@GETTEXT_PACKAGE@", "@LOCALEDIR@")
    gettext.textdomain("@GETTEXT_PACKAGE@")
    _ = gettext.gettext
    import pygtk
    pygtk.require('2.0')
    import gtk
    import os
    import pwd
    import subprocess
    import gnome
except ImportError, e:
    error_msg(_("A required python module is missing!\n%s") % (e))
    sys.exit()

def pair_to_dict(l):
    res = dict()
    for el in l:
        if "=" not in el:
            res[el]=''
        else:
            tmp = el.split('=',1)
            if len(tmp[0]) > 0:
                res[tmp[0]] = tmp[1]
    return res
                                                                

def build_url(uri = "http", hostname="127.0.0.1", port = None, path = None, username = None, password = None):
    if path and path != None:
        if path[0] == "/":
            path = path[1:]
    else:
        path = ""
        
    if username:
        if password:
            username="%s:%s@" % (username,password)
        else:
            username="%s@" % (username)
    else:
        username=""
    if port and port != None:
        hostname="%s:%i" % (hostname,port)
    return "%s://%s%s/%s" % (uri,username,hostname,path)

def get_txt_value(txts, txt):
    if txts.has_key(txt):
        return txts[txt]
    else:
        return None
    
    
def _http_tcp(name, hostname, address, port, txts):
    path = get_txt_value(txts,"path")
    username = get_txt_value(txts,"u")
    password = get_txt_value(txts,"p")
    url = build_url("http",address,port, path, username,password)
    gnome.url_show(url)
        
def _https_tcp(name, hostname, address, port, txts):
    path = get_txt_value(txts,"path")
    username = get_txt_value(txts,"u")
    password = get_txt_value(txts,"p")
    url = build_url("https",address,port, path, username,password)
    gnome.url_show(url)
        
def _ftp_tcp(name, hostname, address, port, txts):
    path = get_txt_value(txts,"path")
    username = get_txt_value(txts,"u")
    password = get_txt_value(txts,"p")
    url = build_url("ftp",address,port, path, username,password)
    gnome.url_show(url)
        
def _sftpssh_tcp(name, hostname, address, port, txts):
    path = get_txt_value(txts,"path")
    username = get_txt_value(txts,"u")
    password = get_txt_value(txts,"p")
    url = build_url("ftp",address,port, path, username,password)
    gnome.url_show(url)

def enter_callback(widget, win):
    win.destroy()

def SshLogin(hostname, username = None):
    global win
    win = gtk.Dialog(_("SSH Connection"), None,
		     gtk.DIALOG_MODAL,
		     (gtk.STOCK_OK, gtk.RESPONSE_OK))

    vbox = gtk.VBox(False, 5)
    win.vbox.pack_start(vbox, True, True, 0)
    vbox.set_border_width(5)
    
    label = gtk.Label()
    label.set_markup(_("Connecting to <b>%s</b>.\nPlease enter your <b>login</b>:") % (hostname))
    
    vbox.pack_start(label, False, False, 0)
    
    # Create our entry
    entry = gtk.Entry()
    if username!= None:
	entry.set_text(username)
    entry.connect("activate", enter_callback, win)
    vbox.pack_start(entry, False, False, 0)
    
    # Create the completion object
    completion = gtk.EntryCompletion()
    
    # Assign the completion to the entry
    entry.set_completion(completion)
    
    # Create a tree model and use it as the completion model
    completion_model = __create_completion_model()
    completion.set_model(completion_model)
    
    # Use model column 0 as the text column
    completion.set_text_column(0)
    
    win.show_all()
    win.run()
    return entry.get_text()
    
def __create_completion_model():
    ''' Creates a tree model containing the completions.
    '''
    store = gtk.ListStore(str)
    
    iter = store.append()
    store.set(iter, 0, "root")
    
    current_user = pwd.getpwuid(os.getuid())[0]
    iter = store.append()
    store.set(iter, 0, current_user)
    
    return store

def _ssh_tcp(name, hostname, address, port, txts):

    if txts.has_key("u"):
	username = SshLogin(name, txts["u"])
    else:
	username = SshLogin(name)

    if username != "":
        sshline = "ssh -l %s -p %i %s" % (username, port, address)
    else:
        sshline = "ssh -p %i %s" % (port, address)

    cmdline = []
    cmdline.append("gnome-terminal")
    cmdline.append("--tab")
    cmdline.append("-t %s" % name)
    cmdline.append("-e %s" % sshline)
    pid = subprocess.Popen(cmdline).pid

def handle(name, stype, hostname, address, port, txts):
    if stype == "_http._tcp":
        _http_tcp(name, hostname, address, port, txts)
        return
    if stype == "_https._tcp":
        _https_tcp(name, hostname, address, port, txts)
        return
    if stype == "_ftp._tcp":
        _ftp_tcp(name, hostname, address, port, txts)
        return
    if stype == "_ssh._tcp":
        _ssh_tcp(name, hostname, address, port, txts)
        return
    if stype == "_sftp-ssh._tcp":
        _sftpssh_tcp(name, hostname, address, port, txts)
        return
            
            
if __name__ == "__main__":
    print build_url()
    print build_url("ftp")
    print build_url("https","www.google.com")
    print build_url("https","www.google.com",80)
    print build_url("https","www.google.com",80)
    print build_url("https","www.google.com",80,"test")
    print build_url("https","www.google.com",80,"/test")
    print build_url("https","www.google.com",80,"/test", "user")
    print build_url("https","www.google.com",0, "", None, "pass")
    print build_url("https","www.google.com",80,"/test", "user", "pass")