-
Notifications
You must be signed in to change notification settings - Fork 0
/
clipmed.c
402 lines (374 loc) · 9.57 KB
/
clipmed.c
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <[email protected]> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return. Joris Dedieu
* ----------------------------------------------------------------------------
*
* $Id$
*
*/
#include "clipme.h"
#include "config.h"
Display *display;
Window win, root;
volatile struct stack *clip_stack;
static pthread_mutex_t mutex;
struct config *config;
int stack_init() {
if((clip_stack = (struct stack *) malloc(sizeof (struct stack))) == NULL)
return EXIT_FAILURE;
else {
clip_stack->top = NULL;
clip_stack->size = 0;
}
return EXIT_SUCCESS;
}
int push(const char *s, unsigned long l) {
struct clip_entry *next;
pthread_mutex_lock(&mutex);
if ((next = (struct clip_entry *) malloc(sizeof (struct clip_entry))) == NULL){
pthread_mutex_unlock(&mutex);
return EXIT_FAILURE;
} else
bzero(next, sizeof(struct clip_entry));
next->len = l+1;
if ((next->entry = (char *) malloc((next->len)*sizeof(char))) == NULL){
pthread_mutex_unlock(&mutex);
return EXIT_FAILURE;
} else {
strncpy(next->entry, s,next->len);
next->entry[next->len-1]='\0';
}
/* If the same string is selected twice
* (maybe a bug in get_selection)
*/
if(clip_stack->top != NULL){
if(next->len == clip_stack->top->len){
if(strcmp(next->entry, clip_stack->top->entry) == 0){
/*Abort*/
free(next->entry);
free(next);
pthread_mutex_unlock(&mutex);
return -1;
}
}
}
/* Suppress the lastone if needed */
if(clip_stack->size == config->number) {
struct clip_entry *c = clip_stack->top;
struct clip_entry *prev;
while(c->next != NULL) {
c = c->next;
prev = c;
}
free(c->entry);
free(c);
prev->next = NULL;
clip_stack->size--;
}
next->next = clip_stack->top;
clip_stack->top = next;
clip_stack->size++;
pthread_mutex_unlock(&mutex);
return EXIT_SUCCESS;
}
struct clip_entry *get_one(int n) {
struct clip_entry *c;
int i;
pthread_mutex_lock(&mutex);
c = clip_stack->top;
for(i=0; i < n; i++) {
if(c->next == NULL){
pthread_mutex_unlock(&mutex);
return NULL;
}
c = c->next;
}
pthread_mutex_unlock(&mutex);
return c;
}
void ulisten(void) {
size_t len, clen;
socklen_t fd, cfd;
char *buffer, *cmd, *args = NULL;
struct sockaddr_un server, client;
struct clip_entry *c;
int i;
#ifdef WITH_TWITTER
CURL *curl;
CURLcode res;
char *login, *message;
#endif /* WITH_TWITTER */
if((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0){
perror("socket");
exit(EXIT_FAILURE);
}
bzero(&server, sizeof(struct sockaddr_un));
server.sun_family = AF_UNIX;
strcpy(server.sun_path, config->sockpath);
len = strlen(server.sun_path) + sizeof(server.sun_family);
if (bind(fd, (struct sockaddr *) &server, len) < 0) {
perror("bind");
exit(EXIT_FAILURE);
}
listen(fd, 5);
for(;;){
clen = sizeof(client);
if((cfd = accept(fd, (struct sockaddr *) &client, &clen)) < 0){
perror("accept");
exit(EXIT_FAILURE);
}
else {
int action=0;
/* get clip command */
if((buffer=netread(cfd)) == NULL){
close(cfd);
continue;
}
if((cmd = (char *) malloc(sizeof(char)*4)) == NULL){
perror("malloc");
continue;
}
(void) strncpy(cmd, buffer, (size_t) 3);
cmd[3]='\0';
if(strlen(buffer) > 3){
/* 4 cmd + ' ' */
if((args = (char *) malloc(sizeof(char) * (strlen(buffer) -4))) == NULL){
perror("malloc");
continue;
}
args = strndup(buffer + 4, strlen(buffer) -4);
}
free(buffer);
if( strcmp("get", cmd ) == 0 ) {
action = ACTION_GET;
} else if( strcmp("set",cmd) == 0 ) {
action = ACTION_SET;
} else if( strcmp("del",cmd) == 0 ) {
action = ACTION_DEL;
} else if( strcmp("siz",cmd) == 0 ) {
action = ACTION_SIZE;
#ifdef WITH_TWITTER
} else if(strcmp("twt", cmd) == 0 ) {
action = ACTION_TWIT;
#endif /* WITH_TWITTER */
}
free(cmd);
switch(action) {
case ACTION_GET:
/* Client wants all off the list */
if(args == NULL) {
for(i=0; i < clip_stack->size; i++){
if((c=get_one(i)) != NULL)
netprintf(cfd, c->entry);
else
break;
}
}
/* Client want a specific clip */
else {
/* check if args is a number */
short valid_arg = 1;
for(i = 0; i < strlen(args); i++){
if(!isdigit(args[i]) ){
netprintf(cfd,"Protocol error");
valid_arg = 0;
}
}
if(!valid_arg)
break;
if( atoi(args) <= clip_stack->size && (c=get_one(atoi(args))) != NULL)
netprintf(cfd,c->entry);
else{
netprintf(cfd, "Out of range clip");
break;
}
}
break;
case ACTION_DEL:
stack_clear();
break;
case ACTION_SET:
stack_add(args);
break;
case ACTION_SIZE:
netprintf(cfd,"%d",clip_stack->size);
break;
#ifdef WITH_TWITTER
case ACTION_TWIT:
if(config->user == NULL || config->pass == NULL){
fprintf(stderr, "-u and -p options must be set for twitter use\n");
break;
}
curl = curl_easy_init();
if((login = (char *) malloc(sizeof(char) *(strlen(config->user) + strlen(config->pass) + 2))) == NULL){
perror("malloc");
break;
}
(void) sprintf(login,"%s:%s", config->user,config->pass);
curl_easy_setopt(curl, CURLOPT_URL, TWIT_URL);
curl_easy_setopt(curl, CURLOPT_USERPWD, login);
curl_easy_setopt(curl, CURLOPT_POST, 1);
for(i = 0; i < strlen(args); i++){
if(!isdigit(args[i])){
netprintf(cfd,"Protocol error");
}
}
if((c=get_one(atoi(args))) == NULL)
netprintf(cfd, "Out of range clip");
else {
if((message = (char *) malloc(sizeof(char) * (10 + strlen(c->entry)))) == NULL){
perror("malloc");
break;
}
(void) sprintf(message, "status=\"%s\"", c->entry);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, message);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if(res == 0)
netprintf(cfd, "Entry : \"%s\" was posted", c->entry);
else
netprintf(cfd, "Error(%d) while posting \"%s\"", res, c->entry);
}
break;
#endif
default:
netprintf(cfd,"Protocol error");
}
close(cfd);
args = NULL;
}
}
}
/* Add clip to stack */
void stack_add(const char * to_add) {
push(to_add, strlen(to_add));
}
int stack_clear(void) {
pthread_mutex_lock(&mutex);
int res = _stack_clear();
pthread_mutex_unlock(&mutex);
return res;
}
int _stack_clear(void) {
struct clip_entry *supp;
if(clip_stack == NULL ) {
return EXIT_SUCCESS;
}
if(clip_stack->size == 0) {
return EXIT_SUCCESS;
}else {
if( clip_stack->top != NULL ) {
supp = clip_stack->top;
clip_stack->top = supp->next;
clip_stack->size--;
if(supp->entry) free(supp->entry);
if(supp) free(supp);
return _stack_clear();
}else{
return EXIT_SUCCESS;
}
}
}
void stack_clear_sig(int signum) {
stack_clear();
}
/* Directly from xclip
* Clean up needed
*/
void get_selection(void) {
unsigned char *sel_buf; /* buffer for selection data */
unsigned long sel_len = 0; /* length of sel_buf */
XEvent evt; /* X Event Structures */
unsigned int context = XCLIB_XCOUT_NONE;
Atom sseln = XA_PRIMARY;
Atom target = XA_UTF8_STRING(display);
xcout(display, win, evt, sseln, target, &sel_buf, &sel_len, &context);
if(context != XCLIB_XCOUT_NONE){
XNextEvent(display, &evt);
xcout(display, win, evt, sseln, target, &sel_buf, &sel_len, &context);
}
if(sel_len) {
if(push((char *)sel_buf, sel_len) > 0)
(void) fprintf(stderr, "push problem\n");
if (sseln == XA_STRING)
XFree(sel_buf);
else
if(sel_buf) free(sel_buf);
}
}
int xlaunch(void) {
int dummy, event_base, ver_major, ver_minor;
XEvent event;
XFixesSelectionNotifyEvent *sevent;
if ((display = XOpenDisplay(NULL)) == NULL) {
(void) fprintf(stderr, "Can't open Display\n");
return EXIT_FAILURE;
}
root = DefaultRootWindow(display);
if( !XFixesQueryExtension(display, &event_base, &dummy )){
(void) fprintf(stderr, "Cannot load XFixes extension\n");
XCloseDisplay(display);
return EXIT_FAILURE;
}
if (!XFixesQueryVersion(display, &ver_major, &ver_minor)) {
(void) fprintf(stderr, "XFixes version not queriable.\n");
XCloseDisplay(display);
return EXIT_FAILURE;
}
win = XCreateSimpleWindow(display, root, 0, 0, 1, 1, 0, 0, 0);
XFixesSelectSelectionInput(display, win, XA_PRIMARY, XFixesSetSelectionOwnerNotifyMask);
for(;;){
XNextEvent(display, &event);
if (event.type == XFixesSelectionNotify + event_base)
{
sevent = (XFixesSelectionNotifyEvent *) & event;
if (sevent->subtype == XFixesSetSelectionOwnerNotify)
{
get_selection();
}
}
}
return EXIT_SUCCESS;
}
void clean_exit(int signum) {
stack_clear();
if(clip_stack) free((void *)clip_stack);
if(config->sockpath != 0) {
unlink(config->sockpath);
}
free_config(config);
XDestroyWindow(display, win);
if(display) XCloseDisplay(display);
exit(0);
}
void usage(void) {
server_usage();
}
int main(int argc, char **argv) {
pthread_t server;
config = parse_args(argc, argv);
stack_init();
/* daemon */
if(config->daemon > 0){
pid_t pid = fork();
if(pid > 0){
printf("%d\n", pid+1);
exit(0);
}
daemon(0,0);
}
signal(SIGINT, clean_exit);
signal(SIGTERM, clean_exit);
signal(SIGHUP, stack_clear_sig);
pthread_mutex_init(&mutex, NULL);
pthread_create(&server, NULL, (void *) ulisten, NULL);
if(xlaunch() > 0) {
clean_exit(0);
return EXIT_FAILURE;
}
clean_exit(0);
return EXIT_SUCCESS;
}