source: trollforge/irc/minv/minv.c @ 608

Revision 605, 4.0 KB checked in by literalka, 17 months ago (diff)

more sorting, jesus fuck

Line 
1#include "../xchat-plugin.h"
2
3#include <stdio.h>
4#include <stdlib.h>
5#include <string.h>
6
7/* if this is defined, minv will always turn on net_throttle before doing
8   the massinvite to make ultra sure you don't flood off
9   downside: it makes massinviting really fucking slow */
10#define AUTO_THROTTLE
11
12static xchat_plugin *ph;
13
14static int
15minv(char *word[], char *word_eol[], void *userdata)
16{
17        #ifdef AUTO_THROTTLE
18        const char *junk;
19        int throttle;
20        #endif
21        unsigned long int i;
22        unsigned long int numusrs = 0;
23        char prefix;
24        char **ulist = NULL;
25        xchat_list *users;
26
27        if (xchat_nickcmp(ph, word[2], xchat_get_info(ph, "channel")) == 0) {
28                xchat_print(ph, "From and to channels are the same.");
29                return XCHAT_EAT_ALL;
30        }
31
32        users = xchat_list_get(ph, "users");
33
34        if (users) {
35
36                #ifdef AUTO_THROTTLE
37                /* turn on throttle so we don't flood off */
38                if (xchat_get_prefs(ph, "net_throttle", &junk, &throttle) != 3) {
39                        xchat_print(ph, "assuming net_throttle is off");
40                        throttle = 0;
41                }
42                if (throttle != 1) {
43                        xchat_command(ph, "SET net_throttle 1");
44                }
45                #endif
46       
47                while (xchat_list_next(ph, users)) {
48
49                        /* don't /invite unwanted people if desired */
50                        prefix = *(xchat_list_str(ph, users, "prefix"));
51                        if ((*(word[3]) != '\0') && (prefix != '\0')) {
52                                if (strchr(word[3], prefix) != NULL) {
53                                        continue;
54                                }
55                        }
56
57                        numusrs++;
58                        ulist = realloc(ulist, sizeof(char *) * numusrs);
59                        ulist[numusrs - 1] = malloc(strlen(xchat_list_str(ph, users, "nick")));
60                        if (ulist[numusrs - 1] == NULL) {
61                                return XCHAT_EAT_ALL;
62                        }
63                        strcpy(ulist[numusrs - 1], xchat_list_str(ph, users, "nick"));
64                }
65                xchat_list_free(ph, users);
66
67                /* start the invite spree */
68                for (i = 0; i < numusrs; i++) {
69                        xchat_commandf(ph, "INVITE %s %s", ulist[i], word[2]);
70                        free(ulist[i]);
71                }
72               
73                free(ulist);
74               
75                #ifdef AUTO_THROTTLE
76                xchat_commandf(ph, "SET net_throttle %i", throttle);
77                #endif
78
79        }
80
81        xchat_print(ph, "Mass invite complete.");
82        return XCHAT_EAT_ALL;
83}
84
85static int
86minvf(char *word[], char *word_eol[], void *userdata)
87{
88        #ifdef AUTO_THROTTLE
89        const char *junk;
90        int throttle;
91        #endif
92        char *chan;
93        size_t chanlen;
94        char *nick;
95        FILE *nickfile;
96       
97        nickfile = fopen(word[2], "r");
98        if (nickfile == NULL) {
99                xchat_printf(ph, "Couldn't open %s.", word[2]);
100                return XCHAT_EAT_ALL;
101        }
102
103        chanlen = strlen(xchat_get_info(ph, "channel"));
104        chan = malloc(chanlen + 1);
105        if (chan == NULL) {
106                return XCHAT_EAT_ALL;
107        }
108        strncpy(chan, xchat_get_info(ph, "channel"), chanlen);
109        chan[chanlen] = '\0';
110               
111        nick = malloc(100);
112        if (nick == NULL) {
113                fclose(nickfile);
114                return XCHAT_EAT_ALL;
115        }
116
117        #ifdef AUTO_THROTTLE
118       
119        /* turn on throttle so we don't flood off */
120        if (xchat_get_prefs(ph, "net_throttle", &junk, &throttle) != 3) {
121                xchat_print(ph, "assuming net_throttle is off");
122                throttle = 0;
123        }
124        if (throttle != 1) {
125                xchat_command(ph, "SET net_throttle 1");
126        }
127       
128        #endif
129       
130        while (!feof(nickfile)) {
131                fgets(nick, 100, nickfile);
132                if (strrchr(nick, '\n')) {
133                        *(strrchr(nick, '\n')) = '\0';
134                }
135                xchat_commandf(ph, "INVITE %s %s", nick, chan);
136        }
137
138        fclose(nickfile);
139        free(nick);
140
141        #ifdef AUTO_THROTTLE
142        xchat_commandf(ph, "SET net_throttle %i", throttle);
143        #endif
144
145        return XCHAT_EAT_ALL;
146}
147
148int
149xchat_plugin_init(xchat_plugin *plugin_handle, char **plugin_name, char **plugin_desc, char **plugin_version, char *arg)
150{
151        ph = plugin_handle;
152
153        *plugin_name = "Minv";
154        *plugin_desc = "Adds /minv and /minvf commands";
155        *plugin_version = "0.3.2";
156
157        xchat_hook_command(ph, "MINV", XCHAT_PRI_NORM, minv, "Usage: MINV <chanto> <noprefix>\nInvite everybody in the current channel into <chanto>.\nIf any second argument (<noprefix>) then those with a nick prefix included in <noprefix> is not invited.", NULL);
158        xchat_hook_command(ph, "MINVF", XCHAT_PRI_NORM, minvf, "Usage: MINVF <nickfile>\nInvite each nick listed in <nickfile> to the current channel.", NULL);
159
160        xchat_print(ph, "Minv loaded successfully.");
161
162        return 1;
163}
Note: See TracBrowser for help on using the repository browser.