cancel

free software for cancelling people and organizations
git clone https://git.parazyd.org/cancel
Log | Files | Refs | README | LICENSE

cancel.c (3481B)


      1 /* 
      2  * gcc -o cancel $(pkg-config --cflags --libs gtk+-2.0) cancel.c
      3  *
      4  * This program is free software: you can redistribute it and/or modify
      5  * it under the terms of the GNU General Public License version 3 as
      6  * published by the Free Software Foundation.
      7  *
      8  * This program is distributed in the hope that it will be useful,
      9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     11  * GNU General Public License for more details.
     12  *
     13  * You should have received a copy of the GNU General Public License
     14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
     15  */
     16 #include <netdb.h>
     17 #include <string.h>
     18 #include <sys/socket.h>
     19 
     20 #include <gtk/gtk.h>
     21 
     22 #define nelem(x) (sizeof (x) / sizeof *(x))
     23 
     24 static const char *karens[] = {
     25 	"karens/karen0.jpg",
     26 	"karens/karen1.jpg",
     27 	"karens/karen2.jpg",
     28 	"karens/karen3.jpg",
     29 };
     30 
     31 static void lynch(int unused, GtkWidget *entry)
     32 {
     33 	int fd;
     34 	char req[64];
     35 	struct hostent *he;
     36 	struct sockaddr_in addr;
     37 
     38 	if ((he = gethostbyname("bitreich.org")) == NULL)
     39 		return;
     40 
     41 	if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
     42 		return;
     43 
     44 	addr.sin_family = AF_INET;
     45 	addr.sin_port = htons(70);
     46 	addr.sin_addr = *((struct in_addr *)he->h_addr);
     47 	memset(&(addr.sin_zero), 0, 8);
     48 
     49 	snprintf(req, sizeof(req), "/cancel\t%s\r\n",
     50 		gtk_entry_get_text(GTK_ENTRY(entry)));
     51 
     52 	if (connect(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr)) < 0)
     53 		goto out;
     54 
     55 	send(fd, req, strlen(req), 0);
     56 
     57 out:
     58 	close(fd);
     59 	(void)unused;
     60 }
     61 
     62 static void cancel()
     63 {
     64 	GtkWidget *dialog, *content_area, *label;
     65 
     66 	dialog = gtk_dialog_new_with_buttons("Cancelled", NULL,
     67 		GTK_DIALOG_MODAL, "OK", GTK_RESPONSE_ACCEPT, NULL);
     68 
     69 	content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
     70 	label = gtk_label_new("\nSuccessfully cancelled!\n");
     71 	gtk_container_add(GTK_CONTAINER(content_area), label);
     72 
     73 	gtk_container_add(GTK_CONTAINER(content_area), gtk_image_new_from_file(
     74 		karens[g_rand_int_range(g_rand_new(), 0, nelem(karens))]));
     75 
     76 	g_signal_connect(dialog, "response", G_CALLBACK(gtk_main_quit), NULL);
     77 
     78 	gtk_widget_show_all(dialog);
     79 }
     80 
     81 int main(int argc, char **argv)
     82 {
     83 	GtkWidget *window, *align, *box, *label, *entry, *lynch_mob, *button;
     84 
     85 	gtk_init(&argc, &argv);
     86 
     87 	window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
     88 	gtk_window_set_default_size(GTK_WINDOW(window), 320, 240);
     89 	gtk_window_set_title(GTK_WINDOW(window), "Cancel Tool");
     90 
     91 	g_signal_connect(window, "delete-event", G_CALLBACK(gtk_main_quit), NULL);
     92 	g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
     93 
     94 	align = gtk_alignment_new(0, 0, 1, 0);
     95 	gtk_alignment_set_padding(GTK_ALIGNMENT(align), 15, 15, 50, 50);
     96 	gtk_container_add(GTK_CONTAINER(window), align);
     97 
     98 	box = gtk_vbox_new(TRUE, 10);
     99 	gtk_container_add(GTK_CONTAINER(align), box);
    100 
    101 	label = gtk_label_new("Who should we cancel today?");
    102 	gtk_container_add(GTK_CONTAINER(box), label);
    103 
    104 	entry = gtk_entry_new();
    105 	gtk_entry_set_text(GTK_ENTRY(entry), "Freedom of speech");
    106 	gtk_container_add(GTK_CONTAINER(box), entry);
    107 
    108 	lynch_mob = gtk_check_button_new_with_label(
    109 		"There is a lynch mob outside my house");
    110 	gtk_container_add(GTK_CONTAINER(box), lynch_mob);
    111 
    112 	button = gtk_button_new_with_label("CANCEL");
    113 	gtk_container_add(GTK_CONTAINER(box), button);
    114 	g_signal_connect(button, "clicked", G_CALLBACK(cancel), window);
    115 	g_signal_connect(button, "clicked", G_CALLBACK(lynch), entry);
    116 
    117 	gtk_widget_show_all(window);
    118 	gtk_main();
    119 
    120 	return 0;
    121 }