updating with fix for desktop/laptop use
[mnenc] / libmnenc / mnenc.cpp
index 0e162da..c2a414b 100644 (file)
@@ -6,6 +6,7 @@
 #include <ctime>
 #include <sys/stat.h>
 #include <sys/types.h>
+
 #include "mnenc.hpp"
 
 using namespace std;
@@ -69,8 +70,7 @@ string mnenc::genkey() {
        for(int i = 0; i < 15; i++) {
                key += (rand() % 10);  //generate a random number between 0 and 9
        }
-       string username = getenv("USER"); //Get username
-       string filename = "/home/" + username + "/.mnenc/.keyfile"; //This is where the key goes 
+       string filename = make_path() + ".keyfile"; //This is where the key goes 
        ofstream os;
        os.open(filename.c_str());
        if(os.is_open()) {
@@ -85,16 +85,17 @@ void mnenc::get_imei() {
         */
          
        string username = getenv("USER"); //Get username
-       string filename = "/home/" + username + "/.mnenc/.keyfile"; //This is where the key goes if we generate it
+       string filename = make_path() + ".keyfile"; //This is where the key goes if we generate it
        string cmd = "dbus-send --system --print-reply --type=method_call --dest=com.nokia.phone.SIM /com/nokia/phone/SIM/security Phone.Sim.Security.get_imei | grep string | sed -e 's/^.*\\\"\\([0-9]\\)/\\1/' -e 's/\\\"//'"; //Command used when shelling out to get imei
-       if(system(cmd.c_str()) == 0) { //if we can get imei from dbus
-               FILE *fp; //a file pointer
-               char buffer[1024]; //Some tmp storage
-               fp = popen(cmd.c_str(), "r"); //Open a pipe whith the command
-               while ( fgets(buffer, 1024, fp) != NULL ) //Read the result
-                       imei.append(buffer); //Add the result to our attribute
-               pclose(fp); //Close the pipe
-        } else { //If we could not get imei
+       
+       FILE *fp; //a file pointer
+       char buffer[1024]; //Some tmp storage
+       fp = popen(cmd.c_str(), "r"); //Open a pipe whith the command
+       while ( fgets(buffer, 1024, fp) != NULL ) //Read the result
+               imei.append(buffer); //Add the result to our attribute
+       pclose(fp); //Close the pipe
+
+    if(!is_imei(imei)) { //If we could not get imei
                ifstream keyfile(filename.c_str()); //open the keyfile
                string k = "";
                string line = "";
@@ -149,11 +150,40 @@ string mnenc::get_user() { // Gets username (Added by hexagon 2010-01-17)
     return user;
 }
 
-void mnenc::make_folder() {
-       string username = getenv("USER"); //Get username
-       string foldername = "/home/" + username + "/.mnenc"; //This is where the key goes 
-       int ret = chdir(foldername.c_str());
+void mnenc::make_folder() { //Create home directory
+       string dirname = make_path(); 
+       int ret = chdir(dirname.c_str());
        if(ret != 0) {
-               mkdir(foldername.c_str(), 0755);
+               mkdir(dirname.c_str(), 0755);
        }
 }
+
+bool mnenc::is_int(char c) { //See if a char is an int
+       return (c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7' || c == '8' || c == '9');
+}
+
+bool mnenc::is_imei(string str) { //See if a string is made up of 15 ints
+       bool proper_length = false; //Is it 15 chars?
+       bool is_ints = true; //Is it only ints?
+       if(str.length() == 15) { //Check length
+               proper_length = true;
+       } else {
+               return false; //Exit early if it fails length test
+       }
+       for(int i = 0; i < (signed) str.length(); i++) { //Check if they are all ints
+               if(!is_int(str[i])) {
+                       is_ints = false;
+               }
+       }
+       if(proper_length && is_ints) { //Return true if it passes the tests
+               return true;
+       } else {
+               return false; //And false if it doesnt
+       }
+}
+
+string mnenc::make_path() { //Create path to qtify home
+       string username = getenv("USER"); //Get username
+       string path = "/home/" + username + "/.qtify/"; //This is home directory of qtify
+       return path;
+}