]> jfr.im git - irc/quakenet/newserv.git/blob - newsearch/ns-notice.c
And then actually remember to run hg add before committing ...
[irc/quakenet/newserv.git] / newsearch / ns-notice.c
1 /*
2 * NOTICE functionality
3 * ---------
4 * Use of markers allows us to use notice in both channel and nick searches so we can have newserv notice everyone on a channel and/or
5 * everyone matching a certain nick_search pattern.
6 */
7
8 #include "newsearch.h"
9
10 #include <stdio.h>
11 #include <stdlib.h>
12
13 #include "../control/control.h" /* controlreply() */
14 #include "../lib/irc_string.h" /* IPtostr() */
15 #include "../lib/strlfunc.h"
16
17 extern nick *senderNSExtern;
18
19 void *notice_exe(struct searchNode *thenode, void *theinput);
20 void notice_free(struct searchNode *thenode);
21
22 struct notice_localdata {
23 unsigned int marker;
24 int count;
25 int type;
26 char message[NSMAX_NOTICE_LEN];
27 };
28
29 struct searchNode *notice_parse(int type, int argc, char **argv) {
30 struct notice_localdata *localdata;
31 struct searchNode *thenode;
32 int len;
33
34 if (!(localdata = (struct notice_localdata *) malloc(sizeof(struct notice_localdata)))) {
35 parseError = "malloc: could not allocate memory for this search.";
36 return NULL;
37 }
38 localdata->count = 0;
39 localdata->type = type;
40 if (type == SEARCHTYPE_CHANNEL)
41 localdata->marker = nextchanmarker();
42 else
43 localdata->marker = nextnickmarker();
44
45 if (argc==1) {
46 char *p = argv[0];
47 if(*p == '\"')
48 p++;
49 len = strlcpy(localdata->message, p, sizeof(localdata->message));
50 if(len >= sizeof(localdata->message)) {
51 localdata->message[sizeof(localdata->message)-1] = '\0';
52 } else {
53 localdata->message[len-1] = '\0';
54 }
55 }
56 else {
57 /* no notice to send out ... */
58 parseError = "Warning: you did not specify a message to notice out.";
59 free(localdata);
60 return NULL;
61 }
62
63 if (!(thenode=(struct searchNode *)malloc(sizeof (struct searchNode)))) {
64 /* couldn't malloc() memory for thenode, so free localdata to avoid leakage */
65 parseError = "malloc: could not allocate memory for this search.";
66 free(localdata);
67 return NULL;
68 }
69
70 thenode->returntype = RETURNTYPE_BOOL;
71 thenode->localdata = localdata;
72 thenode->exe = notice_exe;
73 thenode->free = notice_free;
74
75 return thenode;
76 }
77
78 void *notice_exe(struct searchNode *thenode, void *theinput) {
79 struct notice_localdata *localdata;
80 nick *np;
81 chanindex *cip;
82
83 localdata = thenode->localdata;
84
85 if (localdata->type == SEARCHTYPE_CHANNEL) {
86 cip = (chanindex *)theinput;
87 cip->marker = localdata->marker;
88 localdata->count += cip->channel->users->totalusers;
89 }
90 else {
91 np = (nick *)theinput;
92 np->marker = localdata->marker;
93 localdata->count++;
94 }
95
96 return (void *)1;
97 }
98
99 void notice_free(struct searchNode *thenode) {
100 struct notice_localdata *localdata;
101 nick *np, *nnp;
102 chanindex *cip, *ncip;
103 int i, j;
104 unsigned int nickmarker;
105
106 localdata = thenode->localdata;
107
108 if (localdata->type == SEARCHTYPE_CHANNEL) {
109 nickmarker=nextnickmarker();
110 for (i=0;i<CHANNELHASHSIZE;i++) {
111 for (cip=chantable[i];cip;cip=ncip) {
112 ncip = cip->next;
113 if (cip != NULL && cip->channel != NULL && cip->marker == localdata->marker) {
114 for (j=0;j<cip->channel->users->hashsize;j++) {
115 if (cip->channel->users->content[j]==nouser)
116 continue;
117
118 if ((np=getnickbynumeric(cip->channel->users->content[j])))
119 np->marker=nickmarker;
120 }
121 }
122 }
123 }
124 for (i=0;i<NICKHASHSIZE;i++) {
125 for(np=nicktable[i];np;np=nnp) {
126 nnp = np->next;
127 if (np->marker == nickmarker)
128 controlnotice(np, localdata->message);
129 }
130 }
131 }
132 else {
133 for (i=0;i<NICKHASHSIZE;i++) {
134 for (np=nicktable[i];np;np=nnp) {
135 nnp = np->next;
136 if (np->marker == localdata->marker)
137 controlnotice(np, localdata->message);
138 }
139 }
140 }
141 /* notify opers of the action */
142 controlwall(NO_OPER, NL_BROADCASTS, "%s/%s sent the following message to %d %s: %s", senderNSExtern->nick, senderNSExtern->authname, localdata->count, localdata->count != 1 ? "users" : "user", localdata->message);
143 free(localdata);
144 free(thenode);
145 }