I'm not sure if this is doable or not, it depends solely on whether you can look at the spells a player has memorised and whether you can change them.
During the rest script this would happen before resting actually takes place (you'd probably also run this subscript when they log in)
1) What it would do for specialist wizards would be to look through their spell levels and make sure that ONE slot (lets make it the first one) is either empty or ONLY contains a spell of the particular sphere they specialise in. For spheres like divination there isnt a spell of each level 1-9 that you can use so certain replacement spells would need to be considered to be replacements.
E.g A level one illusionist having 3 magic missiles is illegal for example, ONE slot MUST be an illusion (or the nominated substitute spell)
When any spell level is illegal it will CLEAR the offending slots, and then stop rest and report what the problem is to the player so they can reselect and rest again.
This algorithm would simply look at the spheres of each spell in the spell level. If it doesnt find at least one of the specialist sphere (or the substitute spell) and there is no empty slots this is illegal.
2) For clerics it's much the same though the script would make sure that theres only ONE slot thats either empty or has one of the 2 possible domain spells in it. I'm not sure if you have access to reading what is in the domains.2da file .You cannot have duplicates of
Domains.2da will need to be redone and filled out with close-to canon list of spells for all domains level 1-9.
e.g. Burning hands is in the canon fire domain, if the search finds more than one burning hands the other ones are considered illegal and are cleared and the rest stops. A yellow message is sent to the player telling them the problem.
For this to be implemented also, it would require ALL clerics to have an override domains.2da that is equivalent to the canon domain spell list. Also the new added domains like darkness would also need to be re-selected for example, instead of getting darkness any number of times at level 1 you simply get one of your second level slots having the spell (or the spell from your other domain).
You would also need to run this script as clerics log-on to make sure they didnt select an illegal selection and log out before resting This isnt normally a problem except for when they come in after a server reset , logging on after server reset re-energies all slots.
I imagine the algorithm working by counting up the number of domain spells (either from domain one or domain two) in a spell level. Condition: If there is NO domain spells in that level and there is not one empty slot in this spell level then this is illegal.
Condition: If there is exactly one domain spell this is legal.
Condition: If there is more than 1 non-thislevel-cleric spell in the spell level this is illegal.
How possible is this? and is it desirable? I know a lot of people complain about the power level of clerics mostly because of the domain spells, this would restore them to canon. It all depends on what you can get access to from the server. Can you look to see what a player has memorised (or marked to be memorised when they rest)