summaryrefslogtreecommitdiffstats
path: root/actions/services/__init__.py
diff options
context:
space:
mode:
Diffstat (limited to 'actions/services/__init__.py')
-rwxr-xr-xactions/services/__init__.py112
1 files changed, 112 insertions, 0 deletions
diff --git a/actions/services/__init__.py b/actions/services/__init__.py
new file mode 100755
index 0000000..bb51b49
--- /dev/null
+++ b/actions/services/__init__.py
@@ -0,0 +1,112 @@
+#!/usr/bin/python
+
+import subprocess
+import gnome
+
+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 _ssh_tcp(name, hostname, address, port, txts):
+ if txts.has_key("u"):
+ sshline = "ssh -l %s -p %i %s" % (txts["u"], 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 _sftp-ssh_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 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":
+ _sftp-ssh_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")