comparison mcabber/src/settings.c @ 1299:3b338a5c01fc

OTR support
author Frank Zschockelt <mcabber_otr[at]freakysoft.de>
date Thu, 30 Aug 2007 00:52:51 +0200
parents c3107650d165
children 005df14df743
comparison
equal deleted inserted replaced
1298:af035a304bec 1299:3b338a5c01fc
25 25
26 #include "settings.h" 26 #include "settings.h"
27 #include "commands.h" 27 #include "commands.h"
28 #include "utils.h" 28 #include "utils.h"
29 #include "logprint.h" 29 #include "logprint.h"
30 30 #include "otr.h"
31 // Maximum line length 31
32 // Maximum line length
32 // (probably best to use the same value as INPUTLINE_LENGTH) 33 // (probably best to use the same value as INPUTLINE_LENGTH)
33 #define CONFLINE_LENGTH 1024 34 #define CONFLINE_LENGTH 1024
34 35
35 static GHashTable *option; 36 static GHashTable *option;
36 static GHashTable *alias; 37 static GHashTable *alias;
44 guint pgp_disabled; /* If TRUE, PGP is disabled for outgoing messages */ 45 guint pgp_disabled; /* If TRUE, PGP is disabled for outgoing messages */
45 guint pgp_force; /* If TRUE, PGP is used w/o negotiation */ 46 guint pgp_force; /* If TRUE, PGP is used w/o negotiation */
46 } T_pgpopt; 47 } T_pgpopt;
47 #endif 48 #endif
48 49
50 #ifdef HAVE_LIBOTR
51 static GHashTable *otrpolicy;
52 static enum otr_policy default_policy;
53 #endif
54
49 static inline GHashTable *get_hash(guint type) 55 static inline GHashTable *get_hash(guint type)
50 { 56 {
51 if (type == SETTINGS_TYPE_OPTION) return option; 57 if (type == SETTINGS_TYPE_OPTION) return option;
52 else if (type == SETTINGS_TYPE_ALIAS) return alias; 58 else if (type == SETTINGS_TYPE_ALIAS) return alias;
53 else if (type == SETTINGS_TYPE_BINDING) return binding; 59 else if (type == SETTINGS_TYPE_BINDING) return binding;
60 #ifdef HAVE_LIBOTR
61 else if (type == SETTINGS_TYPE_OTR) return otrpolicy;
62 #endif
54 return NULL; 63 return NULL;
55 } 64 }
56 65
57 /* -- */ 66 /* -- */
58 67
61 option = g_hash_table_new_full(&g_str_hash, &g_str_equal, &g_free, &g_free); 70 option = g_hash_table_new_full(&g_str_hash, &g_str_equal, &g_free, &g_free);
62 alias = g_hash_table_new_full(&g_str_hash, &g_str_equal, &g_free, &g_free); 71 alias = g_hash_table_new_full(&g_str_hash, &g_str_equal, &g_free, &g_free);
63 binding = g_hash_table_new_full(&g_str_hash, &g_str_equal, &g_free, &g_free); 72 binding = g_hash_table_new_full(&g_str_hash, &g_str_equal, &g_free, &g_free);
64 #ifdef HAVE_GPGME 73 #ifdef HAVE_GPGME
65 pgpopt = g_hash_table_new(&g_str_hash, &g_str_equal); 74 pgpopt = g_hash_table_new(&g_str_hash, &g_str_equal);
75 #endif
76 #ifdef HAVE_LIBOTR
77 otrpolicy = g_hash_table_new(&g_str_hash, &g_str_equal);
66 #endif 78 #endif
67 } 79 }
68 80
69 // cfg_read_file(filename, mainfile) 81 // cfg_read_file(filename, mainfile)
70 // Read and parse config file "filename". If filename is NULL, 82 // Read and parse config file "filename". If filename is NULL,
155 167
156 // Ignore empty lines and comments 168 // Ignore empty lines and comments
157 if ((*line == '\n') || (*line == '\0') || (*line == '#')) 169 if ((*line == '\n') || (*line == '\0') || (*line == '#'))
158 continue; 170 continue;
159 171
160 // We only allow assignments line, except for commands "pgp", "source" 172 // We only allow assignments line, except for commands "pgp", "source",
161 // and "color" 173 // "color" and "otrpolicy"
162 if ((strchr(line, '=') != NULL) || 174 if ((strchr(line, '=') != NULL) ||
163 startswith(line, "pgp ", FALSE) || startswith(line, "source ", FALSE) || 175 startswith(line, "pgp ", FALSE) || startswith(line, "source ", FALSE) ||
164 startswith(line, "color ", FALSE)) { 176 startswith(line, "color ", FALSE) ||
177 startswith(line, "otrpolicy", FALSE)) {
165 // Only accept the set, alias, bind, pgp and source commands 178 // Only accept the set, alias, bind, pgp and source commands
166 if (!startswith(line, "set ", FALSE) && 179 if (!startswith(line, "set ", FALSE) &&
167 !startswith(line, "bind ", FALSE) && 180 !startswith(line, "bind ", FALSE) &&
168 !startswith(line, "alias ", FALSE) && 181 !startswith(line, "alias ", FALSE) &&
169 !startswith(line, "pgp ", FALSE) && 182 !startswith(line, "pgp ", FALSE) &&
170 !startswith(line, "source ", FALSE) && 183 !startswith(line, "source ", FALSE) &&
184 !startswith(line, "otrpolicy ", FALSE) &&
171 !startswith(line, "color ", FALSE)) { 185 !startswith(line, "color ", FALSE)) {
172 scr_LogPrint(LPRINT_LOGNORM, 186 scr_LogPrint(LPRINT_LOGNORM,
173 "Error in configuration file (l. %d): bad command", ln); 187 "Error in configuration file (l. %d): bad command", ln);
174 err++; 188 err++;
175 continue; 189 continue;
499 return pgpdata->pgp_keyid; 513 return pgpdata->pgp_keyid;
500 #endif 514 #endif
501 return NULL; 515 return NULL;
502 } 516 }
503 517
518 /* otr settings */
519
520 #ifdef HAVE_LIBOTR
521 static void remove_default_policies(char * k, char * policy, void * defaultp)
522 {
523 if (*(enum otr_policy *)policy == *(enum otr_policy *)defaultp) {
524 g_free((enum otr_policy *) policy);
525 g_hash_table_remove(otrpolicy, k);
526 }
527 }
528 #endif
529
530 void settings_otr_setpolicy(const char *bjid, guint value)
531 {
532 #ifdef HAVE_LIBOTR
533 enum otr_policy *otrdata;
534
535 if (!bjid) {
536 default_policy = value;
537 /* refresh hash */
538 settings_foreach(SETTINGS_TYPE_OTR, &remove_default_policies, &value);
539 return;
540 }
541
542 otrdata = g_hash_table_lookup(otrpolicy, bjid);
543
544 if (value == default_policy) {
545 if (otrdata) {
546 g_free(otrdata);
547 g_hash_table_remove(otrpolicy, bjid);
548 }
549 } else if (otrdata) {
550 *otrdata = value;
551 } else {
552 otrdata = g_new(enum otr_policy, 1);
553 *otrdata = value;
554 g_hash_table_insert(otrpolicy, g_strdup(bjid), otrdata);
555 }
556 #endif
557 }
558
559 guint settings_otr_getpolicy(const char *bjid)
560 {
561 #ifdef HAVE_LIBOTR
562 enum otr_policy * otrdata;
563 if (!bjid)
564 return default_policy;
565
566 otrdata = g_hash_table_lookup(otrpolicy, bjid);
567 if (otrdata)
568 return *otrdata;
569 else
570 return default_policy;
571 #else
572 return 0;
573 #endif
574 }
575
504 guint get_max_history_blocks(void) 576 guint get_max_history_blocks(void)
505 { 577 {
506 int max_num_of_blocks = settings_opt_get_int("max_history_blocks"); 578 int max_num_of_blocks = settings_opt_get_int("max_history_blocks");
507 if (max_num_of_blocks < 0) 579 if (max_num_of_blocks < 0)
508 max_num_of_blocks = 0; 580 max_num_of_blocks = 0;