void
gnomegadu_gadugadu_disconnect ()
{
	GtkComboBox *combobox = GTK_COMBO_BOX (glade_xml_get_widget (gladexml, "StatusComboBox"));

	if (gnomegadu_gadugadu_session)
	{
		gg_logoff (gnomegadu_gadugadu_session);
		gg_free_session (gnomegadu_gadugadu_session);
	}

	gnomegadu_gadugadu_session = NULL;
	gnomegadu_protocol_is_connected = FALSE;

	if (gnomegadu_watch_protocol)
		g_source_remove (gnomegadu_watch_protocol);

	if (gnomegadu_source_chan)
		g_io_channel_unref (gnomegadu_source_chan);

	g_object_set_data (G_OBJECT (combobox), "dont_change_status", (gpointer) TRUE);
	gtk_combo_box_set_active (combobox, 3);
	g_object_set_data (G_OBJECT (combobox), "dont_change_status", (gpointer) FALSE);

	gnomegadu_userlist_cleanup_model_status ();
}
Exemple #2
0
/* destroys Session object */
static int session_destroy(Session *s){
GList *it;

	g_message(L_("Deleting session for '%s'"),s->jid);
	if (s->ping_timeout_func) g_source_remove(s->ping_timeout_func);
	if (s->timeout_func) g_source_remove(s->timeout_func);
	if (s->ping_timer) g_timer_destroy(s->ping_timer);
	session_remove_g_source(s);
	while(s->resources) resource_remove((Resource *)s->resources->data,0);
	if (s->ggs){
		if (s->connected){
			debug("gg_logoff(%p)",s->ggs);
			gg_logoff(s->ggs);
		}
		gg_free_session(s->ggs);
		s->ggs=NULL;
	}
	if (s->connected && s->s && s->jid){
		for(it=g_list_first(s->user->contacts);it;it=g_list_next(it)){
			Contact *c=(Contact *)it->data;

			if (!GG_S_NA(c->status) && c->status!=-1){
				char *ujid;
				ujid=jid_build_full(c->uin);
				presence_send(s->s,ujid,s->user->jid,0,NULL,NULL,0);
				g_free(ujid);
			}
		}
	}
	if (s->query) xmlnode_free(s->query);
	if (s->user) user_unref(s->user);
	g_free(s->gg_status_descr);
	g_free(s);
	return 0;
}
void GaduProtocolSocketNotifiers::handleEventDisconnect(struct gg_event *e)
{
    Q_UNUSED(e)

    // close connection
    gg_logoff(m_session);
    // we don't have connection anymore
    watchFor(nullptr);

    m_protocol->disconnectedFromServer();
    m_protocol->setStatus(Status{}, SourceUser);
}
Exemple #4
0
GGWrapper::~GGWrapper()
{
    delete mpEventSignal;
    mWorkerThread.interrupt();
    mWorkerThread.join();


    if(mpSession)
    {
        gg_logoff(mpSession);
        gg_free_session(mpSession);
    }

    delete mpQueue;
}
Exemple #5
0
int main(void)
{
	struct gg_login_params p;
	struct gg_session *sess;
	struct timeval tv;
	struct gg_event *e;
	fd_set rd, wd;
	time_t last = 0, now;
	int ret;

#ifdef _WIN32
	gg_win32_init_network();
#endif

	gg_debug_level = ~0;

	memset(&p, 0, sizeof(p));
	p.uin = 123456;
	p.password = "******";
	p.async = 1;

	sess = gg_login(&p);

	if (!sess) {
		printf("gg_login failed.\n");
		return 1;
	}

	for (;;) {
		FD_ZERO(&rd);
		FD_ZERO(&wd);

		if ((sess->check & GG_CHECK_READ))
			FD_SET(sess->fd, &rd);
		if ((sess->check & GG_CHECK_WRITE))
			FD_SET(sess->fd, &wd);

		tv.tv_sec = 1;
		tv.tv_usec = 0;

		ret = select(sess->fd + 1, &rd, &wd, NULL, &tv);

		if (ret == -1) {
			perror("select");
			return 1;
		}

		now = time(NULL);

		if (now != last) {
			if (sess->timeout != -1 && sess->timeout-- == 0 &&
				!sess->soft_timeout)
			{
				printf("Przekroczenie czasu operacji.\n");
				gg_free_session(sess);
				return 1;
			}
		}

		if (sess && (FD_ISSET(sess->fd, &rd) ||
			FD_ISSET(sess->fd, &wd) ||
			(sess->timeout == 0 && sess->soft_timeout)))
		{
			if (!(e = gg_watch_fd(sess))) {
				printf("Połączenie zerwane.\n");
				gg_free_session(sess);
				return 1;
			}

			if (e->type == GG_EVENT_CONN_SUCCESS) {
				printf("Połączono z serwerem.\n");
				gg_free_event(e);
				gg_logoff(sess);
				gg_free_session(sess);
				return 0;
			}

			if (e->type == GG_EVENT_CONN_FAILED) {
				printf("Błąd połączenia.\n");
				gg_free_event(e);
				gg_logoff(sess);
				gg_free_session(sess);
				return 1;
			}

			gg_free_event(e);
		}
	}

	return 1;
}
Exemple #6
0
int main()
{
	struct gg_login_params p;
	struct gg_session *sess;
	struct timeval tv;
	struct gg_event *e;
	fd_set rd, wd;
	int ret;

	gg_debug_level = ~0;
	
	memset(&p, 0, sizeof(p));
	p.uin = 123456;
	p.password = "******";
	p.async = 1;
	
	sess = gg_login(&p);

	for (;;) {
		FD_ZERO(&rd);
		FD_ZERO(&wd);

		if ((sess->check & GG_CHECK_READ))
			FD_SET(sess->fd, &rd);
		if ((sess->check & GG_CHECK_WRITE))
			FD_SET(sess->fd, &wd);

		tv.tv_sec = 10;
		tv.tv_usec = 0;
		
		ret = select(sess->fd + 1, &rd, &wd, NULL, &tv);
	
		if (!ret) {
			printf("timeout! wypad!\n");
			return 1;
		} else {
			if (sess && (FD_ISSET(sess->fd, &rd) || FD_ISSET(sess->fd, &wd))) {
				if (!(e = gg_watch_fd(sess))) {
					printf("zerfauo pouontshenie!\n");
					return 1;
				}
				if (e->type == GG_EVENT_CONN_SUCCESS) {
					printf("po³±czy³em siê\n");
					gg_free_event(e);
					gg_logoff(sess);
					gg_free_session(sess);
					return 1;
				}
				if (e->type == GG_EVENT_CONN_FAILED) {
					printf("errrror\n");
					gg_free_event(e);
					gg_logoff(sess);
					gg_free_session(sess);
					return 1;
				}
				gg_free_event(e);
			}
		}
	}
	
	return -1;
}
Exemple #7
0
int main(int argc, char **argv)
{

   // Do po³±czenia
   struct gg_session *sess;
   struct gg_event *e;
   struct gg_login_params p;
   list_t searches = NULL;

   char *last_search_first_name = NULL;
   char *last_search_last_name = NULL;
   char *last_search_nickname = NULL;
   uin_t last_search_uin = 0;

   gg_pubdir50_t res;
   // Do wyszukiwania
   char *user;
   gg_pubdir50_t req;
   int i, all = 0;

   if (argc < 4) {
      fprintf(stderr, "Usage: %s <my_uid> <my_password> <params>\n\n", argv[0]);
      fprintf(stderr, "Parameters:\n");
      fprintf(stderr, " -f <val> (--first)     - first name\n");
      fprintf(stderr, " -l <val> (--last)      - last name\n");
      fprintf(stderr, " -n <val> (--nickname)  - nickname\n");
      fprintf(stderr, " -c <val> (--city)      - city\n");
      fprintf(stderr, " -u <val> (--uin)       - user ID\n");
      fprintf(stderr, " -F       (--female)    - search females only\n");
      fprintf(stderr, " -M       (--male)      - search males only\n");
      fprintf(stderr, " -a       (--active)    - search on-line users only\n");
      fprintf(stderr, " -b <val> (--born)      - year of birth\n");
      fprintf(stderr, " -s <val> (--start)     - search offset\n");
      fprintf(stderr, " -A       (--all)       - show all\n");
      return 1;
   }

   // Poziom debugowania
   gg_debug_level = 5;

   memset(&p, 0, sizeof(p));
   p.uin = atoi(argv[1]);
   p.password = argv[2];


   // Po³±czenie

   if (!(sess = gg_login(&p))) {
      printf("Connection failed: %s\n", strerror(errno));
      gg_free_session(sess);
      return 1;
   }

   printf("Po³±czono.\n");

   // Wyszukiwanie

   if (!sess || sess->state != GG_STATE_CONNECTED) {
      printf("not_connected\n");
      return 1;
   }

   argv = argv + 3;

   // próba odpalenia trybu szukania konkretnego usera
   user = strdup(argv[0]);

   // Konwersja do CP
   for (i = 0; argv[i]; i++) {
      iso_to_cp(argv[i]);
   }

   // Zapytanie
   if (!(req = gg_pubdir50_new(GG_PUBDIR50_SEARCH))) {
      printf("\n");
      return 1;
   }

   // wyszukiwanie UIN
   if (argv[0] && argv[0][0] != '-') {
      uin_t uin = get_uin(user);

      if (!uin) {
	 printf("User not found (%s)\n", user);
	 return 1;
      }

      gg_pubdir50_add(req, GG_PUBDIR50_UIN, itoa(uin));
      i = 1;

   } else {
      i = 0;
   }

   free(user);

   // Parsowanie argumentów
   for (; argv[i]; i++) {
      char *arg = argv[i];

      if (match_arg(arg, 'f', "first", 2) && argv[i + 1]) {
	 gg_pubdir50_add(req, GG_PUBDIR50_FIRSTNAME, argv[++i]);
	 continue;
      }

      if (match_arg(arg, 'l', "last", 2) && argv[i + 1]) {
	 gg_pubdir50_add(req, GG_PUBDIR50_LASTNAME, argv[++i]);
	 continue;
      }

      if (match_arg(arg, 'n', "nickname", 2) && argv[i + 1]) {
	 gg_pubdir50_add(req, GG_PUBDIR50_NICKNAME, argv[++i]);
	 continue;
      }

      if (match_arg(arg, 'c', "city", 2) && argv[i + 1]) {
	 gg_pubdir50_add(req, GG_PUBDIR50_CITY, argv[++i]);
	 continue;
      }

      if (match_arg(arg, 'u', "uin", 2) && argv[i + 1]) {
	 gg_pubdir50_add(req, GG_PUBDIR50_UIN, itoa(get_uin(argv[++i])));
	 continue;
      }

      if (match_arg(arg, 's', "start", 3) && argv[i + 1]) {
	 gg_pubdir50_add(req, GG_PUBDIR50_START, argv[++i]);
	 continue;
      }

      if (match_arg(arg, 'F', "female", 2)) {
	 gg_pubdir50_add(req, GG_PUBDIR50_GENDER, GG_PUBDIR50_GENDER_FEMALE);
	 continue;
      }

      if (match_arg(arg, 'M', "male", 2)) {
	 gg_pubdir50_add(req, GG_PUBDIR50_GENDER, GG_PUBDIR50_GENDER_MALE);
	 continue;
      }

      if (match_arg(arg, 'a', "active", 2)) {
	 gg_pubdir50_add(req, GG_PUBDIR50_ACTIVE, GG_PUBDIR50_ACTIVE_TRUE);
	 continue;
      }

      if (match_arg(arg, 'b', "born", 2) && argv[i + 1]) {
	 char *foo = strchr(argv[++i], ':');

	 if (foo)
	    *foo = ' ';

	 gg_pubdir50_add(req, GG_PUBDIR50_BIRTHYEAR, argv[i]);
	 continue;
      }

      if (match_arg(arg, 'A', "all", 3)) {
	 if (!gg_pubdir50_get(req, 0, GG_PUBDIR50_START))
	    gg_pubdir50_add(req, GG_PUBDIR50_START, "0");
	 all = 1;
	 continue;
      }

      printf("invalid_params\n");

      gg_pubdir50_free(req);

      return 1;
   }

   // Wywo³anie wyszukiwania

   if (!gg_pubdir50(sess, req)) {
      printf("Search failed\n");
      return 1;
   }

   // Dodanie wyniku wyszykiwania do listy
   if (all) {
      list_add(&searches, req, 0);
   } else {
      gg_pubdir50_free(req);
   }

   // Pêtla czekaj±ca na wynik wyszukiwania
   while (1) {
      if (!(e = gg_watch_fd(sess))) {
	 printf("Connection interrupted: %s\n", strerror(errno));
	 gg_logoff(sess);
	 gg_free_session(sess);
	 return 1;
      }

      if (e->type == GG_EVENT_PUBDIR50_SEARCH_REPLY) {
         printf("Received results!\n");
         res = e->event.pubdir50;
         all = i = 0;
         int count, all = 0;
         list_t l;
         uin_t last_uin = 0;

         if ((count = gg_pubdir50_count(res)) < 1) {
            printf("search_not_found\n");
            return 1;
         }

         gg_debug(GG_DEBUG_MISC, "handle_search50, count = %d\n", gg_pubdir50_count(res));

         for (l = searches; l; l = l->next) {
            gg_pubdir50_t req = l->data;

            if (gg_pubdir50_seq(req) == gg_pubdir50_seq(res)) {
               all = 1;
               break;
            }
         }

         for (i = 0; i < count; i++) {
            const char *__fmnumber = gg_pubdir50_get(res, i, "fmnumber");
            const char *uin = (__fmnumber) ? __fmnumber : "?";

            const char *__firstname = gg_pubdir50_get(res, i, "firstname");
            char *firstname = strdup((__firstname) ? __firstname : "");

            const char *__lastname = gg_pubdir50_get(res, i, "lastname");
            char *lastname = strdup((__lastname) ? __lastname : "");

            const char *__nickname = gg_pubdir50_get(res, i, "nickname");
            char *nickname = strdup((__nickname) ? __nickname : "");

            const char *__fmstatus = gg_pubdir50_get(res, i, "fmstatus");
            int status = (__fmstatus) ? atoi(__fmstatus) : GG_STATUS_NOT_AVAIL;

            const char *__birthyear = gg_pubdir50_get(res, i, "birthyear");
            const char *birthyear = (__birthyear && strcmp(__birthyear, "0")) ? __birthyear : "-";

            const char *__city = gg_pubdir50_get(res, i, "city");
            char *city = strdup((__city) ? __city : "");

            char *name, *active;

            const char *target = NULL;

            cp_to_iso(firstname);
            cp_to_iso(lastname);
            cp_to_iso(nickname);
            cp_to_iso(city);

            if (count == 1 && !all) {
               free(last_search_first_name);
               free(last_search_last_name);
               free(last_search_nickname);
               last_search_first_name = strdup(firstname);
               last_search_last_name = strdup(lastname);
               last_search_nickname = strdup(nickname);
               last_search_uin = atoi(uin);
            }

            name = saprintf("%s %s", firstname, lastname);

            switch (status & 0x7f) {
               case GG_STATUS_AVAIL:
               case GG_STATUS_AVAIL_DESCR:
                  active = strdup("Avail");
                  break;
               case GG_STATUS_BUSY:
               case GG_STATUS_BUSY_DESCR:
                  active = strdup("Busy");
                  break;
               case GG_STATUS_INVISIBLE:
               case GG_STATUS_INVISIBLE_DESCR:
                  active = strdup("Invis");
                  break;
               default:
                  active = strdup("Inact");
            }

            printf("UIN\t: %s\n", uin);
            printf("Name\t: %s\n", name);
            printf("Nick\t: %s\n", nickname);
            printf("City\t: %s\n", city);
            printf("Birth\t: %s\n", birthyear);
            printf("Active\t: %s\n\n", active);

            free(name);
            free(active);

            free(firstname);
            free(lastname);
            free(nickname);
            free(city);

            last_uin = atoi(uin);
         }

         /* je¶li mieli¶my ,,/find --all'', szukamy dalej */
         for (l = searches; l; l = l->next) {
            gg_pubdir50_t req = l->data;
            uin_t next;

            if (gg_pubdir50_seq(req) != gg_pubdir50_seq(res))
               continue;

            /* nie ma dalszych? to dziêkujemy */
            if (!(next = gg_pubdir50_next(res)) || !sess || next < last_uin) {
               list_remove(&searches, req, 0);
               gg_pubdir50_free(req);
               break;
            }

            gg_pubdir50_add(req, GG_PUBDIR50_START, itoa(next));
            gg_pubdir50(sess, req);

            break;
         }
	 gg_free_event(e);
	 break;
      }

      gg_free_event(e);
   }

   gg_logoff(sess);
   gg_free_session(sess);

   return 0;
}
Exemple #8
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);
}