2011-05-22 09:27:12 +02:00
|
|
|
/* -*-mode: c; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-*/
|
|
|
|
|
2009-11-25 21:55:06 +01:00
|
|
|
/*
|
2011-05-22 09:27:12 +02:00
|
|
|
** Copyright (C) 2008-2011 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
2009-11-25 21:55:06 +01:00
|
|
|
**
|
|
|
|
** This program is free software; you can redistribute it and/or modify
|
|
|
|
** it under the terms of the GNU General Public License as published by
|
|
|
|
** the Free Software Foundation; either version 3 of the License, or
|
|
|
|
** (at your option) any later version.
|
|
|
|
**
|
|
|
|
** This program is distributed in the hope that it will be useful,
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
** GNU General Public License for more details.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
** along with this program; if not, write to the Free Software Foundation,
|
|
|
|
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
**
|
|
|
|
*/
|
2010-11-20 16:27:17 +01:00
|
|
|
|
|
|
|
|
2010-11-14 21:04:16 +01:00
|
|
|
#if HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif /*HAVE_CONFIG_H*/
|
|
|
|
|
2009-11-25 21:55:06 +01:00
|
|
|
|
|
|
|
#include <glib.h>
|
2010-09-11 10:19:58 +02:00
|
|
|
#include <string.h>
|
2010-11-11 21:04:47 +01:00
|
|
|
#include <ctype.h>
|
2010-11-24 22:30:41 +01:00
|
|
|
#include <stdlib.h>
|
2010-12-11 12:52:03 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
/* hopefully, this should get us a sane PATH_MAX */
|
|
|
|
#include <limits.h>
|
|
|
|
/* not all systems provide PATH_MAX in limits.h */
|
|
|
|
#ifndef PATH_MAX
|
|
|
|
#include <sys/param.h>
|
2011-01-22 02:13:18 +01:00
|
|
|
#ifndef PATH_MAX
|
|
|
|
#define PATH_MAX MAXPATHLEN
|
|
|
|
#endif /*!PATH_MAX*/
|
|
|
|
#endif /*PATH_MAX*/
|
|
|
|
|
|
|
|
#include "mu-str.h"
|
|
|
|
#include "mu-msg-flags.h"
|
|
|
|
#include "mu-msg-fields.h"
|
|
|
|
|
|
|
|
const char*
|
|
|
|
mu_str_date_s (const char* frm, time_t t)
|
|
|
|
{
|
|
|
|
struct tm *tmbuf;
|
|
|
|
static char buf[128];
|
2011-06-01 20:55:32 +02:00
|
|
|
static int is_utf8 = -1;
|
|
|
|
|
|
|
|
if (G_UNLIKELY(is_utf8 == -1))
|
|
|
|
is_utf8 = mu_util_locale_is_utf8 () ? 1 : 0;
|
|
|
|
|
2011-01-22 02:13:18 +01:00
|
|
|
g_return_val_if_fail (frm, NULL);
|
2010-11-03 06:43:46 +01:00
|
|
|
|
2011-01-22 02:13:18 +01:00
|
|
|
tmbuf = localtime(&t);
|
2009-11-25 21:55:06 +01:00
|
|
|
|
2011-01-22 02:13:18 +01:00
|
|
|
strftime (buf, sizeof(buf), frm, tmbuf);
|
2009-11-25 21:55:06 +01:00
|
|
|
|
2011-06-01 20:55:32 +02:00
|
|
|
if (!is_utf8) {
|
|
|
|
/* charset is _not_ utf8, so we need to convert it, so
|
|
|
|
* the date could contain locale-specific characters*/
|
|
|
|
gchar *conv;
|
|
|
|
GError *err;
|
|
|
|
err = NULL;
|
|
|
|
conv = g_locale_to_utf8 (buf, -1, NULL, NULL, &err);
|
|
|
|
if (err) {
|
|
|
|
g_warning ("conversion failed: %s", err->message);
|
|
|
|
g_error_free (err);
|
|
|
|
strcpy (buf, "<error>");
|
|
|
|
} else
|
|
|
|
strncpy (buf, conv, sizeof(buf));
|
|
|
|
|
|
|
|
g_free (conv);
|
|
|
|
}
|
|
|
|
|
2011-01-22 02:13:18 +01:00
|
|
|
return buf;
|
|
|
|
}
|
2009-11-25 21:55:06 +01:00
|
|
|
|
2011-01-22 02:13:18 +01:00
|
|
|
char*
|
|
|
|
mu_str_date (const char *frm, time_t t)
|
|
|
|
{
|
|
|
|
return g_strdup (mu_str_date_s(frm, t));
|
|
|
|
}
|
2009-11-25 21:55:06 +01:00
|
|
|
|
|
|
|
|
2010-11-07 17:24:48 +01:00
|
|
|
|
2011-01-22 02:13:18 +01:00
|
|
|
const char*
|
|
|
|
mu_str_display_date_s (time_t t)
|
|
|
|
{
|
|
|
|
time_t now;
|
|
|
|
static const time_t SECS_IN_DAY = 24 * 60 * 60;
|
2010-11-07 17:24:48 +01:00
|
|
|
|
2011-01-22 02:13:18 +01:00
|
|
|
now = time (NULL);
|
|
|
|
|
|
|
|
if (ABS(now - t) > SECS_IN_DAY)
|
|
|
|
return mu_str_date_s ("%x", t);
|
|
|
|
else
|
|
|
|
return mu_str_date_s ("%X", t);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char*
|
|
|
|
mu_str_size_s (size_t s)
|
|
|
|
{
|
|
|
|
static char buf[32];
|
|
|
|
|
|
|
|
#ifdef HAVE_GLIB216
|
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
tmp = g_format_size_for_display ((goffset)s);
|
|
|
|
strncpy (buf, tmp, sizeof(buf));
|
|
|
|
buf[sizeof(buf) -1] = '\0'; /* just in case */
|
|
|
|
g_free (tmp);
|
|
|
|
|
|
|
|
#else
|
|
|
|
if (s >= 1000 * 1000)
|
|
|
|
g_snprintf(buf, sizeof(buf), "%.1f MB",
|
|
|
|
(double)s/(1000*1000));
|
|
|
|
else
|
|
|
|
g_snprintf(buf, sizeof(buf), "%.1f kB", (double)s/(1000));
|
|
|
|
#endif /*HAVE_GLIB216*/
|
2010-01-22 18:17:00 +01:00
|
|
|
|
2010-08-15 12:32:43 +02:00
|
|
|
|
2011-01-22 02:13:18 +01:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
char*
|
|
|
|
mu_str_size (size_t s)
|
|
|
|
{
|
|
|
|
return g_strdup (mu_str_size_s(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
const char*
|
|
|
|
mu_str_flags_s (MuMsgFlags flags)
|
|
|
|
{
|
|
|
|
return mu_msg_flags_str_s (flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
char*
|
|
|
|
mu_str_flags (MuMsgFlags flags)
|
|
|
|
{
|
|
|
|
return g_strdup (mu_str_flags_s(flags));
|
|
|
|
}
|
|
|
|
|
|
|
|
char*
|
|
|
|
mu_str_summarize (const char* str, size_t max_lines)
|
|
|
|
{
|
|
|
|
char *summary;
|
|
|
|
size_t nl_seen;
|
|
|
|
unsigned i,j;
|
|
|
|
gboolean last_was_blank;
|
|
|
|
|
|
|
|
g_return_val_if_fail (str, NULL);
|
|
|
|
g_return_val_if_fail (max_lines > 0, NULL);
|
2010-09-11 10:19:58 +02:00
|
|
|
|
2011-01-22 02:13:18 +01:00
|
|
|
/* len for summary <= original len */
|
|
|
|
summary = g_new (gchar, strlen(str) + 1);
|
|
|
|
|
|
|
|
/* copy the string up to max_lines lines, replace CR/LF/tab with
|
|
|
|
* single space */
|
|
|
|
for (i = j = 0, nl_seen = 0, last_was_blank = TRUE;
|
|
|
|
nl_seen < max_lines && str[i] != '\0'; ++i) {
|
|
|
|
|
|
|
|
if (str[i] == '\n' || str[i] == '\r' ||
|
|
|
|
str[i] == '\t' || str[i] == ' ' ) {
|
|
|
|
|
|
|
|
if (str[i] == '\n')
|
|
|
|
++nl_seen;
|
|
|
|
|
|
|
|
/* no double-blanks or blank at end of str */
|
|
|
|
if (!last_was_blank && str[i+1] != '\0')
|
|
|
|
summary[j++] = ' ';
|
|
|
|
|
|
|
|
last_was_blank = TRUE;
|
|
|
|
} else {
|
|
|
|
|
|
|
|
summary[j++] = str[i];
|
|
|
|
last_was_blank = FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
summary[j] = '\0';
|
|
|
|
return summary;
|
|
|
|
}
|
|
|
|
|
2011-01-29 14:00:46 +01:00
|
|
|
|
|
|
|
static void
|
|
|
|
cleanup_contact (char *contact)
|
|
|
|
{
|
|
|
|
char *c, *c2;
|
|
|
|
|
|
|
|
/* replace "'<> with space */
|
|
|
|
for (c2 = contact; *c2; ++c2)
|
|
|
|
if (*c2 == '"' || *c2 == '\'' || *c2 == '<' || *c2 == '>')
|
|
|
|
*c2 = ' ';
|
|
|
|
|
|
|
|
/* remove everything between '()' if it's after the 5th pos;
|
|
|
|
* good to cleanup corporate contact address spam... */
|
|
|
|
c = g_strstr_len (contact, -1, "(");
|
|
|
|
if (c && c - contact > 5)
|
2011-05-22 09:27:12 +02:00
|
|
|
*c = '\0';
|
2011-01-29 14:00:46 +01:00
|
|
|
|
|
|
|
g_strstrip (contact);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-22 02:13:18 +01:00
|
|
|
/* this is still somewhat simplistic... */
|
|
|
|
const char*
|
|
|
|
mu_str_display_contact_s (const char *str)
|
|
|
|
{
|
|
|
|
static gchar contact[255];
|
|
|
|
gchar *c, *c2;
|
|
|
|
|
|
|
|
str = str ? str : "";
|
|
|
|
g_strlcpy (contact, str, sizeof(contact));
|
|
|
|
|
|
|
|
/* we check for '<', so we can strip out the address stuff in
|
|
|
|
* e.g. 'Hello World <hello@world.xx>, but only if there is
|
|
|
|
* something alphanumeric before the <
|
|
|
|
*/
|
|
|
|
c = g_strstr_len (contact, -1, "<");
|
|
|
|
if (c != NULL) {
|
|
|
|
for (c2 = contact; c2 < c && !(isalnum(*c2)); ++c2);
|
|
|
|
if (c2 != c) /* apparently, there was something,
|
|
|
|
* so we can remove the <... part*/
|
|
|
|
*c = '\0';
|
|
|
|
}
|
2010-11-07 18:01:34 +01:00
|
|
|
|
2011-01-29 14:00:46 +01:00
|
|
|
cleanup_contact (contact);
|
|
|
|
|
2011-01-22 02:13:18 +01:00
|
|
|
return contact;
|
|
|
|
}
|
2010-11-07 18:01:34 +01:00
|
|
|
|
2011-01-22 02:13:18 +01:00
|
|
|
char*
|
|
|
|
mu_str_display_contact (const char *str)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (str, NULL);
|
2010-11-07 18:01:34 +01:00
|
|
|
|
2011-01-22 02:13:18 +01:00
|
|
|
return g_strdup (mu_str_display_contact_s (str));
|
|
|
|
}
|
2010-11-23 21:05:55 +01:00
|
|
|
|
|
|
|
|
2011-01-22 02:13:18 +01:00
|
|
|
struct _CheckPrefix {
|
|
|
|
const char *pfx;
|
|
|
|
guint len;
|
|
|
|
gboolean match;
|
|
|
|
};
|
|
|
|
typedef struct _CheckPrefix CheckPrefix;
|
2010-11-29 20:21:55 +01:00
|
|
|
|
2011-01-22 02:13:18 +01:00
|
|
|
static void
|
|
|
|
each_check_prefix (MuMsgFieldId mfid, CheckPrefix *cpfx)
|
|
|
|
{
|
|
|
|
const char *field_name;
|
|
|
|
char field_shortcut;
|
2010-11-29 20:21:55 +01:00
|
|
|
|
2011-01-22 02:13:18 +01:00
|
|
|
if (!cpfx || cpfx->match)
|
|
|
|
return;
|
2010-11-29 20:21:55 +01:00
|
|
|
|
2011-01-22 02:13:18 +01:00
|
|
|
field_shortcut = mu_msg_field_shortcut (mfid);
|
|
|
|
if (field_shortcut == cpfx->pfx[0] && cpfx->pfx[1] == ':') {
|
|
|
|
cpfx->match = TRUE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
field_name = mu_msg_field_name (mfid);
|
|
|
|
if (field_name &&
|
|
|
|
strncmp (cpfx->pfx, field_name, cpfx->len) == 0) {
|
|
|
|
cpfx->match = TRUE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-23 07:16:09 +02:00
|
|
|
/* 'colon' points at a position inside q pointing at a ':'
|
|
|
|
* character. function determines whether the prefix is a registered
|
|
|
|
* prefix (like 'subject' or 'from' or 's') */
|
2011-01-22 02:13:18 +01:00
|
|
|
static gboolean
|
|
|
|
is_xapian_prefix (const char *q, const char *colon)
|
|
|
|
{
|
|
|
|
const char *cur;
|
2010-11-29 20:21:55 +01:00
|
|
|
|
2011-01-22 02:13:18 +01:00
|
|
|
if (colon == q)
|
|
|
|
return FALSE; /* : at beginning, not a prefix */
|
2010-11-29 20:21:55 +01:00
|
|
|
|
2011-01-22 02:13:18 +01:00
|
|
|
/* track back from colon until a boundary or beginning of the
|
|
|
|
* str */
|
|
|
|
for (cur = colon - 1; cur >= q; --cur) {
|
2010-11-29 20:21:55 +01:00
|
|
|
|
2011-01-22 02:13:18 +01:00
|
|
|
if (cur == q || !isalpha (*(cur-1))) {
|
2010-11-29 20:21:55 +01:00
|
|
|
|
2011-01-22 02:13:18 +01:00
|
|
|
CheckPrefix cpfx;
|
|
|
|
memset (&cpfx, 0, sizeof(CheckPrefix));
|
2010-11-29 20:21:55 +01:00
|
|
|
|
2011-01-22 02:13:18 +01:00
|
|
|
cpfx.pfx = cur;
|
|
|
|
cpfx.len = (colon - cur);
|
|
|
|
cpfx.match = FALSE;
|
2010-11-29 20:21:55 +01:00
|
|
|
|
2011-01-22 02:13:18 +01:00
|
|
|
mu_msg_field_foreach ((MuMsgFieldForEachFunc)
|
|
|
|
each_check_prefix,
|
|
|
|
&cpfx);
|
2010-11-29 20:21:55 +01:00
|
|
|
|
2011-01-22 02:13:18 +01:00
|
|
|
return (cpfx.match);
|
|
|
|
}
|
|
|
|
}
|
2010-11-29 20:21:55 +01:00
|
|
|
|
2011-01-22 02:13:18 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
time_t
|
2011-06-30 22:46:21 +02:00
|
|
|
mu_str_date_parse_hdwmy (const char *nptr)
|
2011-01-22 02:13:18 +01:00
|
|
|
{
|
2011-05-22 09:27:12 +02:00
|
|
|
long int num;
|
2011-06-30 22:46:21 +02:00
|
|
|
char *endptr;
|
2011-05-22 09:27:12 +02:00
|
|
|
time_t now, delta;
|
|
|
|
time_t never = (time_t)-1;
|
2010-11-30 21:33:15 +01:00
|
|
|
|
2011-06-30 22:46:21 +02:00
|
|
|
g_return_val_if_fail (nptr, never);
|
2010-11-30 21:33:15 +01:00
|
|
|
|
2011-06-30 22:46:21 +02:00
|
|
|
num = strtol (nptr, &endptr, 10);
|
2011-05-22 09:27:12 +02:00
|
|
|
if (num <= 0 || num > 9999)
|
|
|
|
return never;
|
2010-11-30 21:33:15 +01:00
|
|
|
|
2011-06-30 22:46:21 +02:00
|
|
|
if (endptr == NULL || *endptr == '\0')
|
2011-05-22 09:27:12 +02:00
|
|
|
return never;
|
2010-11-30 21:33:15 +01:00
|
|
|
|
2011-06-30 22:46:21 +02:00
|
|
|
switch (endptr[0]) {
|
2011-05-22 09:27:12 +02:00
|
|
|
case 'h': /* hour */
|
|
|
|
delta = num * 60 * 60; break;
|
|
|
|
case 'd': /* day */
|
|
|
|
delta = num * 24 * 60 * 60; break;
|
|
|
|
case 'w': /* week */
|
|
|
|
delta = num * 7 * 24 * 60 * 60; break;
|
2011-05-23 07:16:09 +02:00
|
|
|
case 'm': /* month */
|
2011-05-22 09:27:12 +02:00
|
|
|
delta = num * 30 * 24 * 60 * 60; break;
|
2011-05-23 07:16:09 +02:00
|
|
|
case 'y': /* year */
|
2011-05-22 09:27:12 +02:00
|
|
|
delta = num * 365 * 24 * 60 * 60; break;
|
|
|
|
default:
|
|
|
|
return never;
|
|
|
|
}
|
|
|
|
|
|
|
|
now = time(NULL);
|
|
|
|
return delta <= now ? now - delta : never;
|
2011-01-22 02:13:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
guint64
|
|
|
|
mu_str_size_parse_kmg (const char* str)
|
|
|
|
{
|
2011-05-20 20:31:08 +02:00
|
|
|
gint64 num;
|
2011-01-22 02:13:18 +01:00
|
|
|
char *end;
|
2011-01-06 15:21:09 +01:00
|
|
|
|
2011-01-22 02:13:18 +01:00
|
|
|
g_return_val_if_fail (str, G_MAXUINT64);
|
2011-01-06 15:21:09 +01:00
|
|
|
|
2011-01-22 02:13:18 +01:00
|
|
|
num = strtol (str, &end, 10);
|
2011-05-20 20:31:08 +02:00
|
|
|
if (num < 0)
|
2011-01-22 02:13:18 +01:00
|
|
|
return G_MAXUINT64;
|
2011-01-06 15:21:09 +01:00
|
|
|
|
2011-01-22 02:13:18 +01:00
|
|
|
if (!end || end[1] != '\0')
|
|
|
|
return G_MAXUINT64;
|
2011-01-06 15:21:09 +01:00
|
|
|
|
2011-01-22 02:13:18 +01:00
|
|
|
switch (tolower(end[0])) {
|
2011-05-20 20:31:08 +02:00
|
|
|
case 'b': return num; /* bytes */
|
2011-01-22 02:13:18 +01:00
|
|
|
case 'k': return num * 1000; /* kilobyte */
|
|
|
|
case 'm': return num * 1000 * 1000; /* megabyte */
|
2011-05-28 11:29:53 +02:00
|
|
|
/* case 'g': return num * 1000 * 1000 * 1000; /\* gigabyte *\/ */
|
2011-01-22 02:13:18 +01:00
|
|
|
default:
|
2011-01-06 15:21:09 +01:00
|
|
|
return G_MAXUINT64;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-06-15 22:44:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
char*
|
|
|
|
mu_str_from_list (const GSList *lst, char sepa)
|
|
|
|
{
|
|
|
|
const GSList *cur;
|
|
|
|
char *str;
|
|
|
|
|
|
|
|
g_return_val_if_fail (sepa, NULL);
|
|
|
|
|
|
|
|
for (cur = lst, str = NULL; cur; cur = g_slist_next(cur)) {
|
|
|
|
|
|
|
|
char *tmp;
|
|
|
|
char sep[2] = { '\0', '\0' };
|
|
|
|
sep[0] = cur->next ? sepa : '\0';
|
|
|
|
|
|
|
|
tmp = g_strdup_printf ("%s%s%s",
|
|
|
|
str ? str : "",
|
|
|
|
(gchar*)cur->data,
|
|
|
|
sep);
|
|
|
|
g_free (str);
|
|
|
|
str = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
GSList*
|
|
|
|
mu_str_to_list (const char *str, char sepa)
|
|
|
|
{
|
|
|
|
GSList *lst;
|
|
|
|
gchar **strs, **cur;
|
|
|
|
char sep[] = { '\0', '\0' };
|
|
|
|
|
|
|
|
g_return_val_if_fail (sepa, NULL);
|
|
|
|
|
|
|
|
if (!str)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
sep[0] = sepa;
|
|
|
|
strs = g_strsplit (str, sep, -1);
|
|
|
|
|
|
|
|
for (cur = strs, lst = NULL; cur && *cur; ++cur)
|
|
|
|
lst = g_slist_prepend (lst, g_strdup(*cur));
|
|
|
|
|
|
|
|
lst = g_slist_reverse (lst);
|
|
|
|
g_strfreev (strs);
|
|
|
|
|
|
|
|
return lst;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
mu_str_free_list (GSList *lst)
|
|
|
|
{
|
|
|
|
g_slist_foreach (lst, (GFunc)g_free, NULL);
|
|
|
|
g_slist_free (lst);
|
|
|
|
}
|
|
|
|
|
2011-06-18 17:43:09 +02:00
|
|
|
const gchar*
|
|
|
|
mu_str_subject_normalize (const gchar* str)
|
|
|
|
{
|
|
|
|
gchar *last_colon;
|
|
|
|
g_return_val_if_fail (str, NULL);
|
|
|
|
|
|
|
|
/* FIXME: improve this */
|
|
|
|
last_colon = g_strrstr (str, ":");
|
|
|
|
if (!last_colon)
|
|
|
|
return str;
|
|
|
|
else
|
|
|
|
return g_strchug (last_colon + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-15 22:44:52 +02:00
|
|
|
|
2011-05-22 09:27:12 +02:00
|
|
|
/*
|
|
|
|
* Xapian treats various characters such as '@', '-', ':' and '.'
|
|
|
|
* specially; function below is an ugly hack to make it DWIM in most
|
|
|
|
* cases...*/
|
2010-11-29 20:21:55 +01:00
|
|
|
char*
|
|
|
|
mu_str_ascii_xapian_escape_in_place (char *query)
|
|
|
|
{
|
|
|
|
gchar *cur;
|
|
|
|
gboolean replace_dot;
|
|
|
|
|
|
|
|
g_return_val_if_fail (query, NULL);
|
|
|
|
|
|
|
|
/* only replace the '.' if the string looks like an e-mail
|
|
|
|
* address or msg-id */
|
|
|
|
replace_dot = (g_strstr_len(query, -1, "@") != NULL);
|
|
|
|
|
|
|
|
for (cur = query; *cur; ++cur) {
|
2011-05-24 22:15:20 +02:00
|
|
|
|
2011-05-22 09:27:12 +02:00
|
|
|
*cur = tolower(*cur);
|
2011-05-22 12:45:10 +02:00
|
|
|
|
|
|
|
switch (*cur) {
|
|
|
|
case '@':
|
|
|
|
case '-':
|
|
|
|
*cur = '_'; break;
|
|
|
|
case '.': {
|
2011-05-21 15:08:47 +02:00
|
|
|
/* don't replace a final cur */
|
2011-05-24 22:15:20 +02:00
|
|
|
if (cur[1]== ' ' || cur[1]=='\t' || cur[1]== '.')
|
|
|
|
++cur;
|
|
|
|
else if (cur[1] == '\0')
|
|
|
|
break;
|
2010-11-29 20:21:55 +01:00
|
|
|
else
|
|
|
|
*cur = '_';
|
2011-05-22 12:45:10 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ':':
|
2010-11-29 20:21:55 +01:00
|
|
|
/* if there's a registered xapian prefix before the
|
|
|
|
* ':', don't touch it. Otherwise replace ':' with
|
2011-05-22 09:27:12 +02:00
|
|
|
* a space'... ugh yuck ugly...
|
|
|
|
*/
|
2010-11-29 20:21:55 +01:00
|
|
|
if (!is_xapian_prefix (query, cur))
|
|
|
|
*cur = '_';
|
2011-05-22 12:45:10 +02:00
|
|
|
break;
|
2011-05-22 09:27:12 +02:00
|
|
|
}
|
2010-11-29 20:21:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
2010-12-07 22:19:13 +01:00
|
|
|
char*
|
|
|
|
mu_str_ascii_xapian_escape (const char *query)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (query, NULL);
|
|
|
|
|
|
|
|
return mu_str_ascii_xapian_escape_in_place (g_strdup(query));
|
|
|
|
}
|
2010-12-11 12:52:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* note: this function is *not* re-entrant, it returns a static buffer */
|
|
|
|
const char*
|
|
|
|
mu_str_fullpath_s (const char* path, const char* name)
|
|
|
|
{
|
|
|
|
static char buf[PATH_MAX + 1];
|
|
|
|
|
|
|
|
g_return_val_if_fail (path, NULL);
|
|
|
|
|
|
|
|
snprintf (buf, sizeof(buf), "%s%c%s", path, G_DIR_SEPARATOR,
|
|
|
|
name ? name : "");
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
2011-01-06 12:55:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
char*
|
|
|
|
mu_str_escape_c_literal (const gchar* str)
|
|
|
|
{
|
|
|
|
const char* cur;
|
|
|
|
GString *tmp;
|
|
|
|
|
|
|
|
g_return_val_if_fail (str, NULL);
|
|
|
|
|
|
|
|
tmp = g_string_sized_new (2 * strlen(str));
|
|
|
|
for (cur = str; *cur; ++cur)
|
|
|
|
switch (*cur) {
|
2011-01-06 15:55:43 +01:00
|
|
|
case '\\': tmp = g_string_append (tmp, "\\\\"); break;
|
|
|
|
case '"': tmp = g_string_append (tmp, "\\\""); break;
|
|
|
|
default: tmp = g_string_append_c (tmp, *cur);
|
2011-01-06 12:55:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return g_string_free (tmp, FALSE);
|
|
|
|
}
|
2011-01-06 16:32:42 +01:00
|
|
|
|
2011-03-06 11:14:26 +01:00
|
|
|
|
|
|
|
gchar*
|
|
|
|
mu_str_guess_last_name (const char *name)
|
|
|
|
{
|
|
|
|
const gchar *lastsp;
|
|
|
|
|
|
|
|
if (!name)
|
|
|
|
return g_strdup ("");
|
|
|
|
|
|
|
|
lastsp = g_strrstr (name, " ");
|
|
|
|
|
|
|
|
return g_strdup (lastsp ? lastsp + 1 : "");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
gchar*
|
|
|
|
mu_str_guess_first_name (const char *name)
|
|
|
|
{
|
|
|
|
const gchar *lastsp;
|
|
|
|
|
|
|
|
if (!name)
|
|
|
|
return g_strdup ("");
|
|
|
|
|
|
|
|
lastsp = g_strrstr (name, " ");
|
|
|
|
|
|
|
|
if (lastsp)
|
|
|
|
return g_strndup (name, lastsp - name);
|
|
|
|
else
|
|
|
|
return g_strdup (name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gchar*
|
|
|
|
cleanup_str (const char* str)
|
|
|
|
{
|
|
|
|
gchar *s;
|
|
|
|
const gchar *cur;
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
if (mu_str_is_empty(str))
|
|
|
|
return g_strdup ("");
|
|
|
|
|
|
|
|
s = g_new0 (char, strlen(str) + 1);
|
|
|
|
|
|
|
|
for (cur = str, i = 0; *cur; ++cur) {
|
|
|
|
if (ispunct(*cur) || isspace(*cur))
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
s[i++] = *cur;
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
gchar*
|
|
|
|
mu_str_guess_nick (const char* name)
|
|
|
|
{
|
|
|
|
gchar *fname, *lname, *nick;
|
|
|
|
gchar initial[7];
|
|
|
|
|
|
|
|
fname = mu_str_guess_first_name (name);
|
|
|
|
lname = mu_str_guess_last_name (name);
|
|
|
|
|
|
|
|
/* if there's no last name, use first name as the nick */
|
|
|
|
if (mu_str_is_empty(fname) || mu_str_is_empty(lname)) {
|
|
|
|
g_free (lname);
|
|
|
|
nick = fname;
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset (initial, 0, sizeof(initial));
|
|
|
|
/* couldn't we get an initial for the last name? */
|
|
|
|
if (g_unichar_to_utf8 (g_utf8_get_char (lname), initial) == 0) {
|
|
|
|
g_free (lname);
|
|
|
|
nick = fname;
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
nick = g_strdup_printf ("%s%s", fname, initial);
|
|
|
|
g_free (fname);
|
2011-05-25 07:12:58 +02:00
|
|
|
g_free (lname);
|
2011-03-06 11:14:26 +01:00
|
|
|
|
|
|
|
leave:
|
|
|
|
{
|
|
|
|
gchar *tmp;
|
|
|
|
tmp = cleanup_str (nick);
|
|
|
|
g_free (nick);
|
|
|
|
nick = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nick;
|
|
|
|
}
|