]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/commandinterpreter.php
silently skip email for subs from sandboxed user
[quix0rs-gnu-social.git] / lib / commandinterpreter.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, StatusNet, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
21
22 require_once INSTALLDIR.'/lib/command.php';
23
24 class CommandInterpreter
25 {
26     function handle_command($user, $text)
27     {
28         # XXX: localise
29
30         $text = preg_replace('/\s+/', ' ', trim($text));
31         list($cmd, $arg) = $this->split_arg($text);
32
33         # We try to support all the same commands as Twitter, see
34         # http://getsatisfaction.com/twitter/topics/what_are_the_twitter_commands
35         # There are a few compatibility commands from earlier versions of
36         # StatusNet
37
38         switch(strtolower($cmd)) {
39          case 'help':
40             if ($arg) {
41                 return null;
42             }
43             return new HelpCommand($user);
44          case 'login':
45             if ($arg) {
46                 return null;
47             } else {
48                 return new LoginCommand($user);
49             }
50          case 'on':
51             if ($arg) {
52                 list($other, $extra) = $this->split_arg($arg);
53                 if ($extra) {
54                     return null;
55                 } else {
56                     return new OnCommand($user, $other);
57                 }
58             } else {
59                 return new OnCommand($user);
60             }
61          case 'off':
62             if ($arg) {
63                 list($other, $extra) = $this->split_arg($arg);
64                 if ($extra) {
65                     return null;
66                 } else {
67                     return new OffCommand($user, $other);
68                 }
69             } else {
70                 return new OffCommand($user);
71             }
72          case 'stop':
73          case 'quit':
74             if ($arg) {
75                 return null;
76             } else {
77                 return new OffCommand($user);
78             }
79          case 'join':
80              if (!$arg) {
81                 return null;
82             }
83             list($other, $extra) = $this->split_arg($arg);
84             if ($extra) {
85                 return null;
86             } else {
87                 return new JoinCommand($user, $other);
88             }
89          case 'drop':
90             if (!$arg) {
91                 return null;
92             }
93             list($other, $extra) = $this->split_arg($arg);
94             if ($extra) {
95                 return null;
96             } else {
97                 return new DropCommand($user, $other);
98             }
99          case 'follow':
100          case 'sub':
101             if (!$arg) {
102                 return null;
103             }
104             list($other, $extra) = $this->split_arg($arg);
105             if ($extra) {
106                 return null;
107             } else {
108                 return new SubCommand($user, $other);
109             }
110          case 'leave':
111          case 'unsub':
112             if (!$arg) {
113                 return null;
114             }
115             list($other, $extra) = $this->split_arg($arg);
116             if ($extra) {
117                 return null;
118             } else {
119                 return new UnsubCommand($user, $other);
120             }
121          case 'get':
122          case 'last':
123             if (!$arg) {
124                 return null;
125             }
126             list($other, $extra) = $this->split_arg($arg);
127             if ($extra) {
128                 return null;
129             } else {
130                 return new GetCommand($user, $other);
131             }
132          case 'd':
133          case 'dm':
134             if (!$arg) {
135                 return null;
136             }
137             list($other, $extra) = $this->split_arg($arg);
138             if (!$extra) {
139                 return null;
140             } else {
141                 return new MessageCommand($user, $other, $extra);
142             }
143          case 'r':
144          case 'reply':
145             if (!$arg) {
146                 return null;
147             }
148             list($other, $extra) = $this->split_arg($arg);
149             if (!$extra) {
150                 return null;
151             } else {
152                 return new ReplyCommand($user, $other, $extra);
153             }
154          case 'whois':
155             if (!$arg) {
156                 return null;
157             }
158             list($other, $extra) = $this->split_arg($arg);
159             if ($extra) {
160                 return null;
161             } else {
162                 return new WhoisCommand($user, $other);
163             }
164          case 'fav':
165             if (!$arg) {
166                 return null;
167             }
168             list($other, $extra) = $this->split_arg($arg);
169             if ($extra) {
170                 return null;
171             } else {
172                 return new FavCommand($user, $other);
173             }
174          case 'nudge':
175             if (!$arg) {
176                 return null;
177             }
178             list($other, $extra) = $this->split_arg($arg);
179             if ($extra) {
180                 return null;
181             } else {
182                 return new NudgeCommand($user, $other);
183             }
184          case 'stats':
185             if ($arg) {
186                 return null;
187             }
188             return new StatsCommand($user);
189          case 'invite':
190             if (!$arg) {
191                 return null;
192             }
193             list($other, $extra) = $this->split_arg($arg);
194             if ($extra) {
195                 return null;
196             } else {
197                 return new InviteCommand($user, $other);
198             }
199          case 'track':
200             if (!$arg) {
201                 return null;
202             }
203             list($word, $extra) = $this->split_arg($arg);
204             if ($extra) {
205                 return null;
206             } else if ($word == 'off') {
207                 return new TrackOffCommand($user);
208             } else {
209                 return new TrackCommand($user, $word);
210             }
211          case 'untrack':
212             if (!$arg) {
213                 return null;
214             }
215             list($word, $extra) = $this->split_arg($arg);
216             if ($extra) {
217                 return null;
218             } else if ($word == 'all') {
219                 return new TrackOffCommand($user);
220             } else {
221                 return new UntrackCommand($user, $word);
222             }
223          case 'tracks':
224          case 'tracking':
225             if ($arg) {
226                 return null;
227             }
228             return new TrackingCommand($user);
229          default:
230             return false;
231         }
232     }
233     
234     /**
235      * Split arguments without triggering a PHP notice warning
236      */
237     function split_arg($text)
238     {
239         $pieces = explode(' ', $text, 2);
240         if (count($pieces) == 1) {
241             $pieces[] = null;
242         }
243         return $pieces;
244     }
245 }
246