Scripting in Remote Console

Updates on work in progress and information on new features.

Scripting in Remote Console

Postby Elric Pedder » Sun Jan 23rd, 2011 4:59pm

Mailtraq's Remote Console is used to administer the Mailtraq server using only a TCP/IP connection. This is done using the Remote API which is able to control almost every element of Mailtraq's configuration and some of its functionality too. As of build 2962 a fully functioning scripting interface is available which allows administrators to control functions not exposed in the Remote Console and to perform more complex tasks. You can access this feature through the Execute Script command in the Tools menu of the Remote Console.

ScriptExecute.png
ScriptExecute.png (66.42 KiB) Viewed 7270 times


When you open the Script Execute window a small demonstration script is created. There are two global objects available, Server and Output. The Server object represents the Mailtraq Server while the Output object is used to communicate with the User. This scripting environment includes documentation which appears when you enter "." or "(". For example, if you type "Server." then a list of all members of "Server" will appear in a drop-down box. If you type "Output.Confirm(" then the list of parameters will be displayed. This is a useful way to explore the available functionality.
Mailtraq Development and Escalation Support
Novitraq Incorporated
User avatar
Elric Pedder
Mailtraq Escalation Support
 
Posts: 2675
Joined: Tue Nov 23rd, 2004 1:16pm
Location: Montreal, Canada

Re: Scripting in Remote Console

Postby Elric Pedder » Thu Jan 27th, 2011 9:46am

Here is a sample script which looks for orphaned mailboxes, calendars and address books. This problem happened to some users because of a fault in the Remote Console a few builds back. The script will do some analysis first and won't actually make changes unless you confirm first.

Code: Select all
// checkowners.js
// Mailslot, Address book and Calendar ownership verification script
//
// This script looks at each address book and calendar to see
// if there is a user with the same name who is not the owner.
// If it finds a match, the object can be assigned to the correct
// owner.
//
// If you want some of the changes and not others, then take the
// output (produced in the Output tab when the script is executed)
// and remove the uncommented lines that you don't want, then
// execute the rest.

function VerifyList(objtype, list, apply)
{
  var msg = "";
  for (var i = 0; i < list.Count; i++)
  {
    var obj = list.Get(i);
    Output.WriteLine("// Checking " + objtype + " " + obj.Name);
    if (obj.Name != obj.OwnerName)
    {
      var userid = Server.Users.FindByName(obj.Name);
      if (userid)
      {
        var user = Server.Users.Get(userid);
        if (apply)
        {
          obj.Owner = user.Uid;
          var s = objtype + ": Assigned " + obj.Name + " to user " + user.Name + "\r\n";
          Output.Write("// " + s);
          msg += s;
        }
        else
        {
          var s = "Assign " + objtype + " " + obj.Name + " to user " + user.Name +
            " (was '" + obj.OwnerName + "')\r\n";
          Output.Write("// " + s);
          Output.WriteLine('Server.' + objtype + 's.Get("' + obj.Id + '").Owner = ' + user.Uid + ';');
          msg += s; 
        }
      }
    }
  }
  return msg;
}

function Verify(objtype, list)
{
  var msg = VerifyList(objtype, list, false);
  if (msg != "")
  {
    var confirm = Output.Confirm("Possible errors found.  Changes possible :-\r\n\r\n" +
      msg + "\r\nApply the above changes?");
    if (confirm)
    {
      Output.WriteLine("User confirmed proceed with changes in " + objtype + " list");
      msg = VerifyList(objtype, list, true);
      Output.ShowMessage(msg);
    }
  }
  else
  {
    var s = objtype + " list appears OK";
    Output.ShowMessage(s);
    Output.WriteLine("// " + s);
  }
}

function Execute()
{
  Verify("Mailslot", Server.Mailslots);
  Verify("Addressbook", Server.Addressbooks);
  Verify("Calendar", Server.Calendars);
}

Execute();
Mailtraq Development and Escalation Support
Novitraq Incorporated
User avatar
Elric Pedder
Mailtraq Escalation Support
 
Posts: 2675
Joined: Tue Nov 23rd, 2004 1:16pm
Location: Montreal, Canada

Re: Scripting in Remote Console

Postby Elric Pedder » Sat Feb 12th, 2011 11:26am

Here is a sample script that corrects the Primary Mailbox assignment for users in cases where the primary mailbox has been renamed and the user cannot log in via WebMail any more. The script simply searches for users with invalid primary mailbox references and resets them to the default (which is a mailbox that matches the user name).

Code: Select all
// fixprimarymailboxes.js
//
// This script corrects user references to invalid primary mailboxes

function FixPrimaryMailboxes()
{
  // storing object lists in local variables
  // considerably improves performance
  var list = Server.Users;
  var slots = Server.Mailslots;

  for (var i = 0; i < list.Count; i++)
  {
    var user = list.Get(i);
    var pm = user.SenderMailbox;
    if (pm != "")
    {
      var slot = slots.FindByName(pm);
      if (!slot)
      {
        // setting SenderMailbox to an empty string means that the
        // mailbox with a name matching the owner is always used, even
        // if the owner is renamed later
        user.SenderMailbox = "";
        Output.WriteLine("User " + user.Name + " -> " + pm + " -- invalid, reset");

        // you don't have to call Save() as changes are automatically
        // applied when an object goes out of scope, but this ensures
        // the change happens at the right time even if the script is
        // interrupted later
        user.Save();
      }
      else
      {
        Output.WriteLine("User " + user.Name + " -> " + pm + " -- OK");
      }     
    }
  }
}

FixPrimaryMailboxes();
Mailtraq Development and Escalation Support
Novitraq Incorporated
User avatar
Elric Pedder
Mailtraq Escalation Support
 
Posts: 2675
Joined: Tue Nov 23rd, 2004 1:16pm
Location: Montreal, Canada


Return to Mailtraq News

Who is online

Users browsing this forum: No registered users and 3 guests

cron