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