| 1 | package Mojo::Webqq::Plugin::ProgramCode; | ||
| 2 | our $PRIORITY = 94; | ||
| 3 | use Encode; | ||
| 4 | =head1 SYNOPSIS | ||
| 5 | Support 26 kinds of programming languages | ||
| 6 | usage: | ||
| 7 | code|cpp>>>...your program code... | ||
| 8 | cpp | ||
| 9 | code|cpp>>> | ||
| 10 | #include <iostream> | ||
| 11 | using namespace std; | ||
| 12 | int main() { | ||
| 13 | cout << "Hello World!"; | ||
| 14 | return 0; | ||
| 15 | } | ||
| 16 | c | ||
| 17 | code|c>>> | ||
| 18 | #include <stdio.h> | ||
| 19 | int main() { | ||
| 20 | printf("Hello World!\n"); | ||
| 21 | return 0; | ||
| 22 | } | ||
| 23 | csharp | ||
| 24 | code|csharp>>> | ||
| 25 | using System; | ||
| 26 | class MainClass { | ||
| 27 | static void Main() { | ||
| 28 | Console.WriteLine("Hello World!"); | ||
| 29 | } | ||
| 30 | } | ||
| 31 | d | ||
| 32 | code|d>>> | ||
| 33 | import std.stdio; | ||
| 34 | void main() | ||
| 35 | { | ||
| 36 | writeln("Hello World!"); | ||
| 37 | } | ||
| 38 | erlang | ||
| 39 | code|erlang>>> | ||
| 40 | main(_) -> | ||
| 41 | io:format("Hello World!~n"). | ||
| 42 | go | ||
| 43 | code|go>>> | ||
| 44 | package main | ||
| 45 | import ( | ||
| 46 | "fmt" | ||
| 47 | ) | ||
| 48 | func main() { | ||
| 49 | fmt.Println("Hello World!") | ||
| 50 | } | ||
| 51 | idris | ||
| 52 | code|idris>>> | ||
| 53 | module Main | ||
| 54 | main : IO () | ||
| 55 | main = putStrLn "Hello World!" | ||
| 56 | java | ||
| 57 | code|java>>> | ||
| 58 | public class Main { | ||
| 59 | public static void main(String[] args) { | ||
| 60 | System.out.println("Hello World!"); | ||
| 61 | } | ||
| 62 | } | ||
| 63 | scala | ||
| 64 | code|scala>>> | ||
| 65 | object Main extends App { | ||
| 66 | println("Hello World!") | ||
| 67 | } | ||
| 68 | php | ||
| 69 | code|php>>> | ||
| 70 | <?php | ||
| 71 | echo "Hello World\n"; | ||
| 72 | rust | ||
| 73 | code|rust>>> | ||
| 74 | fn main() { | ||
| 75 | println!("Hello World!"); | ||
| 76 | } | ||
| 77 | assembly | ||
| 78 | code|assembly>>> | ||
| 79 | section .data | ||
| 80 | msg db "Hello World!", 0ah | ||
| 81 | section .text | ||
| 82 | global _start | ||
| 83 | _start: | ||
| 84 | mov rax, 1 | ||
| 85 | mov rdi, 1 | ||
| 86 | mov rsi, msg | ||
| 87 | mov rdx, 13 | ||
| 88 | syscall | ||
| 89 | mov rax, 60 | ||
| 90 | mov rdi, 0 | ||
| 91 | syscall | ||
| 92 | =cut | ||
| 93 | my %languages = ( | ||
| 94 | #code|ruby>>> | ||
| 95 | ruby => 'main.rb',#code|ruby>>>puts "Hello World!" | ||
| 96 | perl => 'main.pl',#code|perl>>>print "Hello World!\n"; | ||
| 97 | clojure => 'main.clj',#code|clojure>>>(println "Hello World!") | ||
| 98 | coffeescript => 'main.coffee',#code|coffeescript>>>console.log "Hello World!" | ||
| 99 | bash => 'main.sh',#code|bash>>>echo Hello World | ||
| 100 | cpp => 'main.cpp', | ||
| 101 | c => 'main.c', | ||
| 102 | assembly => 'main.asm', | ||
| 103 | java => '.java', | ||
| 104 | scala => "main.scala", | ||
| 105 | csharp => 'main.cs', | ||
| 106 | d => 'main.d', | ||
| 107 | erlang => 'main.erl', | ||
| 108 | go => 'main.go', | ||
| 109 | idris => 'main.idr', | ||
| 110 | rust => "main.rs", | ||
| 111 | php => 'main.php', | ||
| 112 | elixir => 'main.ex',#code|elixir>>>IO.puts "Hello World!" | ||
| 113 | fsharp => 'main.fs',#code|fsharp>>>printfn "Hello World!" | ||
| 114 | haskell => 'main.hs',#code|haskell>>>main = putStrLn "Hello World!" | ||
| 115 | javascript => 'main.js',#code|javascript>>>console.log("Hello World!"); | ||
| 116 | julia => 'main.jl',#code|julia>>>println("Hello world!") | ||
| 117 | lua => 'main.lua',#code|lua>>>print("Hello World!"); | ||
| 118 | nim => 'main.nim',#code|nim>>>echo("Hello World!") | ||
| 119 | ocaml => 'main.ml',#code|ocaml>>>print_endline "Hello World!" | ||
| 120 | python => "main.py",#code|python>>>print("Hello World!") | ||
| 121 | ); | ||
| 122 | sub call{ | ||
| 123 | my $client = shift; | ||
| 124 | my $data = shift; | ||
| 125 | my $callback = sub{ | ||
| 126 | my($client,$msg)=@_; | ||
| 127 | if ($msg->content =~ m/^code\s*\|\s*([a-zA-z]+?)\s*>>>(.*)/s) { | ||
| 128 | my $language = $1; | ||
| 129 | my $code = $2; | ||
| 130 | return if not $msg->allow_plugin; | ||
| 131 | return if $msg->class eq "send" and $msg->from ne "api" and $msg->from ne "irc"; | ||
| 132 | return if not exists $languages{$language}; | ||
| 133 | return if not $code; | ||
| 134 | $msg->allow_plugin(0); | ||
| 135 | my $url = "https://glot.io/run/$language?version=latest"; | ||
| 136 | my $filename = $languages{$language}; | ||
| 137 | if ($language eq 'java') { | ||
| 138 | $msg->{content} =~ m/class\s+([\w]+)/g; | ||
| 139 | $filename = $1.$languages{$language}; | ||
| 140 | } | ||
| 141 | my %r = ( | ||
| 142 | files => [{name=>$filename,content=>$code}], | ||
| 143 | command => "", | ||
| 144 | stdin => "", | ||
| 145 | ); | ||
| 146 | $client->http_post($url,{json => 1,Referer => "https://glot.io/run/$language?version=latest"},json=>\%r,sub{ | ||
| 147 | my $json = shift; | ||
| 148 | return unless defined $json; | ||
| 149 | if ($json->{stdout}) { | ||
| 150 | $client->reply_message($msg,"执行<$language>结果:---->\n".$json->{stdout}); | ||
| 151 | }else{ | ||
| 152 | $client->reply_message($msg,"执行<$language>出错:---->\n".$json->{stdout}."--->".$json->{error}."--->".$json->{stderr}); | ||
| 153 | } | ||
| 154 | }); | ||
| 155 | } | ||
| 156 | }; | ||
| 157 | $client->on(receive_message=>$callback,send_message=>$callback); | ||
| 158 | } |