shifty has the essence of it. The actual mechanics are slightly more complicated, but most of it is handled by the script.

It should be possible to adapt the script to include AI players, but I'm slightly concerned that the AI may make deals with your faction without you having any choice.... I guess a way around that would be to take out diplomats