1 /* |
|
2 Copyright 2010 Mikael Berthe |
|
3 |
|
4 Module "extsayng" -- adds a /extsay command |
|
5 Spawns an external editor, using screen |
|
6 |
|
7 This module is free software: you can redistribute it and/or modify |
|
8 it under the terms of the GNU General Public License as published by |
|
9 the Free Software Foundation, either version 2 of the License, or |
|
10 (at your option) any later version. |
|
11 |
|
12 This program is distributed in the hope that it will be useful, |
|
13 but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15 GNU General Public License for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
19 */ |
|
20 |
|
21 #include <unistd.h> |
|
22 #include <stdlib.h> |
|
23 #include <string.h> |
|
24 #include <sys/wait.h> |
|
25 #include <glib/gstdio.h> |
|
26 |
|
27 #include <mcabber/modules.h> |
|
28 #include <mcabber/commands.h> |
|
29 #include <mcabber/settings.h> |
|
30 #include <mcabber/compl.h> |
|
31 #include <mcabber/utils.h> |
|
32 #include <mcabber/logprint.h> |
|
33 |
|
34 static void extsayng_init(void); |
|
35 static void extsayng_uninit(void); |
|
36 |
|
37 /* Module description */ |
|
38 module_info_t info_extsayng = { |
|
39 .branch = MCABBER_BRANCH, |
|
40 .api = MCABBER_API_VERSION, |
|
41 .version = "0.01", |
|
42 .description = "Use external editor to send a message", |
|
43 .requires = NULL, |
|
44 .init = extsayng_init, |
|
45 .uninit = extsayng_uninit, |
|
46 .next = NULL, |
|
47 }; |
|
48 |
|
49 |
|
50 // Run the external helper script with parameters |
|
51 static void screen_run_script(const gchar *args) |
|
52 { |
|
53 GError *err = NULL; |
|
54 gchar *argv[] = { "screen", "-r", "-X", "screen", NULL, |
|
55 NULL, NULL, NULL, NULL }; |
|
56 gchar strwinheight[32]; |
|
57 gchar *fpath; |
|
58 gboolean winsplit, ret; |
|
59 |
|
60 // screen -r -X screen $path/extsay.sh [jid [winsplit [height]]] |
|
61 fpath = (gchar*)settings_opt_get("extsay_script_path"); |
|
62 |
|
63 // Helper script path |
|
64 if (!fpath || !fpath[0]) { |
|
65 scr_log_print(LPRINT_NORMAL, "Please set option 'extsay_script_path'."); |
|
66 return; |
|
67 } |
|
68 fpath = expand_filename(fpath); |
|
69 argv[4] = fpath; |
|
70 |
|
71 // Helper script parameter #1 |
|
72 if (args && *args) |
|
73 argv[5] = (gchar*)args; |
|
74 else |
|
75 argv[5] = "."; |
|
76 |
|
77 // Update parameters for the helper script |
|
78 winsplit = settings_opt_get_int("extsay_split_win"); |
|
79 if (winsplit) { |
|
80 gint winheight = settings_opt_get_int("extsay_win_height"); |
|
81 argv[6] = "winsplit"; // Helper script parameter #2 |
|
82 if (winheight > 0 && winheight < 256) { |
|
83 snprintf(strwinheight, sizeof strwinheight, "%d", winheight); |
|
84 argv[7] = strwinheight; // Helper script parameter #3 |
|
85 } |
|
86 } |
|
87 |
|
88 ret = g_spawn_async(NULL, argv, NULL, |
|
89 G_SPAWN_SEARCH_PATH | |
|
90 G_SPAWN_STDOUT_TO_DEV_NULL|G_SPAWN_STDERR_TO_DEV_NULL, |
|
91 NULL, NULL, NULL, &err); |
|
92 |
|
93 if (!ret) |
|
94 scr_LogPrint(LPRINT_NORMAL, err->message); |
|
95 |
|
96 g_free(fpath); |
|
97 } |
|
98 |
|
99 static void do_extsayng(gchar *args) |
|
100 { |
|
101 gboolean expandfjid = FALSE; |
|
102 gchar *fjid; |
|
103 |
|
104 if (args && !strncmp(args, "." JID_RESOURCE_SEPARATORSTR, 2)) |
|
105 expandfjid = TRUE; |
|
106 |
|
107 if (!args || !*args || expandfjid || !g_strcmp0(args, ".")) { |
|
108 const gchar *res = args+2; |
|
109 gpointer bud; |
|
110 |
|
111 if (!current_buddy) { |
|
112 scr_LogPrint(LPRINT_NORMAL, "Please select a buddy."); |
|
113 return; |
|
114 } |
|
115 |
|
116 bud = BUDDATA(current_buddy); |
|
117 if (!(buddy_gettype(bud) & |
|
118 (ROSTER_TYPE_USER|ROSTER_TYPE_AGENT|ROSTER_TYPE_ROOM))) { |
|
119 scr_LogPrint(LPRINT_NORMAL, "This is not a user."); |
|
120 return; |
|
121 } |
|
122 |
|
123 args = (gchar*)buddy_getjid(bud); |
|
124 if (expandfjid && *res) { |
|
125 char *res_utf8 = to_utf8(res); |
|
126 fjid = g_strdup_printf("%s%c%s", args, JID_RESOURCE_SEPARATOR, res_utf8); |
|
127 g_free(res_utf8); |
|
128 } else { |
|
129 fjid = g_strdup(args); |
|
130 } |
|
131 } else { |
|
132 fjid = to_utf8(args); |
|
133 } |
|
134 |
|
135 if (check_jid_syntax(fjid)) |
|
136 scr_LogPrint(LPRINT_NORMAL, "Please specify a valid Jabber ID."); |
|
137 else |
|
138 screen_run_script(fjid); // Launch helper script with resulting JID |
|
139 |
|
140 g_free(fjid); |
|
141 } |
|
142 |
|
143 static void extsayng_init(void) |
|
144 { |
|
145 cmd_add("extsay", "Use external editor to write a message", |
|
146 COMPL_JID, 0, do_extsayng, NULL); |
|
147 } |
|
148 |
|
149 static void extsayng_uninit(void) |
|
150 { |
|
151 cmd_del("extsay"); |
|
152 } |
|
153 |
|
154 /* vim: set expandtab cindent cinoptions=>2\:2(0: For Vim users... */ |
|