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

OTR support
author Frank Zschockelt <mcabber_otr[at]freakysoft.de>
date Thu, 30 Aug 2007 00:52:51 +0200
parents 382ec54b584e
children 37b41ed9ed35
comparison
equal deleted inserted replaced
1298:af035a304bec 1299:3b338a5c01fc
30 #include "hooks.h" 30 #include "hooks.h"
31 #include "hbuf.h" 31 #include "hbuf.h"
32 #include "utils.h" 32 #include "utils.h"
33 #include "settings.h" 33 #include "settings.h"
34 #include "events.h" 34 #include "events.h"
35 #include "otr.h"
35 36
36 #define IMSTATUS_AWAY "away" 37 #define IMSTATUS_AWAY "away"
37 #define IMSTATUS_ONLINE "online" 38 #define IMSTATUS_ONLINE "online"
38 #define IMSTATUS_OFFLINE "offline" 39 #define IMSTATUS_OFFLINE "offline"
39 #define IMSTATUS_FREE4CHAT "free" 40 #define IMSTATUS_FREE4CHAT "free"
76 static void do_iline(char *arg); 77 static void do_iline(char *arg);
77 static void do_screen_refresh(char *arg); 78 static void do_screen_refresh(char *arg);
78 static void do_chat_disable(char *arg); 79 static void do_chat_disable(char *arg);
79 static void do_source(char *arg); 80 static void do_source(char *arg);
80 static void do_color(char *arg); 81 static void do_color(char *arg);
82 static void do_otr(char *arg);
83 static void do_otrpolicy(char *arg);
81 84
82 // Global variable for the commands list 85 // Global variable for the commands list
83 static GSList *Commands; 86 static GSList *Commands;
84 87
85 88
127 cmd_add("move", "Move the current buddy to another group", COMPL_GROUPNAME, 130 cmd_add("move", "Move the current buddy to another group", COMPL_GROUPNAME,
128 0, &do_move); 131 0, &do_move);
129 cmd_add("msay", "Send a multi-lines message to the selected buddy", 132 cmd_add("msay", "Send a multi-lines message to the selected buddy",
130 COMPL_MULTILINE, 0, &do_msay); 133 COMPL_MULTILINE, 0, &do_msay);
131 cmd_add("pgp", "Manage PGP settings", COMPL_PGP, COMPL_JID, &do_pgp); 134 cmd_add("pgp", "Manage PGP settings", COMPL_PGP, COMPL_JID, &do_pgp);
135 cmd_add("otr", "Manage OTR settings", COMPL_OTR, COMPL_JID, &do_otr);
136 cmd_add("otrpolicy", "Manage OTR policies", COMPL_JID, COMPL_OTRPOLICY, &do_otrpolicy);
132 cmd_add("quit", "Exit the software", 0, 0, NULL); 137 cmd_add("quit", "Exit the software", 0, 0, NULL);
133 cmd_add("rawxml", "Send a raw XML string", 0, 0, &do_rawxml); 138 cmd_add("rawxml", "Send a raw XML string", 0, 0, &do_rawxml);
134 cmd_add("rename", "Rename the current buddy", 0, 0, &do_rename); 139 cmd_add("rename", "Rename the current buddy", 0, 0, &do_rename);
135 cmd_add("request", "Send a Jabber IQ request", COMPL_REQUEST, COMPL_JID, 140 cmd_add("request", "Send a Jabber IQ request", COMPL_REQUEST, COMPL_JID,
136 &do_request); 141 &do_request);
252 compl_add_category_word(COMPL_PGP, "disable"); 257 compl_add_category_word(COMPL_PGP, "disable");
253 compl_add_category_word(COMPL_PGP, "enable"); 258 compl_add_category_word(COMPL_PGP, "enable");
254 compl_add_category_word(COMPL_PGP, "force"); 259 compl_add_category_word(COMPL_PGP, "force");
255 compl_add_category_word(COMPL_PGP, "info"); 260 compl_add_category_word(COMPL_PGP, "info");
256 compl_add_category_word(COMPL_PGP, "setkey"); 261 compl_add_category_word(COMPL_PGP, "setkey");
262
263 // OTR category
264 compl_add_category_word(COMPL_OTR, "start");
265 compl_add_category_word(COMPL_OTR, "stop");
266 compl_add_category_word(COMPL_OTR, "fingerprint");
267 compl_add_category_word(COMPL_OTR, "smpq");
268 compl_add_category_word(COMPL_OTR, "smpr");
269 compl_add_category_word(COMPL_OTR, "smpa");
270 compl_add_category_word(COMPL_OTR, "info");
271 compl_add_category_word(COMPL_OTR, "key");
272
273 // OTR Policy category
274 compl_add_category_word(COMPL_OTRPOLICY, "plain");
275 compl_add_category_word(COMPL_OTRPOLICY, "manual");
276 compl_add_category_word(COMPL_OTRPOLICY, "opportunistic");
277 compl_add_category_word(COMPL_OTRPOLICY, "always");
257 278
258 // Color category 279 // Color category
259 compl_add_category_word(COMPL_COLOR, "roster"); 280 compl_add_category_word(COMPL_COLOR, "roster");
260 compl_add_category_word(COMPL_COLOR, "muc"); 281 compl_add_category_word(COMPL_COLOR, "muc");
261 compl_add_category_word(COMPL_COLOR, "mucnick"); 282 compl_add_category_word(COMPL_COLOR, "mucnick");
2881 } 2902 }
2882 2903
2883 free_arg_lst(paramlst); 2904 free_arg_lst(paramlst);
2884 } 2905 }
2885 2906
2907 static void do_otr(char *arg)
2908 {
2909 #ifdef HAVE_LIBOTR
2910 char **paramlst;
2911 char *fjid, *subcmd, *keyid;
2912 enum {
2913 otr_none,
2914 otr_start,
2915 otr_stop,
2916 otr_fpr,
2917 otr_smpq,
2918 otr_smpr,
2919 otr_smpa,
2920 otr_k,
2921 otr_info
2922 } op = 0;
2923
2924 paramlst = split_arg(arg, 3, 0); // subcmd, jid, [key]
2925 subcmd = *paramlst;
2926 fjid = *(paramlst+1);
2927 keyid = *(paramlst+2);
2928
2929 if (!subcmd)
2930 fjid = NULL;
2931 if (!fjid)
2932 keyid = NULL;
2933
2934 if (subcmd) {
2935 if (!strcasecmp(subcmd, "start"))
2936 op = otr_start;
2937 else if (!strcasecmp(subcmd, "stop"))
2938 op = otr_stop;
2939 else if (!strcasecmp(subcmd, "fingerprint"))
2940 op = otr_fpr;
2941 else if (!strcasecmp(subcmd, "smpq"))
2942 op = otr_smpq;
2943 else if (!strcasecmp(subcmd, "smpr"))
2944 op = otr_smpr;
2945 else if (!strcasecmp(subcmd, "smpa"))
2946 op = otr_smpa;
2947 else if (!strcasecmp(subcmd, "key"))
2948 op = otr_k;
2949 else if (!strcasecmp(subcmd, "info"))
2950 op = otr_info;
2951 }
2952
2953 if (!op) {
2954 scr_LogPrint(LPRINT_NORMAL, "Unrecognized or missing parameter!");
2955 free_arg_lst(paramlst);
2956 return;
2957 }
2958
2959 if(op == otr_k)
2960 otr_key();
2961 else {
2962 // Allow special jid "" or "." (current buddy)
2963 if (fjid && (!*fjid || !strcmp(fjid, ".")))
2964 fjid = NULL;
2965
2966 if (fjid) {
2967 // The JID has been specified. Quick check...
2968 if (check_jid_syntax(fjid) || !strchr(fjid, '@')) {
2969 scr_LogPrint(LPRINT_NORMAL|LPRINT_NOTUTF8,
2970 "<%s> is not a valid Jabber ID.", fjid);
2971 fjid = NULL;
2972 } else {
2973 // Convert jid to lowercase and strip resource
2974 char *p;
2975 for (p = fjid; *p && *p != JID_RESOURCE_SEPARATOR; p++)
2976 *p = tolower(*p);
2977 if (*p == JID_RESOURCE_SEPARATOR)
2978 *p = '\0';
2979 }
2980 } else {
2981 gpointer bud = NULL;
2982 if (current_buddy)
2983 bud = BUDDATA(current_buddy);
2984 if (bud) {
2985 guint type = buddy_gettype(bud);
2986 if (type & ROSTER_TYPE_USER) // Is it a user?
2987 fjid = (char*)buddy_getjid(bud);
2988 else
2989 scr_LogPrint(LPRINT_NORMAL, "The selected item should be a user.");
2990 }
2991 }
2992
2993 if (fjid) { // fjid is actually a bare jid...
2994 switch (op) {
2995 case otr_start:
2996 otr_establish(fjid); break;
2997 case otr_stop:
2998 otr_disconnect(fjid); break;
2999 case otr_fpr:
3000 otr_fingerprint(fjid, keyid); break;
3001 case otr_smpq:
3002 otr_smp_query(fjid, keyid); break;
3003 case otr_smpr:
3004 otr_smp_respond(fjid, keyid); break;
3005 case otr_smpa:
3006 otr_smp_abort(fjid); break;
3007 case otr_info:
3008 otr_print_info(fjid); break;
3009 default:
3010 break;
3011 }
3012 } else
3013 scr_LogPrint(LPRINT_NORMAL, "Please specify a valid Jabber ID.");
3014 }
3015 free_arg_lst(paramlst);
3016
3017 #else
3018 scr_LogPrint(LPRINT_NORMAL, "Please recompile mcabber with libotr enabled.");
3019 #endif /* HAVE_LIBOTR */
3020 }
3021
3022 #ifdef HAVE_LIBOTR
3023 static char * string_for_otrpolicy(enum otr_policy p)
3024 {
3025 switch (p) {
3026 case plain: return "plain";
3027 case opportunistic: return "opportunistic";
3028 case manual: return "manual";
3029 case always: return "always";
3030 default: return "unknown";
3031 }
3032 }
3033
3034 static void dump_otrpolicy(char * k, char * v, void * nothing)
3035 {
3036 scr_LogPrint(LPRINT_NORMAL|LPRINT_NOTUTF8, "otrpolicy for %s: %s", k,
3037 string_for_otrpolicy(*(enum otr_policy*)v));
3038 }
3039 #endif
3040
3041 static void do_otrpolicy(char *arg)
3042 {
3043 #ifdef HAVE_LIBOTR
3044 char **paramlst;
3045 char *fjid, *policy;
3046 enum otr_policy p;
3047
3048 paramlst = split_arg(arg, 2, 0); // [jid|default] policy
3049 fjid = *paramlst;
3050 policy = *(paramlst+1);
3051
3052 if (!fjid && !policy) {
3053 scr_LogPrint(LPRINT_NORMAL, "default otrpolicy: %s",
3054 string_for_otrpolicy(settings_otr_getpolicy(NULL)));
3055 settings_foreach(SETTINGS_TYPE_OTR, &dump_otrpolicy, NULL);
3056 return;
3057 }
3058
3059 if (!policy) {
3060 scr_LogPrint(LPRINT_NORMAL, "Unrecognized or missing parameter!");
3061 free_arg_lst(paramlst);
3062 return;
3063 }
3064
3065 if (!strcasecmp(policy, "plain"))
3066 p = plain;
3067 else if (!strcasecmp(policy, "manual"))
3068 p = manual;
3069 else if (!strcasecmp(policy, "opportunistic"))
3070 p = opportunistic;
3071 else if (!strcasecmp(policy, "always"))
3072 p = always;
3073 else {
3074 /* Fail, we don't know _this_ policy*/
3075 }
3076
3077 if(!strcasecmp(fjid, "default")){
3078 /*set default policy*/
3079 settings_otr_setpolicy(NULL, p);
3080 return;
3081 }
3082 // Allow special jid "" or "." (current buddy)
3083 if (fjid && (!*fjid || !strcmp(fjid, ".")))
3084 fjid = NULL;
3085
3086 if (fjid) {
3087 // The JID has been specified. Quick check...
3088 if (check_jid_syntax(fjid) || !strchr(fjid, '@')) {
3089 scr_LogPrint(LPRINT_NORMAL|LPRINT_NOTUTF8,
3090 "<%s> is not a valid Jabber ID.", fjid);
3091 fjid = NULL;
3092 } else {
3093 // Convert jid to lowercase and strip resource
3094 char *p;
3095 for (p = fjid; *p && *p != JID_RESOURCE_SEPARATOR; p++)
3096 *p = tolower(*p);
3097 if (*p == JID_RESOURCE_SEPARATOR)
3098 *p = '\0';
3099 }
3100 } else {
3101 gpointer bud = NULL;
3102 if (current_buddy)
3103 bud = BUDDATA(current_buddy);
3104 if (bud) {
3105 guint type = buddy_gettype(bud);
3106 if (type & ROSTER_TYPE_USER) // Is it a user?
3107 fjid = (char*)buddy_getjid(bud);
3108 else
3109 scr_LogPrint(LPRINT_NORMAL, "The selected item should be a user.");
3110 }
3111 }
3112
3113 if (fjid)
3114 settings_otr_setpolicy(fjid, p);
3115 else
3116 scr_LogPrint(LPRINT_NORMAL, "Please specify a valid Jabber ID.");
3117
3118 free_arg_lst(paramlst);
3119 #else
3120 scr_LogPrint(LPRINT_NORMAL, "Please recompile mcabber with libotr enabled.");
3121 #endif /* HAVE_LIBOTR */
3122 }
3123
2886 /* !!! 3124 /* !!!
2887 After changing the /iline arguments names here, you must change ones 3125 After changing the /iline arguments names here, you must change ones
2888 in init_bindings(). 3126 in init_bindings().
2889 */ 3127 */
2890 static void do_iline(char *arg) 3128 static void do_iline(char *arg)