· 6 years ago · May 06, 2019, 08:04 PM
1#define FILTERSCRIPT
2#include <a_samp>
3#include <a_mysql>
4
5
6// You must configure these!
7#define SQL_IP ""
8#define SQL_USER ""
9#define SQL_PASSWORD ""
10#define SQL_DB ""
11// End
12
13
14// defines (ignore them)
15#define QUERY(%0) mysql_query(%0, -1, -1, mysql_connection)
16
17#define CREATE_QUERY "CREATE TABLE IF NOT EXISTS `Emails`( \
18 `Sender` varchar(100) NOT NULL,`Reciever` varchar(100) NOT NULL, \
19 `Date` varchar(24) NOT NULL,`Time` varchar(24) NOT NULL,`MK` int(11) NOT NULL, \
20 `New` smallint(6) NOT NULL,`Subject` varchar(100) NOT NULL,`Text` mediumtext NOT NULL) \
21 ENGINE=MyISAM DEFAULT CHARSET=latin1 \
22 COLLATE=latin1_general_cs AUTO_INCREMENT=1"
23#define DIALOGID 7654
24
25#define FREE mysql_free_result(mysql_connection)
26#define STORE mysql_store_result(mysql_connection)
27#define ROWS mysql_num_rows(mysql_connection)
28
29// variables
30
31static mysql_connection;
32new SubjectP[MAX_PLAYERS][100],
33 RecieverP[MAX_PLAYERS][50],
34 MkP[MAX_PLAYERS];
35// functions
36public OnFilterScriptInit()
37{
38 mysql_debug(1);
39 mysql_connection = mysql_connect(SQL_IP, SQL_USER, SQL_DB, SQL_PASSWORD);
40 QUERY(CREATE_QUERY);
41 return 1;
42}
43
44public OnFilterScriptExit()
45{
46 mysql_close(mysql_connection);
47 print("\n--------------------------------------");
48 print(" Mysql E-Mail system by wups Unloaded!");
49 print("--------------------------------------\n");
50
51 return 1;
52}
53
54public OnPlayerConnect(playerid)
55{
56 return 1;
57}
58
59public OnPlayerDisconnect(playerid, reason)
60{
61 return 1;
62}
63
64public OnPlayerSpawn(playerid)
65{
66 return 1;
67}
68
69public OnPlayerDeath(playerid, killerid, reason)
70{
71 return 1;
72}
73
74
75public OnPlayerText(playerid, text[])
76{
77 return 1;
78}
79
80public OnPlayerCommandText(playerid, cmdtext[])
81{
82 if(!strcmp("/email", cmdtext, true))
83 {
84 ShowEmail(playerid);
85 return 1;
86 }
87 return 0;
88}
89stock ShowEmail(playerid)
90{
91 new string[128],
92 name[MAX_PLAYER_NAME],
93 inbox,
94 outbox;
95
96 GetPlayerName(playerid,name,MAX_PLAYER_NAME);
97
98 format(string,sizeof(string),"SELECT `New` FROM `Emails` WHERE `Reciever`='%s' AND `New`=1",name);
99 QUERY(string);
100 STORE;
101 inbox = ROWS;
102 FREE;
103
104 format(string,sizeof(string),"SELECT `New` FROM `Emails` WHERE `Sender`='%s' AND `New`=1",name);
105 QUERY(string);
106 STORE;
107 outbox = ROWS;
108 FREE;
109
110 format(string,sizeof(string),"{FFFFFF}\
111 Inbox({FF0000}%i{FFFFFF} New)\n\
112 Outbox({FF0000}%i{FFFFFF} Unread)\n\
113 Compose",inbox,outbox);
114 ShowPlayerDialog(playerid, DIALOGID, DIALOG_STYLE_LIST,"E-Mail",string,"Ok","Cancel");
115 return 1;
116}
117
118public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
119{
120 mysql_real_escape_string(inputtext,inputtext);
121 switch(dialogid)
122 {
123 case DIALOGID-1: // reply/delete
124 {
125 if(response)
126 {
127 ShowPlayerDialog(playerid,DIALOGID+31,DIALOG_STYLE_INPUT,"Compose","Input the Subject.\nMax Lenght: 100 symbols.","Ok","Back");
128 return 1;
129 }
130 else
131 {
132 new query[128];
133 format(query,sizeof(query),"DELETE FROM `Emails` WHERE MK=%i LIMIT 1",MkP[playerid]);
134 QUERY(query);
135 SendClientMessage(playerid,0xFFFFFF30," * Message deleted!");
136 ShowEmail(playerid);
137 return 1;
138 }
139 }
140 case DIALOGID-2:
141 {
142 ShowEmail(playerid);
143 return 1;
144 }
145 case DIALOGID+1: // inbox
146 {
147 if(response)
148 {
149 new result[1024],
150 name[MAX_PLAYER_NAME],
151 Subject[100],
152 Sender[60],
153 Time[15],
154 Date[20],
155 Message[256],
156 New=0,
157 Mk;
158
159 GetPlayerName(playerid,name,MAX_PLAYER_NAME);
160 format(result,sizeof(result),"SELECT `Sender`,`Subject`,`Time`,`Date`,`New`,`MK`,`Text` FROM `Emails` WHERE `Reciever`='%s' ORDER BY `MK` DESC LIMIT %i,1",name,listitem);
161 QUERY(result);
162 STORE;
163 mysql_fetch_row_format(result,"|",mysql_connection);
164 sscanf(result,"ssssiis",Sender,Subject,Time,Date,New,Mk,Message);
165 replaceChar(Message,'~','\n');
166 format(result,sizeof(result),"{FFFFFF}Date:{FF9933} %s{FFFF33} %s\n{FFFFFF}Sender:{FFFF33} %s\n{FFFFFF}Subject: %s\n\n\n%s",Date,Time,Sender,Subject,Message);
167 FREE;
168
169 format(RecieverP[playerid],60,"%s",Sender);
170 MkP[playerid]=Mk;
171 ShowPlayerDialog(playerid,DIALOGID-1,DIALOG_STYLE_MSGBOX,"Message",result,"Reply","Delete");
172 if(New)
173 {
174 format(result,sizeof(result),"UPDATE `Emails` SET `New`=0 WHERE `MK`='%i' LIMIT 1",Mk);
175 QUERY(result);
176 }
177 }
178 else ShowEmail(playerid);
179 return 1;
180 }
181 case DIALOGID+2: // outbox
182 {
183 if(response)
184 {
185 new result[1024],
186 name[MAX_PLAYER_NAME],
187 Subject[100],
188 Reciever[60],
189 Time[15],
190 Date[20],
191 Message[256];
192
193 GetPlayerName(playerid,name,MAX_PLAYER_NAME);
194 format(result,sizeof(result),"SELECT `Reciever`,`Subject`,`Time`,`Date`,`Text` FROM `Emails` WHERE `Sender`='%s' ORDER BY `MK` DESC LIMIT %i,1",name,listitem);
195 QUERY(result);
196 STORE;
197 mysql_fetch_row_format(result,"|",mysql_connection);
198 sscanf(result,"sssss",Reciever,Subject,Time,Date,Message);
199 format(result,sizeof(result),"{FFFFFF}Date:{FF9933} %s{FFFF33} %s\n{FFFFFF}Reciever:{FFFF33} %s\n{FFFFFF}Subject: %s\n\n\n%s",Date,Time,Reciever,Subject,Message);
200 FREE;
201 ShowPlayerDialog(playerid,DIALOGID-2,DIALOG_STYLE_MSGBOX,"Message",result,"Ok","Cancel");
202 }
203 else ShowEmail(playerid);
204 return 1;
205 }
206 case DIALOGID: // main dialog
207 {
208 if(response)
209 {
210 switch(listitem)
211 {
212 case 0: // inbox
213 {
214 new result[3048],
215 name[MAX_PLAYER_NAME],
216 QR[256],
217 Subject[100],
218 Sender[60],
219 Time[10],
220 New
221 ;
222 GetPlayerName(playerid,name,MAX_PLAYER_NAME);
223 format(QR,sizeof(QR),"SELECT `Sender`,`Subject`,`Time`,`New` FROM `Emails` WHERE `Reciever`='%s' ORDER BY `MK` DESC LIMIT 20",name);
224 QUERY(QR);
225 STORE;
226 if(!ROWS) return SendClientMessage(playerid,0xFFFFFF30," * You don't have any messages in your inbox!");
227 while(mysql_fetch_row_format(QR,"|",mysql_connection))
228 {
229 sscanf(QR,"sssi",Sender,Subject,Time,New);
230 if(!New)
231 format(result,sizeof(result),"%s{FFFFFF}'%s'{FF9933} FROM %s{FFFF33} %s\n",result,Subject,Sender,Time);
232 else
233 format(result,sizeof(result),"%s{FFFFFF}'%s'{FF9933} FROM %s{FFFF33} NEW\n",result,Subject,Sender);
234
235 }
236 FREE;
237 ShowPlayerDialog(playerid,DIALOGID+1,DIALOG_STYLE_LIST,"Inbox",result,"Ok","Back");
238 return 1;
239 }
240 case 1: // outbox
241 {
242 new result[3048],
243 name[MAX_PLAYER_NAME],
244 QR[256],
245 Subject[100],
246 Reciever[60],
247 Time[10],
248 New
249 ;
250 GetPlayerName(playerid,name,MAX_PLAYER_NAME);
251 format(QR,sizeof(QR),"SELECT `Reciever`,`Subject`,`Time`,`New` FROM `Emails` WHERE `Sender`='%s' ORDER BY `MK` DESC LIMIT 20",name);
252 QUERY(QR);
253 STORE;
254 if(!ROWS) return SendClientMessage(playerid,0xFFFFFF30," * You don't have any messages in your outbox!");
255 while(mysql_fetch_row_format(QR,"|",mysql_connection))
256 {
257 sscanf(QR,"sssi",Reciever,Subject,Time,New);
258 if(!New)
259 format(result,sizeof(result),"%s{FFFFFF}'%s'{FF9933} TO %s{FFFF33} %s\n",result,Subject,Reciever,Time);
260 else
261 format(result,sizeof(result),"%s{FFFFFF}'%s'{FF9933} TO %s{FFFF33} UNREAD\n",result,Subject,Reciever);
262 }
263 FREE;
264 ShowPlayerDialog(playerid,DIALOGID+2,DIALOG_STYLE_LIST,"Outbox",result,"Ok","Back");
265 return 1;
266 }
267 case 2: // compose
268 {
269 ShowPlayerDialog(playerid,DIALOGID+30,DIALOG_STYLE_INPUT,"Compose","Input the Recipients E-Mail address.\nFormat - Name","Ok","Back");
270 return 1;
271 }
272 }
273 }
274 }
275 case DIALOGID+30: // compose 1
276 {
277 if(response)
278 {
279 if(strlen(inputtext) > 50 || strlen(inputtext) < 5 || strfind(inputtext,"\n",true) != -1 || strfind(inputtext,"{",true) != -1 || strfind(inputtext,"}",true) != -1 || strfind(inputtext,"|",true) != -1 ) return ShowPlayerDialog(playerid,DIALOGID+30,DIALOG_STYLE_INPUT,"Compose","Input the Recipients E-Mail address.\nFormat - Name\nE-Mail address Name is in wrong format!","Ok","Back");
280 format(RecieverP[playerid],50,inputtext);
281 ShowPlayerDialog(playerid,DIALOGID+31,DIALOG_STYLE_INPUT,"Compose","Input the Subject.\nMax Lenght: 100 symbols.","Ok","Back");
282 return 1;
283 }
284 else
285 {
286 ShowEmail(playerid);
287 return 1;
288 }
289 }
290 case DIALOGID+31: // compose 2
291 {
292
293 if(response)
294 {
295 if(strlen(inputtext) > 98 || strlen(inputtext) < 2 || strfind(inputtext,"\n",true) != -1 || strfind(inputtext,"{",true) != -1 || strfind(inputtext,"}",true) != -1 || strfind(inputtext,"|",true) != -1) return ShowPlayerDialog(playerid,DIALOGID+30,DIALOG_STYLE_INPUT,"Compose","Input the Subject.\nMax Lenght: 100 symbols.\nSubject is invalid format!","Ok","Back");
296 format(SubjectP[playerid],100,inputtext);
297 ShowPlayerDialog(playerid,DIALOGID+32,DIALOG_STYLE_INPUT,"Compose","Write the message.\nMax Lenght: 256 symbols.\n '~' Indicates a new line.","Ok","Back");
298 return 1;
299 }
300 else
301 {
302 ShowEmail(playerid);
303 return 1;
304 }
305 }
306 case DIALOGID+32: // compose 3
307 {
308 if(response)
309 {
310 if(strlen(inputtext) > 255 || strlen(inputtext) < 2) return ShowPlayerDialog(playerid,DIALOGID+32,DIALOG_STYLE_INPUT,"Compose","Write the message.\nMax Lenght: 256 symbols.\nMessage too short or too long!","Ok","Back");
311 if(strfind(inputtext,"\n",true) != -1 || strfind(inputtext,"{",true) != -1 || strfind(inputtext,"}",true) != -1 || strfind(inputtext,"|",true) != -1) return ShowPlayerDialog(playerid,DIALOGID+32,DIALOG_STYLE_INPUT,"Compose","Write the message.\nMax Lenght: 256 symbols.\nMessage contains blocked symbols!","Ok","Back");
312 new
313 query[1024],
314 hour,
315 minute,
316 second,
317 day,
318 month,
319 year,
320 mk,
321 name[MAX_PLAYER_NAME];
322
323 gettime(hour,minute,second);
324 getdate(year,month,day);
325 mk = mktime(hour,minute,second,day,month,year);
326 GetPlayerName(playerid,name,MAX_PLAYER_NAME);
327 format(query,sizeof(query),"INSERT INTO `Emails` (Sender,Reciever,Date,Time,MK,New,Subject,Text) VALUES('%s','%s','%i-%i-%i','%i:%i',%i,1,'%s','%s')",
328 name,RecieverP[playerid],day,month,year,hour,minute,mk,SubjectP[playerid],inputtext);
329 QUERY(query);
330 SendClientMessage(playerid,0xFFFFFF30," * Message sent!");
331 return 1;
332 }
333 else
334 {
335 ShowEmail(playerid);
336 return 1;
337 }
338 }
339 }
340 return 0;
341}
342
343// Libraries
344stock sscanf(string[], format[], {Float,_}:...) // credits Y_Less
345{
346 #if defined isnull
347 if (isnull(string))
348 #else
349 if (string[0] == 0 || (string[0] == 1 && string[1] == 0))
350 #endif
351 {
352 return format[0];
353 }
354 #pragma tabsize 4
355 new
356 formatPos = 0,
357 stringPos = 0,
358 paramPos = 2,
359 paramCount = numargs(),
360 delim = '|';
361 while (string[stringPos] && string[stringPos] <= ' ')
362 {
363 stringPos++;
364 }
365 while (paramPos < paramCount && string[stringPos])
366 {
367 switch (format[formatPos++])
368 {
369 case '\0':
370 {
371 return 0;
372 }
373 case 'i':
374 {
375 new
376 neg = 1,
377 num = 0,
378 ch = string[stringPos];
379 if (ch == '-')
380 {
381 neg = -1;
382 ch = string[++stringPos];
383 }
384 do
385 {
386 stringPos++;
387 if ('0' <= ch <= '9')
388 {
389 num = (num * 10) + (ch - '0');
390 }
391 else
392 {
393 return -1;
394 }
395 }
396 while ((ch = string[stringPos]) > ' ' && ch != delim);
397 setarg(paramPos, 0, num * neg);
398 }
399 case 'd':
400 {
401 new
402 num = 0,
403 ch = string[stringPos];
404 if (ch == '-')
405 {
406 return -1;
407 }
408 do
409 {
410 stringPos++;
411 if ('0' <= ch <= '9')
412 {
413 num = (num * 10) + (ch - '0');
414 }
415 else
416 {
417 return -1;
418 }
419 }
420 while ((ch = string[stringPos]) > ' ' && ch != delim);
421 setarg(paramPos, 0, num);
422 }
423 case 'h', 'x':
424 {
425 new
426 num = 0,
427 ch = string[stringPos];
428 do
429 {
430 stringPos++;
431 switch (ch)
432 {
433 case 'x', 'X':
434 {
435 num = 0;
436 continue;
437 }
438 case '0' .. '9':
439 {
440 num = (num << 4) | (ch - '0');
441 }
442 case 'a' .. 'f':
443 {
444 num = (num << 4) | (ch - ('a' - 10));
445 }
446 case 'A' .. 'F':
447 {
448 num = (num << 4) | (ch - ('A' - 10));
449 }
450 default:
451 {
452 return -1;
453 }
454 }
455 }
456 while ((ch = string[stringPos]) > ' ' && ch != delim);
457 setarg(paramPos, 0, num);
458 }
459 case 'c':
460 {
461 setarg(paramPos, 0, string[stringPos++]);
462 }
463 case 'f':
464 {
465
466 new changestr[16], changepos = 0, strpos = stringPos;
467 while(changepos < 16 && string[strpos] && string[strpos] != delim)
468 {
469 changestr[changepos++] = string[strpos++];
470 }
471 changestr[changepos] = '\0';
472 setarg(paramPos,0,_:floatstr(changestr));
473 }
474 case 'p':
475 {
476 delim = format[formatPos++];
477 continue;
478 }
479 case '\'':
480 {
481 new
482 end = formatPos - 1,
483 ch;
484 while ((ch = format[++end]) && ch != '\'') {}
485 if (!ch)
486 {
487 return -1;
488 }
489 format[end] = '\0';
490 if ((ch = strfind(string, format[formatPos], false, stringPos)) == -1)
491 {
492 if (format[end + 1])
493 {
494 return -1;
495 }
496 return 0;
497 }
498 format[end] = '\'';
499 stringPos = ch + (end - formatPos);
500 formatPos = end + 1;
501 }
502 case 'u':
503 {
504 new
505 end = stringPos - 1,
506 id = 0,
507 bool:num = true,
508 ch;
509 while ((ch = string[++end]) && ch != delim)
510 {
511 if (num)
512 {
513 if ('0' <= ch <= '9')
514 {
515 id = (id * 10) + (ch - '0');
516 }
517 else
518 {
519 num = false;
520 }
521 }
522 }
523 if (num && IsPlayerConnected(id))
524 {
525 setarg(paramPos, 0, id);
526 }
527 else
528 {
529 #if !defined foreach
530 #define foreach(%1,%2) for (new %2 = 0; %2 < MAX_PLAYERS; %2++) if (IsPlayerConnected(%2))
531 #define __SSCANF_FOREACH__
532 #endif
533 string[end] = '\0';
534 num = false;
535 new
536 name[MAX_PLAYER_NAME];
537 id = end - stringPos;
538 foreach (Player, playerid)
539 {
540 GetPlayerName(playerid, name, sizeof (name));
541 if (!strcmp(name, string[stringPos], true, id))
542 {
543 setarg(paramPos, 0, playerid);
544 num = true;
545 break;
546 }
547 }
548 if (!num)
549 {
550 setarg(paramPos, 0, INVALID_PLAYER_ID);
551 }
552 string[end] = ch;
553 #if defined __SSCANF_FOREACH__
554 #undef foreach
555 #undef __SSCANF_FOREACH__
556 #endif
557 }
558 stringPos = end;
559 }
560 case 's', 'z':
561 {
562 new
563 i = 0,
564 ch;
565 if (format[formatPos])
566 {
567 while ((ch = string[stringPos++]) && ch != delim)
568 {
569 setarg(paramPos, i++, ch);
570 }
571 if (!i)
572 {
573 return -1;
574 }
575 }
576 else
577 {
578 while ((ch = string[stringPos++]))
579 {
580 setarg(paramPos, i++, ch);
581 }
582 }
583 stringPos--;
584 setarg(paramPos, i, '\0');
585 }
586 default:
587 {
588 continue;
589 }
590 }
591 while (string[stringPos] && string[stringPos] != delim && string[stringPos] > ' ')
592 {
593 stringPos++;
594 }
595 while (string[stringPos] && (string[stringPos] == delim || string[stringPos] <= ' '))
596 {
597 stringPos++;
598 }
599 paramPos++;
600 }
601 do
602 {
603 if ((delim = format[formatPos++]) > ' ')
604 {
605 if (delim == '\'')
606 {
607 while ((delim = format[formatPos++]) && delim != '\'') {}
608 }
609 else if (delim != 'z')
610 {
611 return delim;
612 }
613 }
614 }
615 while (delim > ' ');
616 return 0;
617}
618// dutils... Credits: DracoBlue
619stock mktime(hour,minute,second,day,month,year) {
620 new timestamp2;
621
622 timestamp2 = second + (minute * 60) + (hour * 3600);
623
624 new days_of_month[12];
625
626 if ( ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0) ) {
627 days_of_month = {31,29,31,30,31,30,31,31,30,31,30,31}; // Schaltjahr
628 } else {
629 days_of_month = {31,28,31,30,31,30,31,31,30,31,30,31}; // keins
630 }
631 new days_this_year = 0;
632 days_this_year = day;
633 if(month > 1) { // No January Calculation, because its always the 0 past months
634 for(new i=0; i<month-1;i++) {
635 days_this_year += days_of_month[i];
636 }
637 }
638 timestamp2 += days_this_year * 86400;
639
640 for(new j=1970;j<year;j++) {
641 timestamp2 += 31536000;
642 if ( ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0) ) timestamp2 += 86400; // Schaltjahr + 1 Tag
643 }
644
645 return timestamp2;
646}
647stock replaceChar(strSrc[], chWhat, chWith)
648{
649 for(new i; strSrc[i] != EOS; ++i)
650 {
651 if(strSrc[i] == chWhat)
652 {
653 strSrc[i] = chWith;
654 }
655 }
656}