How to programmatically disallow some users to comment on some nodes?
I would hook_form_alter the comment form, and use your logic to set #access on the form.  You may need to explicitly call menu_get_item to get your node to use in the logic.

You could also use a template_preprocess_node that would perform your logic, and hide or show the comment form in the node's render array based on your logic (this assumes it is in that render array, which I am not 100% positive about).