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