Created
January 22, 2011 16:25
-
-
Save jbrechtel/791216 to your computer and use it in GitHub Desktop.
wpa_supplicant.conf parser in scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.nevercertain.wifipasswords | |
import scala.collection.JavaConversions._ | |
object NetworkParser { | |
def parse(networksString: String): Seq[WifiNetwork]= { | |
val networkSections = networksString.split("network=").filter(_.trim.size > 0) | |
networkSections.map((networkSection: String) => { | |
val name = parseToken(networkSection, "ssid") | |
new WifiNetwork(name, networkPassword(networkSection), networkType(networkSection)) | |
}) | |
} | |
private def networkType(networkSection: String): NetworkType.Value = { | |
if(hasToken(networkSection,"wep_key0")) | |
NetworkType.WEP | |
else if(hasToken(networkSection,"psk")) | |
NetworkType.WPA | |
else | |
NetworkType.OPEN | |
} | |
private def networkPassword(networkSection: String) = { | |
val ntype = networkType(networkSection) | |
ntype match { | |
case NetworkType.WPA => parseToken(networkSection, "psk") | |
case NetworkType.WEP => parseToken(networkSection, "wep_key0") | |
case _ => "" | |
} | |
} | |
private def hasToken(networkSection: String, tokenName: String) = { | |
tokenLines(networkSection, tokenName).size > 0 | |
} | |
private def tokenLines(networkSection: String, tokenName: String) = { | |
networkSection.lines.filter(_.trim.startsWith(tokenName)) | |
} | |
private def parseToken(networkSection: String, tokenName: String) = { | |
tokenLines(networkSection, tokenName).next.split("=")(1).replace("\"","") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment