Exemplo n.º 1
0
void fetion_login(PurpleAccount * account)
{
    PurpleConnection *gc;
    struct fetion_account_data *sip;
    gchar **userserver;
    gint ret;

    const char *username = purple_account_get_username(account);
    gc = purple_account_get_connection(account);
    gc->proto_data = sip = g_new0(struct fetion_account_data, 1);
    sip->gc = gc;
    sip->tg = 0;		//temp group chat id
    sip->cseq = 0;
    sip->account = account;
    sip->registerexpire = 400;
    sip->reregister = time(NULL) + 100;
    sip->txbuf = purple_circ_buffer_new(0);
    sip->impresa = NULL;
    sip->icon_buf = purple_circ_buffer_new(0);
    sip->GetContactFlag = 0;

    purple_debug_info("Fetion:", "shit\n");
    userserver = g_strsplit(username, "@", 2);
    purple_connection_set_display_name(gc, userserver[0]);
    if (IsCMccNo(userserver[0])) {
        sip->username = NULL;
        sip->mobileno = g_strdup(userserver[0]);
    } else {
        sip->mobileno = NULL;
        sip->username = g_strdup(userserver[0]);
    }
    //      sip->servername = g_strdup(userserver[1]);
    sip->SysCfgServer = g_strdup("nav.fetion.com.cn");
    sip->password = g_strdup(purple_connection_get_password(gc));
    g_strfreev(userserver);

    sip->buddies =
        g_hash_table_new((GHashFunc) fetion_ht_hash_nick,
                         (GEqualFunc) fetion_ht_equals_nick);
    sip->tempgroup =
        g_hash_table_new((GHashFunc) fetion_ht_hash_nick,
                         (GEqualFunc) fetion_ht_equals_nick);
    sip->group =
        g_hash_table_new((GHashFunc) fetion_ht_hash_nick,
                         (GEqualFunc) fetion_ht_equals_nick);
    sip->group2id =
        g_hash_table_new((GHashFunc) fetion_ht_hash_nick,
                         (GEqualFunc) fetion_ht_equals_nick);

    purple_connection_update_progress(gc, _("Connecting"), 1, 2);

    /* TODO: Set the status correctly. */
    sip->status = g_strdup("available");
    sip->registertimeout =
        purple_timeout_add(60000, (GSourceFunc) LoginToSsiPortal, sip);
    //Try to get systemconfig
    sip->ServerVersion = NULL;
    sip->ServiceNoVersion = NULL;
    sip->ParaVersion = NULL;
    sip->HttpAppVersion = NULL;
    sip->ClientCfgVersion = NULL;
    sip->HintsVersion = NULL;
    ret = ParseCfg(sip);
    //if(ret!=0)
    sip->SysCfg.conn =
        purple_proxy_connect(NULL, sip->account, sip->SysCfgServer, 80,
                             (PurpleProxyConnectFunction) RetriveSysCfg,
                             sip);

}
Exemplo n.º 2
0
int main(int argc, char **argv) {
	int c, once, nagios, nagiosexit;
	double temperature, mintemp, maxtemp;
	char *myname, *bmyname, *temp;
	pid_t mypid, sid;
	time_t now;
	struct stat fileStat;
	FILE *pidfile;
	char buf[BUFSIZE], theprobe[BUFSIZE];

	foreground = verbose = numprobes = dolog = dorrd = once = nagios = nagiosexit = 0;
	mintemp = maxtemp = 0.0;
	interval = 300;		// Default interval is 300 seconds (5 minutes)
	memset(theprobe, 0, sizeof(theprobe));

	ParseCfg(CFGFILE);	// Parse CFG now, allowing cmdline to override

	while ((c = getopt (argc, argv, "fvlroi:p:m:M:")) != -1) {
		switch (c) {
			case 'f':
				foreground = 1;
				break;
			case 'v':
				verbose = 1;
				break;
			case 'l':
				dolog = 1;
				break;
			case 'r':
				dorrd = 1;
				break;
			case 'o':
				once = 1;
				break;
			case 'p':
				strncpy (theprobe, optarg, BUFSIZE-1);
				once = nagios = 1;
				break;
			case 'm':
				mintemp = atof(optarg);
				break;
			case 'M':
				maxtemp = atof(optarg);
				break;
			case 'i':
				interval = atoi(optarg);
				if (interval == 0) {
					// atoi is crap. Need to make sure interval is real
					fprintf(stderr, "Interval can not be zero\n");
					exit(EXIT_FAILURE);
				}
				break;
			default:
				printf("usage: %s [ -v ] [ -f ] [ -o ] [ -i interval ] [ -l | -r ]\n", argv[0]);
				exit(EXIT_FAILURE);
		}
	}

	if ((!dolog && !dorrd) && !once) {
		fprintf(stderr, "Please use either logging (-l) or rrd (-r) output\n");
		exit(EXIT_FAILURE);
	}

	if (once) foreground = 1;

	if ((mintemp || maxtemp) && !nagios) {
		fprintf(stderr, "Warning: Minimum and Maximum temperature switches are ignored without -p\n");
	}

// Locate the probes.
	numprobes = findprobes();
	if (numprobes == 0) {
		fprintf(stderr, "Error: No DS18B20 compatible probes located.\n");
		fprintf(stderr, "Please make sure the w1-gpio and w1-therm drivers.\n");
		fprintf(stderr, "are loaded into the kernel.\n");
		exit(EXIT_FAILURE);
	}

	if (verbose) fprintf(stderr, "Probe on commandline is '%s'\n", theprobe);

// Lets see if our logdir exists and is a directory
	if (!foreground) {
		if (stat(LOGROOT, &fileStat) < 0) {
			fprintf(stderr, "Error: %s does not exist.\n", LOGROOT);
			exit(EXIT_FAILURE);
		}
		if (!S_ISDIR(fileStat.st_mode)) {
			fprintf(stderr, "Error: %s is not a directory.\n", LOGROOT);
			exit(EXIT_FAILURE);
		}
		if (verbose) fprintf(stderr, "Log folder '%s' is OK.\n", LOGROOT);

// Now to open all the logfiles
		if ((openlogs()) != numprobes) {
			fprintf(stderr, "Something went wrong opening files.\n");
			exit(EXIT_FAILURE);
		}
	}
// All looks good. Time to prep the main loop.
	if (foreground) {
		if (!nagios)
			fprintf(stderr, "Running in the foreground, not daemonizing.\n");
	} else {
		mypid = fork();
		if (mypid < 0)
			exit(EXIT_FAILURE); // Could not fork.
		if (mypid > 0)
			exit(EXIT_SUCCESS); // Parent can exit here
		// New child process from here.
		sid = setsid();
		if (sid < 0)
			exit(EXIT_FAILURE);
		if ((chdir(LOGROOT)) < 0)
			exit(EXIT_FAILURE);
		// Close the standard file descriptors
		close(STDIN_FILENO);
		close(STDOUT_FILENO);
		close(STDERR_FILENO);
	}

	if (!foreground) {
		mypid = getpid();	// Get my pid
		myname = strdup(argv[0]);
		bmyname = basename(myname);
		snprintf(buf, sizeof(buf)-1, "/var/run/%s.pid", bmyname);
		if ((pidfile = fopen(buf, "w")) == NULL) {
			fprintf(stderr, "Unable to open pidfile '%s'. '%s'\n", buf, strerror(errno));
			exit(EXIT_FAILURE);
		}
		fprintf(pidfile, "%d\n", mypid);
		fflush(pidfile);
		fclose(pidfile);
		openlog(bmyname, LOG_PID|LOG_CONS, LOG_USER);
	}

// SIGHUP makes us close and reopen logs. This is probably a FIXME as signal() is really
// not portable at all and can even behave differently on some Linux systems.
	signal(SIGHUP, handle_sighup);

// Main loop
	while(1) {
		// rewind the logs and read them
		for (c = 0; c < numprobes; c++) {
			probefd[c] = fopen(probepath[c], "r");
			if (probefd[c] == NULL) {
				if (foreground) {
					fprintf(stderr, "Error: Unable to open '%s': %s\n",
						probepath[c], strerror(errno));
				} else {
					syslog(LOG_ERR, "Error: Unable to open '%s': %s\n",
						probepath[c], strerror(errno));
				}
				exit(EXIT_FAILURE);
			}

// First lets see if we are looking for a single probe only
			if (nagios) {
				if (strncasecmp(probename[c], theprobe, PROBENAMELEN-1))
					continue;
			}

/*
 * Example output from a probe. It's two lines and we want the t= part:
 * 5c 01 4b 46 7f ff 04 10 a1 : crc=a1 YES
 * 5c 01 4b 46 7f ff 04 10 a1 t=21750
 */
			fgets(buf, sizeof(buf)-1, probefd[c]);	// we always want line 2
			memset(buf, 0, sizeof(buf));		// so we ignore the first read
			fgets(buf, sizeof(buf)-1, probefd[c]);
			temp = strtok(buf, "t=");		// C stringparsing rules! :)
			temp = strtok(NULL, "t=");		// See?  Easy as pie.
			temperature = atof(temp)/1000;		// change "21750" into "21.750"
			if (dolog && !once) {
				now = time(NULL);
				fprintf(logfd[c], "%u %2.3f\n", (unsigned int) now, temperature);
				fflush(logfd[c]);			// Probably unnecessary to flush.
			}
			if (once) {
				if (nagios) {
					if (mintemp && temperature < mintemp) {
						fprintf(stdout, "CRITICAL: Temperature of probe '%s' is %2.3f which is below %2.3f | 'temperature'=%2.3f\n",
							probename[c], temperature, mintemp, temperature);
						nagiosexit = 2;
					} else if (maxtemp && temperature > maxtemp) {
						fprintf(stdout, "CRITICAL: Temperature of probe '%s' is %2.3f which is over %2.3f | 'temperature'=%2.3f\n",
							probename[c], temperature, maxtemp, temperature);
						nagiosexit = 2;
					} else {
						fprintf(stdout, "OK: Temperature of probe '%s' is %2.3f | 'temperature'=%2.3f\n",
							probename[c], temperature, temperature);
						nagiosexit = 0;
					}
				} else {
					fprintf(stdout, "%s:\t%2.3f\n", probename[c], temperature);
				}
			}
			fclose(probefd[c]);
		}
		if (nagios) exit(nagiosexit);
		if (once) exit(EXIT_SUCCESS);
		sleep(interval);
	}
	exit(EXIT_SUCCESS);	// Never reached. Makes gcc -Wall shut up.
}