Esempio n. 1
0
void reload(GLOBAL *g, struct notify_module *n)
{
	QueryHandle *res, *result;
	char *mailfile = 0;
	char *command;
	int i, j; 
	double balance;

	res = g->db->query(g->db->conn, "SELECT customers.id AS id, pin, name, lastname, "
		"SUM(cash.value) AS balance, "
		"(SELECT contact FROM customercontacts WHERE customerid = customers.id AND type = 8"
		"	ORDER BY id LIMIT 1) AS email "
		"FROM customers "
		"LEFT JOIN cash ON customers.id = cash.customerid "
		"WHERE deleted = 0 AND email IS NOT NULL "
		"GROUP BY customers.id, customer.name, lastname, email, pin");
	
	if( g->db->nrows(res) )
	{
		for(i=0; i<g->db->nrows(res); i++) 
		{
			balance = atof(g->db->get_data(res,i,"balance"));
			
			if( balance < n->limit ) 
			{
				command = strdup(n->command);
				mailfile = load_file(n->mailtemplate);
			
				if( mailfile ) 
				{
					if( strstr(mailfile, "%last_10_in_a_table") )
					{
						char *date, *value, *comment, *temp, *temp2;
						char *last_ten = strdup("");
							
						result = g->db->pquery(g->db->conn, "SELECT comment, time, value FROM cash WHERE customerid = ? ORDER BY time DESC LIMIT 10", g->db->get_data(res,i,"id"));
						
						for(j=0; j<g->db->nrows(result); j++) 
						{
							date = utoc(atof(g->db->get_data(result,j,"time")));
							value = g->db->get_data(result,j,"value");
							comment = g->db->get_data(result,j,"comment");
						
							temp = (char *) malloc(strlen(date)+strlen(value)+strlen(comment)+12);	
							sprintf(temp, "%s\t | %s\t\t | %s\n", date, value, comment);
						
							temp2 = g->str_concat(last_ten, temp);
							free(last_ten);
							last_ten = strdup(temp2);
							free(temp2);
							free(temp);
							free(date);
						}
															
						g->str_replace(&mailfile, "%last_10_in_a_table", last_ten);
						
						g->db->free(&result);
						free(last_ten);
					}
					
					g->str_replace(&mailfile, "%saldo", g->db->get_data(res,i,"balance"));
					g->str_replace(&mailfile, "%B", g->db->get_data(res,i,"balance"));
					g->str_replace(&mailfile, "%b", balance < 0 ? ftoa(balance * -1) : g->db->get_data(res,i,"balance"));
					g->str_replace(&mailfile, "%pin", g->db->get_data(res,i,"pin"));
					g->str_replace(&mailfile, "%name", g->db->get_data(res,i,"name"));
					g->str_replace(&mailfile, "%lastname", g->db->get_data(res,i,"lastname"));
				
					if( write_file(n->file, mailfile) < 0 )
						syslog(LOG_ERR, "[%s/notify] Unable to write temporary file '%s' for message", n->base.instance, n->file);
					free(mailfile);
				
					if( strlen(n->debugmail) < 1 )
						g->str_replace(&command, "%address", g->db->get_data(res,i,"email"));
					else
						g->str_replace(&command, "%address", n->debugmail);
					system(command); 
				}
				free(command);
			}
		}
#ifdef DEBUG1
		syslog(LOG_INFO, "DEBUG: [%s/notify] reloaded",n->base.instance);
#endif
	}
	else
		syslog(LOG_ERR, "[%s/notify] Unable to read database", n->base.instance);

	g->db->free(&res);
	free(n->command);
	free(n->file);
	free(n->mailtemplate);
	free(n->debugmail);
}
Esempio n. 2
0
void reload(GLOBAL *g, struct ggnotify_module *n)
{
    QueryHandle *res, *result;
    char *message = 0;
    int i, j;
    double balance;
    struct gg_session *sess;
    struct gg_login_params p;

    memset(&p, 0, sizeof(p));
    p.uin = n->uin;
    p.password = n->passwd;

    // Najpierw po³±czmy siê z serwerem GG
    if( !(sess = gg_login(&p)) )
    {
        syslog(LOG_ERR, "[%s/ggnotify] Unable to connect to Gadu-Gadu server.", n->base.instance);
        gg_free_session(sess);
    }
    else
    {
#ifdef DEBUG1
        syslog(LOG_INFO, "DEBUG: [%s/ggnotify] Connected to Gadu-Gadu server.",n->base.instance);
#endif

        res = g->db_query(g->conn,
                          "SELECT customers.id AS id, pin, name, lastname, "
                          "SUM(cash.value) AS balance, imessengers.uid AS im "
                          "FROM customers "
                          "LEFT JOIN imessengers ON customers.id = imessengers.customerid "
                          "LEFT JOIN cash ON customers.id = cash.customerid "
                          "WHERE deleted = 0 AND imessengers.type = 0 "
                          "GROUP BY customers.id, imessengers.uid, pin, name, lastname");

        if( g->db_nrows(res) )
        {
            for(i=0; i<g->db_nrows(res); i++)
            {
                if( atoi(g->db_get_data(res,i,"im")) )
                {
                    balance = atof(g->db_get_data(res,i,"balance"));

                    if( balance < n->limit )
                    {
                        message = load_file(n->ggtemplate);

                        if( message )
                        {
                            if( strstr(message, "%last_10_in_a_table") )
                            {
                                char *date, *value, *comment, *last_ten, *temp, *temp2;

                                last_ten = strdup("");

                                result = g->db_pquery(g->conn, "SELECT value, comment, time FROM cash WHERE customerid = ? ORDER BY time DESC LIMIT 10", g->db_get_data(res,i,"id"));

                                for(j=0; j<g->db_nrows(result); j++)
                                {
                                    date = utoc(atof(g->db_get_data(result,j,"time")));
                                    value = g->db_get_data(result,j,"value");
                                    comment = g->db_get_data(result,j,"comment");

                                    temp = (char *) malloc(strlen(date)+strlen(value)+strlen(comment)+12);
                                    sprintf(temp, "%s\t | %s\t\t | %s\n", date, value, comment);

                                    temp2 = g->str_concat(last_ten, temp);
                                    free(last_ten);
                                    last_ten = strdup(temp2);
                                    free(temp2);
                                    free(temp);
                                    free(date);
                                }

                                g->str_replace(&message, "%last_10_in_a_table", last_ten);
                                g->db_free(&result);
                                free(last_ten);
                            }

                            g->str_replace(&message, "%saldo", g->db_get_data(res,i,"balance"));
                            g->str_replace(&message, "%B", g->db_get_data(res,i,"balance"));
                            g->str_replace(&message, "%b", balance < 0 ? ftoa(balance * -1) : g->db_get_data(res,i,"balance"));
                            g->str_replace(&message, "%pin", g->db_get_data(res,i,"pin"));
                            g->str_replace(&message, "%name", g->db_get_data(res,i,"name"));
                            g->str_replace(&message, "%lastname", g->db_get_data(res,i,"lastname"));

                            // Konwersja na windows

                            g->str_replace(&message, "\n", "\n\r");
                            g->str_replace(&message, "\xA1", "\xA5");
                            g->str_replace(&message, "\xA6", "\x8C");
                            g->str_replace(&message, "\xAC", "\x8F");
                            g->str_replace(&message, "\xB1", "\xB9");
                            g->str_replace(&message, "\xB6", "\x9C");
                            g->str_replace(&message, "\xBC", "\x9F");

                            if( n->debuguin )
                            {
                                if (gg_send_message(sess, GG_CLASS_MSG, n->debuguin, message ) == -1)
                                {
                                    syslog(LOG_INFO, "DEBUG: [%s/ggnotify] Connection broken..",n->base.instance);
                                    gg_free_session(sess);
                                }
                            } else {
                                if (gg_send_message(sess, GG_CLASS_MSG, atoi(g->db_get_data(res,i,"im")), message) == -1)
                                {
                                    syslog(LOG_INFO, "DEBUG: [%s/ggnotify] Connection broken..",n->base.instance);
                                    gg_free_session(sess);
                                }
                            }

                            free(message);

                        }
                    }
                }
            }

        } else
            syslog(LOG_ERR, "[%s/ggnotify] Unable to read database", n->base.instance);

        g->db_free(&res);
        gg_logoff(sess);
        gg_free_session(sess);

#ifdef DEBUG1
        syslog(LOG_INFO, "DEBUG: [%s/ggnotify] reloaded",n->base.instance);
#endif
    }

    free(n->passwd);
    free(n->ggtemplate);
}