Exemplo n.º 1
0
GeeArrayList* string_helper_occurrences (const char* s, const char* t) {
	GError * inner_error;
	GRegex* grx;
	GeeArrayList* poss;
	GMatchInfo* m;
	GeeArrayList* _tmp7;
	g_return_val_if_fail (s != NULL, NULL);
	g_return_val_if_fail (t != NULL, NULL);
	inner_error = NULL;
	grx = NULL;
	poss = gee_array_list_new (G_TYPE_INT, NULL, NULL, g_direct_equal);
	m = NULL;
	{
		GRegex* _tmp1;
		char* _tmp0;
		GMatchInfo* _tmp4;
		gboolean _tmp3;
		GMatchInfo* _tmp2;
		_tmp1 = NULL;
		_tmp0 = NULL;
		grx = (_tmp1 = g_regex_new (_tmp0 = g_regex_escape_string (t, -1), 0, 0, &inner_error), (grx == NULL) ? NULL : (grx = (g_regex_unref (grx), NULL)), _tmp1);
		if (inner_error != NULL) {
			if (inner_error->domain == G_REGEX_ERROR) {
				goto __catch5_g_regex_error;
			}
			goto __finally5;
		}
		_tmp0 = (g_free (_tmp0), NULL);
		_tmp4 = NULL;
		_tmp2 = NULL;
		if ((_tmp3 = g_regex_match (grx, s, 0, &_tmp2), m = (_tmp4 = _tmp2, (m == NULL) ? NULL : (m = (g_match_info_free (m), NULL)), _tmp4), _tmp3)) {
			gint spos;
			gint epos;
			spos = 0;
			epos = 0;
			g_match_info_fetch_pos (m, 0, &spos, &epos);
			gee_collection_add ((GeeCollection*) poss, GINT_TO_POINTER (spos));
			while (TRUE) {
				gboolean _tmp5;
				gint mspos;
				gint mepos;
				_tmp5 = g_match_info_next (m, &inner_error);
				if (inner_error != NULL) {
					if (inner_error->domain == G_REGEX_ERROR) {
						goto __catch5_g_regex_error;
					}
					goto __finally5;
				}
				if (!_tmp5) {
					break;
				}
				mspos = 0;
				mepos = 0;
				g_match_info_fetch_pos (m, 0, &mspos, &mepos);
				gee_collection_add ((GeeCollection*) poss, GINT_TO_POINTER (mspos));
			}
		}
	}
	goto __finally5;
	__catch5_g_regex_error:
	{
		GError * e;
		e = inner_error;
		inner_error = NULL;
		{
			GeeArrayList* _tmp6;
			_tmp6 = NULL;
			return (_tmp6 = gee_array_list_new (G_TYPE_INT, NULL, NULL, g_direct_equal), (e == NULL) ? NULL : (e = (g_error_free (e), NULL)), (grx == NULL) ? NULL : (grx = (g_regex_unref (grx), NULL)), (poss == NULL) ? NULL : (poss = (g_object_unref (poss), NULL)), (m == NULL) ? NULL : (m = (g_match_info_free (m), NULL)), _tmp6);
		}
	}
	__finally5:
	if (inner_error != NULL) {
		(grx == NULL) ? NULL : (grx = (g_regex_unref (grx), NULL));
		(poss == NULL) ? NULL : (poss = (g_object_unref (poss), NULL));
		(m == NULL) ? NULL : (m = (g_match_info_free (m), NULL));
		g_critical ("file %s: line %d: uncaught error: %s", __FILE__, __LINE__, inner_error->message);
		g_clear_error (&inner_error);
		return NULL;
	}
	_tmp7 = NULL;
	return (_tmp7 = poss, (grx == NULL) ? NULL : (grx = (g_regex_unref (grx), NULL)), (m == NULL) ? NULL : (m = (g_match_info_free (m), NULL)), _tmp7);
}
Exemplo n.º 2
0
static int getSubEndPos(TGrgx* ud, int n) {
    int end_pos = -1;
    g_match_info_fetch_pos (ud->match_info, n, NULL, &end_pos);
    return end_pos;
}
Exemplo n.º 3
0
static gboolean
cover_thumbnailer_get_title (CoverThumbnailer  *cover,
                             GFile            *gfile,
                             gchar           **ret_title,
                             gchar           **ret_year)
{
  gchar       *basename;
  gboolean     is_series;
  GMatchInfo  *match_info;
  gint         start_pos;
  gint         end_pos;
  gchar       *year = NULL;
  GString     *title;
  const gchar *p;
  gboolean     append_space;
  gunichar     uchar;
  gboolean     succeed;
  gchar       *temp;

  g_return_val_if_fail (G_IS_FILE (gfile), FALSE);
  g_return_val_if_fail (ret_title != NULL, FALSE);
  g_return_val_if_fail (ret_year != NULL, FALSE);

  /* get the basename */
  basename = g_file_get_basename (gfile);

  /* check if the title looks like a serie */
  is_series = g_regex_match (cover->series_regex, basename, 0, &match_info);

  /* if this is not a serie, look for other filename crap */
  if (is_series
      || g_regex_match (cover->abbrev_regex, basename, 0, &match_info))
    {
      /* remove series or abbrev suffix from the filename */
      if (g_match_info_fetch_pos (match_info, 0, &start_pos, NULL)
          && start_pos > 0)
        basename[start_pos] = '\0';
      g_match_info_free (match_info);
    }

  /* for non-series, look for a year in the title */
  if (!is_series
      && g_regex_match (cover->year_regex, basename, 0, &match_info))
    {
      /* store year and remove the suffix from the title */
      if (g_match_info_fetch_pos (match_info, 0, &start_pos, &end_pos)
          && start_pos >= 0
          && end_pos > start_pos)
        {
          year = g_strndup (basename + start_pos, end_pos - start_pos);

          if (start_pos == 0)
            {
              temp = g_strdup (basename + end_pos);
              g_free (basename);
              basename = temp;
            }
          else
            {
              basename[start_pos] = '\0';
            }
        }
      g_match_info_free (match_info);
    }

  /* append the possible title part of the filename */
  title = g_string_sized_new (strlen (basename));
  for (p = basename, append_space = FALSE; *p != '\0'; p = g_utf8_next_char (p))
    {
      uchar = g_utf8_get_char (p);
      if (g_unichar_isalnum (uchar)
          || uchar == '\'' || uchar == '!')
        {
          if (append_space)
            {
              g_string_append_c (title, '+');
              append_space = FALSE;
            }

          /* append the char */
          g_string_append_unichar (title, uchar);
        }
      else if (title->len > 0)
        {
          /* start with a space next time we append a char */
          append_space = TRUE;
        }
    }

  /* finalize */
  g_free (basename);
  succeed = title->len > 1;
  *ret_title = g_string_free (title, !succeed);
  *ret_year = year;

  return succeed;
}
static gchar *
get_file_page_version (const gchar *text,
		       const gchar *regex_place)
{
	GRegex *regex;
	GMatchInfo *match_info;
	GString *string = NULL;
	gchar *unstable = NULL;
	gchar *stable = NULL;

	regex = g_regex_new (regex_place, 0, 0, NULL);
	g_regex_match (regex, text, 0, &match_info);
	while (g_match_info_matches (match_info))
	{
		gint end;
		gint i;

		g_match_info_fetch_pos (match_info, 0, NULL, &end);

		string = g_string_new ("");

		i = end;
		while (text[i] != '/')
		{
			string = g_string_append_c (string, text[i]);
			i++;
		}

		if (is_unstable (string->str))
		{
			g_free (unstable);
			unstable = g_string_free (string, FALSE);
		}
		else
		{
			g_free (stable);
			stable = g_string_free (string, FALSE);
		}

		g_match_info_next (match_info, NULL);
	}
	g_match_info_free (match_info);
	g_regex_unref (regex);

	if ((PLUMA_MINOR_VERSION % 2) == 0)
	{
		g_free (unstable);

		return stable;
	}
	else
	{
		/* We need to check that stable isn't newer than unstable */
		if (newer_version (stable, unstable, FALSE))
		{
			g_free (unstable);

			return stable;
		}
		else
		{
			g_free (stable);

			return unstable;
		}
	}
}
Exemplo n.º 5
0
static int getSubStartPos(TGrgx* ud, int n) {
    int start_pos = -1;
    g_match_info_fetch_pos (ud->match_info, n, &start_pos, NULL);
    return start_pos;
}
Exemplo n.º 6
0
static GList *snr3_replace_pcre(Tsnr3run *s3run, gchar *buffer, gchar **replacedbuffer) {
	gchar *newbuf;
	gchar *bufferpos, *newbufpos;
	gsize buflen;
	gsize alloced;
	Tlineinbuffer lib = {0,1};
	GList *results=NULL;
	GMatchInfo *match_info;
	gsize prevpos=0;

	DEBUG_MSG("snr3_replace_pcre, search for %s, replace with %s\n",s3run->query, s3run->replace);
	buflen = strlen(buffer);

	alloced = MAX(buflen*2,4096);
	newbuf = g_malloc0(alloced);

	bufferpos = buffer;
	newbufpos = newbuf;
	g_regex_match(s3run->regex, buffer, 0, &match_info);
	while(g_match_info_matches(match_info)) {
		gint so, eo;
		guint line, replacelen;
		gchar *replacestring;
		GError *gerror=NULL;
		g_match_info_fetch_pos(match_info,0,&so,&eo);

		memcpy(newbufpos, bufferpos, so-prevpos);
		newbufpos += (so-prevpos);
		bufferpos = buffer+eo;
		prevpos = eo;

		line = calculate_line_in_buffer(&lib, newbuf, (newbufpos-newbuf));
		results = g_list_prepend(results, new_result(line, newbuf, (newbufpos-newbuf)));

		replacestring = g_match_info_expand_references(match_info, s3run->replace, &gerror);
		if (gerror) {
			g_print("replace error %s\n",gerror->message);
			g_error_free(gerror);
		}
		replacelen = strlen(replacestring);

		/* now check if we have enough memory */
		if (alloced < (1+ newbufpos-newbuf + replacelen + buflen-prevpos)) {
			gchar *tmp;
			alloced += MAX(buflen, 4096);
			tmp = g_realloc(newbuf, alloced);
			newbufpos = tmp + (newbufpos-newbuf);
			newbuf=tmp;
		}

		memcpy(newbufpos, replacestring, replacelen);
		newbufpos += replacelen;
		g_free(replacestring);

		g_match_info_next(match_info, NULL);
	}
	g_match_info_free(match_info);

	memcpy(newbufpos, buffer+prevpos, buflen-prevpos+1);
	*replacedbuffer = newbuf;
	return results;
}
Exemplo n.º 7
0
gboolean
mc_search__run_regex (mc_search_t * mc_search, const void *user_data,
                      gsize start_search, gsize end_search, gsize * found_len)
{
    gsize current_pos, virtual_pos;
    int current_chr = 0;
    gint start_pos;
    gint end_pos;

    if (mc_search->regex_buffer != NULL)
        g_string_free (mc_search->regex_buffer, TRUE);

    mc_search->regex_buffer = g_string_new ("");

    virtual_pos = current_pos = start_search;
    while (virtual_pos <= end_search) {
        g_string_set_size (mc_search->regex_buffer, 0);
        mc_search->start_buffer = current_pos;

        while (1) {
            current_chr = mc_search__get_char (mc_search, user_data, current_pos);
            if (current_chr == MC_SEARCH_CB_ABORT)
                break;

            current_pos++;

            if (current_chr == MC_SEARCH_CB_SKIP)
                continue;

            virtual_pos++;

            g_string_append_c (mc_search->regex_buffer, (char) current_chr);


            if (current_chr == 0 || (char) current_chr == '\n')
                break;

            if (virtual_pos > end_search)
                break;

        }
        switch (mc_search__regex_found_cond (mc_search, mc_search->regex_buffer)) {
        case COND__FOUND_OK:
#ifdef SEARCH_TYPE_GLIB
            g_match_info_fetch_pos (mc_search->regex_match_info, 0, &start_pos, &end_pos);
#else /* SEARCH_TYPE_GLIB */
            start_pos = mc_search->iovector[0];
            end_pos = mc_search->iovector[1];
#endif /* SEARCH_TYPE_GLIB */
            if (found_len)
                *found_len = end_pos - start_pos;
            mc_search->normal_offset = mc_search->start_buffer + start_pos;
            return TRUE;
            break;
        case COND__NOT_ALL_FOUND:
            break;
        default:
            g_string_free (mc_search->regex_buffer, TRUE);
            mc_search->regex_buffer = NULL;
            return FALSE;
            break;
        }
        if (mc_search->update_fn != NULL) {
            if ((mc_search->update_fn) (user_data, current_pos) == MC_SEARCH_CB_SKIP) {
                g_string_free (mc_search->regex_buffer, TRUE);
                mc_search->regex_buffer = NULL;
                mc_search->error = MC_SEARCH_E_NOTFOUND;
                mc_search->error_str = NULL;
                return FALSE;
            }
        }
        if (current_chr == MC_SEARCH_CB_ABORT)
            break;
    }
    g_string_free (mc_search->regex_buffer, TRUE);
    mc_search->regex_buffer = NULL;
    mc_search->error = MC_SEARCH_E_NOTFOUND;
    mc_search->error_str = g_strdup (_(STR_E_NOTFOUND));
    return FALSE;
}
Exemplo n.º 8
0
static Token *
maybe_parse_instruction (TextgenTemplate *tpl,
                         const gchar *text, 
                         gint length,
                         gint start,
                         gint start_line,
                         gint start_column,
                         gboolean *ignore_empty_line,
                         gint *after_end,
                         gint *after_end_line,
                         gint *after_end_column,
                         GError **error)
{
  gboolean success = TRUE;
  gboolean found_end = FALSE;
  gchar instr;
  gint content_offset;
  gchar *command = NULL;
  gchar *content = NULL;
  gint content_length;
  gint pos = start;
  gint cur_line = start_line;
  gint cur_column = start_column;
  GError *tmp_error = NULL;

  if (start < length - 1 && text[start] == '[')
    {
      if (text[start + 1] == '!')
        {
          *ignore_empty_line = TRUE;
          instr = text[start + 2];
          content_offset = 3;
        }
      else
        {
          *ignore_empty_line = FALSE;
          instr = text[start + 1];
          content_offset = 2;
        }
    
      if (!IS_VALID_INSTRUCTION_CHAR (instr))
        return NULL;
    }
  else
    return NULL;

  pos += content_offset;
  cur_column += content_offset;

  while (pos < length)
    {
      if (pos < length - 1 && text[pos] == instr && text[pos + 1] == ']')
        {
          gint content_start = start + content_offset;
          content_length = pos - content_start;
          content = g_strndup (text + content_start, content_length);
          *after_end = pos + 2;
          cur_column += 2;
          found_end = TRUE;
          break;
        }

      advance_pos (tpl, text, length, &pos, &cur_line, &cur_column, &tmp_error);
      if (tmp_error)
        {
          g_propagate_error (error, tmp_error);
          success = FALSE;
          goto finish;
        }
    }

  if (!found_end)
    {
      emit_message (tpl, TEXTGEN_MESSAGE_ERROR, start_line, start_column,
                    _("Could not find the end of the instruction"), instr);
      set_parse_error (error);
      success = FALSE;
      goto finish;
    }

  if (instr == TOKEN_COMMAND)
    {
      static GRegex *regex = NULL;
      GMatchInfo *match_info;
      gchar *old_content = content;
      gint start_pos;
      gint end_pos;

      if (!regex)
        regex = g_regex_new ("^\\s*([A-Za-z]+)\\s+(.*)$", G_REGEX_MULTILINE | G_REGEX_OPTIMIZE, 0, NULL);

      if (!g_regex_match (regex, old_content, 0, &match_info))
        {
          emit_message (tpl, TEXTGEN_MESSAGE_ERROR, start_line, start_column,
                        _("A command must look like this: [# COMMAND content #]"));
          set_parse_error (error);
          success = FALSE;
          goto finish;
        }

      command = g_match_info_fetch (match_info, 1);
      content = g_match_info_fetch (match_info, 2);

      g_match_info_fetch_pos (match_info, 2, &start_pos, &end_pos);
      content_length = end_pos - start_pos;
      
      g_match_info_free (match_info);
      g_free (old_content);
    }

  *after_end_line = cur_line;
  *after_end_column = cur_column;

finish:
  if (success)
    {
      Token *token = g_slice_new (Token);
      token->type = instr;
      token->command = command;
      token->content = content;
      token->content_length = content_length;
      token->line = start_line;
      token->column = start_column;
      token->content_column = start_column + content_offset;
      token->owns_content = TRUE;
      token->owns_command = TRUE;
      return token;
    }
  else
    {
      g_free (command);
      g_free (content);
      return NULL;
    }
}
Exemplo n.º 9
0
static void
gb_source_view_update_search (GbSourceView *view)
{
   GbSourceViewPrivate *priv;
   GRegexCompileFlags flags = 0;
   GtkTextBuffer *buffer;
   GtkTextIter begin;
   GtkTextIter end;
   GMatchInfo *match_info = NULL;
   GtkTextTag *search_tag;
   gboolean has_matches = FALSE;
   GRegex *regex = NULL;
   gchar *text;
   gchar *escaped;

   g_assert(GB_IS_SOURCE_VIEW(view));

   priv = view->priv;

   buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(view));
   gtk_text_buffer_get_bounds(buffer, &begin, &end);

   search_tag = gb_source_view_ref_search_tag(view);
   gtk_text_buffer_remove_tag(buffer, search_tag, &begin, &end);

   if (g_str_empty0(priv->search_text) && !priv->search_regex) {
      goto cleanup;
   }

   if (priv->search_text) {
      if (!priv->search_case_sensitive) {
         flags = G_REGEX_CASELESS;
      }
      escaped = g_regex_escape_string(priv->search_text, -1);
      regex = g_regex_new(escaped, flags, 0, NULL);
      g_free(escaped);
   } else if (priv->search_regex) {
      regex = g_regex_ref(priv->search_regex);
   }

   if (regex) {
      text = gtk_text_buffer_get_text(buffer, &begin, &end, TRUE);
      if (g_regex_match(regex, text, 0, &match_info)) {
         guint count;
         guint i;
         gint begin_pos;
         gint end_pos;

         do {
            count = g_match_info_get_match_count(match_info);
            for (i = 0; i < count; i++) {
               if (g_match_info_fetch_pos(match_info, i, &begin_pos, &end_pos)) {
                  gtk_text_buffer_get_iter_at_offset(buffer, &begin, begin_pos);
                  gtk_text_buffer_get_iter_at_offset(buffer, &end, end_pos);
                  gtk_text_buffer_apply_tag(buffer, search_tag, &begin, &end);
                  has_matches = TRUE;
               }
            }
         } while (g_match_info_next(match_info, NULL));
      }
      g_match_info_free(match_info);
      g_regex_unref(regex);
      g_free(text);
   }

cleanup:
   if (priv->has_matches != has_matches) {
      priv->has_matches = has_matches;
      g_object_notify_by_pspec(G_OBJECT(view), gParamSpecs[PROP_HAS_MATCHES]);
   }

   gtk_widget_queue_draw(GTK_WIDGET(view));
   g_object_unref(search_tag);
}
Exemplo n.º 10
0
void linphone_gtk_push_text(GtkWidget *w, const LinphoneAddress *from,
                 gboolean me,LinphoneChatRoom *cr,LinphoneChatMessage *msg, gboolean hist){
	GtkTextView *text=GTK_TEXT_VIEW(linphone_gtk_get_widget(w,"textview"));
	GtkTextBuffer *buffer=gtk_text_view_get_buffer(text);
	GtkTextIter iter;
	char *from_str=linphone_address_as_string_uri_only(from);
	gchar *from_message=(gchar *)g_object_get_data(G_OBJECT(w),"from_message");
	GHashTable *table=(GHashTable*)g_object_get_data(G_OBJECT(w),"table");
	const GRegex *uri_regex = get_uri_regex();
	GMatchInfo *match_info = NULL;
	const char *message = linphone_chat_message_get_text(msg);
	time_t t;
	char buf[80];
	time_t tnow;
	struct tm *tm;
	int tnow_day;
	int tnow_year;
	int pos = 0, start, end;

	gtk_text_buffer_get_end_iter(buffer, &iter);
	if (g_strcmp0(from_message,from_str)!=0){
		gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, get_display_name(from), -1,
		                                         "from", me ? "me" : NULL, NULL);
		gtk_text_buffer_insert_with_tags_by_name(buffer,&iter, " : ", -1,
		                                         "from", me ? "me" : NULL, NULL);
		gtk_text_buffer_insert(buffer,&iter,"\n",-1);
		g_free(from_message);
		g_object_set_data(G_OBJECT(w),"from_message",g_strdup(from_str));
	}
	ms_free(from_str);

	// Inserts message body and tags URIs as hypertext links
	if(message) {
		g_regex_match(uri_regex, message, 0, &match_info);
		while(g_match_info_matches(match_info)) {
			g_match_info_fetch_pos(match_info, 0, &start, &end);
			if(pos < start) write_body(buffer, &iter, &message[pos], start-pos, me, FALSE);
			write_body(buffer, &iter, &message[start], end-start, me, TRUE);
			pos = end;
			g_match_info_next(match_info, NULL);
		}
		if(pos < strlen(message)) write_body(buffer, &iter, &message[pos], -1, me, FALSE); 
		gtk_text_buffer_insert(buffer,&iter,"\n",-1);
		g_match_info_free(match_info);
	}
	
	t=linphone_chat_message_get_time(msg);
	switch (linphone_chat_message_get_state (msg)){
		case LinphoneChatMessageStateInProgress:
			g_hash_table_insert(table,(gpointer)msg,GINT_TO_POINTER(gtk_text_iter_get_line(&iter)));
			gtk_text_buffer_insert_with_tags_by_name(buffer,&iter,"Sending ..",-1,
												"status", me ? "me" : NULL, NULL);
			//g_object_set_data(G_OBJECT(w),"table",table);
			break;
		case LinphoneChatMessageStateDelivered:
			tnow=time(NULL);
			tm=localtime(&tnow);
			tnow_day=tm->tm_yday;
			tnow_year=tm->tm_year;
			tm=localtime(&t);
			if(tnow_day != tm->tm_yday || (tnow_day == tm->tm_yday && tnow_year != tm->tm_year)) {
				strftime(buf,80,"%a %x, %H:%M",tm);
			} else {
				strftime(buf,80,"%H:%M",tm);
			}
			gtk_text_buffer_insert_with_tags_by_name(buffer,&iter,buf,-1,
	                      "status", me ? "me" : NULL, NULL);
			break;
		case  LinphoneChatMessageStateNotDelivered:
			gtk_text_buffer_insert_with_tags_by_name(buffer,&iter,"Message not sent",-1,
	                       "status", me ? "me" : NULL, NULL);
			break;
		default : gtk_text_buffer_insert_with_tags_by_name(buffer,&iter,"Sending ..",-1,
	                       "status", me ? "me" : NULL, NULL);
	}
	gtk_text_buffer_insert(buffer,&iter,"\n",-1);
	g_idle_add((GSourceFunc)scroll_to_end,text);
}
Exemplo n.º 11
0
void find(const char *filename, GRegex *regex)
{
  GFile           *file;
  gchar           *uri;
  PopplerDocument *doc;
  PopplerPage     *page;
  GError          *error = NULL;
  gint             i, n;
  gint             a, b;
  GMatchInfo      *match_info;
  gchar           *text;
  
  file = g_file_new_for_commandline_arg (filename);
  uri = g_file_get_uri (file);
  g_object_unref (file);

  if(!(doc = poppler_document_new_from_file (uri, NULL, &error)))
  {
    fprintf(stderr, "Could not open file %s: %s\n",
            filename, error->message);
    g_error_free(error);
    goto cleanup;
  }

  n = poppler_document_get_n_pages(doc);

  for(i = 0; i < n; ++i)
  {
    page = poppler_document_get_page(doc, i);
    text = poppler_page_get_text(page);
    
    g_regex_match(regex, text, (GRegexMatchFlags) 0, &match_info);
    
    while(g_match_info_matches(match_info))
    {
      bold_on();
      printf("%s:%i ", filename, i + 1);
      bold_off();
      
      g_match_info_fetch_pos(match_info, 0, &a, &b);
      
      for(; a >= 1 && *(text + a - 1) != '\n'; --a);
      for(; *(text + b) && *(text + b) != '\n'; ++b);
      
      
      //print out the entire line
      for(; a < b; ++a)
        printf("%c", *(text + a));
      printf("\n");
      
      g_match_info_next (match_info, NULL);
    }
    g_match_info_free(match_info);
    
    g_object_unref(page);
    
    g_free(text);
    
  }
  
  g_object_unref(doc);
  
cleanup:
  g_free(uri);
}
Exemplo n.º 12
0
gchar * ggp_message_format_to_gg(PurpleConversation *conv, const gchar *text)
{
	gchar *text_new, *tmp;
	GList *rt = NULL; /* reformatted text */
	GMatchInfo *match;
	guint pos = 0;
	GList *pending_objects = NULL;
	GList *font_stack = NULL;
	static int html_sizes_pt[7] = { 7, 8, 9, 10, 12, 14, 16 };

	ggp_font *font_new, *font_current, *font_base;
	gboolean font_changed = FALSE;
	gboolean in_any_tag = FALSE;

	/* TODO: verbose
	 * purple_debug_info("gg", "ggp formatting text: [%s]\n", text);
	 */

	/* default font */
	font_base = ggp_font_new();
	font_current = ggp_font_new();
	font_new = ggp_font_new();

	/* GG11 doesn't use nbsp, it just print spaces */
	text_new = purple_strreplace(text, "&nbsp;", " ");

	/* add end-of-message tag */
	if (strstr(text_new, "<eom>") != NULL) {
		tmp = text_new;
		text_new = purple_strreplace(text_new, "<eom>", "");
		g_free(tmp);
		purple_debug_warning("gg", "ggp_message_format_to_gg: "
			"unexpected <eom> tag\n");
	}
	tmp = text_new;
	text_new = g_strdup_printf("%s<eom></eom>", text_new);
	g_free(tmp);

	g_regex_match(global_data.re_html_tag, text_new, 0, &match);
	while (g_match_info_matches(match)) {
		int m_start, m_end, m_pos;
		gboolean tag_close;
		gchar *tag_str, *attribs_str;
		ggp_html_tag tag;
		gboolean text_before;

		/* reading tag and its contents */
		g_match_info_fetch_pos(match, 0, &m_start, &m_end);
		g_assert(m_start >= 0 && m_end >= 0);
		text_before = ((guint)m_start > pos);
		g_match_info_fetch_pos(match, 1, &m_pos, NULL);
		tag_close = (m_pos >= 0);
		tag_str = g_match_info_fetch(match, 2);
		tag = ggp_html_parse_tag(tag_str);
		attribs_str = g_match_info_fetch(match, 3);
		g_match_info_next(match, NULL);

		if (tag == GGP_HTML_TAG_UNKNOWN) {
			purple_debug_warning("gg", "ggp_message_format_to_gg: "
				"uknown tag %s\n", tag_str);
		}

		/* closing *all* formatting-related tags (GG11 weirness)
		 * and adding pending objects */
		if ((text_before && (font_changed || pending_objects)) ||
			(tag == GGP_HTML_TAG_EOM && tag_close))
		{
			font_changed = FALSE;
			if (in_any_tag) {
				in_any_tag = FALSE;
				if (font_current->s && !GGP_GG11_FORCE_COMPAT)
					rt = g_list_prepend(rt,
						g_strdup("</s>"));
				if (font_current->u)
					rt = g_list_prepend(rt,
						g_strdup("</u>"));
				if (font_current->i)
					rt = g_list_prepend(rt,
						g_strdup("</i>"));
				if (font_current->b)
					rt = g_list_prepend(rt,
						g_strdup("</b>"));
				rt = g_list_prepend(rt, g_strdup("</span>"));
			}
			if (pending_objects) {
				rt = g_list_concat(pending_objects, rt);
				pending_objects = NULL;
			}
		}

		/* opening formatting-related tags again */
		if (text_before && !in_any_tag) {
			gchar *style;
			GList *styles = NULL;
			gboolean has_size = (font_new->size > 0 &&
				font_new->size <= 7 && font_new->size != 3);

			if (has_size)
				styles = g_list_append(styles, g_strdup_printf(
					"font-size:%dpt;",
					html_sizes_pt[font_new->size - 1]));
			if (font_new->face)
				styles = g_list_append(styles, g_strdup_printf(
					"font-family:%s;", font_new->face));
			if (font_new->bgcolor >= 0 && !GGP_GG11_FORCE_COMPAT)
				styles = g_list_append(styles, g_strdup_printf(
					"background-color:#%06x;",
					font_new->bgcolor));
			if (font_new->color >= 0)
				styles = g_list_append(styles, g_strdup_printf(
					"color:#%06x;", font_new->color));

			if (styles) {
				gchar *combined = ggp_strjoin_list(" ", styles);
				g_list_free_full(styles, g_free);
				style = g_strdup_printf(" style=\"%s\"",
					combined);
				g_free(combined);
			} else
				style = g_strdup("");
			rt = g_list_prepend(rt, g_strdup_printf("<span%s>",
				style));
			g_free(style);

			if (font_new->b)
				rt = g_list_prepend(rt, g_strdup("<b>"));
			if (font_new->i)
				rt = g_list_prepend(rt, g_strdup("<i>"));
			if (font_new->u)
				rt = g_list_prepend(rt, g_strdup("<u>"));
			if (font_new->s && !GGP_GG11_FORCE_COMPAT)
				rt = g_list_prepend(rt, g_strdup("<s>"));

			ggp_font_free(font_current);
			font_current = font_new;
			font_new = ggp_font_clone(font_current);

			in_any_tag = TRUE;
		}
		if (text_before) {
			rt = g_list_prepend(rt,
				g_strndup(text_new + pos, m_start - pos));
		}

		/* set formatting of a following text */
		if (tag == GGP_HTML_TAG_B) {
			font_changed |= (font_new->b != !tag_close);
			font_new->b = !tag_close;
		} else if (tag == GGP_HTML_TAG_I) {
			font_changed |= (font_new->i != !tag_close);
			font_new->i = !tag_close;
		} else if (tag == GGP_HTML_TAG_U) {
			font_changed |= (font_new->u != !tag_close);
			font_new->u = !tag_close;
		} else if (tag == GGP_HTML_TAG_S) {
			font_changed |= (font_new->s != !tag_close);
			font_new->s = !tag_close;
		} else if (tag == GGP_HTML_TAG_IMG && !tag_close) {
			GHashTable *attribs = ggp_html_tag_attribs(attribs_str);
			gchar *val = NULL;
			uint64_t id;
			int stored_id = -1;
			ggp_image_prepare_result res = -1;

			if ((val = g_hash_table_lookup(attribs, "src")) != NULL
				&& g_str_has_prefix(val,
				PURPLE_STORED_IMAGE_PROTOCOL))
			{
				val += strlen(PURPLE_STORED_IMAGE_PROTOCOL);
				if (sscanf(val, "%u", &stored_id) != 1)
					stored_id = -1;
			}

			if (stored_id >= 0)
				res = ggp_image_prepare(conv, stored_id, &id);

			if (res == GGP_IMAGE_PREPARE_OK) {
				pending_objects = g_list_prepend(
					pending_objects, g_strdup_printf(
					"<img name=\"" GGP_IMAGE_ID_FORMAT
					"\">", id));
			} else if (res == GGP_IMAGE_PREPARE_TOO_BIG) {
				purple_conversation_write(conv, "",
					_("Image is too large, please try "
					"smaller one."), PURPLE_MESSAGE_ERROR,
					time(NULL));
			} else {
				purple_conversation_write(conv, "",
					_("Image cannot be sent."),
					PURPLE_MESSAGE_ERROR, time(NULL));
			}

			g_hash_table_destroy(attribs);
		} else if (tag == GGP_HTML_TAG_FONT && !tag_close) {
			GHashTable *attribs = ggp_html_tag_attribs(attribs_str);
			gchar *val = NULL;

			font_stack = g_list_prepend(font_stack,
				ggp_font_clone(font_new));

			if ((val = g_hash_table_lookup(attribs, "size")) != NULL
				&& val[0] >= '1' && val[0] <= '7' &&
				val[1] == '\0')
			{
				int size = val[0] - '0';
				font_changed |= (font_new->size != size);
				font_new->size = size;
			}

			if ((val = g_hash_table_lookup(attribs, "face"))
				!= NULL)
			{
				font_changed |=
					(g_strcmp0(font_new->face, val) != 0);
				g_free(font_new->face);
				font_new->face = g_strdup(val);
			}

			if ((val = g_hash_table_lookup(attribs, "color"))
				!= NULL && val[0] == '#' && strlen(val) == 7)
			{
				int color = ggp_html_decode_color(val);
				font_changed |= (font_new->color != color);
				font_new->color = color;
			}

			g_hash_table_destroy(attribs);
		}
		else if ((tag == GGP_HTML_TAG_SPAN || tag == GGP_HTML_TAG_DIV)
			&& !tag_close)
		{
			GHashTable *attribs, *styles = NULL;
			gchar *style = NULL;
			gchar *val = NULL;

			attribs = ggp_html_tag_attribs(attribs_str);

			font_stack = g_list_prepend(font_stack,
				ggp_font_clone(font_new));
			if (tag == GGP_HTML_TAG_DIV)
				pending_objects = g_list_prepend(
					pending_objects, g_strdup("<br>"));

			style = g_hash_table_lookup(attribs, "style");
			if (style)
				styles = ggp_html_css_attribs(style);

			if ((val = g_hash_table_lookup(styles,
				"background-color")) != NULL)
			{
				int color = ggp_html_decode_color(val);
				font_changed |= (font_new->bgcolor != color);
				font_new->bgcolor = color;
			}

			if ((val = g_hash_table_lookup(styles,
				"color")) != NULL)
			{
				int color = ggp_html_decode_color(val);
				font_changed |= (font_new->color != color);
				font_new->color = color;
			}

			if (styles)
				g_hash_table_destroy(styles);
			g_hash_table_destroy(attribs);
		}
		else if ((tag == GGP_HTML_TAG_FONT || tag == GGP_HTML_TAG_SPAN
			|| tag == GGP_HTML_TAG_DIV) && tag_close)
		{
			font_changed = TRUE;

			ggp_font_free(font_new);
			if (font_stack) {
				font_new = (ggp_font*)font_stack->data;
				font_stack = g_list_delete_link(
					font_stack, font_stack);
			}
			else
				font_new = ggp_font_clone(font_base);
		} else if (tag == GGP_HTML_TAG_BR) {
			pending_objects = g_list_prepend(pending_objects,
				g_strdup("<br>"));
		} else if (tag == GGP_HTML_TAG_HR) {
			pending_objects = g_list_prepend(pending_objects,
				g_strdup("<br><span>---</span><br>"));
		} else if (tag == GGP_HTML_TAG_A || tag == GGP_HTML_TAG_EOM) {
			/* do nothing */
		} else if (tag == GGP_HTML_TAG_UNKNOWN) {
			purple_debug_warning("gg", "ggp_message_format_to_gg: "
				"uknown tag %s\n", tag_str);
		} else {
			purple_debug_error("gg", "ggp_message_format_to_gg: "
				"not handled tag %s\n", tag_str);
		}

		pos = m_end;
		g_free(tag_str);
		g_free(attribs_str);
	}
	g_match_info_free(match);

	if (pos < strlen(text_new) || in_any_tag) {
		purple_debug_fatal("gg", "ggp_message_format_to_gg: "
			"end of message not reached\n");
	}

	/* releasing fonts recources */
	ggp_font_free(font_new);
	ggp_font_free(font_current);
	ggp_font_free(font_base);
	g_list_free_full(font_stack, ggp_font_free);

	/* combining reformatted text info one string */
	rt = g_list_reverse(rt);
	g_free(text_new);
	text_new = ggp_strjoin_list("", rt);
	g_list_free_full(rt, g_free);

	/* TODO: verbose
	 * purple_debug_info("gg", "reformatted text: [%s]\n", text_new);
	 */

	return text_new;
}
Exemplo n.º 13
0
void parse_metadata(RIP_MANAGER_INFO * rmi, TRACK_INFO * ti)
{
	int i;
	int eflags;
	int rc;
	int matched;
	mchar query_string[MAX_TRACK_LEN];
	Parse_Rule *rulep;

	/* Has any m/.../s rule matched? */
	BOOL save_track_matched = FALSE;

	/* Has any m/.../x rule matched? */
	BOOL exclude_track_matched = FALSE;

	ti->artist[0] = 0;
	ti->title[0] = 0;
	ti->album[0] = 0;
	ti->composed_metadata[0] = 0;
	ti->save_track = TRUE;

	/* Loop through rules, if we find a matching rule, then use it */
	/* For now, only default rules supported with ascii 
	   regular expressions. */
	debug_printf("Converting query string to wide\n");
	gstring_from_string(rmi, query_string, MAX_TRACK_LEN, ti->raw_metadata, CODESET_METADATA);
	for (rulep = rmi->parse_rules; rulep->cmd; rulep++) {
#if !defined (USE_GLIB_REGEX)
		regmatch_t pmatch[MAX_SUBMATCHES + 1];
#endif
		eflags = 0;
		if (rulep->cmd == PARSERULE_CMD_MATCH) {
			debug_mprintf(m_("Testing match rule: ") m_S m_(" vs. ") m_S m_("\n"),
				      query_string, rulep->match);
			if (rulep->flags & PARSERULE_SKIP) {
#if defined (USE_GLIB_REGEX)
				rc = g_regex_match(rulep->reg, query_string, 0, NULL);
				matched = rc;
#else
				rc = mregexec(rulep->reg, query_string, 0, NULL, eflags);
				matched = !rc;
#endif
				if (!matched) {
					continue;
				}
				/* GCS FIX: We need to return to the 
				   caller that the metadata should be dropped. */
				debug_printf("Skip rule matched\n");
				ti->save_track = FALSE;
				ti->have_track_info = 0;
				return;
			} else if (rulep->flags & PARSERULE_SAVE) {
#if defined (USE_GLIB_REGEX)
				rc = g_regex_match(rulep->reg, query_string, 0, NULL);
				matched = rc;
#else
				rc = mregexec(rulep->reg, query_string, 0, NULL, eflags);
				matched = !rc;
#endif
				if (!matched) {
					if (!save_track_matched)
						ti->save_track = FALSE;
					continue;
				}
				if (!exclude_track_matched) {
					ti->save_track = TRUE;
					save_track_matched = TRUE;
				}
			} else if (rulep->flags & PARSERULE_EXCLUDE) {
#if defined (USE_GLIB_REGEX)
				rc = g_regex_match(rulep->reg, query_string, 0, NULL);
				matched = rc;
#else
				rc = mregexec(rulep->reg, query_string, 0, NULL, eflags);
				matched = !rc;
#endif
				if (matched && !save_track_matched) {
					/* Rule matched => Exclude track */
					ti->save_track = FALSE;
					exclude_track_matched = TRUE;
				}
			} else {
#if defined (USE_GLIB_REGEX)
				GMatchInfo *match_info;
				gint nmatch;

				rc = g_regex_match(rulep->reg, query_string, 0, &match_info);
				if (rc == 0) {
					/* Didn't match rule. */
					continue;
				}
				nmatch = g_match_info_get_match_count(match_info);
				debug_printf("Got %d matches\n", nmatch);
				for (i = 0; i < nmatch; i++) {
					gchar *match = g_match_info_fetch(match_info, i);
					debug_printf("[%d] = %s\n", i, match);
					g_free(match);
				}
				copy_rule_result(ti->artist, match_info, rulep->artist_idx);
				copy_rule_result(ti->title, match_info, rulep->title_idx);
				copy_rule_result(ti->album, match_info, rulep->album_idx);
				copy_rule_result(ti->track_p, match_info, rulep->trackno_idx);
				copy_rule_result(ti->year, match_info, rulep->year_idx);
				g_match_info_free(match_info);
#else
				eflags = 0;
				rc = mregexec(rulep->reg, query_string, MAX_SUBMATCHES + 1, pmatch, eflags);
				if (rc != 0) {
					/* Didn't match rule. */
					continue;
				}

				for (i = 0; i < MAX_SUBMATCHES + 1; i++) {
					debug_printf("pmatch[%d]: (so,eo) = (%d,%d)\n", i,
						     pmatch[i].rm_so, pmatch[i].rm_eo);
				}
				copy_rule_result(ti->artist, query_string, pmatch, rulep->artist_idx);
				copy_rule_result(ti->title, query_string, pmatch, rulep->title_idx);
				copy_rule_result(ti->album, query_string, pmatch, rulep->album_idx);
				copy_rule_result(ti->track_p, query_string, pmatch, rulep->trackno_idx);
				copy_rule_result(ti->year, query_string, pmatch, rulep->year_idx);
#endif
				ti->have_track_info = 1;
				compose_metadata(rmi, ti);
				debug_mprintf(m_("Parsed track info.\n")
					      m_("ARTIST: ") m_S m_("\n")
					      m_("TITLE: ") m_S m_("\n")
					      m_("ALBUM: ") m_S m_("\n")
					      m_("TRACK: ") m_S m_("\n")
					      m_("YEAR: ") m_S m_("\n"),
					      ti->artist, ti->title, ti->album, ti->track_p, ti->year);
				return;
			}
		} else if (rulep->cmd == PARSERULE_CMD_SUBST) {
#if defined (USE_GLIB_REGEX)
			GMatchInfo *match_info;
			gint start_pos, end_pos;
			gchar *tmp, *subst_string;

			debug_mprintf(m_("Testing subst rule: ") m_S m_(" vs. ") m_S m_("\n"),
				      query_string, rulep->match);
			rc = g_regex_match(rulep->reg, query_string, 0, &match_info);
			if (rc == 0) {
				/* Didn't match rule. */
				continue;
			}
			rc = g_match_info_fetch_pos(match_info, 0, &start_pos, &end_pos);
			if (!rc) {
				debug_printf("g_match_info_fetch_pos returned 0\n");
				g_match_info_free(match_info);
				continue;
			}
			debug_printf("Matched at (%d,%d)\n", start_pos, end_pos);
			if (start_pos == -1) {
				g_match_info_free(match_info);
				continue;
			}
			tmp = g_strndup(query_string, start_pos);
			tmp[start_pos] = 0;
			subst_string = g_strconcat(tmp, rulep->subst, &tmp[end_pos], NULL);
			g_free(tmp);
			g_match_info_free(match_info);
			mstrncpy(query_string, subst_string, MAX_TRACK_LEN);
#else
			mchar subst_string[MAX_TRACK_LEN];
			int used, left;
			debug_mprintf(m_("Testing subst rule: ") m_S m_(" vs. ") m_S m_("\n"),
				      query_string, rulep->match);
			rc = mregexec(rulep->reg, query_string, 1, pmatch, eflags);
			if (rc != 0) {
				/* Didn't match rule. */
				continue;
			}
			/* Update the query string and continue. */
			debug_printf("Matched at (%d,%d)\n", pmatch[0].rm_so, pmatch[0].rm_eo);
			mstrncpy(subst_string, query_string, pmatch[0].rm_so + 1);
			debug_mprintf(m_("(1) subst_string = ") m_S m_("\n"), subst_string);
			used = pmatch[0].rm_so;
			left = MAX_TRACK_LEN - used;
			mstrncpy(subst_string + used, rulep->subst, left);
			debug_mprintf(m_("(2) subst_string = ") m_S m_("\n"), subst_string);
			used += mstrlen(rulep->subst);
			left = MAX_TRACK_LEN - used;
			mstrncpy(subst_string + used, query_string + pmatch[0].rm_eo, left);
			debug_mprintf(m_("(3) subst_string = ") m_S m_("\n"), subst_string);
			mstrncpy(query_string, subst_string, MAX_TRACK_LEN);
			debug_mprintf(m_("(4) query_string = ") m_S m_("\n"), query_string);
#endif
		}
	}
	debug_printf("Fell through while parsing data...\n");
	mstrncpy(ti->title, query_string, MAX_TRACK_LEN);
	ti->have_track_info = 1;
	compose_metadata(rmi, ti);
}
Exemplo n.º 14
0
void moloch_config_load()
{

    gboolean  status;
    GError   *error = 0;
    GKeyFile *keyfile;
    int       i;

    keyfile = molochKeyFile = g_key_file_new();

    status = g_key_file_load_from_file(keyfile, config.configFile, G_KEY_FILE_NONE, &error);
    if (!status || error) {
        printf("Couldn't load config file (%s) %s\n", config.configFile, (error?error->message:""));
        exit(1);
    }

    char **includes = moloch_config_str_list(keyfile, "includes", NULL);
    if (includes) {
        moloch_config_load_includes(includes);
        g_strfreev(includes);
        //LOG("KEYFILE:\n%s", g_key_file_to_data(molochKeyFile, NULL, NULL));
    }


    char *rotateIndex       = moloch_config_str(keyfile, "rotateIndex", "daily");

    if (strcmp(rotateIndex, "hourly") == 0)
        config.rotate = MOLOCH_ROTATE_HOURLY;
    else if (strcmp(rotateIndex, "daily") == 0)
        config.rotate = MOLOCH_ROTATE_DAILY;
    else if (strcmp(rotateIndex, "weekly") == 0)
        config.rotate = MOLOCH_ROTATE_WEEKLY;
    else if (strcmp(rotateIndex, "monthly") == 0)
        config.rotate = MOLOCH_ROTATE_MONTHLY;
    else {
        printf("Unknown rotateIndex '%s'\n", rotateIndex);
        exit(1);
    }
    g_free(rotateIndex);

    config.nodeClass        = moloch_config_str(keyfile, "nodeClass", NULL);
    gchar **tags            = moloch_config_str_list(keyfile, "dontSaveTags", NULL);
    if (tags) {
        for (i = 0; tags[i]; i++) {
            if (!(*tags[i]))
                continue;
            int num = 1;
            char *colon = strchr(tags[i], ':');
            if (colon) {
                *colon = 0;
                num = atoi(colon+1);
                if (num < 1)
                    num = 1;
                if (num > 0xffff)
                    num = 0xffff;
            }
            moloch_string_add((MolochStringHash_t *)(char*)&config.dontSaveTags, tags[i], (gpointer)(long)num, TRUE);
        }
        g_strfreev(tags);
    }

    char *bpfsStrs[MOLOCH_FILTER_MAX] = {"dontSaveBPFs", "minPacketsSaveBPFs"};
    int t;
    for (t = 0; t < MOLOCH_FILTER_MAX; t++) {
        config.bpfs[t]     = moloch_config_str_list(keyfile, bpfsStrs[t], NULL);
        if (config.bpfs[t]) {
            for (i = 0; config.bpfs[t][i]; i++);  //empty loop, counting
            config.bpfsNum[t] = i;
            config.bpfsVal[t] = malloc(config.bpfsNum[t]*sizeof(int));

            GRegex     *regex = g_regex_new(":\\s*(\\d+)\\s*$", 0, 0, 0);
            GMatchInfo *match_info;
            for (i = 0; config.bpfs[t][i]; i++) {
                g_regex_match(regex, config.bpfs[t][i], 0, &match_info);
                if (g_match_info_matches(match_info)) {
                    config.bpfsVal[t][i] = atoi(g_match_info_fetch(match_info, 1));
                    gint pos;
                    g_match_info_fetch_pos(match_info, 0, &pos, NULL);
                    config.bpfs[t][i][pos] = 0;
                } else {
                    config.bpfsVal[t][i] = 1;
                }
                g_match_info_free(match_info);
            }
            g_regex_unref(regex);
        }
    }

    config.plugins          = moloch_config_str_list(keyfile, "plugins", NULL);
    config.rootPlugins      = moloch_config_str_list(keyfile, "rootPlugins", NULL);
    config.smtpIpHeaders    = moloch_config_str_list(keyfile, "smtpIpHeaders", NULL);

    if (config.smtpIpHeaders) {
        for (i = 0; config.smtpIpHeaders[i]; i++) {
            int len = strlen(config.smtpIpHeaders[i]);
            char *lower = g_ascii_strdown(config.smtpIpHeaders[i], len);
            g_free(config.smtpIpHeaders[i]);
            config.smtpIpHeaders[i] = lower;
            if (lower[len-1] == ':')
                lower[len-1] = 0;
        }
    }

    config.prefix           = moloch_config_str(keyfile, "prefix", "");
    int len = strlen(config.prefix);
    if (len > 0 && config.prefix[len - 1] != '_') {
        char *tmp  = malloc(len + 2);
        memcpy(tmp, config.prefix, len);
        tmp[len] = '_';
        tmp[len+1] = 0;
        g_free(config.prefix);
        config.prefix = tmp;
    }

    config.elasticsearch    = moloch_config_str(keyfile, "elasticsearch", "localhost:9200");
    config.interface        = moloch_config_str_list(keyfile, "interface", NULL);
    config.pcapDir          = moloch_config_str_list(keyfile, "pcapDir", NULL);
    config.bpf              = moloch_config_str(keyfile, "bpf", NULL);
    config.yara             = moloch_config_str(keyfile, "yara", NULL);
    config.emailYara        = moloch_config_str(keyfile, "emailYara", NULL);
    config.geoipFile        = moloch_config_str(keyfile, "geoipFile", NULL);
    config.rirFile          = moloch_config_str(keyfile, "rirFile", NULL);
    config.geoipASNFile     = moloch_config_str(keyfile, "geoipASNFile", NULL);
    config.geoip6File       = moloch_config_str(keyfile, "geoip6File", NULL);
    config.geoipASN6File    = moloch_config_str(keyfile, "geoipASN6File", NULL);
    config.dropUser         = moloch_config_str(keyfile, "dropUser", NULL);
    config.dropGroup        = moloch_config_str(keyfile, "dropGroup", NULL);
    config.pluginsDir       = moloch_config_str_list(keyfile, "pluginsDir", NULL);
    config.parsersDir       = moloch_config_str_list(keyfile, "parsersDir", " /data/moloch/parsers ; ./parsers ");
    char *offlineRegex      = moloch_config_str(keyfile, "offlineFilenameRegex", "(?i)\\.(pcap|cap)$");

    config.offlineRegex     = g_regex_new(offlineRegex, 0, 0, &error);
    if (!config.offlineRegex || error) {
        printf("Couldn't parse offlineRegex (%s) %s\n", offlineRegex, (error?error->message:""));
        exit(1);
    }
    g_free(offlineRegex);

    config.pcapDirTemplate  = moloch_config_str(keyfile, "pcapDirTemplate", NULL);
    if (config.pcapDirTemplate && config.pcapDirTemplate[0] != '/') {
        printf("pcapDirTemplate MUST start with a / '%s'\n", config.pcapDirTemplate);
        exit(1);
    }

    config.pcapDirAlgorithm = moloch_config_str(keyfile, "pcapDirAlgorithm", "round-robin");
    if (strcmp(config.pcapDirAlgorithm, "round-robin") != 0
            && strcmp(config.pcapDirAlgorithm, "max-free-percent") != 0
            && strcmp(config.pcapDirAlgorithm, "max-free-bytes") != 0) {
        printf("'%s' is not a valid value for pcapDirAlgorithm.  Supported algorithms are round-robin, max-free-percent, and max-free-bytes.\n", config.pcapDirAlgorithm);
        exit(1);
    }

    config.maxFileSizeG          = moloch_config_double(keyfile, "maxFileSizeG", 4, 0.01, 1024);
    config.maxFileSizeB          = config.maxFileSizeG*1024LL*1024LL*1024LL;
    config.maxFileTimeM          = moloch_config_int(keyfile, "maxFileTimeM", 0, 0, 0xffff);
    config.timeouts[SESSION_ICMP]= moloch_config_int(keyfile, "icmpTimeout", 10, 1, 0xffff);
    config.timeouts[SESSION_UDP] = moloch_config_int(keyfile, "udpTimeout", 60, 1, 0xffff);
    config.timeouts[SESSION_TCP] = moloch_config_int(keyfile, "tcpTimeout", 60*8, 10, 0xffff);
    config.tcpSaveTimeout        = moloch_config_int(keyfile, "tcpSaveTimeout", 60*8, 10, 60*120);
    config.maxStreams            = moloch_config_int(keyfile, "maxStreams", 1500000, 1, 16777215);
    config.maxPackets            = moloch_config_int(keyfile, "maxPackets", 10000, 1, 1000000);
    config.maxPacketsInQueue     = moloch_config_int(keyfile, "maxPacketsInQueue", 200000, 10000, 5000000);
    config.dbBulkSize            = moloch_config_int(keyfile, "dbBulkSize", 200000, MOLOCH_HTTP_BUFFER_SIZE*2, 1000000);
    config.dbFlushTimeout        = moloch_config_int(keyfile, "dbFlushTimeout", 5, 1, 60*30);
    config.maxESConns            = moloch_config_int(keyfile, "maxESConns", 20, 5, 1000);
    config.maxESRequests         = moloch_config_int(keyfile, "maxESRequests", 500, 10, 5000);
    config.logEveryXPackets      = moloch_config_int(keyfile, "logEveryXPackets", 50000, 1000, 1000000);
    config.pcapBufferSize        = moloch_config_int(keyfile, "pcapBufferSize", 300000000, 100000, 0xffffffff);
    config.pcapWriteSize         = moloch_config_int(keyfile, "pcapWriteSize", 0x10000, 0x40000, 0x800000);
    config.maxFreeOutputBuffers  = moloch_config_int(keyfile, "maxFreeOutputBuffers", 50, 0, 0xffff);
    config.fragsTimeout          = moloch_config_int(keyfile, "fragsTimeout", 60*8, 60, 0xffff);
    config.maxFrags              = moloch_config_int(keyfile, "maxFrags", 50000, 1000, 0xffffff);

    config.packetThreads         = moloch_config_int(keyfile, "packetThreads", 1, 1, MOLOCH_MAX_PACKET_THREADS);


    config.logUnknownProtocols   = moloch_config_boolean(keyfile, "logUnknownProtocols", config.debug);
    config.logESRequests         = moloch_config_boolean(keyfile, "logESRequests", config.debug);
    config.logFileCreation       = moloch_config_boolean(keyfile, "logFileCreation", config.debug);
    config.parseSMTP             = moloch_config_boolean(keyfile, "parseSMTP", TRUE);
    config.parseSMB              = moloch_config_boolean(keyfile, "parseSMB", TRUE);
    config.parseQSValue          = moloch_config_boolean(keyfile, "parseQSValue", FALSE);
    config.parseCookieValue      = moloch_config_boolean(keyfile, "parseCookieValue", FALSE);
    config.compressES            = moloch_config_boolean(keyfile, "compressES", FALSE);
    config.antiSynDrop           = moloch_config_boolean(keyfile, "antiSynDrop", TRUE);
    config.readTruncatedPackets  = moloch_config_boolean(keyfile, "readTruncatedPackets", FALSE);

}
Exemplo n.º 15
0
//!
//! @brief Analyzes a sentence for misspellings, positions, and stem forms of words
//!
GList*
lw_morphologyengine_hunspell_analyze (LwMorphologyEngine *engine, 
                                      const gchar        *TEXT, 
                                      gboolean            include_spellcheck)
{
    //Sanity checks
    if (engine == NULL) return NULL;
    if (engine->hunspell == NULL) return NULL;
    if (TEXT == NULL) return NULL;

    //Declations
    gint start_offset = 0, end_offset = 0;
    GMatchInfo *match_info = NULL;
    GList *list = NULL;
    gchar *word = NULL;
    LwMorphology *morphology = NULL;

    //Initializations
    gchar *shortened = lw_regex_remove_parenthesis (TEXT);

    //Body
    lw_regex_get_contiguous (shortened, &match_info);

    while (g_match_info_matches (match_info))
    {
        word = g_match_info_fetch (match_info, 0);
        if (word != NULL && !g_unichar_ispunct (g_utf8_get_char (word)) && !lw_util_string_has_japanese (word))
        {
          g_match_info_fetch_pos (match_info, 0, &start_offset, &end_offset);

          gchar *normalized = NULL, *stem = NULL, *canonical = NULL, *spellcheck = NULL;

          //Generate the forms
          normalized = lw_util_normalize_string (word, TRUE, FALSE);    
          stem = lw_morphologyengine_hunspell_stem (engine, word);
          if (stem != NULL) canonical = lw_util_normalize_string (stem, TRUE, FALSE); //You don't want to case fold before hunspell works
          if (include_spellcheck) spellcheck = lw_morphologyengine_hunspell_spellcheck (engine, word);

          //Cleanup identicals
          if (normalized != NULL)
          {
            if (strcmp(normalized, word) == 0) { g_free (normalized); normalized = NULL; }
          }
          if (stem != NULL)
          {
            if (strcmp(stem, word) == 0) { g_free (stem); stem = NULL; }
            if (canonical != NULL && strcmp(stem, canonical) == 0) { g_free (canonical); canonical = NULL; } //Canonical is built on stem
          }

          morphology = lw_morphology_new (
            word,
            normalized,
            stem,
            canonical,
            spellcheck,
            NULL,
            start_offset,
            end_offset
          );
          if (morphology != NULL)
          {
            list = g_list_append (list, morphology);
            morphology = NULL;
          }
          word = NULL;
        }
        else if (word != NULL)
        {
          g_free (word); word = NULL;
        }
        g_match_info_next (match_info, NULL);
    }

errored:

    if (match_info != NULL) g_match_info_free (match_info); match_info = NULL;
    if (shortened != NULL) g_free(shortened); shortened = NULL;
    if (morphology != NULL) lw_morphology_free (morphology); morphology = NULL;
    if (word != NULL) g_free (word); word = NULL;

    return list;
}
Exemplo n.º 16
0
gchar *dls_search_translate_search_string(GHashTable *filter_map,
					  const gchar *search_string)
{
	GRegex *reg;
	gchar *retval = NULL;
	GMatchInfo *match_info = NULL;
	gchar *prop = NULL;
	gchar *op = NULL;
	gchar *value = NULL;
	const gchar *translated_value;
	gchar *translated_type_ex;
	dls_prop_map_t *prop_map;
	GString *str;
	gint start_pos;
	gint end_pos;
	gint old_end_pos = 0;
	unsigned int skipped;
	unsigned int search_string_len = strlen(search_string);
	gchar *root_path;
	gchar *id;

	reg = g_regex_new("(\\w+)\\s+(=|!=|<|<=|>|>|contains|doesNotContain|"\
			  "derivedfrom|exists)\\s+"\
			  "(\"[^\"]*\"|true|false)",
			  0, 0, NULL);
	str = g_string_new("");

	g_regex_match(reg, search_string, 0, &match_info);
	while (g_match_info_matches(match_info)) {
		prop = g_match_info_fetch(match_info, 1);
		if (!prop)
			goto on_error;

		op = g_match_info_fetch(match_info, 2);
		if (!op)
			goto on_error;

		value = g_match_info_fetch(match_info, 3);
		if (!value)
			goto on_error;

		/* Handle special cases where we need to translate
		   value as well as property name */

		if (!strcmp(prop, DLS_INTERFACE_PROP_TYPE)) {
			/* Skip the quotes */

			value[strlen(value) - 1] = 0;
			translated_value = dls_props_media_spec_to_upnp_class(
				value + 1);
			if (!translated_value)
				goto on_error;
			g_free(value);
			value = g_strdup_printf("\"%s\"", translated_value);
		} else if (!strcmp(prop, DLS_INTERFACE_PROP_TYPE_EX)) {
			/* Skip the quotes */

			value[strlen(value) - 1] = 0;
			translated_type_ex =
				dls_props_media_spec_ex_to_upnp_class(
					value + 1);
			if (!translated_type_ex)
				goto on_error;
			g_free(value);
			value = g_strdup_printf("\"%s\"", translated_type_ex);
			g_free(translated_type_ex);
		} else if (!strcmp(prop, DLS_INTERFACE_PROP_PARENT) ||
			   !strcmp(prop, DLS_INTERFACE_PROP_PATH)) {
			value[strlen(value) - 1] = 0;
			if (!dls_path_get_path_and_id(value + 1, &root_path,
						      &id, NULL))
				goto on_error;
			g_free(root_path);
			g_free(value);
			value = g_strdup_printf("\"%s\"", id);
			g_free(id);
		}

		prop_map = g_hash_table_lookup(filter_map, prop);
		if (!prop_map)
			goto on_error;

		if (!prop_map->searchable)
			goto on_error;

		if (!g_match_info_fetch_pos(match_info, 0, &start_pos,
					    &end_pos))
			goto on_error;

		skipped = start_pos - old_end_pos;
		if (skipped > 0)
			g_string_append_len(str, &search_string[old_end_pos],
					    skipped);
		g_string_append_printf(str, "%s %s %s",
				       prop_map->upnp_prop_name, op, value);
		old_end_pos = end_pos;

		g_free(value);
		g_free(prop);
		g_free(op);

		value = NULL;
		prop = NULL;
		op = NULL;

		g_match_info_next(match_info, NULL);
	}

	skipped = search_string_len - old_end_pos;
	if (skipped > 0)
		g_string_append_len(str, &search_string[old_end_pos],
				    skipped);

	retval = g_string_free(str, FALSE);
	str = NULL;

on_error:

	g_free(value);
	g_free(prop);
	g_free(op);

	if (match_info)
		g_match_info_free(match_info);

	if (str)
		g_string_free(str, TRUE);

	g_regex_unref(reg);

	return retval;
}