Fix parsing of cert filename, prevent the Tools / SSL/TLS Certificates dialog from...
[claws.git] / src / common / utils.h
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2016 Hiroyuki Yamamoto and the Claws Mail team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * The code of the g_utf8_substring function below is owned by
19  * Matthias Clasen <matthiasc@src.gnome.org>/<mclasen@redhat.com>
20  * and is got from GLIB 2.30
21  */
22
23 #ifndef __UTILS_H__
24 #define __UTILS_H__
25
26 #ifdef HAVE_CONFIG_H
27 #include "claws-features.h"
28 #endif
29
30 #ifdef HAVE_BACKTRACE
31 #include <execinfo.h>
32 #endif
33
34 #include <glib.h>
35 #include <glib-object.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40 #include <sys/types.h>
41 #include <dirent.h>
42 #include <time.h>
43 #if HAVE_ALLOCA_H
44 #  include <alloca.h>
45 #endif
46 #if HAVE_WCHAR_H
47 #  include <wchar.h>
48 #endif
49
50 /* The Hurd doesn't have these limits */
51 #ifndef PATH_MAX
52   #define PATH_MAX 4196
53 #endif
54 #ifndef HOST_NAME_MAX
55   #define HOST_NAME_MAX 256
56 #endif
57
58 #ifdef G_OS_WIN32
59
60 #define fsync _commit
61
62 #define pipe(phandles)  _pipe (phandles, 4096, _O_BINARY)
63 #endif
64 /* Wrappers for C library function that take pathname arguments. */
65 #  include <glib/gstdio.h>
66
67 /* why is this sometimes undefined !? */
68 #ifndef G_MAXOFFSET
69 typedef gint64 goffset;
70 #define G_MINOFFSET     G_MININT64
71 #define G_MAXOFFSET     G_MAXINT64
72 #endif
73
74 #ifndef BIG_ENDIAN_HOST
75   #if (G_BYTE_ORDER == G_BIG_ENDIAN)
76     #define BIG_ENDIAN_HOST 1
77   #endif
78 #endif
79
80 #define CHDIR_RETURN_IF_FAIL(dir) \
81 { \
82         if (change_dir(dir) < 0) return; \
83 }
84
85 #define CHDIR_RETURN_VAL_IF_FAIL(dir, val) \
86 { \
87         if (change_dir(dir) < 0) return val; \
88 }
89
90 #define CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(dir, val, code) \
91 { \
92         if (change_dir(dir) < 0) { \
93                 code \
94                 return val; \
95         } \
96 }
97
98 #define Xalloca(ptr, size, iffail) \
99 { \
100         if ((ptr = alloca(size)) == NULL) { \
101                 g_warning("can't allocate memory"); \
102                 iffail; \
103         } \
104 }
105
106 #define Xstrdup_a(ptr, str, iffail) \
107 { \
108         gchar *__tmp; \
109  \
110         if ((__tmp = alloca(strlen(str) + 1)) == NULL) { \
111                 g_warning("can't allocate memory"); \
112                 iffail; \
113         } else \
114                 strcpy(__tmp, str); \
115  \
116         ptr = __tmp; \
117 }
118
119 #define Xstrndup_a(ptr, str, len, iffail) \
120 { \
121         gchar *__tmp; \
122  \
123         if ((__tmp = alloca(len + 1)) == NULL) { \
124                 g_warning("can't allocate memory"); \
125                 iffail; \
126         } else { \
127                 memcpy(__tmp, str, len); \
128                 __tmp[len] = '\0'; \
129         } \
130  \
131         ptr = __tmp; \
132 }
133
134 #define Xstrcat_a(ptr, str1, str2, iffail) \
135 { \
136         gchar *__tmp; \
137         gint len1, len2; \
138  \
139         len1 = strlen(str1); \
140         len2 = strlen(str2); \
141         if ((__tmp = alloca(len1 + len2 + 1)) == NULL) { \
142                 g_warning("can't allocate memory"); \
143                 iffail; \
144         } else { \
145                 memcpy(__tmp, str1, len1); \
146                 memcpy(__tmp + len1, str2, len2 + 1); \
147         } \
148  \
149         ptr = __tmp; \
150 }
151
152 #define AUTORELEASE_STR(str, iffail) \
153 { \
154         gchar *__str; \
155         Xstrdup_a(__str, str, iffail); \
156         g_free(str); \
157         str = __str; \
158 }
159
160 #define FILE_OP_ERROR(file, func) \
161 { \
162         g_printerr("%s: ", file); \
163         fflush(stderr); \
164         perror(func); \
165 }
166
167 #define IS_ASCII(c) (((guchar) c) <= 0177 ? 1 : 0)
168
169 /* from NetworkManager */
170 #if (defined(HAVE_BACKTRACE) && !defined(__FreeBSD__))
171 #define print_backtrace()                                               \
172 G_STMT_START                                                            \
173 {                                                                       \
174         void *_call_stack[512];                                         \
175         int  _call_stack_size;                                          \
176         char **_symbols;                                                \
177         _call_stack_size = backtrace (_call_stack,                      \
178                                       G_N_ELEMENTS (_call_stack));      \
179         _symbols = backtrace_symbols (_call_stack, _call_stack_size);   \
180         if (_symbols != NULL)                                           \
181         {                                                               \
182                 int _i;                                                 \
183                 _i = 0;                                                 \
184                 g_print ("traceback:\n");                               \
185                 while (_i < _call_stack_size)                           \
186                 {                                                       \
187                         g_print ("%d:\t%s\n", _i, _symbols[_i]);        \
188                         _i++;                                           \
189                 }                                                       \
190                 free (_symbols);                                        \
191         }                                                               \
192 }                                                                       \
193 G_STMT_END
194 #else
195 #define print_backtrace()                                               \
196 G_STMT_START                                                            \
197 {                                                                       \
198 }                                                                       \
199 G_STMT_END
200 #endif
201
202
203 #define cm_return_val_if_fail(expr,val) G_STMT_START {                  \
204         if (!(expr)) {                                                  \
205                 g_print("%s:%d Condition %s failed\n", __FILE__, __LINE__, #expr);\
206                 print_backtrace();                                      \
207                 g_print("\n");                                          \
208                 return val;                                             \
209         }                                                               \
210 } G_STMT_END
211
212 #define cm_return_if_fail(expr) G_STMT_START {                          \
213         if (!(expr)) {                                                  \
214                 g_print("%s:%d Condition %s failed\n", __FILE__, __LINE__, #expr);\
215                 print_backtrace();                                      \
216                 g_print("\n");                                          \
217                 return;                                                 \
218         }                                                               \
219 } G_STMT_END
220
221 #ifndef MIN
222         #define MIN(a, b) ((a) < (b) ? (a) : (b))
223 #endif
224 #ifndef MAX
225         #define MAX(a, b) ((a) > (b) ? (a) : (b))
226 #endif
227
228 #ifdef __cplusplus
229 extern "C" {
230 #endif
231
232 typedef gpointer (*GNodeMapFunc)        (gpointer nodedata, gpointer data);
233
234 /* debug functions */
235 void debug_set_mode             (gboolean mode);
236 gboolean debug_get_mode         (void);
237
238 #ifndef __CYGWIN__
239 #define debug_print \
240         debug_print_real("%s:%d:", debug_srcname(__FILE__), __LINE__), \
241         debug_print_real
242 #else
243   /* FIXME: cygwin: why debug_srcname couldn't be resolved in library? */
244 #define debug_print \
245         debug_print_real("%s:%d:", __FILE__, __LINE__), \
246         debug_print_real
247 #endif
248
249 /* for macro expansion */
250 #define Str(x)  #x
251 #define Xstr(x) Str(x)
252
253 /* List utilities. */
254
255 GSList *slist_copy_deep         (GSList         *list,
256                                  GCopyFunc       func);
257
258 /* String utilities.  */
259
260 void list_free_strings_full             (GList          *list);
261 void slist_free_strings_full    (GSList         *list);
262
263 void hash_free_strings          (GHashTable     *table);
264
265 gint str_case_equal             (gconstpointer   v,
266                                  gconstpointer   v2);
267 guint str_case_hash             (gconstpointer   key);
268
269 /* number-string conversion */
270 gint to_number                  (const gchar *nstr);
271 gchar *itos_buf                 (gchar       *nstr,
272                                  gint         n);
273 gchar *itos                     (gint         n);
274 gchar *to_human_readable        (goffset      size);
275
276 /* alternative string functions */
277 gint path_cmp           (const gchar    *s1,
278                          const gchar    *s2);
279 gchar *strretchomp      (gchar          *str);
280 gchar *strtailchomp     (gchar          *str,
281                          gchar           tail_char);
282 gchar *strcrchomp       (gchar          *str);
283 gchar *strcasestr       (const gchar    *haystack,
284                          const gchar    *needle);
285 gchar *strncasestr      (const gchar    *haystack,
286                          gint            haystack_len,
287                          const gchar    *needle);
288 gpointer my_memmem      (gconstpointer   haystack,
289                          size_t          haystacklen,
290                          gconstpointer   needle,
291                          size_t          needlelen);
292 gchar *strncpy2         (gchar          *dest,
293                          const gchar    *src,
294                          size_t          n);
295
296 gboolean is_next_nonascii       (const gchar *s);
297 gint get_next_word_len          (const gchar *s);
298
299 /* functions for string parsing */
300 gint subject_compare                    (const gchar    *s1,
301                                          const gchar    *s2);
302 gint subject_compare_for_sort           (const gchar    *s1,
303                                          const gchar    *s2);
304 void trim_subject                       (gchar          *str);
305 void eliminate_parenthesis              (gchar          *str,
306                                          gchar           op,
307                                          gchar           cl);
308 void extract_parenthesis                (gchar          *str,
309                                          gchar           op,
310                                          gchar           cl);
311
312 void extract_quote                      (gchar          *str,
313                                          gchar           quote_chr);
314 gchar *escape_internal_quotes           (gchar          *str,
315                                          gchar           quote_chr);
316 void eliminate_address_comment          (gchar          *str);
317 gchar *strchr_with_skip_quote           (const gchar    *str,
318                                          gint            quote_chr,
319                                          gint            c);
320 void extract_address                    (gchar          *str);
321 void extract_list_id_str                (gchar          *str);
322
323 GSList *address_list_append             (GSList         *addr_list,
324                                          const gchar    *str);
325 GSList *address_list_append_with_comments(GSList        *addr_list,
326                                          const gchar    *str);
327 GSList *references_list_prepend         (GSList         *msgid_list,
328                                          const gchar    *str);
329 GSList *references_list_append          (GSList         *msgid_list,
330                                          const gchar    *str);
331 GSList *newsgroup_list_append           (GSList         *group_list,
332                                          const gchar    *str);
333
334 GList *add_history                      (GList          *list,
335                                          const gchar    *str);
336
337 void remove_return                      (gchar          *str);
338 void remove_space                       (gchar          *str);
339 void unfold_line                        (gchar          *str);
340 void subst_char                         (gchar          *str,
341                                          gchar           orig,
342                                          gchar           subst);
343 void subst_chars                        (gchar          *str,   
344                                          gchar          *orig, 
345                                          gchar          subst);
346 void subst_for_filename                 (gchar          *str);
347 void subst_for_shellsafe_filename       (gchar          *str);
348 gboolean is_ascii_str                   (const gchar    *str);
349 gint get_quote_level                    (const gchar    *str,
350                                          const gchar    *quote_chars);
351 gint check_line_length                  (const gchar    *str,
352                                          gint            max_chars,
353                                          gint           *line);
354
355 gchar **strsplit_with_quote             (const gchar    *str,
356                                          const gchar    *delim,
357                                          gint            max_tokens);
358
359 gchar *get_abbrev_newsgroup_name        (const gchar    *group,
360                                          gint            len);
361 gchar *trim_string                      (const gchar    *str,
362                                          gint            len);
363
364 GList *uri_list_extract_filenames       (const gchar    *uri_list);
365 gboolean is_uri_string                  (const gchar    *str);
366 gchar *get_uri_path                     (const gchar    *uri);
367 gint get_uri_len                        (const gchar    *str);
368 void decode_uri                         (gchar          *decoded_uri,
369                                          const gchar    *encoded_uri);
370 void decode_uri_with_plus               (gchar          *decoded_uri, 
371                                          const gchar    *encoded_uri, 
372                                          gboolean        with_plus);
373 gint scan_mailto_url                    (const gchar    *mailto,
374                                          gchar         **from,
375                                          gchar         **to,
376                                          gchar         **cc,
377                                          gchar         **bcc,
378                                          gchar         **subject,
379                                          gchar         **body,
380                                          gchar         ***attach,
381                                          gchar         **inreplyto);
382
383 /* return static strings */
384 const gchar *get_home_dir               (void);
385 const gchar *get_rc_dir                 (void);
386 void  set_rc_dir                        (const gchar *dir);
387 gboolean rc_dir_is_alt                  (void);
388 const gchar *get_mail_base_dir          (void);
389 const gchar *get_news_cache_dir         (void);
390 const gchar *get_imap_cache_dir         (void);
391 const gchar *get_mime_tmp_dir           (void);
392 const gchar *get_template_dir           (void);
393 const gchar *get_plugin_dir             (void);
394 const gchar *get_tmp_dir                (void);
395 const gchar *get_locale_dir             (void);
396 gchar *get_tmp_file                     (void);
397 const gchar *get_domain_name            (void);
398 const gchar *get_desktop_file(void);
399 #ifdef G_OS_WIN32
400 const gchar *w32_get_themes_dir    (void);
401 const gchar *w32_get_cert_file          (void);
402 #endif
403 /* file / directory handling */
404 off_t get_file_size             (const gchar    *file);
405 time_t get_file_mtime           (const gchar *file);
406
407 gboolean file_exist             (const gchar    *file,
408                                  gboolean        allow_fifo);
409 gboolean is_relative_filename   (const gchar *file);
410 gboolean is_dir_exist           (const gchar    *dir);
411 gboolean is_file_entry_exist    (const gchar    *file);
412 gboolean dirent_is_regular_file (struct dirent  *d);
413
414 #define is_file_exist(file)             file_exist(file, FALSE)
415 #define is_file_or_fifo_exist(file)     file_exist(file, TRUE)
416
417 gint change_dir                 (const gchar    *dir);
418 gint make_dir                   (const gchar    *dir);
419 gint make_dir_hier              (const gchar    *dir);
420 gint remove_all_files           (const gchar    *dir);
421 gint remove_numbered_files      (const gchar    *dir,
422                                  guint           first,
423                                  guint           last);
424 gint remove_numbered_files_not_in_list(const gchar *dir,
425                                        GSList *numberlist);
426 gint remove_all_numbered_files  (const gchar    *dir);
427 gint remove_dir_recursive       (const gchar    *dir);
428 gchar *canonicalize_str         (const gchar    *str);
429 gchar *normalize_newlines       (const gchar    *str);
430
431 gchar *get_outgoing_rfc2822_str (FILE           *fp);
432
433 char *fgets_crlf(char *buf, int size, FILE *stream);
434
435 /* process execution */
436 gint execute_command_line       (const gchar    *cmdline,
437                                  gboolean        async,
438                                  const gchar    *working_directory);
439 gchar *get_command_output       (const gchar    *cmdline);
440
441 /* open URI with external browser */
442 gint open_uri(const gchar *uri, const gchar *cmdline);
443 /* open file with text editor */
444 gint open_txt_editor(const gchar *filepath, const gchar *cmdline);
445
446 /* time functions */
447 time_t remote_tzoffset_sec      (const gchar    *zone);
448 time_t tzoffset_sec             (time_t         *now);
449 gchar *tzoffset                 (time_t         *now);
450 void get_rfc822_date            (gchar          *buf,
451                                  gint            len);
452 void get_rfc822_date_hide_tz    (gchar          *buf,
453                                  gint            len);
454
455 size_t fast_strftime            (gchar                  *buf, 
456                                  gint                    buflen, 
457                                  const gchar            *format, 
458                                  struct tm              *lt);
459
460 /* debugging */
461 void debug_print_real   (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
462 const char * debug_srcname (const char *file);
463
464 /* subject threading */
465 void * subject_table_lookup(GHashTable *subject_table, gchar * subject);
466 void subject_table_insert(GHashTable *subject_table, gchar * subject,
467                           void * data);
468 void subject_table_remove(GHashTable *subject_table, gchar * subject);
469 void utils_free_regex(void);
470 gint subject_get_prefix_length (const gchar *subject);
471
472 /* quoting recognition */
473 const gchar * line_has_quote_char       (const gchar *str,
474                                          const gchar *quote_chars);
475
476 gint g_int_compare      (gconstpointer a, gconstpointer b);
477
478 gchar *generate_mime_boundary   (const gchar *prefix);
479
480 gint quote_cmd_argument(gchar * result, guint size,
481                         const gchar * path);
482 GNode *g_node_map(GNode *node, GNodeMapFunc func, gpointer data);
483
484 gboolean get_hex_value(guchar *out, gchar c1, gchar c2);
485 void get_hex_str(gchar *out, guchar ch);
486
487 /* auto pointer for containers that support GType system */
488
489 #define G_TYPE_AUTO_POINTER     g_auto_pointer_register()
490 typedef struct AutoPointer      GAuto;
491 GType g_auto_pointer_register           (void);
492 GAuto *g_auto_pointer_new               (gpointer pointer);
493 GAuto *g_auto_pointer_new_with_free     (gpointer p, 
494                                          GFreeFunc free);
495 gpointer g_auto_pointer_get_ptr         (GAuto *auto_ptr);
496 GAuto *g_auto_pointer_copy              (GAuto *auto_ptr);
497 void g_auto_pointer_free                (GAuto *auto_ptr);
498 void replace_returns                    (gchar *str);
499 gboolean get_uri_part   (const gchar *start,
500                          const gchar *scanpos,
501                          const gchar **bp,
502                          const gchar **ep,
503                          gboolean hdr);
504 gchar *make_uri_string  (const gchar *bp,
505                          const gchar *ep);
506 gboolean get_email_part (const gchar *start, 
507                          const gchar *scanpos,
508                          const gchar **bp, 
509                          const gchar **ep,
510                          gboolean hdr);
511 gchar *make_email_string(const gchar *bp,
512                          const gchar *ep);
513 gchar *make_http_string (const gchar *bp,
514                          const gchar *ep);
515
516 gchar *mailcap_get_command_for_type(const gchar *type, 
517                                     const gchar *file_to_open);
518 void mailcap_update_default        (const gchar *type,
519                                     const gchar *command);
520
521 gboolean file_is_email(const gchar *filename);
522 gboolean sc_g_list_bigger(GList *list, gint max);
523 gboolean sc_g_slist_bigger(GSList *list, gint max);
524
525 GMutex *cm_mutex_new(void);
526 void cm_mutex_free(GMutex *mutex);
527
528 int cm_canonicalize_filename(const gchar *filename, gchar **canonical_name);
529
530 guchar *g_base64_decode_zero(const gchar *text, gsize *out_len);
531
532 #if !GLIB_CHECK_VERSION(2, 30, 0)
533 gchar   *g_utf8_substring         (const gchar *p,
534                                    glong        start_pos,
535                                    glong        end_pos) G_GNUC_MALLOC;
536 #endif
537
538 gboolean get_random_bytes(void *buf, size_t count);
539
540 #ifdef __cplusplus
541 }
542 #endif
543
544 gboolean get_serverportfp_from_filename(const gchar *str, gchar **server, gchar **port, gchar **fp);
545
546 #endif /* __UTILS_H__ */