Advertisement
Guest User

Untitled

a guest
Jul 1st, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.06 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use Net::IRC;
  4.  
  5. $server = 'irc.magicstar.net';
  6. $channel = '#GWAMS';
  7. $botnick = 'PainBot';
  8. $botuname = 'painbot';
  9. $password = 'foobar';
  10. $botadmin = 'AnnoDomini';
  11. $comment = 'AD\'s Other DiceBot';
  12. $version = '0.26-271110';
  13. $maxline = 400;
  14.  
  15. $irc = new Net::IRC;
  16.  
  17. $conn = $irc->newconn(Nick => $botnick,
  18. Server => $server,
  19. Port => 6667,
  20. Username => $botuname,
  21. Ircname => $comment);
  22.  
  23. $conn->add_global_handler('376', \&on_connect);
  24. $conn->add_global_handler('disconnect', \&on_disconnect);
  25. $conn->add_global_handler('kick', \&on_kick);
  26. $conn->add_global_handler('msg', \&on_priv);
  27. $conn->add_global_handler('cversion', \&on_ctcp_version);
  28. $conn->add_global_handler('public', \&on_public);
  29. $conn->add_global_handler('invite', \&on_invite);
  30. $conn->add_global_handler('cjoin', \&on_ctcp_join);
  31. $conn->add_global_handler('cpart', \&on_ctcp_part);
  32.  
  33. print STDERR "PainBot v".$version." started.\n";
  34. $irc->start;
  35.  
  36. sub on_ctcp_join {
  37. $self = shift;
  38. $event = shift;
  39. foreach $arg ($event->args) {
  40. $chan = $arg;
  41. $self->join($chan);
  42. print STDERR "Joining $chan.\n";
  43. }
  44. }
  45.  
  46. sub on_ctcp_part {
  47. $self = shift;
  48. $event = shift;
  49. foreach $arg ($event->args) {
  50. $self->part($chan);
  51. print STDERR "Parting $chan.\n";
  52. }
  53. }
  54.  
  55. sub on_connect {
  56. $self = shift;
  57. $self->privmsg('nickserv', "identify $password");
  58. $self->join($channel);
  59. print STDERR "Connected to " . $server . ".\n";
  60. }
  61.  
  62. sub on_disconnect {
  63. $self = shift;
  64. $self->connect();
  65. }
  66.  
  67. sub on_kick {
  68. $self = shift;
  69. $event = shift;
  70. print STDERR "Kicked by ".$event->nick.".\n";
  71. }
  72.  
  73. sub on_priv {
  74. $self = shift;
  75. $event = shift;
  76. handle_message($self,$event,$event->nick);
  77. }
  78.  
  79. sub on_public {
  80. $self = shift;
  81. $event = shift;
  82. handle_message($self,$event,$event->to);
  83. }
  84.  
  85. sub handle_message {
  86. $self = shift;
  87. $event = shift;
  88. $dest = shift;
  89.  
  90. foreach $arg ($event->args) {
  91. if (($arg =~ /^!die/i) && ($event->nick eq $botadmin)) {
  92. $self->quit("I return to the Wheel of Suffering.");
  93. print STDERR "Received shutdown command. Exiting.\n";
  94. exit 0;
  95. } elsif ($arg =~ /^!ping/i) {
  96. $self->privmsg($dest, 'pong!');
  97. print STDERR "Ping command activated by ".$event->nick." in $dest.\n";
  98. } elsif ($arg =~ /^!version/i) {
  99. $self->privmsg($dest, 'PainBot v' . $version . ' by AnnoDomini.');
  100. print STDERR "Version command activated by ".$event->nick." in $dest.\n";
  101. } elsif ($arg =~ /^!roll\s+/i) {
  102. $expression = substr($arg,6,$maxline);
  103. command_roll($self,$dest,$event->nick,$expression);
  104. print STDERR "Rolling $expression for ".$event->nick." in $dest.\n";
  105. } elsif ($arg =~ /^!help/i) {
  106. $self->privmsg($event->nick, "You have reached the help function of the PainBot, v$version, by AnnoDomini. The bot is under construction, and more help is unavailable.");
  107. print STDERR "Help requested by ".$event->nick." in $dest.\n";
  108. } elsif ($arg =~ /^!exalted\s+/i) {
  109. $expression = substr($arg,9,$maxline);
  110. command_exalted($self,$dest,$event->nick,$expression);
  111. print STDERR "Rolling Exalted dice for ".$event->nick." in $dest.\n";
  112. } elsif ($arg =~ /^!sr3\s+/i) {
  113. $expression = substr($arg,5,$maxline);
  114. command_sr3($self,$dest,$event->nick,$expression);
  115. print STDERR "Rolling SR3 dice for ".$event->nick." in $dest.\n";
  116. } elsif ($arg =~ /^!nwod\s/i) {
  117. $expression = substr($arg,6,$maxline);
  118. command_nwod($self,$dest,$event->nick,$expression);
  119. print STDERR "Rolling nWoD dice for ".$event->nick." in $dest.\n";
  120. } elsif ($arg =~ /^!space\s+/i) {
  121. $expression = substr($arg,7,$maxline);
  122. command_space($self,$dest,$event->nick,$expression);
  123. print STDERR "Rolling Space dice for ".$event->nick." in $dest.\n";
  124. } elsif ($arg =~ /^!join\s+/i) {
  125. $expression = substr($arg,6,$maxline);
  126. command_join($self,$expression);
  127. print STDERR "Join request in $dest.\n";
  128. } elsif ($arg =~ /^!part\s+/i) {
  129. $expression = substr($arg,6,$maxline);
  130. command_part($self,$expression);
  131. print STDERR "Part request in $dest.\n";
  132. } else {
  133. if (($arg =~ /^!d/i) || ($arg =~ /^!\d/i)) { # crude but effective
  134. $expression = substr($arg,1,$maxline);
  135. command_roll($self,$dest,$event->nick,$expression);
  136. print STDERR "Rolling $expression for ".$event->nick." in $dest.\n";
  137. }
  138. }
  139. }
  140. }
  141.  
  142. sub command_part {
  143. $self = shift;
  144. $chan = shift;
  145. $self->part($chan);
  146. print STDERR "Parting " . $chan . ".\n";
  147. }
  148.  
  149. sub command_join {
  150. $self = shift;
  151. $chan = shift;
  152. $self->join($chan);
  153. print STDERR "Joining " . $chan . ".\n";
  154. }
  155.  
  156. sub on_ctcp_version {
  157. $self = shift;
  158. $event = shift;
  159. $self->ctcp_reply($event->nick,'PainBot v' . $version . ' by AnnoDomini.');
  160. print STDERR "Received and replied to CTCP VERSION from " . $event->nick . ".\n";
  161. }
  162.  
  163. sub command_roll {
  164. $self = shift;
  165. $dest = shift;
  166. $nick = shift;
  167. $args = shift;
  168. $output = "[$nick] rolled ";
  169. $totals = "";
  170. if ($args =~ /:/) {
  171. $colonpos = index $args,':';
  172. $comment = trim(substr($args,$colonpos+1,$maxline));
  173. $expressionwithreps = substr($args,0,$colonpos);
  174. } else {
  175. $comment = $args;
  176. $expressionwithreps = $args;
  177. }
  178. $output .= "\"$comment\": ";
  179. if ($expressionwithreps =~ /,/) {
  180. $commapos = index $expressionwithreps,',';
  181. $repetitions = alltrim(substr($expressionwithreps,$commapos+1,$maxline));
  182. $expression = alltrim(substr($expressionwithreps,0,$commapos));
  183. } else {
  184. $repetitions = 1;
  185. $expression = alltrim($expressionwithreps);
  186. }
  187. $expression =~ s/\^/\*\*/; # legacy
  188. if ($repetitions < 0) { $repetitions = 1; }
  189. if ($repetitions > 30) { $repetitions = 30; }
  190. $explength = length($expression);
  191. $expcopy = $expression;
  192. $mathableresult = "";
  193. for ($rep = 0;$rep<$repetitions;$rep++) { # rep loop
  194. $lastoperator = -1;
  195. for ($pos = 0;$pos<$explength;$pos++) { # string parsing
  196. $curchar = substr($expression,$pos,1);
  197. if (is_operator($curchar) == 1 || $pos == $explength-1) {
  198. if ($pos == $explength-1) {
  199. $mathableresult .= parse_one_roll(substr($expression,$lastoperator+1,$maxline));
  200. } else {
  201. $mathableresult .= parse_one_roll(substr($expression,$lastoperator+1,$pos-$lastoperator-1)) . $curchar;
  202. }
  203. $lastoperator = $pos;
  204. }
  205. }
  206. $output .= "$mathableresult, ";
  207. $totals .= eval($mathableresult)." ";
  208. $mathableresult = "";
  209. }
  210. $output = substr($output,0,-2).". Total: \x02".substr($totals,0,-1)."\x02.";
  211.  
  212. $self->privmsg($dest, $output);
  213.  
  214. }
  215.  
  216. sub is_operator {
  217. $input = shift;
  218. if ($input eq '+') { return 1; }
  219. if ($input eq '-') { return 1; }
  220. if ($input eq '*') { return 1; }
  221. if ($input eq '/') { return 1; }
  222. if ($input eq '^') { return 1; }
  223. if ($input eq '%') { return 1; }
  224. return 0;
  225. }
  226.  
  227. sub parse_one_roll {
  228. $args = shift;
  229. $dpos = index lc($args),'d';
  230. if ($dpos == -1) { return $args; }
  231. if ($args =~ /l/i) { # l
  232. $droplow = 1;
  233. $args =~ s/l//ig;
  234. } else {
  235. $droplow = 0;
  236. }
  237. if ($args =~ /h/i) { # h
  238. $drophigh = 1;
  239. $args =~ s/h//ig;
  240. } else {
  241. $drophigh = 0;
  242. }
  243. if ($args =~ /f/i) { # f
  244. $floating = 1;
  245. $args =~ s/f//ig;
  246. } else {
  247. $floating = 0;
  248. }
  249. if ($dpos == 0) {
  250. $nrofdice = 1;
  251. } else {
  252. $nrofdice = substr($args,0,$dpos);
  253. if ($nrofdice == 0) {
  254. $nrofdice = 1;
  255. }
  256. }
  257. $diesize = substr($args,$dpos+1,$maxline);
  258. $result = '('; $highest = 0; $lowest = $diesize+1;
  259. for ($i = 0;$i<$nrofdice;$i++) {
  260. do {
  261. $current = int(rand($diesize))+1;
  262. if (($droplow == 1) && ($current < $lowest)) { $lowest = $current; }
  263. if (($drophigh == 1) && ($current > $highest)) { $highest = $current; }
  264. $result .= $current . '+';
  265. } while (($current == $diesize) && ($floating == 1));
  266. }
  267. $result = substr($result,0,-1);
  268. if ($droplow == 1) { $result = $result."-$lowest"; }
  269. if ($drophigh == 1) { $result = $result."-$highest"; }
  270. $result = $result.')';
  271. return $result;
  272. }
  273.  
  274. sub trim($) {
  275. my $string = shift;
  276. $string =~ s/^\s+//;
  277. $string =~ s/\s+$//;
  278. return $string;
  279. }
  280.  
  281. sub ltrim($) {
  282. my $string = shift;
  283. $string =~ s/^\s+//;
  284. return $string;
  285. }
  286.  
  287. sub rtrim($) {
  288. my $string = shift;
  289. $string =~ s/\s+$//;
  290. return $string;
  291. }
  292.  
  293. sub alltrim($) {
  294. my $string = shift;
  295. $string =~ s/\s+//g;
  296. return $string;
  297. }
  298.  
  299. sub one_shift {
  300. $shifted = shift;
  301. return $shifted;
  302. }
  303.  
  304. sub on_invite {
  305. $self = shift;
  306. $event = shift;
  307. $chan = one_shift($event->args);
  308. $self->join($chan);
  309. print STDERR "Received invite from " . $event->nick . " to ".$chan.". Joining.\n";
  310. }
  311.  
  312. sub command_exalted {
  313. $self = shift;
  314. $dest = shift;
  315. $nick = shift;
  316. $args = shift;
  317. $output = "[$nick] rolled ";
  318. if ($args =~ /:/) {
  319. $colonpos = index $args,':';
  320. $comment = "\"". trim(substr($args,$colonpos+1,$maxline)) ."\": ";
  321. $exaltedexpression = substr($args,0,$colonpos);
  322. } else {
  323. $comment = "";
  324. $exaltedexpression = $args;
  325. }
  326. ($dice,$tn,$reps,$ext) = split(/ /,$exaltedexpression);
  327. if (($dice =~ /a/i) || ($dice =~ /b/i) || ($dice =~ /l/i) || ($dice =~ /m/i)) {
  328. $tensexplode = 0;
  329. } else {
  330. $tensexplode = 1;
  331. }
  332. if ($dice =~ /f/i) {
  333. $flurry = 1;
  334. } else {
  335. $flurry = 0;
  336. }
  337. $dice =~ s/a//ig; $dice =~ s/m//ig; $dice =~ s/l//ig; $dice =~ s/b//ig; $dice =~ s/f//ig;
  338. $temp = "("; $successes = "";
  339. if (($reps < 1) || (length($reps) == 0)) { $reps = 1; }
  340. if ($reps > 30) { $reps = 30; }
  341. if ($dice > 200) { $dice = 200; }
  342. if (($dice < 1) || (length($dice) == 0)) { $dice = 1; }
  343. if (length($tn) == 0) { $tn = 7; }
  344. if ($tn < 1) { $tn = 1; }
  345. if ($tn > 10) { $tn = 10; }
  346. if (length($ext) == 0) { $ext = 0; }
  347. for ($rep = 0;$rep<$reps;$rep++) {
  348. $currentsuccesses = 0 + $ext;
  349. for ($die = 0;$die<$dice;$die++) {
  350. $current = int(rand(10))+1;
  351. if ($current >= $tn) {
  352. $currentsuccesses++;
  353. if (($tensexplode == 1) && ($current == 10)) {
  354. $currentsuccesses++;
  355. }
  356. }
  357. $temp = $temp . $current . " ";
  358. }
  359. $successes = $successes . $currentsuccesses . "; ";
  360. $temp = substr($temp,0,-1) . "; ";
  361. if ($flurry == 1) {
  362. $dice = $dice - 1;
  363. }
  364. }
  365. $temp = substr($temp,0,-2) . ")";
  366. $output = $output . $comment . $temp . ". Successes (TN " . $tn . ") = \x02" . substr($successes,0,-2) . "\x02.";
  367. $self->privmsg($dest,$output);
  368. }
  369.  
  370. sub command_sr3 {
  371. $self = shift;
  372. $dest = shift;
  373. $nick = shift;
  374. $args = shift;
  375. $output = "[$nick] rolled ";
  376. if ($args =~ /:/) {
  377. $colonpos = index $args,':';
  378. $comment = "\"". trim(substr($args,$colonpos+1,$maxline)) ."\": ";
  379. $sr3expression = substr($args,0,$colonpos);
  380. } else {
  381. $comment = "";
  382. $sr3expression = $args;
  383. }
  384. ($dice,$tn,$reps) = split(/ /,$sr3expression);
  385. $temp = "("; $successes = "";
  386. if (($reps < 1) || (length($reps) == 0)) { $reps = 1; }
  387. if ($reps > 30) { $reps = 30; }
  388. if ($dice > 200) { $dice = 200; }
  389. if (($dice < 1) || (length($dice) == 0)) { $dice = 1; }
  390. if (length($tn) == 0) { $tn = 4; }
  391. if ($tn < 1) { $tn = 1; }
  392. if ($tn > 10) { $tn = 10; }
  393. for ($rep = 0;$rep<$reps;$rep++) {
  394. $currentsuccesses = 0;
  395. for ($die = 0;$die<$dice;$die++) {
  396. $current = 0;
  397. do {
  398. $now = int(rand(6))+1;
  399. $current = $current + $now;
  400. } while ($now == 6);
  401. if ($current >= $tn) {
  402. $currentsuccesses++;
  403. }
  404. $temp = $temp . $current . " ";
  405. }
  406. $successes = $successes . $currentsuccesses . "; ";
  407. $temp = substr($temp,0,-1) . "; ";
  408. }
  409. $temp = substr($temp,0,-2) . ")";
  410. $output = $output . $comment . $temp . ". Successes (TN " . $tn . ") = \x02" . substr($successes,0,-2) . "\x02.";
  411. $self->privmsg($dest,$output);
  412. }
  413.  
  414. sub command_nwod {
  415. $self = shift;
  416. $dest = shift;
  417. $nick = shift;
  418. $args = shift;
  419. $output = "[$nick] rolled ";
  420. if ($args =~ /:/) {
  421. $colonpos = index $args,':';
  422. $comment = "\"". trim(substr($args,$colonpos+1,$maxline)) ."\": ";
  423. $nwodexpression = substr($args,0,$colonpos);
  424. } else {
  425. $comment = "";
  426. $nwodexpression = $args;
  427. }
  428. ($dice,$reps) = split(/ /,$nwodexpression);
  429. if ($dice =~ /n/) {
  430. $tensexplode = 0;
  431. } else {
  432. $tensexplode = 1;
  433. }
  434. if ($dice =~ /c/) {
  435. $tn = 10;
  436. } else {
  437. $tn = 8;
  438. }
  439. $dice =~ s/n//ig; $dice =~ s/c//ig;
  440. $temp = "("; $successes = "";
  441. if (($reps < 1) || (length($reps) == 0)) { $reps = 1; }
  442. if ($reps > 30) { $reps = 30; }
  443. if ($dice > 200) { $dice = 200; }
  444. if (($dice < 1) || (length($dice) == 0)) { $dice = 1; }
  445. for ($rep = 0;$rep<$reps;$rep++) {
  446. $currentsuccesses = 0;
  447. for ($die = 0;$die<$dice;$die++) {
  448. if ($tensexplode == 1) {
  449. do {
  450. $current = int(rand(10))+1;
  451. if ($current >= $tn) {
  452. $currentsuccesses++;
  453. }
  454. $temp = $temp . $current . " ";
  455. } while ($current == 10);
  456. } else {
  457. $current = int(rand(10))+1;
  458. if ($current >= $tn) {
  459. $currentsuccesses++;
  460. }
  461. $temp = $temp . $current . " ";
  462. }
  463.  
  464. }
  465. $successes = $successes . $currentsuccesses . "; ";
  466. $temp = substr($temp,0,-1) . "; ";
  467. }
  468. $temp = substr($temp,0,-2) . ")";
  469. $output = $output . $comment . $temp . ". Successes (TN " . $tn . ") = \x02" . substr($successes,0,-2) . "\x02.";
  470. $self->privmsg($dest,$output);
  471. }
  472.  
  473. sub command_space {
  474. $self = shift;
  475. $dest = shift;
  476. $nick = shift;
  477. $args = shift;
  478. $output = "[$nick] rolled ";
  479. if ($args =~ /:/) {
  480. $colonpos = index $args,':';
  481. $comment = "\"". trim(substr($args,$colonpos+1,$maxline)) ."\": ";
  482. $spaceexpression = substr($args,0,$colonpos);
  483. } else {
  484. $comment = "";
  485. $spaceexpression = $args;
  486. }
  487. ($dice,$reps) = split(/ /,$spaceexpression);
  488. $sixesexplode = 1;
  489. $tn = 4;
  490. $temp = "("; $successes = "";
  491. if (($reps < 1) || (length($reps) == 0)) { $reps = 1; }
  492. if ($reps > 30) { $reps = 30; }
  493. if ($dice > 200) { $dice = 200; }
  494. if (($dice < 1) || (length($dice) == 0)) { $dice = 1; }
  495. for ($rep = 0;$rep<$reps;$rep++) {
  496. $currentsuccesses = 0;
  497. for ($die = 0;$die<$dice;$die++) {
  498. if ($sixesexplode == 1) {
  499. do {
  500. $current = int(rand(6))+1;
  501. if ($current >= $tn) {
  502. $currentsuccesses++;
  503. }
  504. if ($current == 6) {
  505. $temp = $temp . "\x02" . $current . "\x02 ";
  506. } else {
  507. $temp = $temp . $current . " ";
  508. }
  509. } while ($current == 6);
  510. } else {
  511. $current = int(rand(6))+1;
  512. if ($current >= $tn) {
  513. $currentsuccesses++;
  514. }
  515. if ($current == 6) {
  516. $temp = $temp . "\x02" . $current . "\x02 ";
  517. } else {
  518. $temp = $temp . $current . " ";
  519. }
  520. }
  521.  
  522. }
  523. $successes = $successes . $currentsuccesses . "; ";
  524. $temp = substr($temp,0,-1) . "; ";
  525. }
  526. $temp = substr($temp,0,-2) . ")";
  527. $output = $output . $comment . $temp . ". Successes (TN " . $tn . ") = \x02" . substr($successes,0,-2) . "\x02.";
  528. $self->privmsg($dest,$output);
  529. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement