AddEditAlias.pas
上传用户:dh8233980
上传日期:2014-10-16
资源大小:1015k
文件大小:8k
- unit AddEditAlias;
- (******************************************************************************)
- (* *)
- (* Hermes Add / Edit User Alias Dialog Box *)
- (* Part of Hermes SMTP/POP3 Server. *)
- (* Copyright(C) 2000 by Alexander J. Fanti, All Rights Reserver Worldwide. *)
- (* *)
- (* Created January 11, 2000 by Alexander J. Fanti. See License.txt *)
- (* *)
- (* Used by: ManageAliases *)
- (* Uses: DataU1 *)
- (* *)
- (* Description: This Modal dialog window pulls double duty, allowing both *)
- (* adding and editing of user aliases. *)
- (* Adding: Let user specify AliasID and select user *)
- (* Editing: Let user re-specify user, but not change ID *)
- (* *)
- (* Revisions: 1/21/2000 AJF Commented and re-wrote OK button procedure to *)
- (* clarify behavior *)
- (* *)
- (******************************************************************************)
- interface
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- StdCtrls, Buttons;
- type
- TfrmAddEditAlias = class(TForm)
- Label1: TLabel;
- txtAlias: TEdit;
- Label2: TLabel;
- lstUsers: TComboBox;
- Label3: TLabel;
- btnOK: TBitBtn;
- btnCancel: TBitBtn;
- procedure FormShow(Sender: TObject);
- procedure btnOKClick(Sender: TObject);
- private
- { Private declarations }
- AliasID, AliasUser : String; // The ID and Username if the Alias we're
- // Adding or Editing
- procedure DisplayInfo; // Display the Alias information in the dialog
- public
- { Public declarations }
- function Add : Boolean; // Add a new Alias
- function Edit(Alias : String) : Boolean; // Edit an existing alias
- // Both functions return true if successful, false if not (cancelled)
- end;
- var
- frmAddEditAlias: TfrmAddEditAlias;
- implementation
- uses DataU1;
- {$R *.DFM}
- procedure TfrmAddEditAlias.FormShow(Sender: TObject);
- begin
- // Fill the ListBox with the user names
- lstUsers.Clear;
- INI.User_GetList(lstUsers.Items);
- // display the alias and user represented
- DisplayInfo;
- // If we can, set focus on the alias edit box, unless it's disabled
- // (edit mode) in which case, we focus on the user list box.
- if txtAlias.Enabled then txtAlias.SetFocus else lstUsers.SetFocus;
- end;
- procedure TfrmAddEditAlias.DisplayInfo;
- var
- x : Longint;
- begin
- // display the Alias of interest
- txtAlias.Text := AliasID;
- // and find and select the user it represents
- lstUsers.ItemIndex := -1;
- for x := 0 to lstUsers.Items.Count -1 do
- if lstUsers.Items[x] = AliasUser then lstUsers.ItemIndex := x;
- end;
- function TfrmAddEditAlias.Add : Boolean;
- begin
- // We want to use the dialog as an "Add" dialog...
- Caption := 'Add Alias'; // Set the dialog caption to reflect usage
- txtAlias.Enabled := True; // enable the Alias edit box so the user
- // can specify an alias name
- AliasID := ''; // clear the alias we're editing
- AliasUser := ''; // clear the alias user we're editing
- Result := ShowModal = mrOK; // let the user do their thing
- end;
- function TfrmAddEditAlias.Edit(Alias : String) : Boolean;
- begin
- // We want to use the dialog as an "Edit" dialog...
- Caption := 'Edit Alias'; // Set the dialog caption to reflect usage
- txtAlias.Enabled := False; // disable the Alias edit box so the user
- // can't change the alias name
- INI.Alias_Parse(Alias, AliasID, AliasUser);
- // fetch the alias and alias user we'll be editing from the alias passed in.
- // this may be just an alias, or it may be an entire alias string in the
- // form: AliasID ALIASSEPERATOR AliasUser. The Alias_Parse routine goes
- // both ways... at least it better...
- Result := ShowModal = mrOK; // let the user do their thing
- end;
- procedure TfrmAddEditAlias.btnOKClick(Sender: TObject);
- var
- Editing : Boolean; // We'll set this to true to know we're editing...
- // it just makes it that much more readable...
- begin
- // First, let's fetch back the AliasID and AliasUser we've been working with.
- // Remember, it we're editing, the AliasID Edit Box was disabled so it's
- // still got the right AliasID (the one we're editing...
- AliasID := Trim(txtAlias.Text);
- AliasUser := '';
- if lstUsers.ItemIndex > -1 then
- AliasUser := Trim(lstUsers.Items[lstUsers.ItemIndex]);
- Editing := not txtAlias.Enabled; // we know we're editing by the condition
- // of the AliasID Edit Box (Enabled <> Edit)
- // Here, we can make the checks we'll have to do whether we're adding or
- // editing an alias...
- // Like being specified at all...
- if AliasID = '' then begin
- ShowMessage('The Alias requires an ID');
- ModalResult := mrNone; Exit;
- end;
- if AliasUser = '' then begin
- ShowMessage('The Alias requires a User');
- ModalResult := mrNone; Exit;
- end;
- // We behave differently based on editing or adding...
- if Editing then begin
- // We're editing an alias, it should already exist...
- if not INI.Alias_Exists(AliasID) then begin
- // This should never happen, but if it does...
- ShowMessage('The Alias must exist to be edited.');
- ModalResult := mrCancel; Exit;
- end;
- // Find the AliasID and change its AliasUser to this one...
- INI.Alias_Edit(AliasID, AliasUser);
- end else begin
- // We're creating a new alias...
- // First we check to be certain the AliasID doesn't contain any
- // invalid characters (we are allowing the @ sign!)
- if not IsNameValidIncludingAt(AliasID) then begin
- ShowMessage('The Alias ID cannot contain ' + INVALIDNAMECHARACTERS);
- ModalResult := mrNone; Exit;
- end;
- if not IsNameValid(AliasID) then begin
- ShowMessage('Warning: You are making a domain specific Alias.');
- end;
- // Or the "AliasSeperator" string we use for internal storage of the
- // aliasID and AliasUser in a single string...
- if Pos(ALIASSEPERATOR, AliasID) > 0 then begin
- ShowMessage('The Alias ID cannot contain "' + ALIASSEPERATOR + '".');
- ModalResult := mrNone; Exit;
- end;
- // Then we check to be sure the name's available (it hasn't been used
- // already for a User Name or Mailing List Name)
- if INI.User_Exists(AliasID) then begin
- ShowMessage('The Alias ID is already taken by a User.');
- ModalResult := mrNone; Exit;
- end;
- if INI.List_Exists(AliasID) then begin
- ShowMessage('The Alias ID is already taken by a Mail List.');
- ModalResult := mrNone; Exit;
- end;
- // Finally, we verify the Alias does not exist, and we create it.
- if INI.Alias_Exists(AliasID) then begin
- ShowMessage('The Alias already exists, please specify a different ID.');
- ModalResult := mrNone; Exit;
- end;
- // if we've gotten this far, we can safely create the alias.
- // at each check along the way, we would have been thrown out of this
- // procedure by the "ModalResult := mrNone; Exit;" line which
- // would have left the user looking at the dialog again.
- INI.Alias_Create(AliasID, AliasUser);
- end;
- end;
- end.