Justin Shafer was looking at up to 15 years in prison for allegedly harassing an FBI agent and federal judge with emails and social media posts.

But on Thursday he walked out of the federal courthouse in Dallas, smiling and relieved that he is a free man, no longer facing the threat of prosecution.

Shafer pleaded guilty to a lesser misdemeanor charge as part of an agreement with the government, just four days before his trial was set to begin. At the same Thursday hearing, he was sentenced to time he already served behind bars — about eight months — while awaiting trial.

Shafer, 38, a computer technician from North Richland Hills, had faced four felony counts of online stalking and a charge of violating a law that protects people who perform "certain official duties."

Prosecutors previously argued that he harassed the FBI agent who was investigating him for computer hacking, as retaliation. Shafer countered that his actions were protected speech. Under Thursday's agreement, Shafer admitted to committing simple assault by sending intimidating messages on social media to the agent's wife.

Shafer's attorney, Tor Ekeland, a New York-based computer law specialist, said the favorable plea agreement came about after a federal judge recently expressed skepticism about the prosecutors' case.

Legal arguments in the trial would have added to a growing debate about a form of online harassment known as doxing that is testing the limits of free speech.

Doxing, which Shafer was accused of, occurs when someone posts a person's personal information online such as a home address, email address and place of employment. But it is not illegal unless someone makes clear and specific threats or does it while stalking the victim.

However, some have called for new laws addressing "malicious doxing." In June, a bill was introduced in Congress that would have criminalized doxing in some forms.

Shafer had been accused of causing "substantial emotional distress" to the FBI agent and his wife by making "derogatory and inflammatory statements" about him on Twitter and posting identifying information about his wife and other family members.

Senior Judge Janis Graham Jack, from the Southern District of Texas, accepted the plea agreement Thursday. The U.S. attorney's office in Dallas and the judge initially assigned to the case recused themselves.

Shafer did not make a statement during the speedy hearing. Under the plea, he also agreed not to contact the agent or his family or the Dallas judge and his staff. No fine or restitution was ordered.

Shafer's case generated interest in computer tech circles, which were abuzz with speculation about whether the FBI unfairly targeted him with stalking charges because agents could not make an illegal-hacking case.

Justin Shafer (N/A)

Ekeland said he took the case at a heavily discounted rate because it was such an egregious affront to the First Amendment. He said his client never made any direct or implied threats against the agent or his wife, and he didn't call for others to harass or harm them.

"If what he did was cyberstalking, they can go after almost anybody," Ekeland said.

He filed a motion in January to dismiss the indictment, arguing that his client's social media posts are protected by the First Amendment, and he blasted the government for an "outrageous prosecution."

Shafer, he said, merely wanted government officials to return his seized computers, which included family photos. But they ignored his inquiries, Ekeland said. Agents seized the items as part of their hacking investigation.

Assistant U.S. Attorney Douglas W. Gardner said in a previous court filing that Shafer "engaged in a barrage of harassing and intimidating actions."

He said the agent's wife obtained a firearm, lived somewhere "else for a time" and took other security measures out of concern over Shafer's "increasingly belligerent" actions.

Emotional distress

Shafer was arrested in March 2017 for posting about the agent and his wife on social media, which prosecutors say constituted harassment.

The agent was investigating him for the alleged hacking of medical patient data. Agents also suspected Shafer was associated with a notorious computer hacker or hacking group called TheDarkOverlord.

The government updated the indictment earlier this month to add a stalking charge for the voluminous emails Shafer sent to the magistrate judge who signed the FBI warrants authorizing two separate searches of his home.

Shafer contacted U.S. Magistrate Judge Jeffrey Cureton about his seized property and was ignored, Ekeland said.

"Everything in this case returns to Mr. Shafer typing at his keyboard seeking redress for his grievances from an unresponsive government," Ekeland said in his motion to dismiss.

Prosecutors previously said Shafer sent "hundreds of emails with attachments" to Cureton's government email address. "Some of the language in the emails was concerning and caused substantial emotional distress," prosecutors said in a court filing. They said the judge and his staff were concerned about the emails.

The sometimes insulting tweets and Facebook posts in question were posted from May 2016 through March 2017. Shafer posted a past home address for the agent as well as Facebook profiles of his mother, cousin, wife and ex-wife. Shafer also sent a Facebook friend request to the agent's wife and messaged her on Facebook, according to the FBI.

And Shafer followed the agent's wife on Twitter "in an attempt to harass and intimidate her," court records said.

Case law exists that protects crude and abusive tweets directed at another person. A federal district court in Maryland dismissed an indictment against a man in 2011 who was accused of causing a Buddhist religious leader emotional distress with his blogs and tweets about her.

In his tweets, the defendant told the victim to kill herself, among other things. But the judge ruled that the First Amendment protects "uncomfortable" speech online and that none of the tweets were "true threats." The judge also said the tweets and blog posts were similar to a physical bulletin board where recipients of unkind messages could simply ignore them.

Internet vigilante?

Shafer runs a business that does computer work for dental offices. On the side, he is a computer security professional, his lawyer says.

The FBI raided Shafer's house in January 2017 and found a database of about 48,000 records on his computer that TheDarkOverlord was selling, the FBI affidavit said.

Shafer said on his blog that he notified the FBI the year before that the hackers had shared their stolen records with him and he sent the bureau a copy of the database.

A Justin Shafer tweet to the FBI. (Twitter)

Shafer says he searches the internet for public, unsecured computer servers that store confidential patient medical information.

He says he tries to access data and then alerts companies that own the data that it is not secured. He documents his findings as well as his communications with the companies on his blog and Twitter account, sometimes posting screenshots.

Shafer also has reported his findings to federal agencies and news websites. Companies can be fined by the government for using outdated and unsecured technology to store protected data.

"Rather than reward Mr. Shafer for his efforts to make the internet safer, the FBI chose to engage in armed raid of his family home," Ekeland said in his motion to dismiss the charges.

Prosecutors previously called Shafer a "self-styled internet vigilante" who goes beyond merely reporting security breaches and instead improperly downloads the personal information of numerous medical patients.

On Thursday, Gardner told the judge that prosecutors in Dallas have agreed not to prosecute Shafer over the hacking allegations in the search warrant.

Ekeland said Americans have a First Amendment right to talk to the government, especially during a criminal prosecution.

"He asked for an answer and he got silence. And he kept on asking, and all he got was silence. And eventually he is charged with a crime," he said. "It's really, really dangerous what they did here."