Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Apress committed Oct 11, 2016
0 parents commit b17abd6
Show file tree
Hide file tree
Showing 19 changed files with 1,106 additions and 0 deletions.
Binary file added 3541.pdf
Binary file not shown.
Binary file added 3542.pdf
Binary file not shown.
Binary file added 9781590598368.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ADprep.vsd
Binary file not shown.
110 changes: 110 additions & 0 deletions CommandLine.cs
@@ -0,0 +1,110 @@
using System;
using System.Collections.Specialized;
using System.Text.RegularExpressions;

namespace CommandLine.Utility
{
/// <summary>
/// Arguments class
/// </summary>
public class Arguments
{
// Variables
private StringDictionary Parameters;

// Constructor
public Arguments(string[] Args)
{
Parameters = new StringDictionary();
Regex Spliter = new Regex(@"^-|^/|=|:",
RegexOptions.IgnoreCase|RegexOptions.Compiled);

Regex Remover = new Regex(@"^['""]?(.*?)['""]?$",
RegexOptions.IgnoreCase|RegexOptions.Compiled);

string Parameter = null;
string[] Parts;

// Valid parameters forms:
// {-,/,--}param{ ,=,:}((",')value(",'))
// Examples:
// -param1 value1 /param2:"Test-:-work"
// /param3=happy -param4 '--=nice=--'
foreach(string Txt in Args)
{
// Look for new parameters (-,/ or --) and a
// possible enclosed value (=,:)
Parts = Spliter.Split(Txt,3);

switch(Parts.Length)
{
// Found a value (for the last parameter
// found (space separator))
case 1:
if(Parameter != null)
{
if(!Parameters.ContainsKey(Parameter))
{
Parts[0] =
Remover.Replace(Parts[0], "$1");
Parameters.Add(Parameter, Parts[0]);
}
Parameter=null;
}
// else Error: no parameter waiting for a value (skipped)
break;

// Found just a parameter
case 2:
// The last parameter is still waiting.
// With no value, set it to true.
if(Parameter!=null)
{
if(!Parameters.ContainsKey(Parameter))
Parameters.Add(Parameter, "true");
}
Parameter=Parts[1];
break;

// Parameter with enclosed value
case 3:
// The last parameter is still waiting.
// With no value, set it to true.
if(Parameter != null)
{
if(!Parameters.ContainsKey(Parameter))
Parameters.Add(Parameter, "true");
}

Parameter = Parts[1];

// Remove possible enclosing characters (",')
if(!Parameters.ContainsKey(Parameter))
{
Parts[2] = Remover.Replace(Parts[2], "$1");
Parameters.Add(Parameter, Parts[2]);
}

Parameter=null;
break;
}
}
// In case a parameter is still waiting
if(Parameter != null)
{
if(!Parameters.ContainsKey(Parameter))
Parameters.Add(Parameter, "true");
}
}

// Retrieve a parameter value if it exists
// (overriding C# indexer property)
public string this [string Param]
{
get
{
return(Parameters[Param]);
}
}
}
}
Binary file added DeployLCS.vsd
Binary file not shown.
27 changes: 27 additions & 0 deletions LICENSE.txt
@@ -0,0 +1,27 @@
Freeware License, some rights reserved

Copyright (c) 2007 Rui Maximo and Andrew Edney

Permission is hereby granted, free of charge, to anyone obtaining a copy
of this software and associated documentation files (the "Software"),
to work with the Software within the limits of freeware distribution and fair use.
This includes the rights to use, copy, and modify the Software for personal use.
Users are also allowed and encouraged to submit corrections and modifications
to the Software for the benefit of other users.

It is not allowed to reuse, modify, or redistribute the Software for
commercial use in any way, or for a user�s educational materials such as books
or blog articles without prior permission from the copyright holder.

The above copyright notice and this permission notice need to 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 OR APRESS 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.


164 changes: 164 additions & 0 deletions Program.cs
@@ -0,0 +1,164 @@
using System;
using System.Collections.Generic;
using System.Text;
using CommandLine.Utility;

namespace sipchange
{
using System.Management;
using System.Collections;
using System.Collections.Specialized;
using System.DirectoryServices;
using System.Text.RegularExpressions;

class Program
{
static string sQuery;
static string sQueryContacts;
static string SourceDomainURI;
static string TargetDomainURI;

public Program()
{
// query string to return instance ID of user enabled for LCS using WMI
sQuery = "select * from MSFT_SIPESUserSetting where UserDN = '";
// query string to return all contacts given a user's instance ID
sQueryContacts = "select * from MSFT_SIPESUserContactData where UserInstanceID = '";
}

static void Main(string[] args)
{
if (args.Length == 0)
{
Console.WriteLine("\ncommand-line arguments are required.");
return;
}

Arguments CommandLine = new Arguments(args);

if (CommandLine["?"] != null)
{
Console.WriteLine("\ncommand-line arguments:");
Console.WriteLine("\t/source:<name>\t\t- SIP domain to match");
Console.WriteLine("\t/target:<name>\t\t- SIP domain to change");
return;
}
if (CommandLine["source"] != null)
{
SourceDomainURI = CommandLine["source"];
}
if (CommandLine["target"] != null)
{
TargetDomainURI = CommandLine["target"];
}

DirectoryEntry deRoot = null;
try
{
DirectoryEntry deRootDSE = new DirectoryEntry("LDAP://RootDSE");
string sRootDomain = "LDAP://" + deRootDSE.Properties["rootDomainNamingContext"].Value.ToString();
deRoot = new DirectoryEntry(sRootDomain);
}
catch(Exception e)
{
Console.WriteLine("Failed to query Active Directory");
Console.WriteLine(e.Message);
return;
}

// Instantiate class.
Program SipDomain = new Program();

string userDN = null;
try
{
DirectorySearcher Users = new DirectorySearcher(deRoot, "(&(objectCategory=person)(objectCategory=user)(msRTCSIP-UserEnabled=TRUE))");

foreach(SearchResult user in Users.FindAll())
{
// Obtain DN of user by removing the first 7 characters "LDAP://".
userDN = user.Path.Substring(7);
SipDomain.ChangeSIPdomain(userDN);
Console.WriteLine();
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return;
}
}

public void ChangeSIPdomain(string userDN)
{
try
{
// Query for user's instance ID
ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(sQuery + userDN + "'");
ManagementObjectCollection oCollection = oSearcher.Get();

foreach (ManagementObject user in oCollection)
{
Console.Write(user["PrimaryURI"].ToString());

if(Regex.IsMatch(user["PrimaryURI"].ToString(), SourceDomainURI))
{
// Modify user SIP URI domain portion
string URI = Regex.Replace(user["PrimaryURI"].ToString(), @"@[-\w.]+", "@" + TargetDomainURI);
Console.Write(" -> " + URI);
user["PrimaryURI"] = URI;
// Commit changes
//user.Put();
}
Console.WriteLine();

// Check whether user is unassigned
if (user["HomeServerDN"] == null)
{
Console.WriteLine("WARNING: this user is unassigned");
// If the user is unassigned, then it won't be possible to read their contact list
return;
}

UpdateContactsURI(user["InstanceID"].ToString());
}
}
catch (Exception e)
{
Console.WriteLine("ChangeSIPdomain(): " + e.Message);
}
}

private void UpdateContactsURI(string InstanceID)
{
try
{
ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(sQueryContacts + InstanceID + "'");
ManagementObjectCollection oCollection = oSearcher.Get();

// Check whether user has any contacts in their contact list
if (oCollection.Count == 0)
{
return;
}

foreach (ManagementObject contact in oCollection)
{
if (Regex.IsMatch(contact.GetPropertyValue("SIPURI").ToString(), SourceDomainURI))
{
string URI = Regex.Replace(contact.GetPropertyValue("SIPURI").ToString(), @"@[-\w.]+", "@" + TargetDomainURI);
// Display only contacts that are modified
Console.WriteLine("\tcontact: " + contact.GetPropertyValue("SIPURI") + " -> " + URI);
contact.SetPropertyValue("SIPURI", URI);
// Commit change
//contact.Put();
}
}
}
catch (Exception e)
{
Console.WriteLine("WARNING: unable to contact user\'s home server to read contact list");
}
}
}
}
15 changes: 15 additions & 0 deletions README.md
@@ -0,0 +1,15 @@
#Apress Source Code

This repository accompanies [*Pro LCS*](http://www.apress.com/9781590598368) by Rui Maximo and Andrew Edney (Apress, 2007).

![Cover image](9781590598368.jpg)

Download the files as a zip using the green button, or clone the repository to your machine using Git.

##Releases

Release v1.0 corresponds to the code in the published book, without corrections or updates.

##Contributions

See the file Contributing.md for more information on how you can contribute to this repository.

0 comments on commit b17abd6

Please sign in to comment.