Exemple #1
0
int main(int argc, char *argv[])
{
	GoogleVoice gv;
	int a,d,r;

	string an,am;
	string au,ap;		// These can be set as your GV user/passwd (so they dont have to be specified via CLI each time).
						// eg: au="mygmailuser"; ap="mysecretpasswd";

	while((a=getopt(argc,argv, "d::hvu:p:n:m:")) != -1)
	{
		switch(a)
		{
			case 'h':	// help
				PrintUsage(1); return 0;
			case 'v':	// version.
				printf("GoogleVoice API by mm_202.  %s (%s).  http://github.com/mastermind202/GoogleVoice\n\n", gv.GetVersion().c_str(), BUILDTS);
				return 0;
			case 'd':	// debug mode.
				if(optarg) d=atoi(optarg); else d=1;
				gv.debug=d;
				break;
			case 'u':	// username
				au=optarg;
				break;
			case 'p':	// password
				ap=optarg;
				break;
			case 'n':	// number
				an=optarg;
				break;
			case 'm':	// message
				am=optarg;
				break;
			case '?':
				PrintUsage(); return 0;
		}
	}
	if(an.empty() || am.empty() || au.empty() || ap.empty()) {PrintUsage(1); return 1;}

	printf("GoogleVoice API by mm_202.\n");

	if(gv.Init()) {cout << "GoogleVoice() failed to initialize.  Blaming curl.  Dying.\n"; return -1;}

	r = gv.Login(au,ap);
	if((gv.debug&1) || r) printf("gv.Login() returned %d.\n\n", r);

	r = gv.SendSMS(an,am);
	if((gv.debug&2) || r) printf("gv.SendSMS() returned %d.\n\n", r);
	if(!r) printf("SMS send successfully.\n");

	return r;
}
Exemple #2
0
int main(int argc, char *argv[])
{
	GoogleVoice gv;
	int a,d,r;
    bool check_sms = false, send_sms = false, delete_sms = false, contact_info = false;
    string keyword="";
	string an,am;
	string au,ap;

    bool config = true;

	while((a=getopt(argc,argv, "d::h?vu:p:irck:n:m:")) != -1)
	{
		switch(a)
		{
            case 'i':
                contact_info = true;
                break;
			case 'd':	// debug mode.
				if(optarg) d=atoi(optarg); else d=1;
                printf("Debugging level %d enabled\n",d);
				gv.debug=d;
				break;
			case 'u':	// username
                config=false;
				au=optarg;
				break;
			case 'p':	// password
				ap=optarg;
				break;
			case 'n':	// number
				an=optarg;
				break;
			case 'm':	// message
				am=optarg;
                send_sms = true;
				break;
            case 'r':   // recieve messages (delete)
                check_sms = true;
                delete_sms = true;
                break;
            case 'c':   // check messages (mark as read)
                check_sms = true;
                break;
            case 'k':   // keyword for checking messages
                keyword = optarg;
                break;
			case '?':
			case 'h':	// help
				PrintUsage(1); return 0;
			case 'v':	// version.
				printf("GoogleVoice API by Steven Hickson.  %s.  https://github.com/StevenHickson/PiAUISuite\n\n", "2.0");
				return 0;
		}
	}
    if((send_sms && an.empty()) || (!keyword.empty() && !check_sms) || (delete_sms && !check_sms) || (ap.empty() ^ au.empty())) {
        printf("Error, improper flag combination\n");
        PrintUsage(1);
    }

	if(gv.Init()) {cout << "GoogleVoice() failed to initialize.  Blaming curl.  Dying.\n"; return -1;}
    
    //Let's fill in the config options if they weren't specified
    if(config) {
        FILE *fp;
        char *passPath;
        passPath = getenv("HOME");
        if(passPath == NULL) {
            printf("Could not get $HOME\n");
            return -1;
        }
        string path = string(passPath);
        path += "/.gv";
        fp = fopen(path.c_str(),"r");
        if(!fp) {cout << "Couldn't open password file. Dying\n"; return -1;}
        char buf1[100];
        char buf2[100];
        fscanf(fp,"%s\n%s\n",buf1,buf2);
        fclose(fp);
        au = string(buf1);
        ap = string(buf2);
    }

	r = gv.Login(au,ap);
	if((gv.debug&1) || r) printf("gv.Login() returned %d.\n\n", r);
    
    if(contact_info) {
        gv.GetContactInfo();  
    } else if(check_sms) {
        string results;
        string number = "";
        if(!an.empty())
            number += an;
        r = gv.CheckSMS(results,number,keyword,delete_sms);
        printf("%s\n",results.c_str());
        if(!r && gv.debug) printf("SMS Checked Successfully.\n");
    } else {
	    r = gv.SendSMS(an,am);
	    if((gv.debug&2) || r) printf("gv.SendSMS() returned %d.\n\n", r);
	    if(!r) printf("SMS sent successfully.\n");
    }
    
	return r;
}
int main(int argc, char *argv[])
{
    GoogleVoice gv;
    int r;
    if(argc == 2) gv.debug = 3;
    //read username, password, valid number, and keyword from safe file
    FILE *fp;
    char *passPath;
    passPath = getenv("HOME");
    if(passPath == NULL) {
        printf("Could not get $HOME\n");
        return -1;
    }
    string path = string(passPath);
    path += "/.gtext";
    fp = fopen(path.c_str(),"r");
    if(!fp) {
        cout << "Couldn't open password file. Dying\n";
        return -1;
    }
    char buf1[100];
    char buf2[100];
    char buf3[100];
    char buf4[100];
    fscanf(fp,"%s\n%s\n%s\n%s",buf1,buf2,buf3,buf4);
    fclose(fp);

    string username = string(buf1);
    string password = string(buf2);
    string keyword = string(buf3);
    string number = string(buf4);

    //Log in to google voice
    if(gv.Init()) {
        cout << "GoogleVoice() failed to initialize.  Blaming curl.  Dying.\n";
        return -1;
    }
    r = gv.Login(username,password);
    if((gv.debug&1) || r) printf("gv.Login() returned %d.\n\n", r);

    //Check SMS
    string results;
    r = gv.CheckSMS(results,number,keyword,true);
    //printf("%s\n",results.c_str());
    if(!r && gv.debug) printf("SMS Checked Successfully.\n");

    //run given command
    if(!results.empty()) {
        FILE *pf;
        pf = popen(results.c_str(),"r");

        if(!pf) {
            cout << "Could not open command pipe. Dying\n";
            return -1;
        }

        //Grab data from process execution
        char buffer[DATA_SIZE];
        string message = "";
        while(!feof(pf)) {
            if(fgets(buffer, DATA_SIZE, pf) != NULL) {
                message += string(buffer);
            }
        }
        //printf("%d: %s\n",count,message.c_str());

        if (pclose(pf) != 0) {
            cout << "Could not close command pipe. Dying\n";
            return -1;
        }
        replace_all(message, "\t", " ");
        //Send return SMS
        int mLen = message.length();
        if(mLen <= 320)
            r = gv.SendSMS(number,message);
        else {
            //I need to split this into multiple messages
            string::iterator it = message.begin();
            int count = 0;
            string section = "";
            while(it != message.end()) {
                section += *it;
                ++it;
                ++count;
                if(count >= 319) {
                    r = gv.SendSMS(number, section);
                    count = 0;
                    section.clear();
                }
            }
            r = gv.SendSMS(number, section);
        }
        if((gv.debug&2) || r) printf("gv.SendSMS() returned %d.\n\n", r);
        if(gv.debug&1 && !r) printf("SMS send successfully.\n");
    }
    return r;
}