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
|
// Copyright 2006 Alp Toker <alp@atoker.com>
// This software is made available under the MIT License
// See COPYING for details
using System;
using System.Text;
using System.Collections.Generic;
namespace NDesk.DBus
{
public class BadAddressException : Exception
{
public BadAddressException (string reason) : base (reason) {}
}
class AddressEntry
{
public string Method;
public IDictionary<string,string> Properties = new Dictionary<string,string> ();
public override string ToString ()
{
StringBuilder sb = new StringBuilder ();
sb.Append (Method);
sb.Append (':');
bool first = true;
foreach (KeyValuePair<string,string> prop in Properties) {
if (first)
first = false;
else
sb.Append (',');
sb.Append (prop.Key);
sb.Append ('=');
sb.Append (Escape (prop.Value));
}
return sb.ToString ();
}
static string Escape (string str)
{
if (str == null)
return String.Empty;
StringBuilder sb = new StringBuilder ();
int len = str.Length;
for (int i = 0 ; i != len ; i++) {
char c = str[i];
//everything other than the optionally escaped chars _must_ be escaped
if (Char.IsLetterOrDigit (c) || c == '-' || c == '_' || c == '/' || c == '\\' || c == '.')
sb.Append (c);
else
sb.Append (Uri.HexEscape (c));
}
return sb.ToString ();
}
static string Unescape (string str)
{
if (str == null)
return String.Empty;
StringBuilder sb = new StringBuilder ();
int len = str.Length;
int i = 0;
while (i != len) {
if (Uri.IsHexEncoding (str, i))
sb.Append (Uri.HexUnescape (str, ref i));
else
sb.Append (str[i++]);
}
return sb.ToString ();
}
public static AddressEntry Parse (string s)
{
AddressEntry entry = new AddressEntry ();
string[] parts = s.Split (':');
if (parts.Length < 2)
throw new BadAddressException ("No colon found");
if (parts.Length > 2)
throw new BadAddressException ("Too many colons found");
entry.Method = parts[0];
foreach (string propStr in parts[1].Split (',')) {
parts = propStr.Split ('=');
if (parts.Length < 2)
throw new BadAddressException ("No equals sign found");
if (parts.Length > 2)
throw new BadAddressException ("Too many equals signs found");
entry.Properties[parts[0]] = Unescape (parts[1]);
}
return entry;
}
}
static class Address
{
//(unix:(path|abstract)=.*,guid=.*|tcp:host=.*(,port=.*)?);? ...
public static AddressEntry[] Parse (string addresses)
{
if (addresses == null)
throw new ArgumentNullException (addresses);
List<AddressEntry> entries = new List<AddressEntry> ();
foreach (string entryStr in addresses.Split (';'))
entries.Add (AddressEntry.Parse (entryStr));
return entries.ToArray ();
}
const string SYSTEM_BUS_ADDRESS = "unix:path=/var/run/dbus/system_bus_socket";
public static string System
{
get {
string addr = Environment.GetEnvironmentVariable ("DBUS_SYSTEM_BUS_ADDRESS");
if (String.IsNullOrEmpty (addr))
addr = SYSTEM_BUS_ADDRESS;
return addr;
}
}
public static string Session
{
get {
return Environment.GetEnvironmentVariable ("DBUS_SESSION_BUS_ADDRESS");
}
}
public static string Starter
{
get {
return Environment.GetEnvironmentVariable ("DBUS_STARTER_ADDRESS");
}
}
public static string StarterBusType
{
get {
return Environment.GetEnvironmentVariable ("DBUS_STARTER_BUS_TYPE");
}
}
}
}
|