-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.c
More file actions
80 lines (63 loc) · 1.56 KB
/
scripts.c
File metadata and controls
80 lines (63 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <string.h>
#include <fcntl.h>
#include "scripts.h"
typedef struct {
FILE *pipe;
CB *callback;
PurpleConnection *gc;
PurpleBuddy *buddy;
const char *cmd;
} job_t;
int exec_process_cb(job_t *job)
{
char buffer[1024];
FILE *pipe;
buffer[0] = '\0';
pipe = job->pipe;
if(pipe != NULL && !feof(pipe)) {
fgets(buffer, sizeof(buffer), pipe);
if(buffer[0] != '\0') {
if(job->callback) {
job->callback(buffer, job->gc, job->buddy);
}
/* call this function again later */
return TRUE;
}
}
pclose(pipe);
g_free(job);
/* Stop this job */
return FALSE;
}
int exec_process(const char *cmd, const char *cmd_arg, CB *callback, PurpleConnection *gc, PurpleBuddy *buddy)
{
FILE *pipe;
char cmdline[1024];
if(cmd == NULL || strlen(cmd) == 0) {
return 1;
}
if(snprintf(cmdline, sizeof(cmdline), cmd, cmd_arg) >= sizeof(cmdline)) {
fprintf(stderr, "Plainprpl: Command for %s got to big.\n", cmd);
return 1;
}
pipe = popen(cmdline, "r");
if(pipe == NULL) {
fprintf(stderr,"Plainprpl: Can't execute %s\n", cmdline);
return 1;
}
#ifndef __WIN32__
int fflags = fcntl(fileno(pipe), F_GETFL, 0);
fcntl(fileno(pipe), F_SETFL, fflags | O_NONBLOCK);
#else
//TODO implement FILE_FLAG_OVERLAPPED & FILE_FLAG_NO_BUFFERING for windows
#endif
job_t *job = (job_t *) malloc(sizeof(job_t));
job->pipe = pipe;
job->callback = callback;
job->gc = gc;
job->buddy = buddy;
job->cmd = strdup(cmdline);
/* Call every 250 msecs until the callback returns FALSE */
purple_timeout_add(250, (GSourceFunc) exec_process_cb, (gpointer) job);
return 0;
}