In the storm-tossed Middle East, suddenly and unexpectedly there’s a silver lining in a most unlikely place, Iran.

For years, the United States and its allies have been treated as Iran’s enemies and have had little choice but to accept that status. With the bellicose Mahmoud Ahmadinejad as president of Iran for eight years, Washington and its friends had to endure insults and hostility while fretting about Iran’s controversial nuclear ambitions.

The rift over the nuclear issue was so serious — and so hopeless — that the western nations felt their only recourse was to impose increasingly damaging economic sanctions on Iran. It is at least conceivable that the effects of these sanctions contributed significantly to the pending change of command in Teheran.

For all his bluster on international matters, Ahmadinejad’s behavior at home was also so controversial that a rift developed between him and Ayotollah Ali Khamenei, Iran’s supreme leader. Although he was ineligible to run for a third four-year term, Ahmadinejad’s diminished popularity at home surely contributed to the victory earlier this month of an avowed moderate, Hassan Rouhani, who becomes Iran’s president on Aug. 3.

Rouhani, in direct contrast to Ahmadinejad, has promised to engage with the west while allowing greater political freedom at home. That’s certainly a pledge to be welcomed (if cautiously) by those nations frustrated by Ahmadinejad’s contentious approach to negotiations, particularly over the issue of Iran’s nuclear program.

Rouhani’s huge victory margin caught many by surprise, especially since many veteran observers of Iranian politics had expected the religious establishment (led by the ayotollah) to use its authority to deny him a place on the ballot because of his moderate views.

In his campaign, Rouhani had emphasized the importance he gave to the notion of listening to the Iranian majority, which is something the establishment hasn’t done. There have been clear signals for some time now that the majority was unhappy with the way the country has been ruled, so his position no doubt won him many votes.

Rouhani also made it clear he’s determined that Iran will avoid the kind of destructive turmoil that’s been happening all round the area.

“In our region, there were some countries who miscalculated their positions, and you have witnessed what happened to them,” he said in a televised speech. “The world is in a transitional mood, and a new order has yet to be established.”

Then he added a warning: “If we miscalculate our national situation, it will be detrimental for us.”

Perhaps his most surprising remarks had to do with the situation in neighboring Syria, where Iran has been a major supporter of the embattled president, Bashar al-Assad. Rouhani warned the Iranian people to avoid embracing a double standard in respect to the brutality that’s been occurring on both sides of the Syrian civil war.

“We should not describe as oppressive brutal actions in an enemy country while refraining from calling the same actions oppressive if they take place in a friendly country,” he said. “Brutality must be called brutality.”

Then he summed up his thinking: “The majority … voted for moderation, collective wisdom, insight and consultation. Everybody should accept the people’s vote — the government should accept the people’s vote. The people have chosen a new path.”

Only time will tell whether Rouhani’s promising beginning delivers a welcome change in the relations between Tehran and Washington, or if the extremely conservative religious establishment will tolerate his push for moderation. And there’s always the reminder, provided most painfully by Egypt, that good beginnings don’t guarantee good endings.