Example #1
0
File: news.c Project: Mortal/claws
GSList *news_get_group_list(Folder *folder)
{
	gchar *path, *filename;
	FILE *fp;
	GSList *list = NULL;
	GSList *last = NULL;
	gchar buf[BUFFSIZE];

	cm_return_val_if_fail(folder != NULL, NULL);
	cm_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, NULL);

	path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
	if (!is_dir_exist(path))
		make_dir_hier(path);
	filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
	g_free(path);

	if ((fp = g_fopen(filename, "rb")) == NULL) {
		NewsSession *session;
		gint ok;
		clist *grouplist = NULL;
		clistiter *cur;
		fp = g_fopen(filename, "wb");
		
		if (!fp) {
			g_free(filename);
			return NULL;
		}
		session = news_session_get(folder);
		if (!session) {
			fclose(fp);
			g_free(filename);
			return NULL;
		}

		ok = nntp_threaded_list(folder, &grouplist);
		
		if (ok != NEWSNNTP_NO_ERROR) {
			if (ok == NEWSNNTP_ERROR_STREAM) {
				session_destroy(SESSION(session));
				REMOTE_FOLDER(folder)->session = NULL;
			}
			fclose(fp);
			g_free(filename);
			return NULL;
		}
		
		if (grouplist) {
			for (cur = clist_begin(grouplist); cur; cur = clist_next(cur)) {
				struct newsnntp_group_info *info = (struct newsnntp_group_info *)
									clist_content(cur);
				if (fprintf(fp, "%s %d %d %c\n",
					info->grp_name,
					info->grp_last,
					info->grp_first,
					info->grp_type) < 0) {
					log_error(LOG_PROTOCOL, ("Can't write newsgroup list\n"));
					session_destroy(SESSION(session));
					REMOTE_FOLDER(folder)->session = NULL;
					fclose(fp);
					g_free(filename);
					newsnntp_list_free(grouplist);
					return NULL;
				}
			}
			newsnntp_list_free(grouplist);
		}
		if (fclose(fp) == EOF) {
			log_error(LOG_PROTOCOL, ("Can't write newsgroup list\n"));
			session_destroy(SESSION(session));
			REMOTE_FOLDER(folder)->session = NULL;
			g_free(filename);
			return NULL;
		}

		if ((fp = g_fopen(filename, "rb")) == NULL) {
			FILE_OP_ERROR(filename, "fopen");
			g_free(filename);
			return NULL;
		}
	}

	while (fgets(buf, sizeof(buf), fp) != NULL) {
		gchar *p = buf;
		gchar *name;
		gint last_num;
		gint first_num;
		gchar type;
		NewsGroupInfo *ginfo;

		p = strchr(p, ' ');
		if (!p) continue;
		*p = '\0';
		p++;
		name = buf;

		if (sscanf(p, "%d %d %c", &last_num, &first_num, &type) < 3)
			continue;

		ginfo = news_group_info_new(name, first_num, last_num, type);

		if (!last)
			last = list = g_slist_append(NULL, ginfo);
		else {
			last = g_slist_append(last, ginfo);
			last = last->next;
		}
	}

	fclose(fp);
	g_free(filename);

	list = g_slist_sort(list, (GCompareFunc)news_group_info_compare);

	return list;
}
Example #2
0
static int nntpdriver_list_folders(mailsession * session, const char * mb,
				    struct mail_list ** result)
{
  int r;
  clist * group_list;
  newsnntp * nntp;
  clistiter * cur;
  char * new_mb;
  int done;
  clist * list;
  struct mail_list * ml;
  int res;

  nntp = get_nntp_session(session);

  new_mb = NULL;
  if ((mb != NULL) && (*mb != '\0')) {
    new_mb = malloc(strlen(mb) + 3);
    if (new_mb == NULL) {
      res = MAIL_ERROR_MEMORY;
      goto err;
    }
    strcpy(new_mb, mb);
    strcat(new_mb, ".*");
  }

  done = FALSE;
  do {
    if (new_mb != NULL)
      r = newsnntp_list_active(nntp, new_mb, &group_list);
    else
      r = newsnntp_list(nntp, &group_list);

    switch (r) {
    case NEWSNNTP_ERROR_REQUEST_AUTHORIZATION_USERNAME:
      r = nntpdriver_authenticate_user(session);
      if (r != MAIL_NO_ERROR) {
	      if (new_mb != NULL)
		free(new_mb);
	res = r;
	goto err;
      }
      break;
      
    case NEWSNNTP_WARNING_REQUEST_AUTHORIZATION_PASSWORD:
      r = nntpdriver_authenticate_password(session);
      if (r != MAIL_NO_ERROR) {
	      if (new_mb != NULL)
		free(new_mb);
	res = r;
	goto err;
      }
      break;

    case NEWSNNTP_NO_ERROR:
      if (new_mb != NULL)
	free(new_mb);
      done = TRUE;
      break;

    default:
      if (new_mb != NULL)
	free(new_mb);
      return nntpdriver_nntp_error_to_mail_error(r);
    }
  }
  while (!done);

  list = clist_new();
  if (list == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto err;
  }

  for(cur = clist_begin(group_list) ; cur != NULL ;
      cur = clist_next(cur)) {
    struct newsnntp_group_info * info;
    char * new_name;

    info = clist_content(cur);
    new_name = strdup(info->grp_name);
    if (new_name == NULL) {
    res = MAIL_ERROR_MEMORY;
      goto free_list;
    }

    r = clist_append(list, new_name);
    if (r < 0) {
      free(new_name);
      res = MAIL_ERROR_MEMORY;
      goto free_list;
    }
  }

  ml = mail_list_new(list);
  if (ml == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto free_list;
  }

  newsnntp_list_free(group_list);

  * result = ml;

  return MAIL_NO_ERROR;

 free_list:
  clist_foreach(list, (clist_func) free, NULL);
  clist_free(list);
  newsnntp_list_free(group_list);
 err:
  return res;
}